Improve test coverage.

This commit is contained in:
James Cole
2019-08-03 10:50:43 +02:00
parent b8b59b13a7
commit 75c2529d3e
23 changed files with 367 additions and 539 deletions

View File

@@ -509,6 +509,7 @@ class CurrencyControllerTest extends TestCase
$repository->shouldReceive('update')->andReturn(new TransactionCurrency);
$userRepos->shouldReceive('hasRole')->once()->andReturn(true);
$repository->shouldReceive('currencyInUse')->atLeast()->once()->andReturn(true);
Preferences::shouldReceive('mark')->atLeast()->once();
$this->session(['currencies.edit.uri' => 'http://localhost']);

View File

@@ -55,9 +55,9 @@ class ShowControllerTest extends TestCase
$withdrawal = $this->getRandomWithdrawalGroup();
$array = $this->getRandomWithdrawalGroupAsArray();
$array[0]['transactions'][0]['foreign_amount'] = '10';
$array[0]['transactions'][0]['foreign_currency_symbol'] = 'x';
$array[0]['transactions'][0]['foreign_currency_decimal_places'] = 2;
$array['transactions'][0]['foreign_amount'] = '10';
$array['transactions'][0]['foreign_currency_symbol'] = 'x';
$array['transactions'][0]['foreign_currency_decimal_places'] = 2;
$groupRepository = $this->mock(TransactionGroupRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
@@ -65,7 +65,7 @@ class ShowControllerTest extends TestCase
// mock for transformer:
$transformer->shouldReceive('setParameters')->atLeast()->once();
$transformer->shouldReceive('transformObject')->atLeast()->once()->andReturn($array[0]);
$transformer->shouldReceive('transformObject')->atLeast()->once()->andReturn($array);
// mock for repos
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);

View File

