Improve test coverage for API.

This commit is contained in:
James Cole
2018-12-16 13:55:19 +01:00
parent 746bd2ce92
commit 89942ee49c
32 changed files with 1534 additions and 823 deletions

View File

@@ -25,16 +25,19 @@ namespace Tests\Api\V1\Controllers;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\Transformers\AccountTransformer;
use FireflyIII\Transformers\PiggyBankTransformer;
use FireflyIII\Transformers\TransactionTransformer;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Laravel\Passport\Passport;
use Log;
use Mockery;
use Tests\TestCase;
/**
@@ -63,15 +66,15 @@ class AccountControllerTest extends TestCase
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('destroy')->once()->andReturn(true);
$repository->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('destroy')->atLeast()->once()->andReturn(true);
$currencyRepos->shouldReceive('setUser')->once();
// get account:
$account = $this->user()->accounts()->first();
$account = $this->getRandomAsset();
// call API
$response = $this->delete('/api/v1/accounts/' . $account->id);
$response = $this->delete(route('api.v1.accounts.delete', [$account->id]));
$response->assertStatus(204);
}
@@ -89,19 +92,19 @@ class AccountControllerTest extends TestCase
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
// mock calls:
$repository->shouldReceive('setUser');
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('getAccountsByType')->withAnyArgs()->andReturn($accounts)->once();
$currencyRepos->shouldReceive('setUser');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('defaultAsset');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountNumber'])->andReturn('1');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('BIC');
$repository->shouldReceive('getNoteText')->withArgs([Mockery::any()])->andReturn('Hello');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest'])->andReturn('2')->atLeast()->once();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest_period'])->andReturn('daily')->atLeast()->once();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn(true)->atLeast()->once();
// test API
$response = $this->get('/api/v1/accounts');
@@ -115,35 +118,106 @@ class AccountControllerTest extends TestCase
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Test the list of piggy banks.
*
* @covers \FireflyIII\Api\V1\Controllers\AccountController
*/
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);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
// get piggies for this user.
$piggies = factory(PiggyBank::class, 10)->create();
$asset = $this->getRandomAsset();
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('getPiggyBanks')->andReturn($piggies)->atLeast()->once();
// test API
$response = $this->get(route('api.v1.accounts.piggy_banks', [$asset->id]));
$response->assertStatus(200);
$response->assertJson(['data' => [],]);
$response->assertJson(
['meta' => ['pagination' => ['total' => $piggies->count(), 'count' => $piggies->count(), 'per_page' => true, 'current_page' => 1,
'total_pages' => 1]],]
);
$response->assertJson(['links' => ['self' => true, 'first' => true, 'last' => true,],]);
$response->assertSee('page=1'); // default returns this.
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Show an account.
*
* @covers \FireflyIII\Api\V1\Controllers\AccountController
*/
public function testShow(): void
{
// mock stuff:
$account = $this->getRandomAsset();
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
// mock calls:
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
// test API
$response = $this->get(route('api.v1.accounts.show', [$account->id]));
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'accounts', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Opening balance without date.
*
* @covers \FireflyIII\Api\V1\Controllers\AccountController
* @covers \FireflyIII\Api\V1\Requests\AccountRequest
*/
public function testInvalidBalance(): void
public function testStoreInvalidBalance(): void
{
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$currencyRepos->shouldReceive('setUser')->once();
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
// data to submit
$data = [
'name' => 'Some new asset account #' . random_int(1, 10000),
'currency_id' => 1,
'type' => 'asset',
'active' => 1,
'include_net_worth' => 1,
'account_role' => 'defaultAsset',
'opening_balance' => '123.45',
'name' => 'Some new asset account #' . random_int(1, 10000),
'type' => 'asset',
'account_role' => 'defaultAsset',
'opening_balance' => '123.45',
];
// test API
$response = $this->post('/api/v1/accounts', $data, ['Accept' => 'application/json']);
$response = $this->post(route('api.v1.accounts.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(422);
$response->assertExactJson(
[
@@ -156,34 +230,80 @@ class AccountControllerTest extends TestCase
$response->assertHeader('Content-Type', 'application/json');
}
/**
* Send correct data. Should call account repository store method.
*
* @covers \FireflyIII\Api\V1\Controllers\AccountController
* @covers \FireflyIII\Api\V1\Requests\AccountRequest
*/
public function testStoreLiability(): void
{
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::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();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
// data to submit
$data = [
'name' => 'Some new liability account #' . random_int(1, 10000),
'type' => 'liability',
'liability_amount' => '10000',
'liability_start_date' => '2016-01-01',
'liability_type' => 'mortgage',
'active' => true,
'interest' => '1',
'interest_period' => 'daily',
];
// test API
$response = $this->post(route('api.v1.accounts.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'accounts', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* CC type present when account is a credit card.
*
* @covers \FireflyIII\Api\V1\Controllers\AccountController
* @covers \FireflyIII\Api\V1\Requests\AccountRequest
*/
public function testNoCreditCardData(): void
public function testStoreNoCreditCardData(): void
{
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$currencyRepos->shouldReceive('setUser')->once();
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
// data to submit
$data = [
'name' => 'Some new asset account #' . random_int(1, 10000),
'type' => 'asset',
'active' => 1,
'include_net_worth' => 1,
'account_role' => 'ccAsset',
'currency_id' => 1,
'name' => 'Some new asset account #' . random_int(1, 10000),
'type' => 'asset',
'account_role' => 'ccAsset',
];
// test API
$response = $this->post('/api/v1/accounts', $data, ['Accept' => 'application/json']);
$response = $this->post(route('api.v1.accounts.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(422);
$response->assertExactJson(
[
@@ -199,175 +319,45 @@ class AccountControllerTest extends TestCase
}
/**
* No currency information
* No currency information (is allowed).
*
* @covers \FireflyIII\Api\V1\Controllers\AccountController
* @covers \FireflyIII\Api\V1\Requests\AccountRequest
*/
public function testNoCurrencyInfo(): void
public function testStoreNoCurrencyInfo(): void
{
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
// mock calls:
$repository->shouldReceive('setUser')->once();
$currencyRepos->shouldReceive('setUser')->once();
$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
$data = [
'name' => 'Some new asset account #' . random_int(1, 10000),
'type' => 'asset',
'active' => 1,
'include_net_worth' => 1,
'account_role' => 'defaultAsset',
'include_net_worth' => false,
];
// test API
$response = $this->post('/api/v1/accounts', $data, ['Accept' => 'application/json']);
$response->assertStatus(422);
$response->assertExactJson(
[
'message' => 'The given data was invalid.',
'errors' => [
'currency_code' => ['The currency code field is required when currency id is not present.'],
'currency_id' => ['The currency id field is required when currency code is not present.'],
],
]
);
$response->assertHeader('Content-Type', 'application/json');
}
/**
* Test the list of piggy banks.
*
* @covers \FireflyIII\Api\V1\Controllers\AccountController
*/
public function testPiggyBanks(): void
{
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
// get piggies for this user.
$piggies = $this->user()->piggyBanks()->get();
$asset = $this->getRandomAsset();
// mock calls:
$repository->shouldReceive('setUser');
$currencyRepos->shouldReceive('setUser');
$piggyRepos->shouldReceive('setUser');
$repository->shouldReceive('getPiggyBanks')->andReturn($piggies)->once();
$piggyRepos->shouldReceive('getCurrentAmount')->andReturn('12.45');
$piggyRepos->shouldReceive('getSuggestedMonthlyAmount')->andReturn('12.45');
$repository->shouldReceive('getMetaValue')->atLeast()->once()->andReturn('');
// test API
$response = $this->get(route('api.v1.accounts.piggy_banks', [$asset->id]));
$response = $this->post(route('api.v1.accounts.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
$response->assertJson(['data' => [],]);
$response->assertJson(
['meta' => ['pagination' => ['total' => $piggies->count(), 'count' => $piggies->count(), 'per_page' => true, 'current_page' => 1,
'total_pages' => 1]],]
);
$response->assertJson(
['links' => ['self' => true, 'first' => true, 'last' => true,],]
);
$response->assertSee('page=1'); // default returns this.
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Show an account.
*
* @covers \FireflyIII\Api\V1\Controllers\AccountController
*/
public function testShow(): void
{
// create stuff
$account = $this->user()->accounts()->first();
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('setUser');
$currencyRepos->shouldReceive('setUser')->once();
$repository->shouldReceive('getOpeningBalanceAmount')->andReturn('10')->once();
$repository->shouldReceive('getOpeningBalanceDate')->andReturn('2018-01-01')->once();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('defaultAsset');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountNumber'])->andReturn('1');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('BIC');
$repository->shouldReceive('getNoteText')->withArgs([Mockery::any()])->andReturn('Hello');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest'])->andReturn('2')->atLeast()->once();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest_period'])->andReturn('daily')->atLeast()->once();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn(true)->atLeast()->once();
// test API
$response = $this->get('/api/v1/accounts/' . $account->id);
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'accounts', 'links' => true],]);
$response->assertSee('2018-01-01'); // opening balance date
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Send correct data. Should call account repository store method.
*
* @covers \FireflyIII\Api\V1\Controllers\AccountController
* @covers \FireflyIII\Api\V1\Requests\AccountRequest
*/
public function testStoreLiability(): void
{
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$account = $this->user()->accounts()->first();
// mock calls:
$repository->shouldReceive('setUser');
$repository->shouldReceive('store')->once()->andReturn($account);
$repository->shouldReceive('getOpeningBalanceAmount')->andReturn('10');
$repository->shouldReceive('getOpeningBalanceDate')->andReturn('2018-01-01');
$currencyRepos->shouldReceive('setUser')->once();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('defaultAsset');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountNumber'])->andReturn('1');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('BIC');
$repository->shouldReceive('getNoteText')->withArgs([Mockery::any()])->andReturn('Hello');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest'])->andReturn('2')->atLeast()->once();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest_period'])->andReturn('daily')->atLeast()->once();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn(true)->atLeast()->once();
// data to submit
$data = [
'name' => 'Some new liability account #' . random_int(1, 10000),
'currency_id' => 1,
'type' => 'liability',
'active' => 1,
'include_net_worth' => 1,
'liability_amount' => '10000',
'liability_start_date' => '2016-01-01',
'liability_type' => 'mortgage',
'interest' => '1',
'interest_period' => 'daily',
];
// test API
$response = $this->post('/api/v1/accounts', $data, ['Accept' => 'application/json']);
$response->assertSee($account->name);
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'accounts', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Name already in use.
*
@@ -379,12 +369,13 @@ class AccountControllerTest extends TestCase
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$currencyRepos->shouldReceive('setUser')->once();
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$account = $this->user()->accounts()->where('account_type_id', 3)->first();
$account = $this->getRandomAsset();
// data to submit
$data = [
'name' => $account->name,
@@ -396,7 +387,7 @@ class AccountControllerTest extends TestCase
];
// test API
$response = $this->post('/api/v1/accounts', $data, ['Accept' => 'application/json']);
$response = $this->post(route('api.v1.accounts.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(422);
$response->assertExactJson(
[
@@ -420,39 +411,34 @@ class AccountControllerTest extends TestCase
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$account = $this->user()->accounts()->first();
// mock calls:
$repository->shouldReceive('setUser');
$repository->shouldReceive('store')->once()->andReturn($account);
$repository->shouldReceive('getOpeningBalanceAmount')->andReturn('10');
$repository->shouldReceive('getOpeningBalanceDate')->andReturn('2018-01-01');
$currencyRepos->shouldReceive('setUser')->once();
$transformer = $this->mock(AccountTransformer::class);
$account = $this->getRandomAsset();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('defaultAsset');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountNumber'])->andReturn('1');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('BIC');
$repository->shouldReceive('getNoteText')->withArgs([Mockery::any()])->andReturn('Hello');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest'])->andReturn('2')->atLeast()->once();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest_period'])->andReturn('daily')->atLeast()->once();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn(true)->atLeast()->once();
// 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:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
// data to submit
$data = [
'name' => 'Some new asset account #' . random_int(1, 10000),
'currency_id' => 1,
'type' => 'asset',
'active' => 1,
'include_net_worth' => 1,
'account_role' => 'defaultAsset',
'name' => 'Some new asset account #' . random_int(1, 10000),
'type' => 'asset',
'account_role' => 'defaultAsset',
];
// test API
$response = $this->post('/api/v1/accounts', $data, ['Accept' => 'application/json']);
$response = $this->post(route('api.v1.accounts.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'accounts', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
$response->assertSee($account->name);
}
/**
@@ -466,43 +452,35 @@ class AccountControllerTest extends TestCase
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$account = $this->user()->accounts()->first();
$transformer = $this->mock(AccountTransformer::class);
$account = $this->getRandomAsset();
// mock calls:
$repository->shouldReceive('setUser');
$currencyRepos->shouldReceive('setUser')->once();
$repository->shouldReceive('store')->once()->andReturn($account);
$repository->shouldReceive('getOpeningBalanceAmount')->andReturn('10');
$repository->shouldReceive('getOpeningBalanceDate')->andReturn('2018-01-01');
$currencyRepos->shouldReceive('findByCodeNull')->andReturn(TransactionCurrency::find(1));
$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());
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('defaultAsset');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountNumber'])->andReturn('1');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('BIC');
$repository->shouldReceive('getNoteText')->withArgs([Mockery::any()])->andReturn('Hello');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest'])->andReturn('2')->atLeast()->once();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest_period'])->andReturn('daily')->atLeast()->once();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn(true)->atLeast()->once();
// functions to expect:
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
// data to submit
$data = [
'name' => 'Some new asset account #' . random_int(1, 10000),
'currency_code' => 'EUR',
'type' => 'asset',
'active' => 1,
'include_net_worth' => 1,
'account_role' => 'defaultAsset',
'name' => 'Some new asset account #' . random_int(1, 10000),
'currency_code' => 'EUR',
'type' => 'asset',
'account_role' => 'defaultAsset',
];
// test API
$response = $this->post('/api/v1/accounts', $data, ['Accept' => 'application/json']);
$response = $this->post(route('api.v1.accounts.store'), $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'accounts', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
$response->assertSee($account->name);
}
/**
@@ -512,22 +490,19 @@ class AccountControllerTest extends TestCase
*/
public function testTransactionsBasic(): void
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsByType')
->andReturn($this->user()->accounts()->where('account_type_id', 3)->get());
$asset = $this->getRandomAsset();
// get some transactions using the collector:
$repository = $this->mock(JournalRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(TransactionCollectorInterface::class);
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$paginator = new LengthAwarePaginator(new Collection, 0, 50);
$repository->shouldReceive('setUser');
$currencyRepository->shouldReceive('setUser');
$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:
$paginator = new LengthAwarePaginator(new Collection, 0, 50);
$collector->shouldReceive('setUser')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
@@ -537,12 +512,11 @@ class AccountControllerTest extends TestCase
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('setTypes')->andReturnSelf();
$collector->shouldReceive('getPaginatedTransactions')->andReturn($paginator);
// mock some calls:
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$asset = $this->getRandomAsset();
// test API
$response = $this->get(route('api.v1.accounts.transactions', [$asset->id]));
@@ -560,31 +534,36 @@ class AccountControllerTest extends TestCase
*/
public function testTransactionsOpposing(): void
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsByType')
->andReturn($this->user()->accounts()->where('account_type_id', 3)->get());
$revenue = $this->getRandomRevenue();
$repository = $this->mock(JournalRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(TransactionCollectorInterface::class);
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$paginator = new LengthAwarePaginator(new Collection, 0, 50);
$transformer = $this->mock(TransactionTransformer::class);
$repository->shouldReceive('setUser');
$currencyRepository->shouldReceive('setUser');
// default mocks
$accountRepos->shouldReceive('setUser')->atLeast()->once();
$currencyRepository->shouldReceive('setUser')->atLeast()->once();
$accountRepos->shouldReceive('isAsset')->atLeast()->once()->andReturnFalse();
// mock collector:
$paginator = new LengthAwarePaginator(new Collection, 0, 50);
$collector->shouldReceive('setUser')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('withBudgetInformation')->andReturnSelf();
$collector->shouldReceive('setOpposingAccounts')->andReturnSelf();
$collector->shouldReceive('setAccounts')->andReturnSelf();
$collector->shouldReceive('removeFilter')->andReturnSelf();
$collector->shouldReceive('setOpposingAccounts')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('setTypes')->andReturnSelf();
$collector->shouldReceive('getPaginatedTransactions')->andReturn($paginator);
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$revenue = $this->getRandomAsset();
// test API
$response = $this->get(route('api.v1.accounts.transactions', [$revenue->id]));
$response->assertStatus(200);
@@ -601,19 +580,19 @@ class AccountControllerTest extends TestCase
*/
public function testTransactionsRange(): void
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('getAccountsByType')
->andReturn($this->user()->accounts()->where('account_type_id', 3)->get());
$asset = $this->getRandomAsset();
$repository = $this->mock(JournalRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(TransactionCollectorInterface::class);
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$paginator = new LengthAwarePaginator(new Collection, 0, 50);
$repository->shouldReceive('setUser');
$currencyRepository->shouldReceive('setUser');
$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:
$paginator = new LengthAwarePaginator(new Collection, 0, 50);
$collector->shouldReceive('setUser')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
@@ -624,12 +603,11 @@ class AccountControllerTest extends TestCase
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('setTypes')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('getPaginatedTransactions')->andReturn($paginator);
// mock some calls:
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$asset = $this->getRandomAsset();
// test API
$response = $this->get(route('api.v1.accounts.transactions', [$asset->id]) . '?' . http_build_query(['start' => '2018-01-01', 'end' => '2018-01-31']));
@@ -651,40 +629,35 @@ class AccountControllerTest extends TestCase
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
// mock calls:
$repository->shouldReceive('setUser');
$repository->shouldReceive('update')->once();
$currencyRepos->shouldReceive('setUser')->once();
$repository->shouldReceive('getOpeningBalanceAmount')->andReturn('10');
$repository->shouldReceive('getOpeningBalanceDate')->andReturn('2018-01-01');
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('update')->atLeast()->once();
$currencyRepos->shouldReceive('findByCodeNull')->andReturn(null);
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('defaultAsset');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountNumber'])->andReturn('1');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('BIC');
$repository->shouldReceive('getNoteText')->withArgs([Mockery::any()])->andReturn('Hello');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest'])->andReturn('2')->atLeast()->once();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest_period'])->andReturn('daily')->atLeast()->once();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn(true)->atLeast()->once();
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
$account = $this->user()->accounts()->first();
$account = $this->getRandomAsset();
// data to submit
$data = [
'name' => $account->name,
'currency_id' => 1,
'type' => 'asset',
'active' => 1,
'include_net_worth' => 1,
'account_role' => 'defaultAsset',
'name' => $account->name,
'type' => 'asset',
'account_role' => 'defaultAsset',
];
// test API
$response = $this->put('/api/v1/accounts/' . $account->id, $data, ['Accept' => 'application/json']);
$response = $this->put(route('api.v1.accounts.update', [$account->id]), $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'accounts', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
$response->assertSee($account->name);
}
/**
@@ -698,41 +671,36 @@ class AccountControllerTest extends TestCase
// mock repositories
$repository = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$transformer = $this->mock(AccountTransformer::class);
// mock calls:
$repository->shouldReceive('setUser');
$repository->shouldReceive('update')->once();
$currencyRepos->shouldReceive('setUser')->once();
$repository->shouldReceive('getOpeningBalanceAmount')->andReturn('10');
$repository->shouldReceive('getOpeningBalanceDate')->andReturn('2018-01-01');
$repository->shouldReceive('setUser')->atLeast()->once();
$currencyRepos->shouldReceive('setUser')->atLeast()->once();
$repository->shouldReceive('update')->atLeast()->once();
$currencyRepos->shouldReceive('findByCodeNull')->andReturn(TransactionCurrency::find(1));
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountRole'])->andReturn('defaultAsset');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'accountNumber'])->andReturn('1');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'BIC'])->andReturn('BIC');
$repository->shouldReceive('getNoteText')->withArgs([Mockery::any()])->andReturn('Hello');
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest'])->andReturn('2')->atLeast()->once();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'interest_period'])->andReturn('daily')->atLeast()->once();
$repository->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'include_net_worth'])->andReturn(true)->atLeast()->once();
// mock calls to transformer:
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
$account = $this->user()->accounts()->first();
$account = $this->getRandomAsset();
// data to submit
$data = [
'name' => $account->name,
'currency_code' => 'EUR',
'type' => 'asset',
'active' => 1,
'include_net_worth' => 1,
'account_role' => 'defaultAsset',
'name' => $account->name,
'type' => 'asset',
'currency_code' => 'EUR',
'account_role' => 'defaultAsset',
];
// test API
$response = $this->put('/api/v1/accounts/' . $account->id, $data, ['Accept' => 'application/json']);
$response = $this->put(route('api.v1.accounts.update', [$account->id]), $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
$response->assertJson(['data' => ['type' => 'accounts', 'links' => true],]);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
$response->assertSee($account->name);
}