Some new tests and fixes. [skip ci]

This commit is contained in:
James Cole
2014-07-23 06:57:51 +02:00
parent 9d142aa8b0
commit cc71a1116a
15 changed files with 397 additions and 97 deletions

View File

@@ -14,20 +14,21 @@ class ChartControllerTest extends TestCase
public function testHomeAccount()
{
// mock preference:
$pref = $this->mock('Preference');
$pref->shouldReceive('getAttribute', 'data')->andReturn('1M');
$pref2 = $this->mock('Preference');
$pref2->shouldReceive('getAttribute', 'data')->andReturn([]);
// mock preferences helper:
$preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface');
$preferences->shouldReceive('get')->with('viewRange', '1M')->once()->andReturn($pref);
$preferences->shouldReceive('get')->with('frontpageAccounts', [])->once()->andReturn($pref2);
// mock toolkit:
$start = new Carbon\Carbon;
$start->startOfMonth();
$end = new \Carbon\Carbon;
$end->endOfMonth();
$toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->shouldReceive('getDateRange')->andReturn(null);
$toolkit->shouldReceive('getDateRange')->with()->once()->andReturn([$start, $end]);
// create a semi-mocked collection of accounts:
@@ -55,22 +56,65 @@ class ChartControllerTest extends TestCase
$this->assertResponseOk();
}
public function testHomeAccountWithPref()
{
// mock toolkit:
$start = new Carbon\Carbon;
$start->startOfMonth();
$end = new \Carbon\Carbon;
$end->endOfMonth();
$toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->shouldReceive('getDateRange')->with()->once()->andReturn([$start, $end]);
// mock account(s)
$personal = $this->mock('AccountType');
$personal->shouldReceive('jsonSerialize')->andReturn('');
$one = $this->mock('Account');
$one->shouldReceive('getAttribute')->andReturn($personal);
$one->shouldReceive('balance')->andReturn(0);
$one->shouldReceive('predict')->andReturn(null);
// collection:
$c = new \Illuminate\Database\Eloquent\Collection([$one]);
// mock preference:
$pref2 = $this->mock('Preference');
$pref2->shouldReceive('getAttribute', 'data')->andReturn([$one->id]);
// mock preferences helper:
$preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface');
$preferences->shouldReceive('get')->with('frontpageAccounts', [])->once()->andReturn($pref2);
// create a semi-mocked collection of accounts:
// mock account repository:
$accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts->shouldReceive('getByIds')->andReturn($c);
// call
$this->call('GET', '/chart/home/account');
// test
$this->assertResponseOk();
}
public function testHomeAccountWithInput()
{
// save actual account:
$account = FactoryMuffin::create('Account');
// mock preference:
$pref = $this->mock('Preference');
$pref->shouldReceive('getAttribute', 'data')->andReturn('week');
// mock preferences helper:
$preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface');
$preferences->shouldReceive('get')->with('viewRange', '1M')->once()->andReturn($pref);
// mock toolkit:
$start = new Carbon\Carbon;
$start->startOfMonth();
$end = new \Carbon\Carbon;
$end->endOfMonth();
$toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->shouldReceive('getDateRange')->andReturn(null);
$toolkit->shouldReceive('getDateRange')->with()->once()->andReturn([$start, $end]);
// mock account repository:
$accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
@@ -89,30 +133,17 @@ class ChartControllerTest extends TestCase
$start = new \Carbon\Carbon;
$end = new \Carbon\Carbon;
// mock preferences & pref:
$pref = $this->mock('Preference');
$pref->shouldReceive('getAttribute', 'data')->andReturn([]);
// mock journals:
$transaction = FactoryMuffin::create('Transaction');
$journal = FactoryMuffin::create('TransactionJournal');
$journal->transactions()->save($transaction);
$tj = $this->mock('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$tj->shouldReceive('getByDateRange')->once()->with($start, $end)->andReturn([$journal]);
// mock toolkit
$toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->shouldReceive('getDateRange')->andReturn([$start, $end]);
//
// // mock preference?
$preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface');
$preferences->shouldReceive('get')->with('viewRange', '1M')->once()->andReturn($pref);
//
//
// // mock toolkit:
//// $class = $this->getMockClass('Foo', array('baz'));
//// $toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface');
//// $toolkit::static ->shouldReceive('getDateRange')->once()->andReturn([$start, $end]);
//
//// // mock transaction journal
//// $tj = $this->mock('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
//// $tj->shouldReceive('getByDateRange')->with($start, $end)->andReturn([]);
////
// call
$this->call('GET', '/chart/home/categories');
@@ -121,6 +152,64 @@ class ChartControllerTest extends TestCase
$this->assertResponseOk();
}
/**
* @expectedException \Firefly\Exception\FireflyException
*/
public function testhomeCategoriesException()
{
$start = new \Carbon\Carbon;
$end = new \Carbon\Carbon;
// mock journals:
$journal = FactoryMuffin::create('TransactionJournal');
$tj = $this->mock('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$tj->shouldReceive('getByDateRange')->once()->with($start, $end)->andReturn([$journal]);
// mock toolkit
$toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->shouldReceive('getDateRange')->andReturn([$start, $end]);
// call
$this->call('GET', '/chart/home/categories');
// test
$this->assertResponseOk();
}
public function testHomeAccountInfo()
{
$account = FactoryMuffin::create('Account');
$second = FactoryMuffin::create('Account');
$date = \Carbon\Carbon::createFromDate('2012','01','01');
$transaction = FactoryMuffin::create('Transaction');
$transaction->account()->associate($second);
$journal = FactoryMuffin::create('TransactionJournal');
$journal->transactions()->save($transaction);
// mock account repository:
$accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');
$accounts->shouldReceive('findByName')->with($account->name)->andReturn($account);
// mock journal:
$tj = $this->mock('Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface');
$tj->shouldReceive('getByAccountAndDate')->once()->andReturn([$journal]);
// call
$this->call('GET', '/chart/home/info/'.$account->name.'/'.$date->format('d/m/Y'));
// test
$this->assertResponseOk();
} //($name, $day, $month, $year)
public function tearDown()
{
Mockery::close();