From 08131e42af3e0f38ec4a3d073a9d03614b64dbb5 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 24 Jan 2016 20:38:58 +0100 Subject: [PATCH] Implemented some new tests. --- app/Http/Controllers/PiggyBankController.php | 3 + .../PiggyBank/PiggyBankRepository.php | 8 + .../PiggyBankRepositoryInterface.php | 5 + database/seeds/TestDataSeeder.php | 252 +++++++++++------- .../Chart/ChartBudgetControllerTest.php | 1 - .../Controllers/PiggyBankControllerTest.php | 137 +++++----- 6 files changed, 248 insertions(+), 158 deletions(-) diff --git a/app/Http/Controllers/PiggyBankController.php b/app/Http/Controllers/PiggyBankController.php index a3b93b97ac..56ea5322ab 100644 --- a/app/Http/Controllers/PiggyBankController.php +++ b/app/Http/Controllers/PiggyBankController.php @@ -10,6 +10,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; use Illuminate\Support\Collection; use Input; +use Log; use Preferences; use Session; use Steam; @@ -244,6 +245,7 @@ class PiggyBankController extends Controller Session::flash('success', 'Added ' . Amount::format($amount, false) . ' to "' . e($piggyBank->name) . '".'); Preferences::mark(); } else { + Log::error('Cannot add ' . $amount . ' because max amount is ' . $maxAmount . ' (left on account is ' . $leftOnAccount . ')'); Session::flash('error', 'Could not add ' . Amount::format($amount, false) . ' to "' . e($piggyBank->name) . '".'); } @@ -322,6 +324,7 @@ class PiggyBankController extends Controller 'targetamount' => round($request->get('targetamount'), 2), 'remind_me' => false, 'reminder_skip' => 0, + 'order' => $repository->getMaxOrder() + 1, 'targetdate' => strlen($request->get('targetdate')) > 0 ? new Carbon($request->get('targetdate')) : null, ]; diff --git a/app/Repositories/PiggyBank/PiggyBankRepository.php b/app/Repositories/PiggyBank/PiggyBankRepository.php index 77b63a35eb..8d49f3c74f 100644 --- a/app/Repositories/PiggyBank/PiggyBankRepository.php +++ b/app/Repositories/PiggyBank/PiggyBankRepository.php @@ -60,6 +60,14 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface return $piggyBank->piggyBankEvents()->orderBy('date', 'DESC')->orderBy('id', 'DESC')->get(); } + /** + * @return int + */ + public function getMaxOrder() + { + return intval(Auth::user()->piggyBanks()->max('order')); + } + /** * @return Collection */ diff --git a/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php b/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php index 3bf29f36d6..8d0f81b654 100644 --- a/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php +++ b/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php @@ -42,6 +42,11 @@ interface PiggyBankRepositoryInterface */ public function getEvents(PiggyBank $piggyBank); + /** + * @return int + */ + public function getMaxOrder(); + /** * @return Collection */ diff --git a/database/seeds/TestDataSeeder.php b/database/seeds/TestDataSeeder.php index 006a912516..bff773ef8c 100644 --- a/database/seeds/TestDataSeeder.php +++ b/database/seeds/TestDataSeeder.php @@ -21,6 +21,18 @@ use Illuminate\Database\Seeder; */ class TestDataSeeder extends Seeder { + /** @var Carbon */ + public $start; + + /** + * TestDataSeeder constructor. + */ + public function __construct() + { + $this->start = Carbon::create()->subYear()->startOfYear(); + + } + /** * Run the database seeds. * @@ -59,6 +71,9 @@ class TestDataSeeder extends Seeder // create journal + attachment: $this->createAttachments($user); + + // create opening balance for savings account: + $this->openingBalanceSavings($user); } /** @@ -66,7 +81,7 @@ class TestDataSeeder extends Seeder */ private function createAssetAccounts(User $user) { - $assets = ['TestData Checking Account', 'TestData Savings', 'TestData Shared', 'TestData Creditcard', 'Emergencies', 'STE']; + $assets = ['TestData Checking Account', 'TestData Savings', 'TestData Shared', 'TestData Creditcard', 'Emergencies', 'STE']; // first two ibans match test-upload.csv $ibans = ['NL11XOLA6707795988', 'NL96DZCO4665940223', 'NL81RCQZ7160379858', 'NL19NRAP2367994221', 'NL40UKBK3619908726', 'NL38SRMN4325934708']; $assetMeta = [ @@ -97,6 +112,83 @@ class TestDataSeeder extends Seeder } + /** + * @param User $user + */ + private function createAttachments(User $user) + { + + $toAccount = $this->findAccount($user, 'TestData Checking Account'); + $fromAccount = $this->findAccount($user, 'Job'); + + $journal = TransactionJournal::create( + [ + 'user_id' => $user->id, + 'transaction_type_id' => 2, + 'transaction_currency_id' => 1, + 'description' => 'Some journal for attachment', + 'completed' => 1, + 'date' => new Carbon, + ] + ); + Transaction::create( + [ + 'account_id' => $fromAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => -100, + + ] + ); + Transaction::create( + [ + 'account_id' => $toAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => 100, + + ] + ); + + // and now attachments + $encrypted = Crypt::encrypt('I are secret'); + Attachment::create( + [ + 'attachable_id' => $journal->id, + 'attachable_type' => 'FireflyIII\Models\TransactionJournal', + 'user_id' => $user->id, + 'md5' => md5('Hallo'), + 'filename' => 'empty-file.txt', + 'title' => 'Empty file', + 'description' => 'This file is empty', + 'notes' => 'What notes', + 'mime' => 'text/plain', + 'size' => strlen($encrypted), + 'uploaded' => 1, + ] + ); + + + // and now attachment. + Attachment::create( + [ + 'attachable_id' => $journal->id, + 'attachable_type' => 'FireflyIII\Models\TransactionJournal', + 'user_id' => $user->id, + 'md5' => md5('Ook hallo'), + 'filename' => 'empty-file-2.txt', + 'title' => 'Empty file 2', + 'description' => 'This file is empty too', + 'notes' => 'What notes do', + 'mime' => 'text/plain', + 'size' => strlen($encrypted), + 'uploaded' => 1, + ] + ); + // echo crypted data to the file. + file_put_contents(storage_path('upload/at-1.data'), $encrypted); + file_put_contents(storage_path('upload/at-2.data'), $encrypted); + + } + /** * @param User $user */ @@ -172,6 +264,29 @@ class TestDataSeeder extends Seeder Category::firstOrCreateEncrypted(['name' => 'Car', 'user_id' => $user->id]); } + /** + * @param User $user + */ + private function createExpenseAccounts(User $user) + { + $expenses = ['Adobe', 'Google', 'Vitens', 'Albert Heijn', 'PLUS', 'Apple', 'Bakker', 'Belastingdienst', 'bol.com', 'Cafe Central', 'conrad.nl', + 'coolblue', 'Shell', + 'DUO', 'Etos', 'FEBO', 'Greenchoice', 'Halfords', 'XS4All', 'iCentre', 'Jumper', 'Land lord']; + foreach ($expenses as $name) { + // create account: + Account::create( + [ + 'user_id' => $user->id, + 'account_type_id' => 4, + 'name' => $name, + 'active' => 1, + 'encrypted' => 1, + ] + ); + } + + } + /** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @param User $user @@ -310,48 +425,6 @@ class TestDataSeeder extends Seeder } - /** - * @param User $user - * @param $name - * - * @return Account|null - */ - private function findAccount(User $user, $name) - { - /** @var Account $account */ - foreach ($user->accounts()->get() as $account) { - if ($account->name == $name) { - return $account; - break; - } - } - - return null; - } - - /** - * @param User $user - */ - private function createExpenseAccounts(User $user) - { - $expenses = ['Adobe', 'Google', 'Vitens', 'Albert Heijn', 'PLUS', 'Apple', 'Bakker', 'Belastingdienst', 'bol.com', 'Cafe Central', 'conrad.nl', - 'coolblue', 'Shell', - 'DUO', 'Etos', 'FEBO', 'Greenchoice', 'Halfords', 'XS4All', 'iCentre', 'Jumper', 'Land lord']; - foreach ($expenses as $name) { - // create account: - Account::create( - [ - 'user_id' => $user->id, - 'account_type_id' => 4, - 'name' => $name, - 'active' => 1, - 'encrypted' => 1, - ] - ); - } - - } - /** * @param User $user */ @@ -374,79 +447,70 @@ class TestDataSeeder extends Seeder /** * @param User $user + * @param $name + * + * @return Account|null */ - private function createAttachments(User $user) + private function findAccount(User $user, $name) { + /** @var Account $account */ + foreach ($user->accounts()->get() as $account) { + if ($account->name == $name) { + return $account; + break; + } + } - $toAccount = $this->findAccount($user, 'TestData Checking Account'); - $fromAccount = $this->findAccount($user, 'Job'); + return null; + } + + /** + * @param User $user + */ + private function openingBalanceSavings(User $user) + { + // opposing account for opening balance: + $opposing = Account::create( + [ + 'user_id' => $user->id, + 'account_type_id' => 6, + 'name' => 'Opposing for savings', + 'active' => 1, + 'encrypted' => 1, + ] + ); + + // savings + $savings = $this->findAccount($user, 'TestData Savings'); $journal = TransactionJournal::create( [ 'user_id' => $user->id, - 'transaction_type_id' => 2, + 'transaction_type_id' => 4, 'transaction_currency_id' => 1, - 'description' => 'Some journal for attachment', + 'description' => 'Opening balance for savings account', 'completed' => 1, - 'date' => new Carbon, + 'date' => $this->start->format('Y-m-d'), ] ); + + // transactions Transaction::create( [ - 'account_id' => $fromAccount->id, + 'account_id' => $opposing->id, 'transaction_journal_id' => $journal->id, - 'amount' => -100, - + 'amount' => -10000, ] ); + Transaction::create( [ - 'account_id' => $toAccount->id, + 'account_id' => $savings->id, 'transaction_journal_id' => $journal->id, - 'amount' => 100, - + 'amount' => 10000, ] ); - // and now attachments - $encrypted = Crypt::encrypt('I are secret'); - Attachment::create( - [ - 'attachable_id' => $journal->id, - 'attachable_type' => 'FireflyIII\Models\TransactionJournal', - 'user_id' => $user->id, - 'md5' => md5('Hallo'), - 'filename' => 'empty-file.txt', - 'title' => 'Empty file', - 'description' => 'This file is empty', - 'notes' => 'What notes', - 'mime' => 'text/plain', - 'size' => strlen($encrypted), - 'uploaded' => 1, - ] - ); - - - - // and now attachment. - Attachment::create( - [ - 'attachable_id' => $journal->id, - 'attachable_type' => 'FireflyIII\Models\TransactionJournal', - 'user_id' => $user->id, - 'md5' => md5('Ook hallo'), - 'filename' => 'empty-file-2.txt', - 'title' => 'Empty file 2', - 'description' => 'This file is empty too', - 'notes' => 'What notes do', - 'mime' => 'text/plain', - 'size' => strlen($encrypted), - 'uploaded' => 1, - ] - ); - // echo crypted data to the file. - file_put_contents(storage_path('upload/at-1.data'), $encrypted); - file_put_contents(storage_path('upload/at-2.data'), $encrypted); } } diff --git a/tests/acceptance/Controllers/Chart/ChartBudgetControllerTest.php b/tests/acceptance/Controllers/Chart/ChartBudgetControllerTest.php index 51371da772..ab8cc339ee 100644 --- a/tests/acceptance/Controllers/Chart/ChartBudgetControllerTest.php +++ b/tests/acceptance/Controllers/Chart/ChartBudgetControllerTest.php @@ -29,7 +29,6 @@ class ChartBudgetControllerTest extends TestCase $this->be($this->user()); $this->call('GET', '/chart/budget/1'); $this->assertResponseStatus(200); - //$this->markTestSkipped('Skipped because sqlite does not support DATE_FORMAT.'); } /** diff --git a/tests/acceptance/Controllers/PiggyBankControllerTest.php b/tests/acceptance/Controllers/PiggyBankControllerTest.php index 5a8ea4618a..2ab8691f3d 100644 --- a/tests/acceptance/Controllers/PiggyBankControllerTest.php +++ b/tests/acceptance/Controllers/PiggyBankControllerTest.php @@ -15,26 +15,22 @@ class PiggyBankControllerTest extends TestCase { /** * @covers FireflyIII\Http\Controllers\PiggyBankController::add - * @todo Implement testAdd(). */ public function testAdd() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->be($this->user()); + $this->call('GET', '/piggy-banks/add/1'); + $this->assertResponseStatus(200); } /** * @covers FireflyIII\Http\Controllers\PiggyBankController::create - * @todo Implement testCreate(). */ public function testCreate() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->be($this->user()); + $this->call('GET', '/piggy-banks/create'); + $this->assertResponseStatus(200); } /** @@ -43,58 +39,56 @@ class PiggyBankControllerTest extends TestCase */ public function testDelete() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->be($this->user()); + $this->call('GET', '/piggy-banks/delete/1'); + $this->assertResponseStatus(200); } /** * @covers FireflyIII\Http\Controllers\PiggyBankController::destroy - * @todo Implement testDestroy(). */ public function testDestroy() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->be($this->user()); + $this->session(['piggy-banks.delete.url' => 'http://localhost']); + $this->call('POST', '/piggy-banks/destroy/2'); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); } /** * @covers FireflyIII\Http\Controllers\PiggyBankController::edit - * @todo Implement testEdit(). */ public function testEdit() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->be($this->user()); + $this->call('GET', '/piggy-banks/edit/1'); + $this->assertResponseStatus(200); } /** * @covers FireflyIII\Http\Controllers\PiggyBankController::index - * @todo Implement testIndex(). */ public function testIndex() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->be($this->user()); + $this->call('GET', '/piggy-banks'); + $this->assertResponseStatus(200); } /** * @covers FireflyIII\Http\Controllers\PiggyBankController::order - * @todo Implement testOrder(). */ public function testOrder() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $args = [ + 1 => 1, + 2 => 2, + ]; + + $this->be($this->user()); + $this->call('POST', '/piggy-banks/sort', $args); + $this->assertResponseStatus(200); } /** @@ -103,69 +97,86 @@ class PiggyBankControllerTest extends TestCase */ public function testPostAdd() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $args = [ + 'amount' => 100, + ]; + + $this->be($this->user()); + $this->call('POST', '/piggy-banks/add/1', $args); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); } /** * @covers FireflyIII\Http\Controllers\PiggyBankController::postRemove - * @todo Implement testPostRemove(). */ public function testPostRemove() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $args = [ + 'amount' => 100, + ]; + + $this->be($this->user()); + $this->call('POST', '/piggy-banks/remove/1', $args); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); } /** * @covers FireflyIII\Http\Controllers\PiggyBankController::remove - * @todo Implement testRemove(). */ public function testRemove() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->be($this->user()); + $this->call('GET', '/piggy-banks/remove/1'); + $this->assertResponseStatus(200); } /** * @covers FireflyIII\Http\Controllers\PiggyBankController::show - * @todo Implement testShow(). */ public function testShow() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->be($this->user()); + $this->call('GET', '/piggy-banks/show/1'); + $this->assertResponseStatus(200); } /** * @covers FireflyIII\Http\Controllers\PiggyBankController::store - * @todo Implement testStore(). */ public function testStore() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->be($this->user()); + $this->session(['piggy-banks.create.url' => 'http://localhost/']); + + $args = [ + 'name' => 'New', + 'targetamount' => 100, + 'account_id' => 2, + ]; + + $this->call('POST', '/piggy-banks/store', $args); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); } /** * @covers FireflyIII\Http\Controllers\PiggyBankController::update - * @todo Implement testUpdate(). */ public function testUpdate() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->be($this->user()); + $this->session(['piggy-banks.edit.url' => 'http://localhost/']); + + $args = [ + 'name' => 'Updated', + 'targetamount' => 100, + 'account_id' => 2, + ]; + + $this->call('POST', '/piggy-banks/update/1', $args); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); } }