From b5ae008386ca39c354825b7629a931699b9e29db Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 9 Aug 2014 12:23:42 +0200 Subject: [PATCH] Some cleaning up and new tests for the piggy bank controller. --- app/controllers/MigrationController.php | 77 -------- .../Firefly/Helper/HelperServiceProvider.php | 6 - app/models/Piggybank.php | 30 ++- app/models/RecurringTransaction.php | 31 +++ app/models/TransactionJournal.php | 4 + app/models/User.php | 1 + app/routes.php | 10 +- .../controllers/PiggybankControllerTest.php | 183 ++++++++++++++++++ 8 files changed, 243 insertions(+), 99 deletions(-) delete mode 100644 app/controllers/MigrationController.php create mode 100644 app/tests/controllers/PiggybankControllerTest.php diff --git a/app/controllers/MigrationController.php b/app/controllers/MigrationController.php deleted file mode 100644 index afe552882a..0000000000 --- a/app/controllers/MigrationController.php +++ /dev/null @@ -1,77 +0,0 @@ -_migration = $migration; - View::share('menu', 'home'); - - } - - /** - * Dev method - */ - public function dev() - { - $file = Config::get('dev.import'); - if (file_exists($file)) { - $user = User::find(1); - - /** @noinspection PhpParamsInspection */ - Auth::login($user); - /** @var Firefly\Helper\Migration\MigrationHelperInterface $migration */ - $migration = App::make('Firefly\Helper\Migration\MigrationHelperInterface'); - $migration->loadFile($file); - if ($migration->validFile()) { - $migration->migrate(); - } else { - throw new \Firefly\Exception\FireflyException('Invalid file.'); - } - } - - return 'home'; - } - - /** - * @return \Illuminate\View\View - */ - public function index() - { - return View::make('migrate.index'); - } - - /** - * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\View\View - */ - public function postIndex() - { - if (Input::hasFile('exportFile')) { - - // get content: - $file = Input::file('exportFile'); - $path = $file->getRealPath(); - - $this->_migration->loadFile($path); - - if (!$this->_migration->validFile()) { - return View::make('error')->with('message', 'Invalid JSON content.'); - } - $this->_migration->migrate(); - - return Redirect::route('index'); - } else { - return View::make('error')->with('message', 'No file selected'); - } - } -} \ No newline at end of file diff --git a/app/lib/Firefly/Helper/HelperServiceProvider.php b/app/lib/Firefly/Helper/HelperServiceProvider.php index 2b812009c4..7a3023c90a 100644 --- a/app/lib/Firefly/Helper/HelperServiceProvider.php +++ b/app/lib/Firefly/Helper/HelperServiceProvider.php @@ -42,12 +42,6 @@ class HelperServiceProvider extends ServiceProvider 'Firefly\Helper\Email\EmailHelper' ); - // migration: - $this->app->bind( - 'Firefly\Helper\Migration\MigrationHelperInterface', - 'Firefly\Helper\Migration\MigrationHelper' - ); - // settings: $this->app->bind( 'Firefly\Helper\Preferences\PreferencesHelperInterface', diff --git a/app/models/Piggybank.php b/app/models/Piggybank.php index 2afb4908a3..f9e3c08f17 100644 --- a/app/models/Piggybank.php +++ b/app/models/Piggybank.php @@ -1,19 +1,20 @@ 'required:min:1', ]; + public static function factory() + { + $start = new Carbon; + $start->endOfMonth(); + + return [ + 'name' => 'string', + 'account_id' => 'factory|Account', + 'targetdate' => $start, + 'amount' => 0, + 'target' => 100, + 'order' => 1 + ]; + } + public function account() { return $this->belongsTo('Account'); diff --git a/app/models/RecurringTransaction.php b/app/models/RecurringTransaction.php index 72cf81a330..dbdac05da0 100644 --- a/app/models/RecurringTransaction.php +++ b/app/models/RecurringTransaction.php @@ -1,6 +1,37 @@ 'auth'], function () { // piggy bank controller Route::get('/piggybanks',['uses' => 'PiggybankController@index','as' => 'piggybanks.index']); Route::get('/piggybanks/create', ['uses' => 'PiggybankController@create','as' => 'piggybanks.create']); + Route::get('/piggybanks/show/{piggybank}', ['uses' => 'PiggybankController@show','as' => 'piggybanks.show']); Route::get('/piggybanks/edit/{piggybank}', ['uses' => 'PiggybankController@edit','as' => 'piggybanks.edit']); Route::get('/piggybanks/delete/{piggybank}', ['uses' => 'PiggybankController@delete','as' => 'piggybanks.delete']); Route::post('/piggybanks/updateAmount/{piggybank}',['uses' => 'PiggybankController@updateAmount','as' => 'piggybanks.updateAmount']); @@ -165,9 +166,6 @@ Route::group(['before' => 'auth'], function () { // user controller Route::get('/logout', ['uses' => 'UserController@logout', 'as' => 'logout']); - // migration controller - Route::get('/migrate', ['uses' => 'MigrationController@index', 'as' => 'migrate']); - } ); @@ -215,17 +213,11 @@ Route::group(['before' => 'csrf|auth'], function () { Route::post('/transaction/update/{tj}',['uses' => 'TransactionController@update','as' => 'transactions.update']); Route::post('/transaction/destroy/{tj}',['uses' => 'TransactionController@destroy','as' => 'transactions.destroy']); - // migration controller - Route::post('/migrate', ['uses' => 'MigrationController@postIndex']); - } ); // guest routes: Route::group(['before' => 'guest'], function () { - // dev import route: - Route::get('/dev',['uses' => 'MigrationController@dev']); - // user controller Route::get('/login', ['uses' => 'UserController@login', 'as' => 'login']); Route::get('/register', ['uses' => 'UserController@register', 'as' => 'register']); diff --git a/app/tests/controllers/PiggybankControllerTest.php b/app/tests/controllers/PiggybankControllerTest.php new file mode 100644 index 0000000000..15466bc956 --- /dev/null +++ b/app/tests/controllers/PiggybankControllerTest.php @@ -0,0 +1,183 @@ +_user = m::mock('User', 'Eloquent'); + $this->_accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface'); + $this->_piggybanks = $this->mock('Firefly\Storage\Piggybank\PiggybankRepositoryInterface'); + + } + + public function tearDown() + { + m::close(); + } + + public function testCreate() + { + $this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->once()->andReturn([]); + + $this->action('GET', 'PiggybankController@create'); + $this->assertResponseOk(); + } + + public function testDelete() + { + $piggyBank = f::create('Piggybank'); + + // for binding + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn( + $piggyBank->account()->first()->user_id + ); + $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email'); + + + $this->action('GET', 'PiggybankController@delete', $piggyBank->id); + $this->assertResponseOk(); + } + + public function testDestroy() + { + $piggyBank = f::create('Piggybank'); + // for binding + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn( + $piggyBank->account()->first()->user_id + ); + $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); + + $this->action('POST', 'PiggybankController@destroy', $piggyBank->id); + $this->assertResponseStatus(302); + } + + public function testEdit() + { + $piggyBank = f::create('Piggybank'); + + $this->_accounts->shouldReceive('getActiveDefaultAsSelectList')->once()->andReturn([]); + + + // for binding + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn( + $piggyBank->account()->first()->user_id + ); + $this->_user->shouldReceive('getAttribute')->with('email')->once()->andReturn('some@email'); + + $this->action('GET', 'PiggybankController@edit', $piggyBank->id); + $this->assertResponseOk(); + } + + public function testIndex() + { + $aOne = f::create('Account'); + $aTwo = f::create('Account'); + + $one = f::create('Piggybank'); + $one->account()->associate($aOne); + $two = f::create('Piggybank'); + $two->account()->associate($aOne); + $three = f::create('Piggybank'); + $three->account()->associate($aTwo); + $this->_piggybanks->shouldReceive('get')->andReturn([$one,$two,$three]); + $this->_piggybanks->shouldReceive('count')->andReturn(1); + $this->action('GET', 'PiggybankController@index'); + $this->assertResponseOk(); + } + + public function testShow() + { + $piggyBank = f::create('Piggybank'); + // for binding + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->once()->andReturn( + $piggyBank->account()->first()->user_id + ); + $this->_user->shouldReceive('getAttribute')->andReturn('some@email'); + + $this->action('GET', 'PiggybankController@show', $piggyBank->id); + $this->assertResponseOk(); + } + + public function testStore() + { + $piggyBank = f::create('Piggybank'); + $this->_piggybanks->shouldReceive('store')->andReturn($piggyBank); + $this->action('POST', 'PiggybankController@store'); + $this->assertResponseStatus(302); + } + + public function testStoreFails() + { + $piggyBank = f::create('Piggybank'); + unset($piggyBank->id); + $this->_piggybanks->shouldReceive('store')->andReturn($piggyBank); + $this->action('POST', 'PiggybankController@store'); + $this->assertResponseStatus(302); + } + + public function testStoreRedirect() + { + $piggyBank = f::create('Piggybank'); + $this->_piggybanks->shouldReceive('store')->andReturn($piggyBank); + $this->action('POST', 'PiggybankController@store',['create' => '1']); + $this->assertResponseStatus(302); + } + + public function testUpdate() + { + $piggyBank = f::create('Piggybank'); + + $this->_piggybanks->shouldReceive('update')->andReturn($piggyBank); + + // for binding + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->andReturn( + $piggyBank->account()->first()->user_id + ); + $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); + + $this->action('POST', 'PiggybankController@update', $piggyBank->id); + $this->assertResponseStatus(302); + } + + public function testUpdateAmount() + { + $piggyBank = f::create('Piggybank'); + $this->_piggybanks->shouldReceive('updateAmount')->andReturn($piggyBank); + // for binding + Auth::shouldReceive('user')->andReturn($this->_user); + Auth::shouldReceive('check')->andReturn(true); + $this->_user->shouldReceive('getAttribute')->with('id')->andReturn( + $piggyBank->account()->first()->user_id + ); + $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email'); + + $this->action('POST', 'PiggybankController@updateAmount', $piggyBank->id); + $this->assertResponseOk(); + } + + +} \ No newline at end of file