From 7dfde51b84a365e8d91e5a9b471a05a66c7779b7 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 26 May 2015 19:38:52 +0200 Subject: [PATCH] Fixed tests. --- app/Models/TransactionJournal.php | 22 -- app/Validation/FireflyValidator.php | 2 +- composer.json | 5 +- composer.lock | 98 ++++----- tests/controllers/CategoryControllerTest.php | 1 + tests/factories/all.php | 33 ++- .../helpers/ConnectJournalToPiggyBankTest.php | 47 ----- tests/helpers/ReportHelperTest.php | 46 +---- tests/helpers/ReportQueryTest.php | 85 +++----- tests/models/TransactionJournalModelTest.php | 195 +++++++++--------- tests/repositories/AccountRepositoryTest.php | 158 +++++--------- tests/repositories/BillRepositoryTest.php | 9 +- tests/repositories/CategoryRepositoryTest.php | 3 +- tests/repositories/TagRepositoryTest.php | 21 +- 14 files changed, 282 insertions(+), 443 deletions(-) diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php index 236c8a8ed4..a6f868937e 100644 --- a/app/Models/TransactionJournal.php +++ b/app/Models/TransactionJournal.php @@ -223,28 +223,6 @@ class TransactionJournal extends Model return $this->hasMany('FireflyIII\Models\Transaction'); } - /** - * @return float - */ - public function getCorrectedActualAmountAttribute() - { - $amount = '0'; - $type = $this->transactionType->type; - /** @var Transaction $t */ - foreach ($this->transactions as $t) { - if ($t->amount > 0 && $type != 'Withdrawal') { - $amount = $t->amount; - break; - } - if ($t->amount < 0 && $type == 'Withdrawal') { - $amount = $t->amount; - break; - } - } - - return $amount; - } - /** * @codeCoverageIgnore * @return array diff --git a/app/Validation/FireflyValidator.php b/app/Validation/FireflyValidator.php index 9263fd13d4..9ee4a7eaed 100644 --- a/app/Validation/FireflyValidator.php +++ b/app/Validation/FireflyValidator.php @@ -167,7 +167,7 @@ class FireflyValidator extends Validator { $query = DB::table($parameters[0])->where($parameters[1], $value); $query->where('user_id', Auth::user()->id); - if (isset($paramers[2])) { + if (isset($parameters[2])) { $query->where('id', '!=', $parameters[2]); } $count = $query->count(); diff --git a/composer.json b/composer.json index 3a3e55afc2..abc1ca1ffc 100644 --- a/composer.json +++ b/composer.json @@ -38,8 +38,9 @@ "satooshi/php-coveralls": "0.6.1", "mockery/mockery": "0.9.*", "league/factory-muffin": "~2.1", - "codeclimate/php-test-reporter": "^0.1.2" - + "codeclimate/php-test-reporter": "^0.1.2", + "fzaninotto/faker": "^1.4" + }, diff --git a/composer.lock b/composer.lock index 36256fec53..d5201cc8a3 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "5c085b2cc90ffa610e386897066315a7", + "hash": "46b7813f499f70942e9997e916e68145", "packages": [ { "name": "classpreloader/classpreloader", @@ -673,6 +673,54 @@ ], "time": "2014-09-09 13:34:57" }, + { + "name": "fzaninotto/faker", + "version": "v1.4.0", + "source": { + "type": "git", + "url": "https://github.com/fzaninotto/Faker.git", + "reference": "010c7efedd88bf31141a02719f51fb44c732d5a0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/fzaninotto/Faker/zipball/010c7efedd88bf31141a02719f51fb44c732d5a0", + "reference": "010c7efedd88bf31141a02719f51fb44c732d5a0", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.0", + "squizlabs/php_codesniffer": "~1.5" + }, + "type": "library", + "extra": { + "branch-alias": [] + }, + "autoload": { + "psr-0": { + "Faker": "src/", + "Faker\\PHPUnit": "test/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "François Zaninotto" + } + ], + "description": "Faker is a PHP library that generates fake data for you.", + "keywords": [ + "data", + "faker", + "fixtures" + ], + "time": "2014-06-04 14:43:02" + }, { "name": "grumpydictator/gchart", "version": "1.0.9", @@ -2800,54 +2848,6 @@ ], "time": "2014-10-13 12:58:55" }, - { - "name": "fzaninotto/faker", - "version": "v1.4.0", - "source": { - "type": "git", - "url": "https://github.com/fzaninotto/Faker.git", - "reference": "010c7efedd88bf31141a02719f51fb44c732d5a0" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/fzaninotto/Faker/zipball/010c7efedd88bf31141a02719f51fb44c732d5a0", - "reference": "010c7efedd88bf31141a02719f51fb44c732d5a0", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "require-dev": { - "phpunit/phpunit": "~4.0", - "squizlabs/php_codesniffer": "~1.5" - }, - "type": "library", - "extra": { - "branch-alias": [] - }, - "autoload": { - "psr-0": { - "Faker": "src/", - "Faker\\PHPUnit": "test/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "François Zaninotto" - } - ], - "description": "Faker is a PHP library that generates fake data for you.", - "keywords": [ - "data", - "faker", - "fixtures" - ], - "time": "2014-06-04 14:43:02" - }, { "name": "guzzle/guzzle", "version": "v3.9.3", diff --git a/tests/controllers/CategoryControllerTest.php b/tests/controllers/CategoryControllerTest.php index 8a260fb8af..50644b4252 100644 --- a/tests/controllers/CategoryControllerTest.php +++ b/tests/controllers/CategoryControllerTest.php @@ -159,6 +159,7 @@ class CategoryControllerTest extends TestCase Amount::shouldReceive('format')->andReturn('xx'); Amount::shouldReceive('getCurrencyCode')->andReturn('xx'); + Amount::shouldReceive('formatJournal')->andReturn('xx'); $this->call('GET', '/categories/show/' . $category->id); $this->assertResponseOk(); diff --git a/tests/factories/all.php b/tests/factories/all.php index 986efab4a4..fa4e472d90 100644 --- a/tests/factories/all.php +++ b/tests/factories/all.php @@ -1,4 +1,6 @@ 'email', + 'email' => function () { + $faker = Faker\Factory::create(); + + return $faker->email; + }, 'password' => bcrypt('james'), ] ); @@ -286,5 +293,27 @@ FactoryMuffin::define( 'date' => 'date', 'encrypted' => '1', 'order' => '0', - ] + ], function (TransactionJournal $object, $saved) { + if ($saved) { + $one = FactoryMuffin::create('FireflyIII\Models\Account'); + $two = FactoryMuffin::create('FireflyIII\Models\Account'); + + Transaction::create( + [ + 'account_id' => $one->id, + 'transaction_journal_id' => $object->id, + 'amount' => 100 + ] + ); + Transaction::create( + [ + 'account_id' => $two->id, + 'transaction_journal_id' => $object->id, + 'amount' => -100 + ] + ); + + } + +} ); diff --git a/tests/helpers/ConnectJournalToPiggyBankTest.php b/tests/helpers/ConnectJournalToPiggyBankTest.php index 602d945703..c580db3fce 100644 --- a/tests/helpers/ConnectJournalToPiggyBankTest.php +++ b/tests/helpers/ConnectJournalToPiggyBankTest.php @@ -61,23 +61,6 @@ class ConnectJournalToPiggyBankTest extends TestCase $rep->forceDelete(); } - Transaction::create( - [ - 'account_id' => $account1->id, - 'transaction_journal_id' => $journal->id, - 'amount' => 100 - ] - ); - Transaction::create( - [ - 'account_id' => $account2->id, - 'transaction_journal_id' => $journal->id, - 'amount' => -100 - ] - ); - - // two transactions: - $event = new JournalCreated($journal, $piggyBank->id); $class = new ConnectJournalToPiggyBank(); @@ -141,21 +124,6 @@ class ConnectJournalToPiggyBankTest extends TestCase ] ); - Transaction::create( - [ - 'account_id' => $account1->id, - 'transaction_journal_id' => $journal->id, - 'amount' => 100 - ] - ); - Transaction::create( - [ - 'account_id' => $account2->id, - 'transaction_journal_id' => $journal->id, - 'amount' => -100 - ] - ); - $event = new JournalCreated($journal, $piggyBank->id); $class = new ConnectJournalToPiggyBank(); $result = $class->handle($event); @@ -202,21 +170,6 @@ class ConnectJournalToPiggyBankTest extends TestCase ] ); - Transaction::create( - [ - 'account_id' => $account1->id, - 'transaction_journal_id' => $journal->id, - 'amount' => -100 - ] - ); - Transaction::create( - [ - 'account_id' => $account2->id, - 'transaction_journal_id' => $journal->id, - 'amount' => 100 - ] - ); - $event = new JournalCreated($journal, $piggyBank->id); $class = new ConnectJournalToPiggyBank(); $result = $class->handle($event); diff --git a/tests/helpers/ReportHelperTest.php b/tests/helpers/ReportHelperTest.php index e321854f4f..3598c959eb 100644 --- a/tests/helpers/ReportHelperTest.php +++ b/tests/helpers/ReportHelperTest.php @@ -122,20 +122,6 @@ class ReportHelperTest extends TestCase for ($i = 0; $i < 5; $i++) { $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); - Transaction::create( - [ - 'account_id' => $left->id, - 'transaction_journal_id' => $journal->id, - 'amount' => rand(-100, 100) - ] - ); - Transaction::create( - [ - 'account_id' => $right->id, - 'transaction_journal_id' => $journal->id, - 'amount' => rand(-100, 100) - ] - ); $journals->push($journal); } @@ -257,25 +243,11 @@ class ReportHelperTest extends TestCase $journal->transaction_type_id = $type->id; $journal->user_id = $user->id; $journal->save(); - Transaction::create( - [ - 'account_id' => $left->id, - 'transaction_journal_id' => $journal->id, - 'amount' => 100 - ] - ); - Transaction::create( - [ - 'account_id' => $right->id, - 'transaction_journal_id' => $journal->id, - 'amount' => -100 - ] - ); } // test! $object = $this->object->getExpenseReport(Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth(), true); - $this->assertCount(1, $object->getExpenses()); + $this->assertCount(5, $object->getExpenses()); } /** @@ -322,25 +294,11 @@ class ReportHelperTest extends TestCase $journal->transaction_type_id = $type->id; $journal->user_id = $user->id; $journal->save(); - Transaction::create( - [ - 'account_id' => $left->id, - 'transaction_journal_id' => $journal->id, - 'amount' => 100 - ] - ); - Transaction::create( - [ - 'account_id' => $right->id, - 'transaction_journal_id' => $journal->id, - 'amount' => -100 - ] - ); } // test! $object = $this->object->getIncomeReport(Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth(), true); - $this->assertCount(1, $object->getIncomes()); + $this->assertCount(5, $object->getIncomes()); } diff --git a/tests/helpers/ReportQueryTest.php b/tests/helpers/ReportQueryTest.php index eb12e6e35d..af78cd15e8 100644 --- a/tests/helpers/ReportQueryTest.php +++ b/tests/helpers/ReportQueryTest.php @@ -80,21 +80,15 @@ class ReportQueryTest extends TestCase ] ); - Transaction::create( - [ - 'account_id' => $account2->id, - 'transaction_journal_id' => $journal->id, - 'amount' => 100 - ] - ); + // update both transactions + $journal->transactions[0]->account_id = $account1->id; + $journal->transactions[0]->amount = -100; + $journal->transactions[0]->save(); + + $journal->transactions[1]->account_id = $account2->id; + $journal->transactions[1]->amount = 100; + $journal->transactions[1]->save(); - Transaction::create( - [ - 'account_id' => $account1->id, - 'transaction_journal_id' => $journal->id, - 'amount' => -100 - ] - ); } $this->be($user); @@ -149,21 +143,14 @@ class ReportQueryTest extends TestCase ] ); - Transaction::create( - [ - 'account_id' => $account2->id, - 'transaction_journal_id' => $journal->id, - 'amount' => 100 - ] - ); + // update both transactions + $journal->transactions[0]->account_id = $account1->id; + $journal->transactions[0]->amount = -100; + $journal->transactions[0]->save(); - Transaction::create( - [ - 'account_id' => $account1->id, - 'transaction_journal_id' => $journal->id, - 'amount' => -100 - ] - ); + $journal->transactions[1]->account_id = $account2->id; + $journal->transactions[1]->amount = 100; + $journal->transactions[1]->save(); } $this->be($user); @@ -271,21 +258,14 @@ class ReportQueryTest extends TestCase ] ); - Transaction::create( - [ - 'account_id' => $account2->id, - 'transaction_journal_id' => $journal->id, - 'amount' => -100 - ] - ); + // update both transactions + $journal->transactions[0]->account_id = $account1->id; + $journal->transactions[0]->amount = 100; + $journal->transactions[0]->save(); - Transaction::create( - [ - 'account_id' => $account1->id, - 'transaction_journal_id' => $journal->id, - 'amount' => 100 - ] - ); + $journal->transactions[1]->account_id = $account2->id; + $journal->transactions[1]->amount = -100; + $journal->transactions[1]->save(); } $this->be($user); @@ -339,21 +319,14 @@ class ReportQueryTest extends TestCase ] ); - Transaction::create( - [ - 'account_id' => $account2->id, - 'transaction_journal_id' => $journal->id, - 'amount' => -100 - ] - ); + // update both transactions + $journal->transactions[0]->account_id = $account1->id; + $journal->transactions[0]->amount = -100; + $journal->transactions[0]->save(); - Transaction::create( - [ - 'account_id' => $account1->id, - 'transaction_journal_id' => $journal->id, - 'amount' => 100 - ] - ); + $journal->transactions[1]->account_id = $account2->id; + $journal->transactions[1]->amount = 100; + $journal->transactions[1]->save(); } $this->be($user); diff --git a/tests/models/TransactionJournalModelTest.php b/tests/models/TransactionJournalModelTest.php index dbdf86f035..d05f285529 100644 --- a/tests/models/TransactionJournalModelTest.php +++ b/tests/models/TransactionJournalModelTest.php @@ -47,13 +47,11 @@ class TransactionJournalModelTest extends TestCase $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); $account = FactoryMuffin::create('FireflyIII\Models\Account'); - Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal->id, - 'amount' => 123.45 - ] - ); + $journal->transactions[0]->amount = '123.45'; + $journal->transactions[0]->save(); + $journal->transactions[1]->amount = '-123.45'; + $journal->transactions[1]->save(); + $amount = $journal->actual_amount; $this->assertEquals('123.45', $amount); } @@ -88,13 +86,22 @@ class TransactionJournalModelTest extends TestCase $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); $asset = FactoryMuffin::create('FireflyIII\Models\Account'); - // for withdrawal, asset to expense account and reversed: //89,88 - Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => -300]); - Transaction::create(['account_id' => $expense->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => 300]); + // transactions are already in place, update them: + $withdrawal->transactions[0]->account_id = $asset->id; + $withdrawal->transactions[0]->amount = -300; + $withdrawal->transactions[0]->save(); - Transaction::create(['account_id' => $revenue->id, 'transaction_journal_id' => $deposit->id, 'amount' => -89.88]); - Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $deposit->id, 'amount' => 89.88]); + $withdrawal->transactions[1]->account_id = $expense->id; + $withdrawal->transactions[1]->amount = 300; + $withdrawal->transactions[1]->save(); + $deposit->transactions[0]->account_id = $revenue->id; + $deposit->transactions[0]->amount = -89.88; + $deposit->transactions[0]->save(); + + $deposit->transactions[1]->account_id = $asset->id; + $deposit->transactions[1]->amount = 89.88; + $deposit->transactions[1]->save(); // connect to tag: $tag->transactionJournals()->save($withdrawal); @@ -138,17 +145,27 @@ class TransactionJournalModelTest extends TestCase // make accounts: $expense = FactoryMuffin::create('FireflyIII\Models\Account'); - $asset2 = FactoryMuffin::create('FireflyIII\Models\Account'); + $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); $asset = FactoryMuffin::create('FireflyIII\Models\Account'); - $asset2->account_type_id = $asset->account_type_id; - $asset2->save(); + $revenue->account_type_id = $asset->account_type_id; + $revenue->save(); - // make transactions: - Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => -123.45]); - Transaction::create(['account_id' => $expense->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => 123.45]); + // transactions are already in place, update them: + $withdrawal->transactions[0]->account_id = $asset->id; + $withdrawal->transactions[0]->amount = -123.45; + $withdrawal->transactions[0]->save(); - Transaction::create(['account_id' => $asset2->id, 'transaction_journal_id' => $transfer->id, 'amount' => -123.45]); - Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $transfer->id, 'amount' => 123.45]); + $withdrawal->transactions[1]->account_id = $expense->id; + $withdrawal->transactions[1]->amount = 123.45; + $withdrawal->transactions[1]->save(); + + $transfer->transactions[0]->account_id = $revenue->id; + $transfer->transactions[0]->amount = -123.45; + $transfer->transactions[0]->save(); + + $transfer->transactions[1]->account_id = $asset->id; + $transfer->transactions[1]->amount = 123.45; + $transfer->transactions[1]->save(); $amount = $withdrawal->amount; @@ -161,15 +178,13 @@ class TransactionJournalModelTest extends TestCase public function testGetAmountAttributeNoTags() { $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); - $account = FactoryMuffin::create('FireflyIII\Models\Account'); - Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal->id, - 'amount' => 123.45 - ] - ); + $journal->transactions[0]->amount = 123.45; + $journal->transactions[0]->save(); + + $journal->transactions[1]->amount = -123.45; + $journal->transactions[1]->save(); + $amount = $journal->amount; $this->assertEquals('123.45', $amount); } @@ -195,8 +210,13 @@ class TransactionJournalModelTest extends TestCase $expense = FactoryMuffin::create('FireflyIII\Models\Account'); $asset = FactoryMuffin::create('FireflyIII\Models\Account'); - Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => -300]); - Transaction::create(['account_id' => $expense->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => 300]); + $withdrawal->transactions[0]->amount = -300; + $withdrawal->transactions[0]->account_id = $asset->id; + $withdrawal->transactions[0]->save(); + + $withdrawal->transactions[1]->amount = 300; + $withdrawal->transactions[1]->account_id = $expense->id; + $withdrawal->transactions[1]->save(); // connect to tag: $tag->transactionJournals()->save($withdrawal); @@ -224,8 +244,15 @@ class TransactionJournalModelTest extends TestCase $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); $asset = FactoryMuffin::create('FireflyIII\Models\Account'); - Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $deposit->id, 'amount' => 300]); - Transaction::create(['account_id' => $revenue->id, 'transaction_journal_id' => $deposit->id, 'amount' => -300]); + // update transactions + $deposit->transactions[0]->account_id = $asset->id; + $deposit->transactions[0]->amount = 300; + $deposit->transactions[0]->save(); + + $deposit->transactions[1]->account_id = $revenue->id; + $deposit->transactions[1]->amount = -300; + $deposit->transactions[1]->save(); + // get asset account: $result = $deposit->asset_account; @@ -240,19 +267,24 @@ class TransactionJournalModelTest extends TestCase { FactoryMuffin::create('FireflyIII\Models\TransactionType'); + // make accounts + FactoryMuffin::create('FireflyIII\Models\Account'); + $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + // make withdrawal $depositType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); $deposit->transaction_type_id = $depositType->id; $deposit->save(); - // make accounts - FactoryMuffin::create('FireflyIII\Models\Account'); - $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); - $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + $deposit->transactions[0]->account_id = $asset->id; + $deposit->transactions[0]->amount = 300; + $deposit->transactions[0]->save(); - Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $deposit->id, 'amount' => -300]); - Transaction::create(['account_id' => $revenue->id, 'transaction_journal_id' => $deposit->id, 'amount' => -300]); + $deposit->transactions[1]->account_id = $revenue->id; + $deposit->transactions[1]->amount = -300; + $deposit->transactions[1]->save(); // get asset account: $result = $deposit->asset_account; @@ -265,18 +297,25 @@ class TransactionJournalModelTest extends TestCase */ public function testGetAssetAccountAttributeWithdrawal() { + // make accounts + $expense = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + // make withdrawal $withdrawalType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); $withdrawal->transaction_type_id = $withdrawalType->id; $withdrawal->save(); - // make accounts - $expense = FactoryMuffin::create('FireflyIII\Models\Account'); - $asset = FactoryMuffin::create('FireflyIII\Models\Account'); - Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => -300]); - Transaction::create(['account_id' => $expense->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => 300]); + + $withdrawal->transactions[0]->account_id = $asset->id; + $withdrawal->transactions[0]->amount = -300; + $withdrawal->transactions[0]->save(); + + $withdrawal->transactions[1]->account_id = $expense->id; + $withdrawal->transactions[1]->amount = 300; + $withdrawal->transactions[1]->save(); // get asset account: $result = $withdrawal->asset_account; @@ -284,58 +323,6 @@ class TransactionJournalModelTest extends TestCase $this->assertEquals($asset->id, $result->id); } - /** - * @covers FireflyIII\Models\TransactionJournal::getCorrectedActualAmountAttribute - */ - public function testGetCorrectedActualAmountAttributeDeposit() - { - - FactoryMuffin::create('FireflyIII\Models\TransactionType'); - $depositType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); - $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); - $deposit->transaction_type_id = $depositType->id; - $deposit->save(); - - // make accounts - FactoryMuffin::create('FireflyIII\Models\Account'); - $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); - $asset = FactoryMuffin::create('FireflyIII\Models\Account'); - - Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $deposit->id, 'amount' => 300]); - Transaction::create(['account_id' => $revenue->id, 'transaction_journal_id' => $deposit->id, 'amount' => -300]); - - // get asset account: - $result = $deposit->corrected_actual_amount; - - $this->assertEquals('300', $result); - } - - /** - * @covers FireflyIII\Models\TransactionJournal::getCorrectedActualAmountAttribute - */ - public function testGetCorrectedActualAmountAttributeWithdrawal() - { - - // make withdrawal - $withdrawalType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); - $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); - $withdrawal->transaction_type_id = $withdrawalType->id; - $withdrawal->save(); - - // make accounts - $expense = FactoryMuffin::create('FireflyIII\Models\Account'); - FactoryMuffin::create('FireflyIII\Models\Account'); - $asset = FactoryMuffin::create('FireflyIII\Models\Account'); - - Transaction::create(['account_id' => $expense->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => 300]); - Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $withdrawal->id, 'amount' => -300]); - - // get asset account: - $result = $withdrawal->corrected_actual_amount; - - $this->assertEquals('-300', $result); - } - /** * @covers FireflyIII\Models\TransactionJournal::getDestinationAccountAttribute */ @@ -352,8 +339,13 @@ class TransactionJournalModelTest extends TestCase $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); $asset = FactoryMuffin::create('FireflyIII\Models\Account'); - Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $deposit->id, 'amount' => 300]); - Transaction::create(['account_id' => $revenue->id, 'transaction_journal_id' => $deposit->id, 'amount' => -300]); + $deposit->transactions[0]->account_id = $asset->id; + $deposit->transactions[0]->amount = 300; + $deposit->transactions[0]->save(); + + $deposit->transactions[1]->account_id = $revenue->id; + $deposit->transactions[1]->amount = -300; + $deposit->transactions[1]->save(); // get asset account: $result = $deposit->destination_account; @@ -377,8 +369,13 @@ class TransactionJournalModelTest extends TestCase $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); $asset = FactoryMuffin::create('FireflyIII\Models\Account'); - Transaction::create(['account_id' => $asset->id, 'transaction_journal_id' => $deposit->id, 'amount' => -300]); - Transaction::create(['account_id' => $revenue->id, 'transaction_journal_id' => $deposit->id, 'amount' => -300]); + $deposit->transactions[0]->account_id = $asset->id; + $deposit->transactions[0]->amount = -300; + $deposit->transactions[0]->save(); + + $deposit->transactions[1]->account_id = $revenue->id; + $deposit->transactions[1]->amount = -300; + $deposit->transactions[1]->save(); // get asset account: $result = $deposit->destination_account; diff --git a/tests/repositories/AccountRepositoryTest.php b/tests/repositories/AccountRepositoryTest.php index 59b1b53973..d96994e8b0 100644 --- a/tests/repositories/AccountRepositoryTest.php +++ b/tests/repositories/AccountRepositoryTest.php @@ -2,8 +2,6 @@ use Carbon\Carbon; use FireflyIII\Models\Account; use FireflyIII\Models\AccountMeta; -use FireflyIII\Models\AccountType; -use FireflyIII\Models\PiggyBankRepetition; use FireflyIII\Models\Preference; use FireflyIII\Models\Transaction; use FireflyIII\Repositories\Account\AccountRepository; @@ -117,30 +115,18 @@ class AccountRepositoryTest extends TestCase { $account = FactoryMuffin::create('FireflyIII\Models\Account'); $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $first = $journal->transactions()->orderBy('date', 'DESC')->first(); + $first->account_id = $account->id; + $first->save(); - // two matching transactions: - $first = Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal->id, - 'amount' => 100, - ] - ); - Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal->id, - 'amount' => -100, - ] - ); // login $this->be($account->user); $oldest = $this->object->getFirstTransaction($journal, $account); - $this->assertEquals($oldest->amount, $first->amount); - $this->assertEquals($oldest->id, $first->id); + $this->assertEquals($first->amount, $oldest->amount); + $this->assertEquals($first->id, $oldest->id); } @@ -215,28 +201,12 @@ class AccountRepositoryTest extends TestCase $journal2->save(); $journal3->save(); - // transactions to match the dates (one per journal will do) - Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal1->id, - 'amount' => 100 - ] - ); - Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal2->id, - 'amount' => 100 - ] - ); - Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal3->id, - 'amount' => 100 - ] - ); + $journal1->transactions[0]->account_id = $account->id; + $journal1->transactions[0]->save(); + $journal2->transactions[0]->account_id = $account->id; + $journal2->transactions[0]->save(); + $journal3->transactions[0]->account_id = $account->id; + $journal3->transactions[0]->save(); // be user $this->be($journal1->user); @@ -277,28 +247,12 @@ class AccountRepositoryTest extends TestCase $journal2->save(); $journal3->save(); - // transactions to match the dates (one per journal will do) - Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal1->id, - 'amount' => 100 - ] - ); - Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal2->id, - 'amount' => 100 - ] - ); - Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal3->id, - 'amount' => 100 - ] - ); + $journal1->transactions[0]->account_id = $account->id; + $journal1->transactions[0]->save(); + $journal2->transactions[0]->account_id = $account->id; + $journal2->transactions[0]->save(); + $journal3->transactions[0]->account_id = $account->id; + $journal3->transactions[0]->save(); // be user $this->be($journal1->user); @@ -329,14 +283,8 @@ class AccountRepositoryTest extends TestCase $journal->user_id = $account->user_id; $journal->save(); - // transaction to match the date (one will do) - Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal->id, - 'amount' => 100 - ] - ); + $journal->transactions[0]->account_id = $account->id; + $journal->transactions[0]->save(); // be user $this->be($journal->user); @@ -478,28 +426,25 @@ class AccountRepositoryTest extends TestCase $journal2->transaction_type_id = $journal1->transaction_type_id; $journal3->transaction_type_id = $journal1->transaction_type_id; - // three transactions: - Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal1->id, - 'amount' => 100 - ] - ); - Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal2->id, - 'amount' => 100 - ] - ); - Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal3->id, - 'amount' => 100 - ] - ); + // transactions are already present, update them! + $journal1->transactions[0]->account_id = $account->id; + $journal1->transactions[0]->amount = 100; + $journal1->transactions[1]->account_id = $account->id; + $journal1->transactions[1]->amount = 100; + $journal2->transactions[0]->account_id = $account->id; + $journal2->transactions[0]->amount = 100; + $journal2->transactions[1]->account_id = $account->id; + $journal2->transactions[1]->amount = 100; + $journal3->transactions[0]->account_id = $account->id; + $journal3->transactions[0]->amount = 100; + $journal3->transactions[1]->account_id = $account->id; + $journal3->transactions[1]->amount = 100; + $journal1->transactions[0]->save(); + $journal1->transactions[1]->save(); + $journal2->transactions[0]->save(); + $journal2->transactions[1]->save(); + $journal3->transactions[0]->save(); + $journal3->transactions[1]->save(); // check date: $start = new Carbon('2014-01-01'); @@ -557,21 +502,6 @@ class AccountRepositoryTest extends TestCase $account = FactoryMuffin::create('FireflyIII\Models\Account'); $journal1 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); $journal2 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); - // two transactions: - Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal1->id, - 'amount' => 100 - ] - ); - Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal2->id, - 'amount' => 100 - ] - ); // dates $one = new Carbon('2013-01-15'); @@ -582,6 +512,18 @@ class AccountRepositoryTest extends TestCase $journal1->user_id = $account->user_id; $journal2->date = $two; $journal2->user_id = $account->user_id; + + // add account things: + $journal1->transactions[0]->account_id = $account->id; + $journal1->transactions[1]->account_id = $account->id; + $journal2->transactions[0]->account_id = $account->id; + $journal2->transactions[1]->account_id = $account->id; + $journal1->transactions[0]->save(); + $journal1->transactions[1]->save(); + $journal2->transactions[0]->save(); + $journal2->transactions[1]->save(); + + $journal1->save(); $journal2->save(); diff --git a/tests/repositories/BillRepositoryTest.php b/tests/repositories/BillRepositoryTest.php index 5c2c1df2de..5c1de9fe92 100644 --- a/tests/repositories/BillRepositoryTest.php +++ b/tests/repositories/BillRepositoryTest.php @@ -176,13 +176,8 @@ class BillRepositoryTest extends TestCase $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); $journal->user_id = $bill1->user_id; $journal->save(); - Transaction::create( - [ - 'account_id' => $account->id, - 'transaction_journal_id' => $journal->id, - 'amount' => rand(101, 999), - ] - ); + $journal->transactions[0]->account_id = $account->id; + $journal->transactions[0]->save(); } $this->be($bill1->user); diff --git a/tests/repositories/CategoryRepositoryTest.php b/tests/repositories/CategoryRepositoryTest.php index 2ade3fae1e..9ea3727bc4 100644 --- a/tests/repositories/CategoryRepositoryTest.php +++ b/tests/repositories/CategoryRepositoryTest.php @@ -129,7 +129,8 @@ class CategoryRepositoryTest extends TestCase $this->assertCount(10, $set); reset($set); - $this->assertEquals(0, current($set)['sum']); + // every journal has amount 100. + $this->assertEquals(100, current($set)['sum']); } /** diff --git a/tests/repositories/TagRepositoryTest.php b/tests/repositories/TagRepositoryTest.php index 9dcb52145d..3a8962dc29 100644 --- a/tests/repositories/TagRepositoryTest.php +++ b/tests/repositories/TagRepositoryTest.php @@ -180,9 +180,14 @@ class TagRepositoryTest extends TestCase $journal2 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); // transactions for both: - Transaction::create(['account_id' => $account->id, 'transaction_journal_id' => $journal1->id, 'amount' => 100]); - Transaction::create(['account_id' => $account->id, 'transaction_journal_id' => $journal2->id, 'amount' => 100]); - + $journal1->transactions[0]->account_id = $account->id; + $journal2->transactions[0]->account_id = $account->id; + $journal1->transactions[1]->account_id = $account->id; + $journal2->transactions[1]->account_id = $account->id; + $journal1->transactions[0]->save(); + $journal2->transactions[0]->save(); + $journal1->transactions[1]->save(); + $journal2->transactions[1]->save(); $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); @@ -228,8 +233,14 @@ class TagRepositoryTest extends TestCase $journal2 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); // transactions for both: - Transaction::create(['account_id' => $account1->id, 'transaction_journal_id' => $journal1->id, 'amount' => 100]); - Transaction::create(['account_id' => $account2->id, 'transaction_journal_id' => $journal2->id, 'amount' => 100]); + $journal1->transactions[0]->account_id = $account1->id; + $journal2->transactions[0]->account_id = $account2->id; + $journal1->transactions[1]->account_id = $account1->id; + $journal2->transactions[1]->account_id = $account2->id; + $journal1->transactions[0]->save(); + $journal2->transactions[0]->save(); + $journal1->transactions[1]->save(); + $journal2->transactions[1]->save(); $tag = FactoryMuffin::create('FireflyIII\Models\Tag');