From 659cea7c7465dbef3b83e5ef4ff3318367068dbf Mon Sep 17 00:00:00 2001 From: Sander Dorigo Date: Tue, 8 Jul 2014 15:17:18 +0200 Subject: [PATCH] Built chart controller tests. --- app/lib/Firefly/Helper/Toolkit/Toolkit.php | 2 +- .../Helper/Toolkit/ToolkitInterface.php | 3 +- app/tests/controllers/ChartControllerTest.php | 135 ++++++++++++++++++ 3 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 app/tests/controllers/ChartControllerTest.php diff --git a/app/lib/Firefly/Helper/Toolkit/Toolkit.php b/app/lib/Firefly/Helper/Toolkit/Toolkit.php index ecb54d5733..088cbf828c 100644 --- a/app/lib/Firefly/Helper/Toolkit/Toolkit.php +++ b/app/lib/Firefly/Helper/Toolkit/Toolkit.php @@ -3,7 +3,7 @@ namespace Firefly\Helper\Toolkit; -class Toolkit +class Toolkit implements ToolkitInterface { /** diff --git a/app/lib/Firefly/Helper/Toolkit/ToolkitInterface.php b/app/lib/Firefly/Helper/Toolkit/ToolkitInterface.php index 1b83643997..b158e0c47e 100644 --- a/app/lib/Firefly/Helper/Toolkit/ToolkitInterface.php +++ b/app/lib/Firefly/Helper/Toolkit/ToolkitInterface.php @@ -3,6 +3,7 @@ namespace Firefly\Helper\Toolkit; -class ToolkitInterface { +interface ToolkitInterface { + public static function getDateRange(); } \ No newline at end of file diff --git a/app/tests/controllers/ChartControllerTest.php b/app/tests/controllers/ChartControllerTest.php new file mode 100644 index 0000000000..1932694509 --- /dev/null +++ b/app/tests/controllers/ChartControllerTest.php @@ -0,0 +1,135 @@ +mock('Preference'); + $pref->shouldReceive('getAttribute','data')->andReturn('week'); + + // mock preferences helper: + $preferences = $this->mock('Firefly\Helper\Preferences\PreferencesHelperInterface'); + $preferences->shouldReceive('get')->with('viewRange', 'week')->once()->andReturn($pref); + + // mock toolkit: + $toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface'); + $toolkit->shouldReceive('getDateRange')->andReturn(null); + + // create a semi-mocked collection of accounts: + + // 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); + + // collection: + $c = new \Illuminate\Database\Eloquent\Collection([$one]); + + // mock account repository: + $accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface'); + $accounts->shouldReceive('getActiveDefault')->andReturn($c); + + + // call + $this->call('GET', '/chart/home/account'); + + // test + $this->assertResponseOk(); + } + + public function testHomeAccountWithInput() + { + // save actual account: + $type = new AccountType; + $type->description = 'An account'; + $type->save(); + + $user = new User; + $user->email = 'bla'; + $user->migrated = false; + $user->password = 'bla'; + $user->save(); + $account = new Account; + $account->accountType()->associate($type); + $account->user()->associate($user); + $account->name = 'Hello'; + $account->active = true; + $account->save(); + + // 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', 'week')->once()->andReturn($pref); + + // mock toolkit: + $toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface'); + $toolkit->shouldReceive('getDateRange')->andReturn(null); + + // mock account repository: + $accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface'); + $accounts->shouldReceive('find')->with(1)->andReturn($account); + + // call + $this->call('GET', '/chart/home/account/' . $account->id); + + // test + $this->assertResponseOk(); + } + + public function testHomeAccountWithInvalidInput() + { + // save actual account: + $type = new AccountType; + $type->description = 'An account'; + $type->save(); + + $user = new User; + $user->email = 'bla'; + $user->migrated = false; + $user->password = 'bla'; + $user->save(); + $account = new Account; + $account->accountType()->associate($type); + $account->user()->associate($user); + $account->name = 'Hello'; + $account->active = true; + $account->save(); + + // 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', 'week')->once()->andReturn($pref); + + // mock toolkit: + $toolkit = $this->mock('Firefly\Helper\Toolkit\ToolkitInterface'); + $toolkit->shouldReceive('getDateRange')->andReturn(null); + + // mock account repository: + $accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface'); + $accounts->shouldReceive('find')->with(1)->andReturn(null); + + // call + $this->call('GET', '/chart/home/account/' . $account->id); + + // test + $this->assertResponseOk(); + $this->assertViewHas('message'); + } +} \ No newline at end of file