Update tests and code.

This commit is contained in:
James Cole
2018-12-21 16:38:10 +01:00
parent a5520d45e7
commit 2c7d94e5e9
24 changed files with 378 additions and 150 deletions

View File

@@ -63,12 +63,10 @@ class AccountControllerTest extends TestCase
public function testDelete(): void
{
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('destroy')->atLeast()->once()->andReturn(true);
$currencyRepos->shouldReceive('setUser')->once();
// get account:
$account = $this->getRandomAsset();
@@ -90,9 +88,8 @@ class AccountControllerTest extends TestCase
$accounts = factory(Account::class, 10)->create();
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
@@ -103,7 +100,6 @@ class AccountControllerTest extends TestCase
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('getAccountsByType')->withAnyArgs()->andReturn($accounts)->once();
// test API
@@ -126,10 +122,9 @@ class AccountControllerTest extends TestCase
public function testPiggyBanks(): void
{
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$transformer = $this->mock(PiggyBankTransformer::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
$transformer = $this->mock(PiggyBankTransformer::class);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
@@ -144,7 +139,7 @@ class AccountControllerTest extends TestCase
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('getPiggyBanks')->andReturn($piggies)->atLeast()->once();
// test API
@@ -169,10 +164,10 @@ class AccountControllerTest extends TestCase
public function testShow(): void
{
// mock stuff:
$account = $this->getRandomAsset();
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
$account = $this->getRandomAsset();
$repository = $this->mock(AccountRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
@@ -183,7 +178,7 @@ class AccountControllerTest extends TestCase
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
// test API
$response = $this->get(route('api.v1.accounts.show', [$account->id]));
@@ -201,12 +196,12 @@ class AccountControllerTest extends TestCase
public function testStoreInvalidBalance(): void
{
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
// data to submit
$data = [
@@ -239,16 +234,14 @@ class AccountControllerTest extends TestCase
public function testStoreLiability(): void
{
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
$account = $this->getRandomAsset();
$repository = $this->mock(AccountRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
$account = $this->getRandomAsset();
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('store')->atLeast()->once()->andReturn($account);
$currencyRepos->shouldReceive('findByCodeNull')->andReturn(null);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
@@ -287,13 +280,13 @@ class AccountControllerTest extends TestCase
public function testStoreNoCreditCardData(): void
{
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
// data to submit
$data = [
@@ -327,9 +320,9 @@ class AccountControllerTest extends TestCase
public function testStoreNoCurrencyInfo(): void
{
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
@@ -340,8 +333,6 @@ class AccountControllerTest extends TestCase
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('findByCodeNull')->atLeast()->once()->andReturnNull();
$repository->shouldReceive('store')->once()->andReturn(new Account);
// data to submit
@@ -367,13 +358,13 @@ class AccountControllerTest extends TestCase
public function testStoreNotUnique(): void
{
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$account = $this->getRandomAsset();
// data to submit
@@ -409,15 +400,13 @@ class AccountControllerTest extends TestCase
public function testStoreValid(): void
{
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
$account = $this->getRandomAsset();
$repository = $this->mock(AccountRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
$account = $this->getRandomAsset();
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('findByCodeNull')->atLeast()->once()->andReturnNull();
$repository->shouldReceive('store')->atLeast()->once()->andReturn($account);
// mock calls to transformer:
@@ -450,16 +439,15 @@ class AccountControllerTest extends TestCase
public function testStoreWithCurrencyCode(): void
{
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
$account = $this->getRandomAsset();
$repository = $this->mock(AccountRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
$account = $this->getRandomAsset();
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('store')->atLeast()->once()->andReturn($account);
$currencyRepos->shouldReceive('findByCodeNull')->atLeast()->once()->andReturn(new TransactionCurrency());
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
@@ -493,12 +481,10 @@ class AccountControllerTest extends TestCase
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(TransactionCollectorInterface::class);
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(TransactionTransformer::class);
// default mocks
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepository->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('isAsset')->atLeast()->once()->andReturnTrue();
// mock collector:
@@ -537,12 +523,10 @@ class AccountControllerTest extends TestCase
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(TransactionCollectorInterface::class);
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(TransactionTransformer::class);
// default mocks
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepository->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('isAsset')->atLeast()->once()->andReturnFalse();
// mock collector:
@@ -583,12 +567,10 @@ class AccountControllerTest extends TestCase
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(TransactionCollectorInterface::class);
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(TransactionTransformer::class);
// default mocks
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepository->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('isAsset')->atLeast()->once()->andReturnTrue();
// mock collector:
@@ -627,15 +609,14 @@ class AccountControllerTest extends TestCase
public function testUpdate(): void
{
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('update')->atLeast()->once();
$currencyRepos->shouldReceive('findByCodeNull')->andReturn(null);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
@@ -669,15 +650,14 @@ class AccountControllerTest extends TestCase
public function testUpdateCurrencyCode(): void
{
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('update')->atLeast()->once();
$currencyRepos->shouldReceive('findByCodeNull')->andReturn(TransactionCurrency::find(1));
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();

View File

@@ -260,26 +260,29 @@ class AttachmentControllerTest extends TestCase
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
// mock calls:
$journal = $this->getRandomWithdrawal();
$repository->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('store')->once()->andReturn($attachment);
$repository->shouldReceive('getNoteText')->andReturn('Hi There');
$journalRepos->shouldReceive('setUser')->once();
$journalRepos->shouldReceive('findNull')->once()->andReturn($this->user()->transactionJournals()->find(1));
$journalRepos->shouldReceive('findNull')->once()->andReturn($journal );
// data to submit
$data = [
'filename' => 'Some new att',
'description' => sprintf('Attempt #%d', random_int(1, 10000)),
'model' => TransactionJournal::class,
'model_id' => 1,
'model' => 'TransactionJournal',
'model_id' => $journal->id,
];
// test API
$response = $this->post(route('api.v1.attachments.store'), $data);
$response = $this->post(route('api.v1.attachments.store'), $data, ['accept' => 'application/json']);
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
@@ -314,7 +317,7 @@ class AttachmentControllerTest extends TestCase
$data = [
'filename' => $attachment->filename,
'description' => sprintf('Attempt #%d', random_int(1, 10000)),
'model' => TransactionJournal::class,
'model' => 'TransactionJournal',
'model_id' => 1,
];

View File

@@ -24,6 +24,8 @@ declare(strict_types=1);
namespace Tests\Api\V1\Controllers;
use Amount;
use FireflyIII\Factory\TransactionCurrencyFactory;
use FireflyIII\Models\AvailableBudget;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
@@ -60,6 +62,7 @@ class AvailableBudgetControllerTest extends TestCase
$repository = $this->mock(BudgetRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AvailableBudgetTransformer::class);
$factory = $this->mock(TransactionCurrencyFactory::class);
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
@@ -85,6 +88,7 @@ class AvailableBudgetControllerTest extends TestCase
$repository = $this->mock(BudgetRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AvailableBudgetTransformer::class);
$factory = $this->mock(TransactionCurrencyFactory::class);
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
@@ -115,6 +119,7 @@ class AvailableBudgetControllerTest extends TestCase
$repository = $this->mock(BudgetRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AvailableBudgetTransformer::class);
$factory = $this->mock(TransactionCurrencyFactory::class);
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
@@ -143,6 +148,7 @@ class AvailableBudgetControllerTest extends TestCase
$repository = $this->mock(BudgetRepositoryInterface::class);
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AvailableBudgetTransformer::class);
$factory = $this->mock(TransactionCurrencyFactory::class);
$availableBudget = new AvailableBudget;
// mock transformer
@@ -151,15 +157,15 @@ class AvailableBudgetControllerTest extends TestCase
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
$factory->shouldReceive('find')->withArgs([2, ''])->once()->andReturn(TransactionCurrency::find(2));
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('setAvailableBudget')->once()->andReturn($availableBudget);
$currencyRepository->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::find(1));
// data to submit
$data = [
'currency_id' => '1',
'currency_id' => '2',
'amount' => '100',
'start' => '2018-01-01',
'end' => '2018-01-31',
@@ -183,8 +189,9 @@ class AvailableBudgetControllerTest extends TestCase
{
// mock stuff:
$repository = $this->mock(BudgetRepositoryInterface::class);
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AvailableBudgetTransformer::class);
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$factory = $this->mock(TransactionCurrencyFactory::class);
$availableBudget = new AvailableBudget;
// mock transformer
@@ -193,20 +200,19 @@ class AvailableBudgetControllerTest extends TestCase
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
$factory->shouldReceive('find')->withArgs([0, ''])->once()->andReturnNull();
Amount::shouldReceive('getDefaultCurrency')->once()->andReturn(TransactionCurrency::find(5));
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepository->shouldReceive('findNull')->withArgs([1])->andReturn(null)->once();
$currencyRepository->shouldReceive('findByCodeNull')->withArgs(['EUR'])->andReturn(null)->once();
$repository->shouldReceive('setAvailableBudget')->once()->andReturn($availableBudget);
// data to submit
$data = [
'currency_id' => '1',
'currency_code' => 'EUR',
'amount' => '100',
'start' => '2018-01-01',
'end' => '2018-01-31',
'amount' => '100',
'start' => '2018-01-01',
'end' => '2018-01-31',
];
@@ -228,9 +234,10 @@ class AvailableBudgetControllerTest extends TestCase
$availableBudget = $this->user()->availableBudgets()->first();
// mock stuff:
$repository = $this->mock(BudgetRepositoryInterface::class);
$repository = $this->mock(BudgetRepositoryInterface::class);
$transformer = $this->mock(AvailableBudgetTransformer::class);
$factory = $this->mock(TransactionCurrencyFactory::class);
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AvailableBudgetTransformer::class);
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
@@ -239,15 +246,15 @@ class AvailableBudgetControllerTest extends TestCase
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
$factory->shouldReceive('find')->withArgs([0, 'EUR'])->once()->andReturnNull();
Amount::shouldReceive('getDefaultCurrency')->once()->andReturn(TransactionCurrency::find(5));
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('setAvailableBudget')->once()->andReturn($availableBudget);
$currencyRepository->shouldReceive('findNull')->withArgs([1])->andReturn(null)->once();
$currencyRepository->shouldReceive('findByCodeNull')->withArgs(['EUR'])->andReturn(TransactionCurrency::find(1))->once();
// data to submit
$data = [
'currency_id' => '1',
'currency_code' => 'EUR',
'amount' => '100',
'start' => '2018-01-01',
@@ -275,6 +282,7 @@ class AvailableBudgetControllerTest extends TestCase
$repository = $this->mock(BudgetRepositoryInterface::class);
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AvailableBudgetTransformer::class);
$factory = $this->mock(TransactionCurrencyFactory::class);
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
@@ -283,6 +291,8 @@ class AvailableBudgetControllerTest extends TestCase
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
$factory->shouldReceive('find')->withArgs([1, ''])->once()->andReturnNull();
/** @var AvailableBudget $availableBudget */
$availableBudget = $this->user()->availableBudgets()->first();
@@ -295,8 +305,8 @@ class AvailableBudgetControllerTest extends TestCase
$data = [
'currency_id' => '1',
'amount' => '100',
'start' => '2018-01-01',
'end' => '2018-01-31',
'start' => '2018-01-01',
'end' => '2018-01-31',
];
// test API