From cae4faad0aac853014b1eff3493a4c7542bee1d5 Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 19 Feb 2018 20:02:27 +0100 Subject: [PATCH] Expand tests. --- app/Api/V1/Requests/TransactionRequest.php | 4 - app/Factory/PiggyBankEventFactory.php | 3 +- app/Factory/TransactionJournalFactory.php | 3 +- .../Transaction/SingleController.php | 4 + database/factories/ModelFactory.php | 1 - .../Controllers/TransactionControllerTest.php | 127 +++++++++++++++++- 6 files changed, 130 insertions(+), 12 deletions(-) diff --git a/app/Api/V1/Requests/TransactionRequest.php b/app/Api/V1/Requests/TransactionRequest.php index 37d5e0d711..2a9f3902eb 100644 --- a/app/Api/V1/Requests/TransactionRequest.php +++ b/app/Api/V1/Requests/TransactionRequest.php @@ -32,7 +32,6 @@ use Illuminate\Validation\Validator; /** - * todo cannot submit using currency not part of source / dest * Class TransactionRequest */ class TransactionRequest extends Request @@ -146,9 +145,6 @@ class TransactionRequest extends Request 'transactions.*.source_name' => 'between:1,255|nullable', 'transactions.*.destination_id' => ['numeric', 'nullable', new BelongsUser], 'transactions.*.destination_name' => 'between:1,255|nullable', - - // todo tags - ]; } diff --git a/app/Factory/PiggyBankEventFactory.php b/app/Factory/PiggyBankEventFactory.php index a7dcbec5f1..ca4d301e6f 100644 --- a/app/Factory/PiggyBankEventFactory.php +++ b/app/Factory/PiggyBankEventFactory.php @@ -45,12 +45,13 @@ class PiggyBankEventFactory */ public function create(TransactionJournal $journal, ?PiggyBank $piggyBank): ?PiggyBankEvent { + Log::debug(sprintf('Now in PiggyBankEventCreate for a %s', $journal->transactionType->type)); if (is_null($piggyBank)) { return null; } // is a transfer? - if (!TransactionType::TRANSFER === $journal->transactionType->type) { + if (!(TransactionType::TRANSFER === $journal->transactionType->type)) { Log::info(sprintf('Will not connect %s #%d to a piggy bank.', $journal->transactionType->type, $journal->id)); return null; diff --git a/app/Factory/TransactionJournalFactory.php b/app/Factory/TransactionJournalFactory.php index 201695d018..d735a3c98e 100644 --- a/app/Factory/TransactionJournalFactory.php +++ b/app/Factory/TransactionJournalFactory.php @@ -78,9 +78,10 @@ class TransactionJournalFactory // link bill: $this->connectBill($journal, $data); - // link piggy bank: + // link piggy bank (if transfer) $this->connectPiggyBank($journal, $data); + // link tags: $this->connectTags($journal, $data); diff --git a/app/Http/Controllers/Transaction/SingleController.php b/app/Http/Controllers/Transaction/SingleController.php index 3f83446d6e..4a7d2475d4 100644 --- a/app/Http/Controllers/Transaction/SingleController.php +++ b/app/Http/Controllers/Transaction/SingleController.php @@ -339,6 +339,10 @@ class SingleController extends Controller $doSplit = 1 === intval($request->get('split_journal')); $createAnother = 1 === intval($request->get('create_another')); $data = $request->getJournalData(); + + // todo call factory instead of repository + + $journal = $repository->store($data); if (null === $journal->id) { // error! diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index bb4eeeaedd..923658054f 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -100,7 +100,6 @@ $factory->define( 'user_id' => 1, 'transaction_type_id' => 1, 'bill_id' => null, - // TODO update this transaction currency reference. 'transaction_currency_id' => 1, 'description' => $faker->words(3, true), 'date' => '2017-01-01', diff --git a/tests/Api/V1/Controllers/TransactionControllerTest.php b/tests/Api/V1/Controllers/TransactionControllerTest.php index c805057870..b194e87c09 100644 --- a/tests/Api/V1/Controllers/TransactionControllerTest.php +++ b/tests/Api/V1/Controllers/TransactionControllerTest.php @@ -34,11 +34,7 @@ use Laravel\Passport\Passport; use Tests\TestCase; /** - * todo test bad budget, bad category - * todo test bad piggy, bad bill * todo test fire of rules with parameter - * todo test bad currency, bad foreign currency - * todo test reconciled, identifier * Class TransactionControllerTest */ class TransactionControllerTest extends TestCase @@ -76,6 +72,80 @@ class TransactionControllerTest extends TestCase } + /** + * Submit with bad currency code + * + * @covers \FireflyIII\Api\V1\Controllers\TransactionController::store + * @covers \FireflyIII\Api\V1\Requests\TransactionRequest + */ + public function testFailCurrencyCode() + { + $account = $this->user()->accounts()->where('account_type_id', 3)->first(); + $data = [ + 'description' => 'Some transaction #' . rand(1, 1000), + 'date' => '2018-01-01', + 'type' => 'withdrawal', + 'transactions' => [ + [ + 'amount' => '10', + 'currency_code' => 'FU2', + 'source_id' => $account->id, + ], + ], + ]; + + // test API + $response = $this->post('/api/v1/transactions', $data, ['Accept' => 'application/json']); + $response->assertStatus(422); + $response->assertExactJson( + [ + 'message' => 'The given data was invalid.', + 'errors' => [ + 'transactions.0.currency_code' => [ + 'The selected transactions.0.currency_code is invalid.', + ], + ], + ] + ); + } + + /** + * Submit with bad currency ID. + * + * @covers \FireflyIII\Api\V1\Controllers\TransactionController::store + * @covers \FireflyIII\Api\V1\Requests\TransactionRequest + */ + public function testFailCurrencyId() + { + $account = $this->user()->accounts()->where('account_type_id', 3)->first(); + $data = [ + 'description' => 'Some transaction #' . rand(1, 1000), + 'date' => '2018-01-01', + 'type' => 'withdrawal', + 'transactions' => [ + [ + 'amount' => '10', + 'currency_id' => 1991, + 'source_id' => $account->id, + ], + ], + ]; + + // test API + $response = $this->post('/api/v1/transactions', $data, ['Accept' => 'application/json']); + $response->assertStatus(422); + $response->assertExactJson( + [ + 'message' => 'The given data was invalid.', + 'errors' => [ + 'transactions.0.currency_id' => [ + 'The selected transactions.0.currency_id is invalid.', + ], + ], + ] + ); + } + /** * Empty descriptions * @@ -1852,6 +1922,54 @@ class TransactionControllerTest extends TestCase ); } + /** + * Submit the minimum amount of data required to create a withdrawal. + * When sending a piggy bank by name, this must be reflected in the output. + * + * @covers \FireflyIII\Api\V1\Controllers\TransactionController::store + * @covers \FireflyIII\Api\V1\Requests\TransactionRequest + */ + public function testSuccessStorePiggyDeposit() + { + $dest = auth()->user()->accounts()->where('account_type_id', 3)->first(); + $piggy = auth()->user()->piggyBanks()->first(); + $data = [ + 'description' => 'Some deposit #' . rand(1, 1000), + 'date' => '2018-01-01', + 'type' => 'deposit', + 'piggy_bank_name' => $piggy->name, + 'transactions' => [ + [ + 'amount' => '10', + 'currency_id' => 1, + 'destination_id' => $dest->id, + ], + ], + ]; + // test API + $response = $this->post('/api/v1/transactions?include=piggy_bank_events', $data, ['Accept' => 'application/json']); + $this->assertFalse(isset($response->json()['included'])); + $response->assertStatus(200); + $response->assertJson( + [ + 'data' => [ + 'type' => 'transactions', + 'attributes' => [ + 'description' => $data['description'], + 'date' => $data['date'], + 'type' => 'Deposit', + 'destination_id' => $dest->id, + 'destination_name' => $dest->name, + 'destination_type' => 'Asset account', + 'amount' => 10, + ], + 'links' => [], + ], + ] + ); + + } + /** * Submit the minimum amount of data required to create a withdrawal. * When sending a piggy bank by name, this must be reflected in the output. @@ -1914,7 +2032,6 @@ class TransactionControllerTest extends TestCase /** * Submit the minimum amount of data required to create a withdrawal. * When sending a piggy bank by name, this must be reflected in the output. - * TODO only when sending a transfer. Ignore it with withdrawals. * * @covers \FireflyIII\Api\V1\Controllers\TransactionController::store * @covers \FireflyIII\Api\V1\Requests\TransactionRequest