Fix unit tests.

This commit is contained in:
James Cole
2018-07-05 20:15:20 +02:00
parent c0d2cd8962
commit 1675a0d442
5 changed files with 37 additions and 26 deletions

View File

@@ -85,8 +85,13 @@ class CurrencyExchangeRateControllerTest extends TestCase
$response->assertStatus(200); $response->assertStatus(200);
$response->assertJson( $response->assertJson(
['data' => [ ['data' => [
'type' => 'currency_exchange_rates',
'id' => '0',
'attributes' => [
'rate' => 0.5, 'rate' => 0.5,
],
'links' => [ 'links' => [
[ [
'rel' => 'self', 'rel' => 'self',
'uri' => '/currency_exchange_rates/', 'uri' => '/currency_exchange_rates/',

View File

@@ -1329,12 +1329,13 @@ class TransactionControllerTest extends TestCase
*/ */
public function testShowDeposit(): void public function testShowDeposit(): void
{ {
$loop = 0;
do { do {
// this is kind of cheating but OK.
/** @var TransactionJournal $journal */ /** @var TransactionJournal $journal */
$journal = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 2)->whereNull('deleted_at')->first(); $journal = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 2)->whereNull('deleted_at')->first();
$count = $journal->transactions()->count(); $count = $journal->transactions()->count();
} while ($count !== 2); $loop++;
} while ($count !== 2 && $loop < 30);
$transaction = $journal->transactions()->first(); $transaction = $journal->transactions()->first();
@@ -1392,12 +1393,14 @@ class TransactionControllerTest extends TestCase
*/ */
public function testShowWithdrawal(): void public function testShowWithdrawal(): void
{ {
$loop = 0;
do { do {
// this is kind of cheating but OK. // this is kind of cheating but OK.
/** @var TransactionJournal $journal */ /** @var TransactionJournal $journal */
$journal = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 1)->whereNull('deleted_at')->first(); $journal = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 1)->whereNull('deleted_at')->first();
$count = $journal->transactions()->count(); $count = $journal->transactions()->count();
} while ($count !== 2); $loop++;
} while ($count !== 2 && $loop < 30);
/** @var Transaction $transaction */ /** @var Transaction $transaction */
$transaction = $journal->transactions()->first(); $transaction = $journal->transactions()->first();
$transaction->description = null; $transaction->description = null;

View File

@@ -139,6 +139,7 @@ class UserControllerTest extends TestCase
// mock // mock
$userRepos = $this->mock(UserRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->once()->andReturn(true);
$userRepos->shouldReceive('store')->once()->andReturn($this->user()); $userRepos->shouldReceive('store')->once()->andReturn($this->user());
// test API // test API
@@ -162,7 +163,7 @@ class UserControllerTest extends TestCase
// mock // mock
$userRepos = $this->mock(UserRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->once()->andReturn(true);
// test API // test API
$response = $this->post('/api/v1/users', $data, ['Accept' => 'application/json']); $response = $this->post('/api/v1/users', $data, ['Accept' => 'application/json']);
$response->assertStatus(422); $response->assertStatus(422);
@@ -198,6 +199,7 @@ class UserControllerTest extends TestCase
// mock // mock
$userRepos = $this->mock(UserRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('update')->once()->andReturn($user); $userRepos->shouldReceive('update')->once()->andReturn($user);
$userRepos->shouldReceive('hasRole')->once()->andReturn(true);
// call API // call API
$response = $this->put('/api/v1/users/' . $user->id, $data); $response = $this->put('/api/v1/users/' . $user->id, $data);

View File

@@ -36,12 +36,14 @@ class FromAccountStartsTest extends TestCase
*/ */
public function testTriggered(): void public function testTriggered(): void
{ {
$transaction = null; $count = 0;
do { do {
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); $journal = $this->user()->transactionJournals()->inRandomOrder()->whereNull('deleted_at')->first();
$transaction = $journal->transactions()->where('amount', '<', 0)->first(); $transaction = $journal->transactions()->where('amount', '<', 0)->first();
} while (null === $transaction); $transactionCount = $journal->transactions()->count();
$account = $transaction->account; $account = $transaction->account;
$count++;
} while ($account === null && $count < 30 && $transactionCount !== 2);
$trigger = FromAccountStarts::makeFromStrings(substr($account->name, 0, -3), false); $trigger = FromAccountStarts::makeFromStrings(substr($account->name, 0, -3), false);
$result = $trigger->triggered($journal); $result = $trigger->triggered($journal);
@@ -53,13 +55,14 @@ class FromAccountStartsTest extends TestCase
*/ */
public function testTriggeredLonger(): void public function testTriggeredLonger(): void
{ {
$transaction = null; $count = 0;
do { do {
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); $journal = $this->user()->transactionJournals()->inRandomOrder()->whereNull('deleted_at')->first();
$transaction = $journal->transactions()->where('amount', '<', 0)->first(); $transaction = $journal->transactions()->where('amount', '<', 0)->first();
} while (null === $transaction); $transactionCount = $journal->transactions()->count();
$account = $transaction->account; $account = $transaction->account;
$count++;
} while ($account === null && $count < 30 && $transactionCount !== 2);
$trigger = FromAccountStarts::makeFromStrings('bla-bla-bla' . $account->name, false); $trigger = FromAccountStarts::makeFromStrings('bla-bla-bla' . $account->name, false);
$result = $trigger->triggered($journal); $result = $trigger->triggered($journal);

View File

@@ -37,14 +37,13 @@ class ToAccountIsTest extends TestCase
public function testTriggered(): void public function testTriggered(): void
{ {
$count = 0; $count = 0;
$transactionCount = 0;
do { do {
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
$transaction = $journal->transactions()->where('amount', '>', 0)->first(); $transaction = $journal->transactions()->where('amount', '>', 0)->first();
$transactionCount = $journal->transactions()->count(); $transactionCount = $journal->transactions()->count();
$account = $transaction->account; $account = $transaction->account;
$count++; $count++;
} while ($account === null && $count < 30 && $transactionCount !== 1); } while ($account === null && $count < 30 && $transactionCount !== 2);
$trigger = ToAccountIs::makeFromStrings($account->name, false); $trigger = ToAccountIs::makeFromStrings($account->name, false);
$result = $trigger->triggered($journal); $result = $trigger->triggered($journal);
@@ -57,14 +56,13 @@ class ToAccountIsTest extends TestCase
public function testTriggeredNot(): void public function testTriggeredNot(): void
{ {
$count = 0; $count = 0;
$transactionCount = 0;
do { do {
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first(); $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
$transaction = $journal->transactions()->where('amount', '>', 0)->first(); $transaction = $journal->transactions()->where('amount', '>', 0)->first();
$transactionCount = $journal->transactions()->count(); $transactionCount = $journal->transactions()->count();
$account = $transaction->account; $account = $transaction->account;
$count++; $count++;
} while ($account === null && $count < 30 && $transactionCount !== 1); } while ($account === null && $count < 30 && $transactionCount !== 2);
$trigger = ToAccountIs::makeFromStrings('some name' . random_int(1, 234), false); $trigger = ToAccountIs::makeFromStrings('some name' . random_int(1, 234), false);
$result = $trigger->triggered($journal); $result = $trigger->triggered($journal);