Expand tests.

This commit is contained in:
James Cole
2018-02-19 20:02:27 +01:00
parent e389d0f7fa
commit cae4faad0a
6 changed files with 130 additions and 12 deletions

View File

@@ -32,7 +32,6 @@ use Illuminate\Validation\Validator;
/** /**
* todo cannot submit using currency not part of source / dest
* Class TransactionRequest * Class TransactionRequest
*/ */
class TransactionRequest extends Request class TransactionRequest extends Request
@@ -146,9 +145,6 @@ class TransactionRequest extends Request
'transactions.*.source_name' => 'between:1,255|nullable', 'transactions.*.source_name' => 'between:1,255|nullable',
'transactions.*.destination_id' => ['numeric', 'nullable', new BelongsUser], 'transactions.*.destination_id' => ['numeric', 'nullable', new BelongsUser],
'transactions.*.destination_name' => 'between:1,255|nullable', 'transactions.*.destination_name' => 'between:1,255|nullable',
// todo tags
]; ];
} }

View File

@@ -45,12 +45,13 @@ class PiggyBankEventFactory
*/ */
public function create(TransactionJournal $journal, ?PiggyBank $piggyBank): ?PiggyBankEvent public function create(TransactionJournal $journal, ?PiggyBank $piggyBank): ?PiggyBankEvent
{ {
Log::debug(sprintf('Now in PiggyBankEventCreate for a %s', $journal->transactionType->type));
if (is_null($piggyBank)) { if (is_null($piggyBank)) {
return null; return null;
} }
// is a transfer? // 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)); Log::info(sprintf('Will not connect %s #%d to a piggy bank.', $journal->transactionType->type, $journal->id));
return null; return null;

View File

@@ -78,9 +78,10 @@ class TransactionJournalFactory
// link bill: // link bill:
$this->connectBill($journal, $data); $this->connectBill($journal, $data);
// link piggy bank: // link piggy bank (if transfer)
$this->connectPiggyBank($journal, $data); $this->connectPiggyBank($journal, $data);
// link tags: // link tags:
$this->connectTags($journal, $data); $this->connectTags($journal, $data);

View File

@@ -339,6 +339,10 @@ class SingleController extends Controller
$doSplit = 1 === intval($request->get('split_journal')); $doSplit = 1 === intval($request->get('split_journal'));
$createAnother = 1 === intval($request->get('create_another')); $createAnother = 1 === intval($request->get('create_another'));
$data = $request->getJournalData(); $data = $request->getJournalData();
// todo call factory instead of repository
$journal = $repository->store($data); $journal = $repository->store($data);
if (null === $journal->id) { if (null === $journal->id) {
// error! // error!

View File

@@ -100,7 +100,6 @@ $factory->define(
'user_id' => 1, 'user_id' => 1,
'transaction_type_id' => 1, 'transaction_type_id' => 1,
'bill_id' => null, 'bill_id' => null,
// TODO update this transaction currency reference.
'transaction_currency_id' => 1, 'transaction_currency_id' => 1,
'description' => $faker->words(3, true), 'description' => $faker->words(3, true),
'date' => '2017-01-01', 'date' => '2017-01-01',

View File

@@ -34,11 +34,7 @@ use Laravel\Passport\Passport;
use Tests\TestCase; use Tests\TestCase;
/** /**
* todo test bad budget, bad category
* todo test bad piggy, bad bill
* todo test fire of rules with parameter * todo test fire of rules with parameter
* todo test bad currency, bad foreign currency
* todo test reconciled, identifier
* Class TransactionControllerTest * Class TransactionControllerTest
*/ */
class TransactionControllerTest extends TestCase 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 * 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. * 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. * 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. * 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. * 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\Controllers\TransactionController::store
* @covers \FireflyIII\Api\V1\Requests\TransactionRequest * @covers \FireflyIII\Api\V1\Requests\TransactionRequest