From 445de5a1d8435ee0c32050448ac2a249f8597369 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 15 Jul 2014 07:36:01 +0200 Subject: [PATCH] Fixed all tests. --- app/models/Preference.php | 1 + app/tests/controllers/ChartControllerTest.php | 16 ++-- app/tests/controllers/HomeControllerTest.php | 3 - app/tests/controllers/JsonControllerTest.php | 39 ++++++++++ .../controllers/PreferencesControllerTest.php | 12 ++- .../controllers/TransactionControllerTest.php | 25 ++++++ app/tests/models/AllModelsTest.php | 77 ++++++++++++------- 7 files changed, 131 insertions(+), 42 deletions(-) create mode 100644 app/tests/controllers/JsonControllerTest.php create mode 100644 app/tests/controllers/TransactionControllerTest.php diff --git a/app/models/Preference.php b/app/models/Preference.php index b9c82a3f09..cd5f58161e 100644 --- a/app/models/Preference.php +++ b/app/models/Preference.php @@ -25,6 +25,7 @@ class Preference extends Elegant $this->attributes['data'] = json_encode($value); } + // public function getDataAttribute($value) { return json_decode($value); diff --git a/app/tests/controllers/ChartControllerTest.php b/app/tests/controllers/ChartControllerTest.php index 57bab4bad0..20aaf8c18d 100644 --- a/app/tests/controllers/ChartControllerTest.php +++ b/app/tests/controllers/ChartControllerTest.php @@ -13,11 +13,11 @@ class ChartControllerTest extends TestCase { // mock preference: $pref = $this->mock('Preference'); - $pref->shouldReceive('getAttribute', 'data')->andReturn('week'); + $pref->shouldReceive('getAttribute', 'data')->andReturn('1M'); // mock preferences helper: $preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface'); - $preferences->shouldReceive('get')->with('viewRange', 'week')->once()->andReturn($pref); + $preferences->shouldReceive('get')->with('viewRange', '1M')->once()->andReturn($pref); // mock toolkit: $toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface'); @@ -73,7 +73,7 @@ class ChartControllerTest extends TestCase // mock preferences helper: $preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface'); - $preferences->shouldReceive('get')->with('viewRange', 'week')->once()->andReturn($pref); + $preferences->shouldReceive('get')->with('viewRange', '1M')->once()->andReturn($pref); // mock toolkit: $toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface'); @@ -111,11 +111,11 @@ class ChartControllerTest extends TestCase // mock preference: $pref = $this->mock('Preference'); - $pref->shouldReceive('getAttribute', 'data')->andReturn('week'); + $pref->shouldReceive('getAttribute', 'data')->andReturn('1M'); // mock preferences helper: $preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface'); - $preferences->shouldReceive('get')->with('viewRange', 'week')->once()->andReturn($pref); + $preferences->shouldReceive('get')->with('viewRange', '1M')->once()->andReturn($pref); // mock toolkit: $toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface'); @@ -153,7 +153,7 @@ class ChartControllerTest extends TestCase // mock preferences helper: $preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface'); - $preferences->shouldReceive('get')->with('viewRange', 'week')->once()->andReturn($pref); + $preferences->shouldReceive('get')->with('viewRange', '1M')->once()->andReturn($pref); // call @@ -183,7 +183,7 @@ class ChartControllerTest extends TestCase // mock preferences helper: $preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface'); - $preferences->shouldReceive('get')->with('viewRange', 'week')->once()->andReturn($pref); + $preferences->shouldReceive('get')->with('viewRange', '1M')->once()->andReturn($pref); // call @@ -213,7 +213,7 @@ class ChartControllerTest extends TestCase // mock preferences helper: $preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface'); - $preferences->shouldReceive('get')->with('viewRange', 'week')->once()->andReturn($pref); + $preferences->shouldReceive('get')->with('viewRange', '1M')->once()->andReturn($pref); // call diff --git a/app/tests/controllers/HomeControllerTest.php b/app/tests/controllers/HomeControllerTest.php index 794203a057..62c42829f8 100644 --- a/app/tests/controllers/HomeControllerTest.php +++ b/app/tests/controllers/HomeControllerTest.php @@ -26,10 +26,8 @@ class HomeControllerTest extends TestCase $accounts->shouldReceive('count')->andReturn(0); $accounts->shouldReceive('getActiveDefault')->andReturn([]); - // mock preferences helper: $preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface'); $preferences->shouldReceive('get')->with('frontpageAccounts',[])->andReturn(new \Preference)->once(); - $preferences->shouldReceive('get')->with('viewRange', 'week')->once()->andReturn('week'); // call $this->call('GET', '/'); @@ -73,7 +71,6 @@ class HomeControllerTest extends TestCase $preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface'); $preferences->shouldReceive('get')->with('frontpageAccounts',[])->andReturn($pref)->once(); - $preferences->shouldReceive('get')->with('viewRange', 'week')->once()->andReturn('week'); // call $this->call('GET', '/'); diff --git a/app/tests/controllers/JsonControllerTest.php b/app/tests/controllers/JsonControllerTest.php new file mode 100644 index 0000000000..3ddac24187 --- /dev/null +++ b/app/tests/controllers/JsonControllerTest.php @@ -0,0 +1,39 @@ +name = 'Bla bla'; + + // mock account repository: + $accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface'); + $accounts->shouldReceive('getBeneficiaries')->andReturn([$obj]); + + $this->call('GET', '/json/beneficiaries'); + + // test + $this->assertResponseOk(); + + } + + public function testCategories() { + $obj = new stdClass; + $obj->name = 'Bla bla'; + + // mock category repository: + $categories = $this->mock('Firefly\Storage\Category\CategoryRepositoryInterface'); + $categories->shouldReceive('get')->andReturn([$obj]); + + $this->call('GET', '/json/categories'); + + // test + $this->assertResponseOk(); + } + +} \ No newline at end of file diff --git a/app/tests/controllers/PreferencesControllerTest.php b/app/tests/controllers/PreferencesControllerTest.php index 3192e13125..7d1b3110a1 100644 --- a/app/tests/controllers/PreferencesControllerTest.php +++ b/app/tests/controllers/PreferencesControllerTest.php @@ -14,18 +14,21 @@ class PreferencesControllerTest extends TestCase $pref = $this->mock('Preference'); $pref->shouldReceive('getAttribute', 'data')->andReturn([]); + $viewPref = $this->mock('Preference'); + $viewPref->shouldReceive('getAttribute', 'data')->andReturn('1M'); // mock view: View::shouldReceive('share'); View::shouldReceive('make')->with('preferences.index')->once()->andReturn(\Mockery::self()) ->shouldReceive('with')->once()->with('accounts', [])->andReturn(\Mockery::self()) + ->shouldReceive('with')->once()->with('viewRange', '1M')->andReturn(\Mockery::self()) ->shouldReceive('with')->once()->with('frontpageAccounts', $pref)->andReturn(\Mockery::self()); - $preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface'); $preferences->shouldReceive('get')->with('frontpageAccounts', [])->andReturn($pref); + $preferences->shouldReceive('get')->with('viewRange', '1M')->andReturn($viewPref); // mock account repository: $accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface'); @@ -39,14 +42,15 @@ class PreferencesControllerTest extends TestCase $this->assertResponseOk(); } - public function testPostIndex() { + public function testPostIndex() + { // mock $preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface'); $preferences->shouldReceive('set')->with('frontpageAccounts', [1])->andReturn(true); + $preferences->shouldReceive('set')->with('viewRange', '1M')->andReturn(true); // call - $this->call('POST', '/preferences',['frontpageAccounts' => [1]]); - + $this->call('POST', '/preferences', ['frontpageAccounts' => [1], 'viewRange' => '1M']); // test diff --git a/app/tests/controllers/TransactionControllerTest.php b/app/tests/controllers/TransactionControllerTest.php new file mode 100644 index 0000000000..823c42be47 --- /dev/null +++ b/app/tests/controllers/TransactionControllerTest.php @@ -0,0 +1,25 @@ +with('transactions.withdrawal')->andReturn(\Mockery::self()) + ->shouldReceive('with')->once() + ->with('accounts', []) + ->andReturn(Mockery::self()); + + // mock account repository: + $accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface'); + $accounts->shouldReceive('getActiveDefaultAsSelectList')->andReturn([]); + + // call + $this->call('GET', '/transactions/add/withdrawal'); + + // test + $this->assertResponseOk(); + } + +} \ No newline at end of file diff --git a/app/tests/models/AllModelsTest.php b/app/tests/models/AllModelsTest.php index 2d03e42789..219a04ef11 100644 --- a/app/tests/models/AllModelsTest.php +++ b/app/tests/models/AllModelsTest.php @@ -33,13 +33,28 @@ class AllModelsTest extends TestCase $pref = FactoryMuff::create('Preference'); $account = FactoryMuff::create('Account'); + // some more stuff: + $component = FactoryMuff::create('Component'); + $budget = FactoryMuff::create('Budget'); + $category = FactoryMuff::create('Category'); + $account->user()->associate($user); $pref->user()->associate($user); $user->accounts()->save($account); $user->preferences()->save($pref); + $user->components()->save($component); + $user->budgets()->save($budget); + $user->categories()->save($category); $this->assertEquals($account->user_id, $user->id); - $this->assertEquals($pref->user_id,$user->id); + $this->assertEquals($pref->user_id, $user->id); + $this->assertEquals($budget->user_id, $user->id); + $this->assertEquals($category->user_id, $user->id); + $this->assertEquals($component->user_id, $user->id); + + // test pref? + $pref->data = 'Blabla'; + $this->assertEquals($pref->data,'Blabla'); $this->assertCount(1, $user->accounts()->get()); $this->assertCount(1, $user->preferences()->get()); @@ -63,7 +78,8 @@ class AllModelsTest extends TestCase * Transaction journal tests. */ - public function testTransactionJournals() { + public function testTransactionJournals() + { $tj = FactoryMuff::create('TransactionJournal'); $t1 = FactoryMuff::create('Transaction'); @@ -80,28 +96,30 @@ class AllModelsTest extends TestCase $tj->components()->save($budget); $tj->components()->save($category); - $this->assertCount(2,$tj->components()->get()); - $this->assertCount(1,$tj->budgets()->get()); - $this->assertCount(1,$tj->categories()->get()); + $this->assertCount(2, $tj->components()->get()); + $this->assertCount(1, $tj->budgets()->get()); + $this->assertCount(1, $tj->categories()->get()); - $this->assertCount(3,$tj->transactions()->get()); + $this->assertCount(3, $tj->transactions()->get()); $this->assertTrue($tj->isValid()); - $this->assertEquals($tj->transaction_type_id,$tj->transactionType()->first()->id); - $this->assertEquals($tj->transaction_currency_id,$tj->transactionCurrency()->first()->id); + $this->assertEquals($tj->transaction_type_id, $tj->transactionType()->first()->id); + $this->assertEquals($tj->transaction_currency_id, $tj->transactionCurrency()->first()->id); } - public function testTransactionJournalScope() { + public function testTransactionJournalScope() + { $tj = FactoryMuff::create('TransactionJournal'); $tj->date = new \Carbon\Carbon('2012-01-02'); $set = $tj->after(new \Carbon\Carbon)->before(new \Carbon\Carbon)->get(); - $this->assertCount(0,$set); + $this->assertCount(0, $set); } - public function testTransactionType() { + public function testTransactionType() + { $j1 = FactoryMuff::create('TransactionJournal'); $j2 = FactoryMuff::create('TransactionJournal'); @@ -109,11 +127,12 @@ class AllModelsTest extends TestCase $type->transactionjournals()->save($j1); $type->transactionjournals()->save($j2); - $this->assertCount(2,$type->transactionjournals()->get()); + $this->assertCount(2, $type->transactionjournals()->get()); } - public function testTransactionCurrency() { + public function testTransactionCurrency() + { $j1 = FactoryMuff::create('TransactionJournal'); $j2 = FactoryMuff::create('TransactionJournal'); @@ -121,11 +140,12 @@ class AllModelsTest extends TestCase $currency->transactionjournals()->save($j1); $currency->transactionjournals()->save($j2); - $this->assertCount(2,$currency->transactionjournals()->get()); + $this->assertCount(2, $currency->transactionjournals()->get()); } - public function testAccountTypes() { + public function testAccountTypes() + { $type = FactoryMuff::create('AccountType'); $a1 = FactoryMuff::create('Account'); $a2 = FactoryMuff::create('Account'); @@ -133,10 +153,11 @@ class AllModelsTest extends TestCase $type->accounts()->save($a1); $type->accounts()->save($a2); - $this->assertCount(2,$type->accounts()->get()); + $this->assertCount(2, $type->accounts()->get()); } - public function testTransactions() { + public function testTransactions() + { $transaction = FactoryMuff::create('Transaction'); $budget = FactoryMuff::create('Budget'); @@ -149,15 +170,16 @@ class AllModelsTest extends TestCase $transaction->account()->associate($account); $transaction->transactionjournal()->associate($journal); - $this->assertCount(1,$transaction->transactionjournal()->get()); - $this->assertCount(1,$transaction->account()->get()); - $this->assertCount(2,$transaction->components()->get()); - $this->assertCount(1,$transaction->budgets()->get()); - $this->assertCount(1,$transaction->categories()->get()); + $this->assertCount(1, $transaction->transactionjournal()->get()); + $this->assertCount(1, $transaction->account()->get()); + $this->assertCount(2, $transaction->components()->get()); + $this->assertCount(1, $transaction->budgets()->get()); + $this->assertCount(1, $transaction->categories()->get()); } - public function testComponents() { + public function testComponents() + { $component = FactoryMuff::create('Component'); $user = FactoryMuff::create('User'); $transaction = FactoryMuff::create('Transaction'); @@ -167,8 +189,9 @@ class AllModelsTest extends TestCase $component->user()->associate($user); $component->transactions()->save($transaction); - $this->assertCount(1,$component->transactionjournals()->get()); - $this->assertCount(1,$component->user()->get()); - $this->assertCount(1,$component->transactions()->get()); + $this->assertCount(1, $component->transactionjournals()->get()); + $this->assertCount(1, $component->user()->get()); + $this->assertCount(1, $component->transactions()->get()); } -} \ No newline at end of file + +} \ No newline at end of file