@@ -41,6 +41,7 @@ use FireflyIII\Models\Configuration;
use FireflyIII\Models\CurrencyExchangeRate;
use FireflyIII\Models\ImportJob;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankEvent;
use FireflyIII\Models\Preference;
use FireflyIII\Models\Recurrence;
use FireflyIII\Models\Rule;
@@ -67,6 +68,14 @@ use RuntimeException;
abstract class TestCase extends BaseTestCase
{
/**
* @return ImportJob
*/
public function getRandomPiggyBankEvent(): PiggyBankEvent
{
return PiggyBankEvent::inRandomOrder()->first();
}
/**
* @return ImportJob
*/
@@ -347,7 +356,6 @@ abstract class TestCase extends BaseTestCase
/**
* @return array
* @throws Exception
*/
public function getRandomWithdrawalGroupAsArray(): array
{
@@ -360,12 +368,15 @@ abstract class TestCase extends BaseTestCase
$e->getMessage();
}
return [
return
[
'group_title' => null,
'transactions' => [
[
'updated_at' => new Carbon,
'created_at' => new Carbon,
'transaction_journal_id' => $withdrawal->id,
'transaction_type_type' => 'Withdrawal',
'currency_id' => $euro->id,
'foreign_currency_id' => null,
'date' => $date,
@@ -380,8 +391,47 @@ abstract class TestCase extends BaseTestCase
'budget_id' => $budget->id,
],
],
],
];
];
}
/**
* @return array
*/
public function getRandomDepositGroupAsArray(): array
{
$deposit = $this->getRandomDeposit();
$euro = $this->getEuro();
$budget = $this->getRandomBudget();
try {
$date = new Carbon;
} catch (Exception $e) {
$e->getMessage();
}
return
[
'group_title' => null,
'transactions' => [
[
'updated_at' => new Carbon,
'created_at' => new Carbon,
'transaction_journal_id' => $deposit->id,
'transaction_type_type' => 'Deposit',
'currency_id' => $euro->id,
'foreign_currency_id' => null,
'date' => $date,
'source_id' => 1,
'destination_id' => 4,
'currency_name' => $euro->name,
'currency_code' => $euro->code,
'currency_symbol' => $euro->symbol,
'currency_decimal_places' => $euro->decimal_places,
'amount' => '-30',
'foreign_amount' => null,
'budget_id' => $budget->id,
],
],
];
}
/**

View File

@@ -65,22 +65,23 @@ class MonthReportGeneratorTest extends TestCase
$dollar = $this->getDollar();
$return = [
[
'description' => 'Hello',
'amount' => '10',
'foreign_currency_id' => null,
'currency_id' => $euro->id,
'source_id' => $asset->id,
'source_name' => $asset->name,
'description' => 'Hello',
'amount' => '10',
'foreign_currency_id' => null,
'currency_id' => $euro->id,
'source_id' => $asset->id,
'source_name' => $asset->name,
'transaction_journal_id' => 1,
],
[
'description' => 'Hello2',
'amount' => '10',
'foreign_amount' => '10',
'foreign_currency_id' => $euro->id,
'currency_id' => $dollar->id,
'source_id' => $asset->id,
'source_name' => $asset->name,
'description' => 'Hello2',
'amount' => '10',
'foreign_amount' => '10',
'foreign_currency_id' => $euro->id,
'currency_id' => $dollar->id,
'source_id' => $asset->id,
'source_name' => $asset->name,
'transaction_journal_id' => 1,
],
];
@@ -100,14 +101,15 @@ class MonthReportGeneratorTest extends TestCase
// mock calls
Steam::shouldReceive('balance')->times(2)->andReturn('100');
$accountRepos->shouldReceive('setUser')->once();
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1')->once();
//$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1')->once();
$accountRepos->shouldReceive('getAccountCurrency')->atLeast()->once()->andReturn($euro);
// mock collector:
$collector->shouldReceive('setAccounts')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('withAccountInformation')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn($return);
$currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn($euro)->once();
//$currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn($euro)->once();
try {
$result = $generator->getAuditReport($asset, $date);

View File

@@ -116,7 +116,7 @@ class BinderTest extends TestCase
public function testAccountListEmpty(): void
{
Route::middleware(Binder::class)->any(
'/_test/binder/{accountList}', function (Collection $accounts) {
'/_test/binder/{accountList}', static function (Collection $accounts) {
return 'count: ' . $accounts->count();
}
);

View File

@@ -144,7 +144,8 @@ class AssetAccountMapperTest extends TestCase
// mock repository:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('findByIbanNull')->once()->withArgs([$searchValue, [AccountType::ASSET]])->andReturn($expected);
$repository->shouldReceive('findByIbanNull')->once()
->withArgs([$searchValue, [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]])->andReturn($expected);
$mapper = new AssetAccountMapper;
$mapper->setUser($this->user());
@@ -164,7 +165,8 @@ class AssetAccountMapperTest extends TestCase
// mock repository:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('findByName')->once()->withArgs([$searchValue, [AccountType::ASSET]])->andReturn($expected);
$repository->shouldReceive('findByName')->once()
->withArgs([$searchValue, [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]])->andReturn($expected);
$mapper = new AssetAccountMapper;
$mapper->setUser($this->user());
@@ -184,7 +186,8 @@ class AssetAccountMapperTest extends TestCase
// mock repository:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('findByAccountNumber')->once()->withArgs([$searchValue, [AccountType::ASSET]])->andReturn($expected);
$repository->shouldReceive('findByAccountNumber')->once()
->withArgs([$searchValue, [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]])->andReturn($expected);
$mapper = new AssetAccountMapper;
$mapper->setUser($this->user());

View File

@@ -90,6 +90,9 @@ class OpposingAccountMapperTest extends TestCase
$repository->shouldReceive('findNull')->andReturn($expected)->once();
$repository->shouldReceive('findByName')->withArgs([$expected->name, [AccountType::EXPENSE]])->andReturnNull();
$repository->shouldReceive('findByName')->withArgs([$expected->name, [AccountType::ASSET]])->andReturnNull();
$repository->shouldReceive('findByName')->withArgs([$expected->name, [AccountType::DEBT]])->andReturnNull();
$repository->shouldReceive('findByName')->withArgs([$expected->name, [AccountType::MORTGAGE]])->andReturnNull();
$repository->shouldReceive('findByName')->withArgs([$expected->name, [AccountType::LOAN]])->andReturnNull();
$repository->shouldReceive('store')->withArgs([$expectedArgs])->once()
->andReturn(new Account);
@@ -127,8 +130,15 @@ class OpposingAccountMapperTest extends TestCase
$repository->shouldReceive('findNull')->andReturn($expected)->once();
$repository->shouldReceive('findByIbanNull')->withArgs([$expected->iban, [AccountType::EXPENSE]])->andReturnNull();
$repository->shouldReceive('findByIbanNull')->withArgs([$expected->iban, [AccountType::ASSET]])->andReturnNull();
$repository->shouldReceive('findByIbanNull')->withArgs([$expected->iban, [AccountType::DEBT]])->andReturnNull();
$repository->shouldReceive('findByIbanNull')->withArgs([$expected->iban, [AccountType::MORTGAGE]])->andReturnNull();
$repository->shouldReceive('findByIbanNull')->withArgs([$expected->iban, [AccountType::LOAN]])->andReturnNull();
$repository->shouldReceive('findByName')->withArgs([$expected->name, [AccountType::EXPENSE]])->andReturnNull();
$repository->shouldReceive('findByName')->withArgs([$expected->name, [AccountType::ASSET]])->andReturnNull();
$repository->shouldReceive('findByName')->withArgs([$expected->name, [AccountType::DEBT]])->andReturnNull();
$repository->shouldReceive('findByName')->withArgs([$expected->name, [AccountType::MORTGAGE]])->andReturnNull();
$repository->shouldReceive('findByName')->withArgs([$expected->name, [AccountType::LOAN]])->andReturnNull();
$repository->shouldReceive('store')->withArgs([$expectedArgs])->once()
->andReturn(new Account);

View File

@@ -74,8 +74,8 @@ class StageImportDataHandlerTest extends TestCase
$today = new Carbon;
// create fake transactions:
$op1 = 'Some opposing account #' . random_int(1, 100);
$op2 = 'Some opposing revenue account #' . random_int(1, 100);
$op1 = 'Some opposing account #' . $this->randomInt();
$op2 = 'Some opposing revenue account #' . $this->randomInt();
$transactions = [
new SpectreTransaction(
[
@@ -85,7 +85,7 @@ class StageImportDataHandlerTest extends TestCase
'made_on' => $today->toW3cString(),
'amount' => -123.45,
'currency_code' => 'EUR',
'description' => 'Fake description #' . random_int(1, 100),
'description' => 'Fake description #' . $this->randomInt(),
'category' => 'some-category',
'duplicated' => false,
'extra' => [
@@ -104,7 +104,7 @@ class StageImportDataHandlerTest extends TestCase
'made_on' => $today->toW3cString(),
'amount' => 563.21,
'currency_code' => 'EUR',
'description' => 'Fake second description #' . random_int(1, 100),
'description' => 'Fake second description #' . $this->randomInt(),
'category' => 'some-other-category',
'duplicated' => false,
'extra' => [
@@ -142,28 +142,26 @@ class StageImportDataHandlerTest extends TestCase
$lrRequest = $this->mock(ListTransactionsRequest::class);
$mapper = $this->mock(OpposingAccountMapper::class);
// expected result
$expected = [
0 => [
'type' => 'withdrawal',
'date' => $today->format('Y-m-d'),
'tags' => ['mode', 'active'],
'user' => $job->user_id,
'notes' => "Imported from \"Fake Spectre Account\" \npayee: " . $op1 . " \n",
'external_id' => '1',
// journal data:
'description' => $transactions[0]->getDescription(),
'piggy_bank_id' => null,
'piggy_bank_name' => null,
'bill_id' => null,
'bill_name' => null,
'original-source' => sprintf('spectre-v%s', config('firefly.version')),
// transaction data:
'transactions' => [
[
'transactions' => [
0 => [
// transaction here
'date' => $today->format('Y-m-d'),
'tags' => ['mode', 'active'],
'type' => 'withdrawal',
'user' => $job->user_id,
'notes' => "Imported from \"Fake Spectre Account\" \npayee: " . $op1 . " \n",
'external_id' => '1',
// journal data:
'description' => $transactions[0]->getDescription(),
'piggy_bank_id' => null,
'piggy_bank_name' => null,
'bill_id' => null,
'bill_name' => null,
'original-source' => sprintf('spectre-v%s', config('firefly.version')),
'currency_id' => null,
'currency_code' => 'EUR',
'description' => null,
'amount' => '-123.45',
'budget_id' => null,
'budget_name' => null,
@@ -180,28 +178,26 @@ class StageImportDataHandlerTest extends TestCase
'identifier' => 0,
],
],
],
1 => [
'type' => 'deposit',
'date' => $today->format('Y-m-d'),
'tags' => ['mode', 'active'],
'user' => $job->user_id,
'notes' => "Imported from \"Fake Spectre Account\" \npayee: " . $op2 . " \n",
'external_id' => '2',
// journal data:
'description' => $transactions[1]->getDescription(),
'piggy_bank_id' => null,
'piggy_bank_name' => null,
'bill_id' => null,
'bill_name' => null,
'original-source' => sprintf('spectre-v%s', config('firefly.version')),
// transaction data:
'transactions' => [
[
'transactions' => [
0 => [
// transaction here
'date' => $today->format('Y-m-d'),
'tags' => ['mode', 'active'],
'type' => 'deposit',
'user' => $job->user_id,
'notes' => "Imported from \"Fake Spectre Account\" \npayee: " . $op2 . " \n",
'external_id' => '2',
// journal data:
'description' => $transactions[1]->getDescription(),
'piggy_bank_id' => null,
'piggy_bank_name' => null,
'bill_id' => null,
'bill_name' => null,
'original-source' => sprintf('spectre-v%s', config('firefly.version')),
'currency_id' => null,
'currency_code' => 'EUR',
'description' => null,
'amount' => '563.21',
'budget_id' => null,
'budget_name' => null,
@@ -218,14 +214,13 @@ class StageImportDataHandlerTest extends TestCase
'identifier' => 0,
],
],
],
];
$accountRepos->shouldReceive('setUser')->once();
$accountRepos->shouldReceive('findNull')->once()->withArgs([322])->andReturn($account);
$importRepos->shouldReceive('setUser')->once();
$importRepos->shouldReceive('setTransactions')->once()
->withArgs([Mockery::any(), $expected]);
$importRepos->shouldReceive('setTransactions')->once()->withArgs([Mockery::any(), $expected]);
$lrRequest->shouldReceive('setUser')->once();
$lrRequest->shouldReceive('setAccount')->once()->withArgs([Mockery::any()]);
$lrRequest->shouldReceive('call')->once();
@@ -273,9 +268,9 @@ class StageImportDataHandlerTest extends TestCase
$today = new Carbon;
// create fake transactions:
$op1 = 'Some opposing account #' . random_int(1, 100);
$op2 = 'Some opposing revenue account #' . random_int(1, 100);
$transactions = [
$op1 = 'Some opposing account #' . $this->randomInt();
$op2 = 'Some opposing revenue account #' . $this->randomInt();
$transactions = [
new SpectreTransaction(
[
'id' => 1,
@@ -284,7 +279,7 @@ class StageImportDataHandlerTest extends TestCase
'made_on' => $today->toW3cString(),
'amount' => -123.45,
'currency_code' => 'EUR',
'description' => 'Fake description #' . random_int(1, 100),
'description' => 'Fake description #' . $this->randomInt(),
'category' => 'some-category',
'duplicated' => true,
'extra' => [
@@ -304,7 +299,7 @@ class StageImportDataHandlerTest extends TestCase
'made_on' => $today->toW3cString(),
'amount' => 563.21,
'currency_code' => 'EUR',
'description' => 'Fake second description #' . random_int(1, 100),
'description' => 'Fake second description #' . $this->randomInt(),
'category' => 'some-other-category',
'duplicated' => false,
'extra' => [
@@ -345,29 +340,26 @@ class StageImportDataHandlerTest extends TestCase
$lrRequest = $this->mock(ListTransactionsRequest::class);
$mapper = $this->mock(OpposingAccountMapper::class);
// expected result
$expected = [
0 => [
'type' => 'withdrawal',
'date' => $today->format('Y-m-d'),
'tags' => ['mode', 'active', 'possibly-duplicated'],
'user' => $job->user_id,
'notes' => "Imported from \"Fake Spectre Account\" \npayee: " . $op1 . " \n",
'external_id' => '1',
// journal data:
'description' => $transactions[0]->getDescription(),
'piggy_bank_id' => null,
'piggy_bank_name' => null,
'bill_id' => null,
'bill_name' => null,
'original-source' => sprintf('spectre-v%s', config('firefly.version')),
// transaction data:
'transactions' => [
[
'transactions' => [
0 => [
// data here.
'date' => $today->format('Y-m-d'),
'type' => 'withdrawal',
'tags' => ['mode', 'active', 'possibly-duplicated'],
'user' => $job->user_id,
'notes' => "Imported from \"Fake Spectre Account\" \npayee: " . $op1 . " \n",
'external_id' => '1',
// journal data:
'description' => $transactions[0]->getDescription(),
'piggy_bank_id' => null,
'piggy_bank_name' => null,
'bill_id' => null,
'bill_name' => null,
'original-source' => sprintf('spectre-v%s', config('firefly.version')),
'currency_id' => null,
'currency_code' => 'EUR',
'description' => null,
'amount' => '-123.45',
'budget_id' => null,
'budget_name' => null,
@@ -386,26 +378,24 @@ class StageImportDataHandlerTest extends TestCase
],
],
1 => [
'type' => 'deposit',
'date' => $today->format('Y-m-d'),
'tags' => ['mode', 'active', 'cat-name'],
'user' => $job->user_id,
'notes' => "Imported from \"Fake Spectre Account\" \npayee: " . $op2 . " \n",
'external_id' => '2',
// journal data:
'description' => $transactions[1]->getDescription(),
'piggy_bank_id' => null,
'piggy_bank_name' => null,
'bill_id' => null,
'bill_name' => null,
'original-source' => sprintf('spectre-v%s', config('firefly.version')),
// transaction data:
'transactions' => [
[
'transactions' => [
0 => [
// data here.
'date' => $today->format('Y-m-d'),
'type' => 'deposit',
'tags' => ['mode', 'active', 'cat-name'],
'user' => $job->user_id,
'notes' => "Imported from \"Fake Spectre Account\" \npayee: " . $op2 . " \n",
'external_id' => '2',
// journal data:
'description' => $transactions[1]->getDescription(),
'piggy_bank_id' => null,
'piggy_bank_name' => null,
'bill_id' => null,
'bill_name' => null,
'original-source' => sprintf('spectre-v%s', config('firefly.version')),
'currency_id' => null,
'currency_code' => 'EUR',
'description' => null,
'amount' => '563.21',
'budget_id' => null,
'budget_name' => null,
@@ -424,6 +414,7 @@ class StageImportDataHandlerTest extends TestCase
],
],
];
$accountRepos->shouldReceive('setUser')->once();
$accountRepos->shouldReceive('findNull')->once()->withArgs([322])->andReturn($account);
$importRepos->shouldReceive('setUser')->once();
@@ -474,8 +465,8 @@ class StageImportDataHandlerTest extends TestCase
$today = new Carbon;
// create fake transactions:
$op1 = 'Some opposing account #' . random_int(1, 100);
$op2 = 'Some opposing revenue account #' . random_int(1, 100);
$op1 = 'Some opposing account #' . $this->randomInt();
$op2 = 'Some opposing revenue account #' . $this->randomInt();
$transactions = [
new SpectreTransaction(
[
@@ -485,7 +476,7 @@ class StageImportDataHandlerTest extends TestCase
'made_on' => $today->toW3cString(),
'amount' => -123.45,
'currency_code' => 'EUR',
'description' => 'Fake description #' . random_int(1, 100),
'description' => 'Fake description #' . $this->randomInt(),
'category' => 'some-category',
'duplicated' => true,
'extra' => [
@@ -504,7 +495,7 @@ class StageImportDataHandlerTest extends TestCase
'made_on' => $today->toW3cString(),
'amount' => 563.21,
'currency_code' => 'EUR',
'description' => 'Fake second description #' . random_int(1, 100),
'description' => 'Fake second description #' . $this->randomInt(),
'category' => 'some-other-category',
'duplicated' => false,
'extra' => [
@@ -543,29 +534,26 @@ class StageImportDataHandlerTest extends TestCase
$lrRequest = $this->mock(ListTransactionsRequest::class);
$mapper = $this->mock(OpposingAccountMapper::class);
// expected result
$expected = [
0 => [
'type' => 'withdrawal',
'date' => $today->format('Y-m-d'),
'tags' => ['mode', 'active', 'possibly-duplicated'],
'user' => $job->user_id,
'notes' => "Imported from \"Fake Spectre Account\" \npayee: " . $op1 . " \n",
'external_id' => '1',
// journal data:
'description' => $transactions[0]->getDescription(),
'piggy_bank_id' => null,
'piggy_bank_name' => null,
'bill_id' => null,
'bill_name' => null,
'original-source' => sprintf('spectre-v%s', config('firefly.version')),
// transaction data:
'transactions' => [
[
'transactions' => [
0 => [
// data here
'date' => $today->format('Y-m-d'),
'type' => 'withdrawal',
'tags' => ['mode', 'active', 'possibly-duplicated'],
'user' => $job->user_id,
'notes' => "Imported from \"Fake Spectre Account\" \npayee: " . $op1 . " \n",
'external_id' => '1',
// journal data:
'description' => $transactions[0]->getDescription(),
'piggy_bank_id' => null,
'piggy_bank_name' => null,
'bill_id' => null,
'bill_name' => null,
'original-source' => sprintf('spectre-v%s', config('firefly.version')),
'currency_id' => null,
'currency_code' => 'EUR',
'description' => null,
'amount' => '-123.45',
'budget_id' => null,
'budget_name' => null,
@@ -584,26 +572,24 @@ class StageImportDataHandlerTest extends TestCase
],
],
1 => [
'type' => 'deposit',
'date' => $today->format('Y-m-d'),
'tags' => ['mode', 'active', 'cat-name'],
'user' => $job->user_id,
'notes' => "Imported from \"Fake Spectre Account\" \npayee: " . $op2 . " \n",
'external_id' => '2',
// journal data:
'description' => $transactions[1]->getDescription(),
'piggy_bank_id' => null,
'piggy_bank_name' => null,
'bill_id' => null,
'bill_name' => null,
'original-source' => sprintf('spectre-v%s', config('firefly.version')),
// transaction data:
'transactions' => [
[
'transactions' => [
0 => [
// data here
'date' => $today->format('Y-m-d'),
'type' => 'deposit',
'tags' => ['mode', 'active', 'cat-name'],
'user' => $job->user_id,
'notes' => "Imported from \"Fake Spectre Account\" \npayee: " . $op2 . " \n",
'external_id' => '2',
// journal data:
'description' => $transactions[1]->getDescription(),
'piggy_bank_id' => null,
'piggy_bank_name' => null,
'bill_id' => null,
'bill_name' => null,
'original-source' => sprintf('spectre-v%s', config('firefly.version')),
'currency_id' => null,
'currency_code' => 'EUR',
'description' => null,
'amount' => '563.21',
'budget_id' => null,
'budget_name' => null,
@@ -622,6 +608,7 @@ class StageImportDataHandlerTest extends TestCase
],
],
];
$accountRepos->shouldReceive('setUser')->once();
$accountRepos->shouldReceive('findNull')->once()->withArgs([322])->andReturn($account);
$importRepos->shouldReceive('setUser')->once();

View File

@@ -109,10 +109,7 @@ class BillTransformerTest extends TestCase
$this->assertEquals('2018-03-01', $result['next_expected_match']);
$this->assertEquals(['2018-01-01'], $result['pay_dates']);
$this->assertEquals(
['2018-01-02', '2018-01-09', '2018-01-16', '2018-01-21', '2018-01-30',]
, $result['paid_dates']
);
$this->assertEquals(['2018-01-02', '2018-01-09', '2018-01-16', '2018-01-21', '2018-01-30',], $result['paid_dates']);
}
}

View File

@@ -68,16 +68,18 @@ class PiggyBankEventTransformerTest extends TestCase
// mock calls:
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->atLeast()->once()->andReturn(1);
$currencyRepos->shouldReceive('findNull')->withArgs([1])->atLeast()->once()->andReturn($this->getEuro());
$piggyRepos->shouldReceive('getTransactionWithEvent')->atLeast()->once()->andReturn(123);
$event = PiggyBankEvent::first();
$event = $this->getRandomPiggyBankEvent();
$transformer = app(PiggyBankEventTransformer::class);
$transformer->setParameters(new ParameterBag);
$result = $transformer->transform($event);
$this->assertEquals($event->id, $result['id']);
$this->assertEquals(245, $result['amount']);
$this->assertEquals(123, $result['transaction_id']);
$this->assertEquals($event->amount, $result['amount']);
$this->assertEquals($event->transaction_journal_id, $result['transaction_journal_id']);
}
@@ -100,18 +102,17 @@ class PiggyBankEventTransformerTest extends TestCase
// mock calls:
$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->atLeast()->once()->andReturn(1);
$currencyRepos->shouldReceive('findNull')->withArgs([1])->atLeast()->once()->andReturn(null);
$piggyRepos->shouldReceive('getTransactionWithEvent')->atLeast()->once()->andReturn(123);
Amount::shouldReceive('getDefaultCurrencyByUser')->andReturn($this->getEuro())->atLeast()->once();
$event = PiggyBankEvent::first();
$event = $this->getRandomPiggyBankEvent();
$transformer = app(PiggyBankEventTransformer::class);
$transformer->setParameters(new ParameterBag);
$result = $transformer->transform($event);
$this->assertEquals($event->id, $result['id']);
$this->assertEquals(245, $result['amount']);
$this->assertEquals(123, $result['transaction_id']);
$this->assertEquals($event->amount, $result['amount']);
$this->assertEquals($event->transaction_journal_id, $result['transaction_journal_id']);
}
}

View File

@@ -25,18 +25,15 @@ namespace Tests\Unit\Transformers;
use Carbon\Carbon;
use FireflyIII\Factory\CategoryFactory;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\Recurrence;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\RecurrenceTransaction;
use FireflyIII\Models\RecurrenceTransactionMeta;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
use FireflyIII\Transformers\RecurrenceTransformer;
use Log;
use Mockery;
use Symfony\Component\HttpFoundation\ParameterBag;
use Tests\TestCase;
@@ -56,7 +53,7 @@ class RecurrenceTransformerTest extends TestCase
}
/**
*
* @covers \FireflyIII\Transformers\RecurrenceTransformer
*/
public function testBasic(): void
{
@@ -65,12 +62,13 @@ class RecurrenceTransformerTest extends TestCase
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$factory = $this->mock(CategoryFactory::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$category = Category::first();
$budget = Budget::first();
$piggy = PiggyBank::first();
$bill = Bill::first();
$foreignCurrency = TransactionCurrency::find(2);
$category = $this->getRandomCategory();
$budget = $this->getRandomBudget();
$piggy = $this->getRandomPiggyBank();
$bill = $this->getRandomBill();
$foreignCurrency = $this->getDollar();
$ranges = [new Carbon];
$recurrence = $this->getRandomRecurrence();
// mock calls:
$recurrenceRepos->shouldReceive('setUser')->atLeast()->once();
$billRepos->shouldReceive('setUser')->atLeast()->once();
@@ -82,47 +80,21 @@ class RecurrenceTransformerTest extends TestCase
$recurrenceRepos->shouldReceive('getNoteText')->once()->andReturn('Hi there');
$recurrenceRepos->shouldReceive('repetitionDescription')->once()->andReturn('Rep descr');
$recurrenceRepos->shouldReceive('getXOccurrences')->andReturn($ranges)->atLeast()->once();
$factory->shouldReceive('findOrCreate')->atLeast()->once()->withArgs([null, 'House'])->andReturn($category);
$factory->shouldReceive('findOrCreate')->atLeast()->once()->withArgs([null,Mockery::any()])->andReturn($category);
$budgetRepos->shouldReceive('findNull')->atLeast()->once()->withArgs([2])->andReturn($budget);
$piggyRepos->shouldReceive('findNull')->atLeast()->once()->withArgs([1])->andReturn($piggy);
$billRepos->shouldReceive('find')->atLeast()->once()->withArgs([1])->andReturn($bill);
// basic transformation:
/** @var Recurrence $recurrence */
$recurrence = Recurrence::find(1);
$transformer = app(RecurrenceTransformer::class);
$transformer->setParameters(new ParameterBag);
$result = $transformer->transform($recurrence);
$this->assertEquals(1, $result['id']);
$this->assertEquals($recurrence->id, $result['id']);
$this->assertEquals('withdrawal', $result['transaction_type']);
$this->assertEquals(true, $result['apply_rules']);
$this->assertEquals(
[
[
'value' => 'auto-generated',
'tags' => ['auto-generated'],
'name' => 'tags',
],
[
'name' => 'piggy_bank_id',
'piggy_bank_id' => 1,
'piggy_bank_name' => 'New camera',
'value' => '1',
],
[
'bill_id' => 1,
'bill_name' => 'Rent',
'name' => 'bill_id',
'value' => '1',
],
]
, $result['meta']
);
$this->assertEquals($foreignCurrency->code, $result['transactions'][0]['foreign_currency_code']);
$this->assertEquals('Rep descr', $result['recurrence_repetitions'][0]['description']);

View File

@@ -0,0 +1,120 @@
<?php
/**
* TransactionGroupTransformerTest.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Tests\Unit\Transformers;
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
use FireflyIII\Support\NullArrayObject;
use FireflyIII\Transformers\TransactionGroupTransformer;
use Log;
use Mockery;
use Tests\TestCase;
/**
* Class TransactionGroupTransformerTest
*/
class TransactionGroupTransformerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
Log::info(sprintf('Now in %s.', get_class($this)));
}
/**
* @covers \FireflyIII\Transformers\TransactionGroupTransformer
*/
public function testBasic(): void
{
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
$group = $this->getRandomWithdrawalGroup();
$first = $group->transactionJournals()->first();
// mock calls
$repository->shouldReceive('getMetaFields')->withArgs([$first->id, Mockery::any()])->andReturn(new NullArrayObject([]))->atLeast()->once();
$repository->shouldReceive('getMetaDateFields')->withArgs([$first->id, Mockery::any()])->andReturn(new NullArrayObject([]))->atLeast()->once();
$repository->shouldReceive('getNoteText')->atLeast()->once()->andReturn('note');
$repository->shouldReceive('getTags')->atLeast()->once()->andReturn([]);
$transformer = new TransactionGroupTransformer;
$result = $transformer->transformObject($group);
}
/**
* @covers \FireflyIII\Transformers\TransactionGroupTransformer
*/
public function testArray(): void {
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
$group = $this->getRandomWithdrawalGroupAsArray();
// mock calls
$repository->shouldReceive('getMetaFields')->withArgs([Mockery::any(), Mockery::any()])->andReturn(new NullArrayObject([]))->atLeast()->once();
$repository->shouldReceive('getMetaDateFields')->withArgs([Mockery::any(), Mockery::any()])->andReturn(new NullArrayObject([]))->atLeast()->once();
$repository->shouldReceive('getNoteText')->atLeast()->once()->andReturn('note');
$repository->shouldReceive('getTags')->atLeast()->once()->andReturn([]);
$transformer = new TransactionGroupTransformer;
$result = $transformer->transform($group);
}
/**
* @covers \FireflyIII\Transformers\TransactionGroupTransformer
*/
public function testArrayDeposit(): void {
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
$group = $this->getRandomDepositGroupAsArray();
// mock calls
$repository->shouldReceive('getMetaFields')->withArgs([Mockery::any(), Mockery::any()])->andReturn(new NullArrayObject([]))->atLeast()->once();
$repository->shouldReceive('getMetaDateFields')->withArgs([Mockery::any(), Mockery::any()])->andReturn(new NullArrayObject([]))->atLeast()->once();
$repository->shouldReceive('getNoteText')->atLeast()->once()->andReturn('note');
$repository->shouldReceive('getTags')->atLeast()->once()->andReturn([]);
$transformer = new TransactionGroupTransformer;
$result = $transformer->transform($group);
}
/**
* @covers \FireflyIII\Transformers\TransactionGroupTransformer
*/
public function testDeposit(): void
{
$repository = $this->mock(TransactionGroupRepositoryInterface::class);
$group = $this->getRandomDepositGroup();
$first = $group->transactionJournals()->first();
// mock calls
$repository->shouldReceive('getMetaFields')->withArgs([$first->id, Mockery::any()])->andReturn(new NullArrayObject([]))->atLeast()->once();
$repository->shouldReceive('getMetaDateFields')->withArgs([$first->id, Mockery::any()])->andReturn(new NullArrayObject([]))->atLeast()->once();
$repository->shouldReceive('getNoteText')->atLeast()->once()->andReturn('note');
$repository->shouldReceive('getTags')->atLeast()->once()->andReturn([]);
$transformer = new TransactionGroupTransformer;
$result = $transformer->transformObject($group);
}
}