From 89942ee49c6bc23859ffdb9d4da5e3fab779033b Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 16 Dec 2018 13:55:19 +0100 Subject: [PATCH] Improve test coverage for API. --- app/Api/V1/Requests/RecurrenceRequest.php | 2 +- app/Api/V1/Requests/RuleRequest.php | 31 +- .../Internal/Destroy/BillDestroyService.php | 2 +- app/Transformers/AttachmentTransformer.php | 10 +- .../AvailableBudgetTransformer.php | 10 +- app/Transformers/BillTransformer.php | 10 +- app/Transformers/BudgetLimitTransformer.php | 10 +- app/Transformers/BudgetTransformer.php | 12 +- app/Validation/FireflyValidator.php | 4 +- .../V1/Controllers/AboutControllerTest.php | 20 +- .../V1/Controllers/AccountControllerTest.php | 606 +++++++++--------- .../Controllers/AttachmentControllerTest.php | 71 +- .../AvailableBudgetControllerTest.php | 105 ++- .../Api/V1/Controllers/BillControllerTest.php | 130 ++-- .../V1/Controllers/BudgetControllerTest.php | 111 ++-- .../Controllers/BudgetLimitControllerTest.php | 80 ++- .../V1/Controllers/CategoryControllerTest.php | 66 +- .../ConfigurationControllerTest.php | 11 +- .../V1/Controllers/CurrencyControllerTest.php | 255 +++++--- .../CurrencyExchangeRateControllerTest.php | 34 +- .../V1/Controllers/ImportControllerTest.php | 30 +- .../V1/Controllers/LinkTypeControllerTest.php | 50 +- .../Controllers/PiggyBankControllerTest.php | 80 ++- .../Controllers/PreferencesControllerTest.php | 51 +- .../Controllers/RecurrenceControllerTest.php | 132 ++-- .../Api/V1/Controllers/RuleControllerTest.php | 52 +- .../Controllers/RuleGroupControllerTest.php | 47 +- .../Api/V1/Controllers/TagControllerTest.php | 67 +- .../Controllers/TransactionControllerTest.php | 149 ++++- .../TransactionLinkControllerTest.php | 55 +- .../Api/V1/Controllers/UserControllerTest.php | 60 +- .../Controllers/PiggyBankControllerTest.php | 4 +- 32 files changed, 1534 insertions(+), 823 deletions(-) diff --git a/app/Api/V1/Requests/RecurrenceRequest.php b/app/Api/V1/Requests/RecurrenceRequest.php index dc901d8658..99030ebb22 100644 --- a/app/Api/V1/Requests/RecurrenceRequest.php +++ b/app/Api/V1/Requests/RecurrenceRequest.php @@ -96,7 +96,7 @@ class RecurrenceRequest extends Request $today = Carbon::now()->addDay(); return [ - 'type' => 'required|in:Withdrawal,Transfer,Deposit', + 'type' => 'required|in:withdrawal,transfer,deposit', 'title' => 'required|between:1,255|uniqueObjectForUser:recurrences,title', 'description' => 'between:1,65000', 'first_date' => sprintf('required|date|after:%s', $today->format('Y-m-d')), diff --git a/app/Api/V1/Requests/RuleRequest.php b/app/Api/V1/Requests/RuleRequest.php index daec700662..36ae924e7c 100644 --- a/app/Api/V1/Requests/RuleRequest.php +++ b/app/Api/V1/Requests/RuleRequest.php @@ -92,28 +92,23 @@ class RuleRequest extends Request // some triggers and actions require text: $contextTriggers = implode(',', config('firefly.context-rule-triggers')); $contextActions = implode(',', config('firefly.context-rule-actions')); - - - $rules = [ - 'title' => 'required|between:1,100|uniqueObjectForUser:rules,title', - 'description' => 'between:1,5000|nullable', - 'rule_group_id' => 'required|belongsToUser:rule_groups|required_without:rule_group_title', - 'rule_group_title' => 'nullable|between:1,255|required_without:rule_group_id|belongsToUser:rule_groups,title', - 'trigger' => 'required|in:store-journal,update-journal', - + $rules = [ + 'title' => 'required|between:1,100|uniqueObjectForUser:rules,title', + 'description' => 'between:1,5000|nullable', + 'rule_group_id' => 'required|belongsToUser:rule_groups|required_without:rule_group_title', + 'rule_group_title' => 'nullable|between:1,255|required_without:rule_group_id|belongsToUser:rule_groups,title', + 'trigger' => 'required|in:store-journal,update-journal', 'triggers.*.type' => 'required|in:' . implode(',', $validTriggers), 'triggers.*.value' => 'required_if:actions.*.type,' . $contextTriggers . '|min:1|ruleTriggerValue', 'triggers.*.stop_processing' => [new IsBoolean], 'triggers.*.active' => [new IsBoolean], - - 'actions.*.type' => 'required|in:' . implode(',', $validActions), - 'actions.*.value' => 'required_if:actions.*.type,' . $contextActions . '|ruleActionValue', - 'actions.*.stop_processing' => [new IsBoolean], - 'actions.*.active' => [new IsBoolean], - - 'strict' => [new IsBoolean], - 'stop_processing' => [new IsBoolean], - 'active' => [new IsBoolean], + 'actions.*.type' => 'required|in:' . implode(',', $validActions), + 'actions.*.value' => 'required_if:actions.*.type,' . $contextActions . '|ruleActionValue', + 'actions.*.stop_processing' => [new IsBoolean], + 'actions.*.active' => [new IsBoolean], + 'strict' => [new IsBoolean], + 'stop_processing' => [new IsBoolean], + 'active' => [new IsBoolean], ]; return $rules; diff --git a/app/Services/Internal/Destroy/BillDestroyService.php b/app/Services/Internal/Destroy/BillDestroyService.php index c78bafb473..13d170421f 100644 --- a/app/Services/Internal/Destroy/BillDestroyService.php +++ b/app/Services/Internal/Destroy/BillDestroyService.php @@ -38,7 +38,7 @@ class BillDestroyService */ public function __construct() { - if ('testing' === env('APP_ENV')) { + if ('testing' === config('app.env')) { Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this))); } } diff --git a/app/Transformers/AttachmentTransformer.php b/app/Transformers/AttachmentTransformer.php index 576567fa72..f8d521adf1 100644 --- a/app/Transformers/AttachmentTransformer.php +++ b/app/Transformers/AttachmentTransformer.php @@ -33,11 +33,8 @@ use Symfony\Component\HttpFoundation\ParameterBag; /** * Class AttachmentTransformer */ -class AttachmentTransformer extends TransformerAbstract +class AttachmentTransformer extends AbstractTransformer { - /** @var ParameterBag */ - protected $parameters; - /** @var AttachmentRepositoryInterface */ private $repository; @@ -45,12 +42,9 @@ class AttachmentTransformer extends TransformerAbstract * BillTransformer constructor. * * @codeCoverageIgnore - * - * @param ParameterBag $parameters */ - public function __construct(ParameterBag $parameters) + public function __construct() { - $this->parameters = $parameters; $this->repository = app(AttachmentRepositoryInterface::class); if ('testing' === config('app.env')) { Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this))); diff --git a/app/Transformers/AvailableBudgetTransformer.php b/app/Transformers/AvailableBudgetTransformer.php index c217547059..4b4b30b7d3 100644 --- a/app/Transformers/AvailableBudgetTransformer.php +++ b/app/Transformers/AvailableBudgetTransformer.php @@ -32,21 +32,15 @@ use Symfony\Component\HttpFoundation\ParameterBag; /** * Class AvailableBudgetTransformer */ -class AvailableBudgetTransformer extends TransformerAbstract +class AvailableBudgetTransformer extends AbstractTransformer { - /** @var ParameterBag */ - protected $parameters; - /** * CurrencyTransformer constructor. * * @codeCoverageIgnore - * - * @param ParameterBag $parameters */ - public function __construct(ParameterBag $parameters) + public function __construct() { - $this->parameters = $parameters; if ('testing' === config('app.env')) { Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this))); } diff --git a/app/Transformers/BillTransformer.php b/app/Transformers/BillTransformer.php index 53cdc1ce24..829314541f 100644 --- a/app/Transformers/BillTransformer.php +++ b/app/Transformers/BillTransformer.php @@ -37,11 +37,8 @@ use Symfony\Component\HttpFoundation\ParameterBag; /** * Class BillTransformer */ -class BillTransformer extends TransformerAbstract +class BillTransformer extends AbstractTransformer { - /** @var ParameterBag */ - protected $parameters; - /** @var BillRepositoryInterface */ private $repository; @@ -49,12 +46,9 @@ class BillTransformer extends TransformerAbstract * BillTransformer constructor. * * @codeCoverageIgnore - * - * @param ParameterBag $parameters */ - public function __construct(ParameterBag $parameters) + public function __construct() { - $this->parameters = $parameters; $this->repository = app(BillRepositoryInterface::class); if ('testing' === config('app.env')) { Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this))); diff --git a/app/Transformers/BudgetLimitTransformer.php b/app/Transformers/BudgetLimitTransformer.php index 0419efe726..bbe1ace88c 100644 --- a/app/Transformers/BudgetLimitTransformer.php +++ b/app/Transformers/BudgetLimitTransformer.php @@ -31,21 +31,15 @@ use Symfony\Component\HttpFoundation\ParameterBag; /** * Class BudgetLimitTransformer */ -class BudgetLimitTransformer extends TransformerAbstract +class BudgetLimitTransformer extends AbstractTransformer { - /** @var ParameterBag */ - protected $parameters; - /** * CurrencyTransformer constructor. * * @codeCoverageIgnore - * - * @param ParameterBag $parameters */ - public function __construct(ParameterBag $parameters) + public function __construct() { - $this->parameters = $parameters; if ('testing' === config('app.env')) { Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this))); } diff --git a/app/Transformers/BudgetTransformer.php b/app/Transformers/BudgetTransformer.php index e150e6f543..431d4b91fc 100644 --- a/app/Transformers/BudgetTransformer.php +++ b/app/Transformers/BudgetTransformer.php @@ -27,28 +27,20 @@ namespace FireflyIII\Transformers; use FireflyIII\Models\Budget; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use Illuminate\Support\Collection; -use League\Fractal\TransformerAbstract; use Log; -use Symfony\Component\HttpFoundation\ParameterBag; /** * Class BudgetTransformer */ -class BudgetTransformer extends TransformerAbstract +class BudgetTransformer extends AbstractTransformer { - /** @var ParameterBag */ - protected $parameters; - /** * BudgetTransformer constructor. * * @codeCoverageIgnore - * - * @param ParameterBag $parameters */ - public function __construct(ParameterBag $parameters) + public function __construct() { - $this->parameters = $parameters; if ('testing' === config('app.env')) { Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this))); } diff --git a/app/Validation/FireflyValidator.php b/app/Validation/FireflyValidator.php index 902987585b..0d90a18d1e 100644 --- a/app/Validation/FireflyValidator.php +++ b/app/Validation/FireflyValidator.php @@ -257,7 +257,7 @@ class FireflyValidator extends Validator $index = (int)($parts[1] ?? '0'); // get the name of the trigger from the data array: - $actionType = $this->data['actions'][$index]['name'] ?? 'invalid'; + $actionType = $this->data['actions'][$index]['type'] ?? 'invalid'; // if it's "invalid" return false. if ('invalid' === $actionType) { @@ -320,7 +320,7 @@ class FireflyValidator extends Validator $index = (int)($parts[1] ?? '0'); // get the name of the trigger from the data array: - $triggerType = $this->data['triggers'][$index]['name'] ?? 'invalid'; + $triggerType = $this->data['triggers'][$index]['type'] ?? 'invalid'; // invalid always returns false: if ('invalid' === $triggerType) { diff --git a/tests/Api/V1/Controllers/AboutControllerTest.php b/tests/Api/V1/Controllers/AboutControllerTest.php index 3e99f3e932..986a4db796 100644 --- a/tests/Api/V1/Controllers/AboutControllerTest.php +++ b/tests/Api/V1/Controllers/AboutControllerTest.php @@ -23,12 +23,13 @@ declare(strict_types=1); namespace Tests\Api\V1\Controllers; +use FireflyIII\Transformers\UserTransformer; use Laravel\Passport\Passport; use Log; use Tests\TestCase; /** - * Class AboutControllerTest + * Class AboutControllerTest. */ class AboutControllerTest extends TestCase { @@ -74,16 +75,17 @@ class AboutControllerTest extends TestCase */ public function testUser(): void { + $transformer = $this->mock(UserTransformer::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]); + $response = $this->get(route('api.v1.about.user')); $response->assertStatus(200); - $response->assertJson( - [ - 'data' => [ - 'attributes' => true, - 'links' => true, - ], - ] - ); } diff --git a/tests/Api/V1/Controllers/AccountControllerTest.php b/tests/Api/V1/Controllers/AccountControllerTest.php index ec8dfa5a7b..ebd548256b 100644 --- a/tests/Api/V1/Controllers/AccountControllerTest.php +++ b/tests/Api/V1/Controllers/AccountControllerTest.php @@ -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); } diff --git a/tests/Api/V1/Controllers/AttachmentControllerTest.php b/tests/Api/V1/Controllers/AttachmentControllerTest.php index 70cb52f6fe..6549c54ad9 100644 --- a/tests/Api/V1/Controllers/AttachmentControllerTest.php +++ b/tests/Api/V1/Controllers/AttachmentControllerTest.php @@ -29,6 +29,7 @@ use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; +use FireflyIII\Transformers\AttachmentTransformer; use Laravel\Passport\Passport; use Log; use Tests\TestCase; @@ -59,15 +60,16 @@ class AttachmentControllerTest extends TestCase // mock stuff: $repository = $this->mock(AttachmentRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(AttachmentTransformer::class); // mock calls: - $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('destroy')->once()->andReturn(true); // get attachment: $attachment = $this->user()->attachments()->first(); // call API - $response = $this->delete('/api/v1/attachments/' . $attachment->id); + $response = $this->delete(route('api.v1.attachments.delete', [$attachment->id])); $response->assertStatus(204); } @@ -81,10 +83,11 @@ class AttachmentControllerTest extends TestCase // mock stuff: $repository = $this->mock(AttachmentRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(AttachmentTransformer::class); $content = 'Attachment content ' . random_int(100, 1000); // mock calls: - $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('exists')->andReturn(true)->once(); $repository->shouldReceive('getContent')->andReturn($content)->once(); @@ -92,7 +95,7 @@ class AttachmentControllerTest extends TestCase $attachment = $this->user()->attachments()->first(); // call API - $response = $this->get('/api/v1/attachments/' . $attachment->id . '/download'); + $response = $this->get(route('api.v1.attachments.download', [$attachment->id])); $response->assertStatus(200); $response->assertSee($content); @@ -109,17 +112,18 @@ class AttachmentControllerTest extends TestCase // mock stuff: $repository = $this->mock(AttachmentRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(AttachmentTransformer::class); $content = 'Attachment content ' . random_int(100, 1000); // mock calls: - $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('exists')->andReturn(false)->once(); // get attachment: $attachment = $this->user()->attachments()->first(); // call API - $response = $this->get('/api/v1/attachments/' . $attachment->id . '/download'); + $response = $this->get(route('api.v1.attachments.download', [$attachment->id])); $response->assertStatus(500); $response->assertSee('Could not find the indicated attachment. The file is no longer there.'); @@ -135,9 +139,10 @@ class AttachmentControllerTest extends TestCase // mock stuff: $repository = $this->mock(AttachmentRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(AttachmentTransformer::class); // mock calls: - $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('setUser')->atLeast()->once(); // create attachment $attachment = Attachment::create( @@ -155,7 +160,7 @@ class AttachmentControllerTest extends TestCase ); // call API - $response = $this->get('/api/v1/attachments/' . $attachment->id . '/download'); + $response = $this->get(route('api.v1.attachments.download', [$attachment->id])); $response->assertStatus(500); $response->assertSee('No file has been uploaded for this attachment (yet).'); @@ -174,15 +179,21 @@ class AttachmentControllerTest extends TestCase // mock stuff: $repository = $this->mock(AttachmentRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(AttachmentTransformer::class); + // mock 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(); $repository->shouldReceive('get')->once()->andReturn($attachments); - $repository->shouldReceive('getNoteText')->andReturn('Hi There'); // test API - $response = $this->get('/api/v1/attachments'); + $response = $this->get(route('api.v1.attachments.index')); $response->assertStatus(200); $response->assertJson(['data' => [],]); $response->assertJson(['meta' => ['pagination' => ['total' => 10, 'count' => 10, 'per_page' => true, 'current_page' => 1, 'total_pages' => 1]],]); @@ -203,17 +214,23 @@ class AttachmentControllerTest extends TestCase // mock stuff: $repository = $this->mock(AttachmentRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(AttachmentTransformer::class); // mock calls: - $repository->shouldReceive('setUser'); - $repository->shouldReceive('getNoteText')->andReturn('Hi There'); + $repository->shouldReceive('setUser')->atLeast()->once(); + + // mock 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]); // test API - $response = $this->get('/api/v1/attachments/' . $attachment->id); + $response = $this->get(route('api.v1.attachments.show', [$attachment->id])); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]); - $response->assertSee($attachment->filename); // attachment file name $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -233,7 +250,14 @@ class AttachmentControllerTest extends TestCase $repository = $this->mock(AttachmentRepositoryInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(AttachmentTransformer::class); + // mock 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(); @@ -252,10 +276,9 @@ class AttachmentControllerTest extends TestCase // test API - $response = $this->post('/api/v1/attachments', $data); + $response = $this->post(route('api.v1.attachments.store'), $data); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]); - $response->assertSee($attachment->filename); // the file name. $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -270,6 +293,14 @@ class AttachmentControllerTest extends TestCase // mock repositories $repository = $this->mock(AttachmentRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(AttachmentTransformer::class); + + // mock 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]); /** @var Attachment $attachment */ @@ -288,11 +319,10 @@ class AttachmentControllerTest extends TestCase ]; // test API - $response = $this->put('/api/v1/attachments/' . $attachment->id, $data, ['Accept' => 'application/json']); + $response = $this->put(route('api.v1.attachments.update', [$attachment->id]), $data, ['Accept' => 'application/json']); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); - $response->assertSee($attachment->description); } @@ -306,6 +336,7 @@ class AttachmentControllerTest extends TestCase { $repository = $this->mock(AttachmentRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(AttachmentTransformer::class); $repository->shouldReceive('setUser')->once(); @@ -316,7 +347,7 @@ class AttachmentControllerTest extends TestCase $helper = $this->mock(AttachmentHelperInterface::class); $helper->shouldReceive('saveAttachmentFromApi')->once(); - $response = $this->call('POST', '/api/v1/attachments/' . $attachment->id . '/upload', [], [], [], [], $content); + $response = $this->call('POST', route('api.v1.attachments.upload', [$attachment->id]), [], [], [], [], $content); //$response = $this->post('/api/v1/attachments/' . $attachment->id . '/upload',$content, ['Accept' => 'application/json']); $response->assertStatus(204); } diff --git a/tests/Api/V1/Controllers/AvailableBudgetControllerTest.php b/tests/Api/V1/Controllers/AvailableBudgetControllerTest.php index 69b8b2a8ac..84c9cdf2f5 100644 --- a/tests/Api/V1/Controllers/AvailableBudgetControllerTest.php +++ b/tests/Api/V1/Controllers/AvailableBudgetControllerTest.php @@ -28,6 +28,7 @@ use FireflyIII\Models\AvailableBudget; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; +use FireflyIII\Transformers\AvailableBudgetTransformer; use Laravel\Passport\Passport; use Log; use Tests\TestCase; @@ -58,16 +59,17 @@ class AvailableBudgetControllerTest extends TestCase // mock stuff: $repository = $this->mock(BudgetRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(AvailableBudgetTransformer::class); // mock calls: - $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('destroyAvailableBudget')->once()->andReturn(true); // get available budget: $availableBudget = $this->user()->availableBudgets()->first(); // call API - $response = $this->delete('/api/v1/available_budgets/' . $availableBudget->id); + $response = $this->delete(route('api.v1.available_budgets.delete', [$availableBudget->id])); $response->assertStatus(204); } @@ -82,13 +84,21 @@ class AvailableBudgetControllerTest extends TestCase // mock stuff: $repository = $this->mock(BudgetRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(AvailableBudgetTransformer::class); // mock calls: - $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('getAvailableBudgets')->once()->andReturn($availableBudgets); + // mock 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]); + // call API - $response = $this->get('/api/v1/available_budgets'); + $response = $this->get(route('api.v1.available_budgets.index')); $response->assertStatus(200); $response->assertSee($availableBudgets->first()->id); } @@ -104,12 +114,20 @@ class AvailableBudgetControllerTest extends TestCase // mock stuff: $repository = $this->mock(BudgetRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(AvailableBudgetTransformer::class); // mock calls: - $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('setUser')->atLeast()->once(); + + // mock 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]); // call API - $response = $this->get('/api/v1/available_budgets/' . $availableBudget->id); + $response = $this->get(route('api.v1.available_budgets.show', [$availableBudget->id])); $response->assertStatus(200); $response->assertSee($availableBudget->id); } @@ -122,15 +140,20 @@ class AvailableBudgetControllerTest extends TestCase */ public function testStore(): void { - /** @var AvailableBudget $availableBudget */ - $availableBudget = $this->user()->availableBudgets()->first(); - - // mock stuff: $repository = $this->mock(BudgetRepositoryInterface::class); $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(AvailableBudgetTransformer::class); + $availableBudget = new AvailableBudget; + + // mock 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(); + $repository->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('setAvailableBudget')->once()->andReturn($availableBudget); $currencyRepository->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::find(1)); @@ -138,16 +161,15 @@ class AvailableBudgetControllerTest extends TestCase $data = [ 'currency_id' => '1', 'amount' => '100', - 'start_date' => '2018-01-01', - 'end_date' => '2018-01-31', + 'start' => '2018-01-01', + 'end' => '2018-01-31', ]; // test API - $response = $this->post('/api/v1/available_budgets', $data); + $response = $this->post(route('api.v1.available_budgets.store'), $data); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'available_budgets', 'links' => true],]); - $response->assertSee($availableBudget->amount); // the amount $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -162,27 +184,36 @@ class AvailableBudgetControllerTest extends TestCase // mock stuff: $repository = $this->mock(BudgetRepositoryInterface::class); $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(AvailableBudgetTransformer::class); + $availableBudget = new AvailableBudget; + + // mock 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(); + $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_date' => '2018-01-01', - 'end_date' => '2018-01-31', + 'start' => '2018-01-01', + 'end' => '2018-01-31', ]; // test API - $response = $this->post('/api/v1/available_budgets', $data, ['Accept' => 'application/json']); - $response->assertStatus(500); - $response->assertSee('Could not find the indicated currency.'); // the amount - $response->assertHeader('Content-Type', 'application/json'); + $response = $this->post(route('api.v1.available_budgets.store'), $data); + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/vnd.api+json'); } /** @@ -199,6 +230,14 @@ class AvailableBudgetControllerTest extends TestCase // mock stuff: $repository = $this->mock(BudgetRepositoryInterface::class); $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(AvailableBudgetTransformer::class); + + // mock 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(); @@ -211,16 +250,15 @@ class AvailableBudgetControllerTest extends TestCase 'currency_id' => '1', 'currency_code' => 'EUR', 'amount' => '100', - 'start_date' => '2018-01-01', - 'end_date' => '2018-01-31', + 'start' => '2018-01-01', + 'end' => '2018-01-31', ]; // test API - $response = $this->post('/api/v1/available_budgets', $data); + $response = $this->post(route('api.v1.available_budgets.store'), $data); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'available_budgets', 'links' => true],]); - $response->assertSee($availableBudget->amount); // the amount $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -236,6 +274,14 @@ class AvailableBudgetControllerTest extends TestCase // mock repositories $repository = $this->mock(BudgetRepositoryInterface::class); $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(AvailableBudgetTransformer::class); + + // mock 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]); /** @var AvailableBudget $availableBudget */ $availableBudget = $this->user()->availableBudgets()->first(); @@ -249,16 +295,15 @@ class AvailableBudgetControllerTest extends TestCase $data = [ 'currency_id' => '1', 'amount' => '100', - 'start_date' => '2018-01-01', - 'end_date' => '2018-01-31', + 'start' => '2018-01-01', + 'end' => '2018-01-31', ]; // test API - $response = $this->put('/api/v1/available_budgets/' . $availableBudget->id, $data, ['Accept' => 'application/json']); + $response = $this->put(route('api.v1.available_budgets.update', $availableBudget->id), $data, ['Accept' => 'application/json']); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'available_budgets', 'links' => true],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); - $response->assertSee($availableBudget->amount); } diff --git a/tests/Api/V1/Controllers/BillControllerTest.php b/tests/Api/V1/Controllers/BillControllerTest.php index 6003470e6f..495b877c4f 100644 --- a/tests/Api/V1/Controllers/BillControllerTest.php +++ b/tests/Api/V1/Controllers/BillControllerTest.php @@ -25,13 +25,15 @@ namespace Tests\Api\V1\Controllers; use FireflyIII\Helpers\Collector\TransactionCollectorInterface; -use FireflyIII\Models\Attachment; -use FireflyIII\Models\Bill; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; +use FireflyIII\Transformers\AttachmentTransformer; +use FireflyIII\Transformers\BillTransformer; +use FireflyIII\Transformers\RuleTransformer; +use FireflyIII\Transformers\TransactionTransformer; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Laravel\Passport\Passport; @@ -62,25 +64,26 @@ class BillControllerTest extends TestCase public function testAttachments(): void { // create stuff - $attachments = factory(Attachment::class, 10)->create(); - $bill = $this->user()->bills()->first(); + $bill = $this->user()->bills()->first(); + // mock stuff: $repository = $this->mock(AttachmentRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); $billRepos = $this->mock(BillRepositoryInterface::class); + $transformer = $this->mock(AttachmentTransformer::class); + // mock calls to transformer: + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // mock calls: - $repository->shouldReceive('setUser'); - $billRepos->shouldReceive('setUser'); - $billRepos->shouldReceive('getAttachments')->once()->andReturn($attachments); - $repository->shouldReceive('getNoteText')->andReturn('Hi There'); + $billRepos->shouldReceive('setUser')->atLeast()->once(); + $billRepos->shouldReceive('getAttachments')->once()->andReturn(new Collection); // test API $response = $this->get(route('api.v1.bills.attachments', [$bill->id])); $response->assertStatus(200); $response->assertJson(['data' => [],]); - $response->assertJson(['meta' => ['pagination' => ['total' => 10, 'count' => 10, 'per_page' => true, 'current_page' => 1, 'total_pages' => 1]],]); + $response->assertJson(['meta' => ['pagination' => ['total' => 0, 'count' => 0, 'per_page' => true, 'current_page' => 1, 'total_pages' => 1]],]); $response->assertJson(['links' => ['self' => true, 'first' => true, 'last' => true,],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -93,7 +96,8 @@ class BillControllerTest extends TestCase public function testDelete(): void { // mock stuff: - $repository = $this->mock(BillRepositoryInterface::class); + $repository = $this->mock(BillRepositoryInterface::class); + $transformer = $this->mock(BillTransformer::class); // mock calls: $repository->shouldReceive('setUser')->once(); @@ -103,7 +107,7 @@ class BillControllerTest extends TestCase $bill = $this->user()->bills()->first(); // call API - $response = $this->delete('/api/v1/bills/' . $bill->id); + $response = $this->delete(route('api.v1.bills.delete', [$bill->id])); $response->assertStatus(204); } @@ -115,22 +119,22 @@ class BillControllerTest extends TestCase public function testIndex(): void { // create stuff - $bills = factory(Bill::class, 10)->create(); - $paginator = new LengthAwarePaginator($bills, 10, 50, 1); - // mock stuff: - $repository = $this->mock(BillRepositoryInterface::class); + $paginator = new LengthAwarePaginator(new Collection, 0, 50, 1); + $repository = $this->mock(BillRepositoryInterface::class); + $transformer = $this->mock(BillTransformer::class); // mock calls: - $repository->shouldReceive('setUser'); + $repository->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('getPaginator')->withAnyArgs()->andReturn($paginator)->once(); - $repository->shouldReceive('getRulesForBill')->withAnyArgs()->andReturn(new Collection()); - $repository->shouldReceive('getNoteText')->andReturn('Hi there'); + + // mock calls to transformer: + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // test API - $response = $this->get('/api/v1/bills'); + $response = $this->get(route('api.v1.bills.index')); $response->assertStatus(200); $response->assertJson(['data' => [],]); - $response->assertJson(['meta' => ['pagination' => ['total' => 10, 'count' => 10, 'per_page' => 50, 'current_page' => 1, 'total_pages' => 1]],]); + $response->assertJson(['meta' => ['pagination' => ['total' => 0, 'count' => 0, 'per_page' => 50, 'current_page' => 1, 'total_pages' => 1]],]); $response->assertJson( ['links' => ['self' => true, 'first' => true, 'last' => true,],] ); @@ -142,16 +146,18 @@ class BillControllerTest extends TestCase */ public function testRules(): void { - $rules = $this->user()->rules()->get(); - $bill = $this->user()->bills()->first(); - $billRepos = $this->mock(BillRepositoryInterface::class); - $billRepos->shouldReceive('setUser')->once(); - $billRepos->shouldReceive('getRulesForBill')->once()->andReturn($rules); + $bill = $this->user()->bills()->first(); + $billRepos = $this->mock(BillRepositoryInterface::class); + $transformer = $this->mock(RuleTransformer::class); + + // mock calls to transformer: + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + $billRepos->shouldReceive('setUser')->atLeast()->once(); + $billRepos->shouldReceive('getRulesForBill')->atLeast()->once()->andReturn(new Collection); // call API $response = $this->get(route('api.v1.bills.rules', [$bill->id])); $response->assertStatus(200); - $response->assertSee($rules->first()->title); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -163,20 +169,25 @@ class BillControllerTest extends TestCase public function testShow(): void { // create stuff - $bill = $this->user()->bills()->first(); - $repository = $this->mock(BillRepositoryInterface::class); + $bill = $this->user()->bills()->first(); + $repository = $this->mock(BillRepositoryInterface::class); + $transformer = $this->mock(BillTransformer::class); + + // mock 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('getRulesForBill')->withAnyArgs()->andReturn(new Collection()); - $repository->shouldReceive('getNoteText')->andReturn('Hi there'); // test API - $response = $this->get('/api/v1/bills/' . $bill->id); + $response = $this->get(route('api.v1.bills.show', [$bill->id])); $response->assertStatus(200); $response->assertJson( ['data' => [ 'type' => 'bills', - 'id' => $bill->id, ],] ); $response->assertHeader('Content-Type', 'application/vnd.api+json'); @@ -191,13 +202,13 @@ class BillControllerTest extends TestCase public function testStoreMinOverMax(): void { // create stuff - $bill = $this->user()->bills()->first(); - $repository = $this->mock(BillRepositoryInterface::class); + $bill = $this->user()->bills()->first(); + $repository = $this->mock(BillRepositoryInterface::class); + $transformer = $this->mock(BillTransformer::class); // mock calls: $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('store')->andReturn($bill); - $repository->shouldReceive('getNoteText')->andReturn('Hi there'); // data to submit: $data = [ @@ -215,7 +226,7 @@ class BillControllerTest extends TestCase ]; // test API - $response = $this->post('/api/v1/bills', $data, ['Accept' => 'application/json']); + $response = $this->post(route('api.v1.bills.store'), $data, ['Accept' => 'application/json']); $response->assertStatus(422); $response->assertExactJson( [ @@ -237,14 +248,21 @@ class BillControllerTest extends TestCase public function testStoreValid(): void { // create stuff - $bill = $this->user()->bills()->first(); - $repository = $this->mock(BillRepositoryInterface::class); + $bill = $this->user()->bills()->first(); + $repository = $this->mock(BillRepositoryInterface::class); + $transformer = $this->mock(BillTransformer::class); + + // mock 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()->times(2); + $repository->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('store')->andReturn($bill); - $repository->shouldReceive('getNoteText')->andReturn('Hi there'); - $repository->shouldReceive('getRulesForBill')->withAnyArgs()->andReturn(new Collection()); + // data to submit: $data = [ 'name' => 'New bill #' . random_int(1, 10000), @@ -261,11 +279,10 @@ class BillControllerTest extends TestCase ]; // test API - $response = $this->post('/api/v1/bills', $data, ['Accept' => 'application/json']); + $response = $this->post(route('api.v1.bills.store'), $data, ['Accept' => 'application/json']); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'bills', 'links' => true],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); - $response->assertSee($bill->name); } /** @@ -281,6 +298,7 @@ class BillControllerTest extends TestCase $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $billRepos = $this->mock(BillRepositoryInterface::class); + $transformer = $this->mock(TransactionTransformer::class); $paginator = new LengthAwarePaginator(new Collection, 0, 50); $billRepos->shouldReceive('setUser'); $repository->shouldReceive('setUser'); @@ -297,6 +315,9 @@ class BillControllerTest extends TestCase $collector->shouldReceive('setAllAssetAccounts')->andReturnSelf(); $collector->shouldReceive('getPaginatedTransactions')->andReturn($paginator); + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + // mock some calls: @@ -321,6 +342,7 @@ class BillControllerTest extends TestCase $collector = $this->mock(TransactionCollectorInterface::class); $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); $billRepos = $this->mock(BillRepositoryInterface::class); + $transformer = $this->mock(TransactionTransformer::class); $paginator = new LengthAwarePaginator(new Collection, 0, 50); $billRepos->shouldReceive('setUser'); $repository->shouldReceive('setUser'); @@ -338,6 +360,9 @@ class BillControllerTest extends TestCase $collector->shouldReceive('setTypes')->andReturnSelf(); $collector->shouldReceive('setRange')->andReturnSelf(); + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + $collector->shouldReceive('getPaginatedTransactions')->andReturn($paginator); @@ -362,14 +387,13 @@ class BillControllerTest extends TestCase public function testUpdateValid(): void { // create stuff - $bill = $this->user()->bills()->first(); - $repository = $this->mock(BillRepositoryInterface::class); + $bill = $this->user()->bills()->first(); + $repository = $this->mock(BillRepositoryInterface::class); + $transformer = $this->mock(BillTransformer::class); // mock calls: - $repository->shouldReceive('setUser')->atleast()->times(2); - $repository->shouldReceive('getNoteText')->andReturn('Hi there'); + $repository->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('update')->andReturn($bill); - $repository->shouldReceive('getRulesForBill')->withAnyArgs()->andReturn(new Collection()); // data to submit: $data = [ 'name' => 'New bill #' . random_int(1, 10000), @@ -384,12 +408,18 @@ class BillControllerTest extends TestCase 'currency_id' => 1, ]; + // mock 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]); + // test API - $response = $this->put('/api/v1/bills/' . $bill->id, $data, ['Accept' => 'application/json']); + $response = $this->put(route('api.v1.bills.update', [$bill->id]), $data, ['Accept' => 'application/json']); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'bills', 'links' => true],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); - $response->assertSee($bill->name); } } diff --git a/tests/Api/V1/Controllers/BudgetControllerTest.php b/tests/Api/V1/Controllers/BudgetControllerTest.php index 91ddca843b..ec91db3ee8 100644 --- a/tests/Api/V1/Controllers/BudgetControllerTest.php +++ b/tests/Api/V1/Controllers/BudgetControllerTest.php @@ -32,6 +32,9 @@ use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; +use FireflyIII\Transformers\BudgetLimitTransformer; +use FireflyIII\Transformers\BudgetTransformer; +use FireflyIII\Transformers\TransactionTransformer; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Laravel\Passport\Passport; @@ -61,18 +64,18 @@ class BudgetControllerTest extends TestCase */ public function testBudgetLimits(): void { - $budget = $this->user()->budgets()->first(); - $budgetLimits = BudgetLimit::get(); - $repository = $this->mock(BudgetRepositoryInterface::class); + $budget = $this->user()->budgets()->first(); + $repository = $this->mock(BudgetRepositoryInterface::class); + $transformer = $this->mock(BudgetLimitTransformer::class); // mock calls: - $repository->shouldReceive('setUser')->once(); - $repository->shouldReceive('getBudgetLimits')->once()->andReturn($budgetLimits); + $repository->shouldReceive('setUser')->once()->atLeast()->once(); + $repository->shouldReceive('getBudgetLimits')->once()->andReturn(new Collection()); + $transformer->shouldReceive('setParameters')->atLeast()->once(); // call API $response = $this->get(route('api.v1.budgets.budget_limits', [$budget->id])); $response->assertStatus(200); - $response->assertSee('budget_limits'); } /** @@ -86,14 +89,14 @@ class BudgetControllerTest extends TestCase $repository = $this->mock(BudgetRepositoryInterface::class); // mock calls: - $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('destroy')->once()->andReturn(true); // get budget: $budget = $this->user()->budgets()->first(); // call API - $response = $this->delete('/api/v1/budgets/' . $budget->id); + $response = $this->delete(route('api.v1.budgets.delete', [$budget->id])); $response->assertStatus(204); } @@ -104,18 +107,17 @@ class BudgetControllerTest extends TestCase */ public function testIndex(): void { - $budgets = $this->user()->budgets()->get(); - // mock stuff: - $repository = $this->mock(BudgetRepositoryInterface::class); + $repository = $this->mock(BudgetRepositoryInterface::class); + $transformer = $this->mock(BudgetTransformer::class); // mock calls: - $repository->shouldReceive('setUser')->once(); - $repository->shouldReceive('getBudgets')->once()->andReturn($budgets); + $repository->shouldReceive('setUser')->atLeast()->once(); + $repository->shouldReceive('getBudgets')->once()->andReturn(new Collection); + $transformer->shouldReceive('setParameters')->atLeast()->once(); // call API - $response = $this->get('/api/v1/budgets'); + $response = $this->get(route('api.v1.budgets.index')); $response->assertStatus(200); - $response->assertSee($budgets->first()->name); } /** @@ -125,17 +127,23 @@ class BudgetControllerTest extends TestCase */ public function testShow(): void { - $budget = $this->user()->budgets()->first(); - // mock stuff: - $repository = $this->mock(BudgetRepositoryInterface::class); + $budget = $this->user()->budgets()->first(); + $repository = $this->mock(BudgetRepositoryInterface::class); + $transformer = $this->mock(BudgetTransformer::class); // mock calls: - $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('setUser')->atLeast()->once(); + + // mock 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]); // call API - $response = $this->get('/api/v1/budgets/' . $budget->id); + $response = $this->get(route('api.v1.budgets.show', [$budget->id])); $response->assertStatus(200); - $response->assertSee($budget->name); } /** @@ -147,26 +155,32 @@ class BudgetControllerTest extends TestCase public function testStore(): void { /** @var Budget $budget */ - $budget = $this->user()->budgets()->first(); + $budget = new Budget; // mock stuff: - $repository = $this->mock(BudgetRepositoryInterface::class); + $repository = $this->mock(BudgetRepositoryInterface::class); + $transformer = $this->mock(BudgetTransformer::class); // mock calls: - $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('store')->once()->andReturn($budget); + // mock 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 budget', - 'active' => '1', + 'name' => 'Some budget', ]; // test API - $response = $this->post('/api/v1/budgets', $data); + $response = $this->post(route('api.v1.budgets.store'), $data); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'budgets', 'links' => true],]); - $response->assertSee($budget->name); // the amount $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -179,14 +193,7 @@ class BudgetControllerTest extends TestCase public function testStoreBudgetLimit(): void { $budget = $this->user()->budgets()->first(); - $budgetLimit = BudgetLimit::create( - [ - 'budget_id' => $budget->id, - 'start_date' => '2018-01-01', - 'end_date' => '2018-01-31', - 'amount' => 1, - ] - ); + $budgetLimit = new BudgetLimit; $data = [ 'budget_id' => $budget->id, @@ -196,8 +203,16 @@ class BudgetControllerTest extends TestCase ]; // mock stuff: $repository = $this->mock(BudgetRepositoryInterface::class); + $transformer = $this->mock(BudgetLimitTransformer::class); + $repository->shouldReceive('storeBudgetLimit')->andReturn($budgetLimit)->once(); + // mock 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(); @@ -224,6 +239,11 @@ class BudgetControllerTest extends TestCase $accountRepos = $this->mock(AccountRepositoryInterface::class); $billRepos = $this->mock(BillRepositoryInterface::class); $budgetRepos = $this->mock(BudgetRepositoryInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + $paginator = new LengthAwarePaginator(new Collection, 0, 50); $billRepos->shouldReceive('setUser'); $repository->shouldReceive('setUser'); @@ -265,6 +285,11 @@ class BudgetControllerTest extends TestCase $billRepos = $this->mock(BillRepositoryInterface::class); $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $paginator = new LengthAwarePaginator(new Collection, 0, 50); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + $billRepos->shouldReceive('setUser'); $repository->shouldReceive('setUser'); $currencyRepository->shouldReceive('setUser'); @@ -302,13 +327,20 @@ class BudgetControllerTest extends TestCase { // mock repositories $repository = $this->mock(BudgetRepositoryInterface::class); - + $transformer = $this->mock(BudgetTransformer::class); /** @var Budget $budget */ $budget = $this->user()->budgets()->first(); // mock calls: $repository->shouldReceive('setUser'); - $repository->shouldReceive('update')->once()->andReturn($budget); + $repository->shouldReceive('update')->once()->andReturn(new Budget); + + // 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 = [ @@ -317,11 +349,10 @@ class BudgetControllerTest extends TestCase ]; // test API - $response = $this->put('/api/v1/budgets/' . $budget->id, $data, ['Accept' => 'application/json']); + $response = $this->put(route('api.v1.budgets.update', $budget->id), $data, ['Accept' => 'application/json']); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'budgets', 'links' => true],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); - $response->assertSee($budget->name); } } diff --git a/tests/Api/V1/Controllers/BudgetLimitControllerTest.php b/tests/Api/V1/Controllers/BudgetLimitControllerTest.php index 22a6cdfb29..10b9068079 100644 --- a/tests/Api/V1/Controllers/BudgetLimitControllerTest.php +++ b/tests/Api/V1/Controllers/BudgetLimitControllerTest.php @@ -32,6 +32,8 @@ use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; +use FireflyIII\Transformers\BudgetLimitTransformer; +use FireflyIII\Transformers\TransactionTransformer; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Laravel\Passport\Passport; @@ -60,7 +62,9 @@ class BudgetLimitControllerTest extends TestCase public function testDelete(): void { // mock stuff: - $repository = $this->mock(BudgetRepositoryInterface::class); + $repository = $this->mock(BudgetRepositoryInterface::class); + $transformer = $this->mock(BudgetLimitTransformer::class); + // mock calls: $repository->shouldReceive('setUser')->once(); @@ -79,7 +83,7 @@ class BudgetLimitControllerTest extends TestCase ); // call API - $response = $this->delete('/api/v1/budgets/limits/' . $budgetLimit->id); + $response = $this->delete(route('api.v1.budget_limits.delete', $budgetLimit->id)); $response->assertStatus(204); } @@ -93,18 +97,20 @@ class BudgetLimitControllerTest extends TestCase /** @var Budget $budget */ $budget = $this->user()->budgets()->first(); // mock stuff: - $repository = $this->mock(BudgetRepositoryInterface::class); + $repository = $this->mock(BudgetRepositoryInterface::class); + $transformer = $this->mock(BudgetLimitTransformer::class); + + // mock calls to transformer: + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // mock calls: $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('findNull')->andReturn($budget); - $repository->shouldReceive('getBudgetLimits')->once()->andReturn($budget->budgetlimits()->get()); + $repository->shouldReceive('getBudgetLimits')->once()->andReturn(new Collection); // call API - $params = [ - 'budget_id' => $budget->id, - ]; - $response = $this->get('/api/v1/budgets/limits?' . http_build_query($params)); + $params = ['budget_id' => $budget->id,]; + $response = $this->get(route('api.v1.budget_limits.index') . '?' . http_build_query($params)); $response->assertStatus(200); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -119,19 +125,23 @@ class BudgetLimitControllerTest extends TestCase /** @var Budget $budget */ $budget = $this->user()->budgets()->first(); // mock stuff: - $repository = $this->mock(BudgetRepositoryInterface::class); + $repository = $this->mock(BudgetRepositoryInterface::class); + $transformer = $this->mock(BudgetLimitTransformer::class); + + // mock calls to transformer: + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // mock calls: $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('findNull')->andReturn(null); - $repository->shouldReceive('getAllBudgetLimits')->once()->andReturn($budget->budgetlimits()->get()); + $repository->shouldReceive('getAllBudgetLimits')->once()->andReturn(new Collection); // call API $params = [ 'start' => '2018-01-01', 'end' => '2018-01-31', ]; - $uri = '/api/v1/budgets/limits?' . http_build_query($params); + $uri = route('api.v1.budget_limits.index') . '?' . http_build_query($params); $response = $this->get($uri); $response->assertStatus(200); $response->assertHeader('Content-Type', 'application/vnd.api+json'); @@ -147,12 +157,16 @@ class BudgetLimitControllerTest extends TestCase /** @var Budget $budget */ $budget = $this->user()->budgets()->first(); // mock stuff: - $repository = $this->mock(BudgetRepositoryInterface::class); + $repository = $this->mock(BudgetRepositoryInterface::class); + $transformer = $this->mock(BudgetLimitTransformer::class); + + // mock calls to transformer: + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // mock calls: $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('findNull')->andReturn($budget); - $repository->shouldReceive('getBudgetLimits')->once()->andReturn($budget->budgetlimits()->get()); + $repository->shouldReceive('getBudgetLimits')->once()->andReturn(new Collection); // call API $params = [ @@ -160,7 +174,7 @@ class BudgetLimitControllerTest extends TestCase 'start' => '2018-01-01', 'end' => '2018-01-31', ]; - $response = $this->get('/api/v1/budgets/limits?' . http_build_query($params)); + $response = $this->get(route('api.v1.budget_limits.index') . '?' . http_build_query($params)); $response->assertStatus(200); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -171,7 +185,15 @@ class BudgetLimitControllerTest extends TestCase public function testShow(): void { // mock stuff: - $repository = $this->mock(BudgetRepositoryInterface::class); + $repository = $this->mock(BudgetRepositoryInterface::class); + $transformer = $this->mock(BudgetLimitTransformer::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(); @@ -189,7 +211,7 @@ class BudgetLimitControllerTest extends TestCase ); - $response = $this->get('/api/v1/budgets/limits/' . $budgetLimit->id); + $response = $this->get(route('api.v1.budget_limits.show', $budgetLimit->id)); $response->assertStatus(200); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -203,6 +225,14 @@ class BudgetLimitControllerTest extends TestCase public function testStore(): void { $budget = $this->user()->budgets()->first(); + $transformer = $this->mock(BudgetLimitTransformer::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]); $budgetLimit = BudgetLimit::create( [ 'budget_id' => $budget->id, @@ -228,7 +258,7 @@ class BudgetLimitControllerTest extends TestCase $repository->shouldReceive('setUser')->once(); // call API - $response = $this->post('/api/v1/budgets/limits', $data, ['Accept' => 'application/json']); + $response = $this->post(route('api.v1.budget_limits.store'), $data, ['Accept' => 'application/json']); $response->assertStatus(200); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -251,13 +281,13 @@ class BudgetLimitControllerTest extends TestCase // mock stuff: $repository = $this->mock(BudgetRepositoryInterface::class); $repository->shouldReceive('findNull')->andReturn(null)->once(); - + $transformer = $this->mock(BudgetLimitTransformer::class); // mock calls: $repository->shouldReceive('setUser')->once(); // call API - $response = $this->post('/api/v1/budgets/limits', $data); + $response = $this->post(route('api.v1.budget_limits.store'), $data); $response->assertStatus(500); $response->assertSee('Unknown budget.'); } @@ -276,6 +306,7 @@ class BudgetLimitControllerTest extends TestCase $accountRepos = $this->mock(AccountRepositoryInterface::class); $billRepos = $this->mock(BillRepositoryInterface::class); $budgetRepos = $this->mock(BudgetRepositoryInterface::class); + $transformer = $this->mock(TransactionTransformer::class); $paginator = new LengthAwarePaginator(new Collection, 0, 50); $billRepos->shouldReceive('setUser'); $repository->shouldReceive('setUser'); @@ -294,7 +325,8 @@ class BudgetLimitControllerTest extends TestCase $collector->shouldReceive('setAllAssetAccounts')->andReturnSelf(); $collector->shouldReceive('getPaginatedTransactions')->andReturn($paginator); - + // mock calls to transformer: + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // mock some calls: // test API @@ -314,6 +346,7 @@ class BudgetLimitControllerTest extends TestCase */ public function testUpdate(): void { + $transformer= $this->mock(BudgetLimitTransformer::class); $budget = $this->user()->budgets()->first(); $budgetLimit = BudgetLimit::create( [ @@ -333,7 +366,12 @@ class BudgetLimitControllerTest extends TestCase // mock stuff: $repository = $this->mock(BudgetRepositoryInterface::class); $repository->shouldReceive('updateBudgetLimit')->andReturn($budgetLimit)->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]); // mock calls: $repository->shouldReceive('setUser')->once(); diff --git a/tests/Api/V1/Controllers/CategoryControllerTest.php b/tests/Api/V1/Controllers/CategoryControllerTest.php index 9c3652d42c..37aefc5b07 100644 --- a/tests/Api/V1/Controllers/CategoryControllerTest.php +++ b/tests/Api/V1/Controllers/CategoryControllerTest.php @@ -31,6 +31,8 @@ use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Category\CategoryRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; +use FireflyIII\Transformers\CategoryTransformer; +use FireflyIII\Transformers\TransactionTransformer; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Laravel\Passport\Passport; @@ -71,7 +73,7 @@ class CategoryControllerTest extends TestCase $category = $this->user()->categories()->first(); // call API - $response = $this->delete('/api/v1/categories/' . $category->id); + $response = $this->delete(route('api.v1.categories.delete', $category->id)); $response->assertStatus(204); } @@ -82,18 +84,19 @@ class CategoryControllerTest extends TestCase */ public function testIndex(): void { - $categories = $this->user()->categories()->get(); - // mock stuff: - $repository = $this->mock(CategoryRepositoryInterface::class); + $repository = $this->mock(CategoryRepositoryInterface::class); + $transformer = $this->mock(CategoryTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // mock calls: - $repository->shouldReceive('setUser')->once(); - $repository->shouldReceive('getCategories')->once()->andReturn($categories); + $repository->shouldReceive('setUser')->atLeast()->once(); + $repository->shouldReceive('getCategories')->once()->andReturn(new Collection); // call API - $response = $this->get('/api/v1/categories'); + $response = $this->get(route('api.v1.categories.index')); $response->assertStatus(200); - $response->assertSee($categories->first()->name); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -106,15 +109,21 @@ class CategoryControllerTest extends TestCase { $category = $this->user()->categories()->first(); // mock stuff: - $repository = $this->mock(CategoryRepositoryInterface::class); + $repository = $this->mock(CategoryRepositoryInterface::class); + $transformer = $this->mock(CategoryTransformer::class); // mock calls: $repository->shouldReceive('setUser')->once(); + // mock 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]); // call API - $response = $this->get('/api/v1/categories/' . $category->id); + $response = $this->get(route('api.v1.categories.show', [$category->id])); $response->assertStatus(200); - $response->assertSee($category->name); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -130,7 +139,15 @@ class CategoryControllerTest extends TestCase $category = $this->user()->categories()->first(); // mock stuff: - $repository = $this->mock(CategoryRepositoryInterface::class); + $repository = $this->mock(CategoryRepositoryInterface::class); + $transformer = $this->mock(CategoryTransformer::class); + + // mock 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(); @@ -143,10 +160,9 @@ class CategoryControllerTest extends TestCase ]; // test API - $response = $this->post('/api/v1/categories', $data); + $response = $this->post(route('api.v1.categories.store'), $data); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'categories', 'links' => true],]); - $response->assertSee($category->name); // the amount $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -158,6 +174,7 @@ class CategoryControllerTest extends TestCase public function testTransactionsBasic(): void { $category = $this->user()->categories()->first(); + $transformer = $this->mock(TransactionTransformer::class); $repository = $this->mock(JournalRepositoryInterface::class); $categoryRepos = $this->mock(CategoryRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); @@ -183,7 +200,8 @@ class CategoryControllerTest extends TestCase $collector->shouldReceive('getPaginatedTransactions')->andReturn($paginator); - // mock some calls: + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // test API $response = $this->get(route('api.v1.categories.transactions', [$category->id])); @@ -208,6 +226,7 @@ class CategoryControllerTest extends TestCase $currencyRepository = $this->mock(CurrencyRepositoryInterface::class); $billRepos = $this->mock(BillRepositoryInterface::class); $paginator = new LengthAwarePaginator(new Collection, 0, 50); + $transformer = $this->mock(TransactionTransformer::class); $categoryRepos->shouldReceive('setUser')->atLeast()->once(); $billRepos->shouldReceive('setUser'); @@ -226,6 +245,9 @@ class CategoryControllerTest extends TestCase $collector->shouldReceive('setRange')->andReturnSelf(); $collector->shouldReceive('getPaginatedTransactions')->andReturn($paginator); + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + $response = $this->get( route('api.v1.categories.transactions', [$category->id]) . '?' . http_build_query(['start' => '2018-01-01', 'end' => '2018-01-31']) ); @@ -245,11 +267,18 @@ class CategoryControllerTest extends TestCase public function testUpdate(): void { // mock repositories - $repository = $this->mock(CategoryRepositoryInterface::class); - + $repository = $this->mock(CategoryRepositoryInterface::class); + $transformer = $this->mock(CategoryTransformer::class); /** @var Category $category */ $category = $this->user()->categories()->first(); + // mock 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('update')->once()->andReturn($category); @@ -261,11 +290,10 @@ class CategoryControllerTest extends TestCase ]; // test API - $response = $this->put('/api/v1/categories/' . $category->id, $data, ['Accept' => 'application/json']); + $response = $this->put(route('api.v1.categories.update', $category->id), $data, ['Accept' => 'application/json']); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'categories', 'links' => true],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); - $response->assertSee($category->name); } } diff --git a/tests/Api/V1/Controllers/ConfigurationControllerTest.php b/tests/Api/V1/Controllers/ConfigurationControllerTest.php index f706e62566..6f3923f791 100644 --- a/tests/Api/V1/Controllers/ConfigurationControllerTest.php +++ b/tests/Api/V1/Controllers/ConfigurationControllerTest.php @@ -87,7 +87,7 @@ class ConfigurationControllerTest extends TestCase ], ]; - $response = $this->get('/api/v1/configuration'); + $response = $this->get(route('api.v1.configuration.index')); $response->assertStatus(200); $response->assertExactJson($expected); $response->assertHeader('Content-Type', 'application/vnd.api+json'); @@ -105,7 +105,7 @@ class ConfigurationControllerTest extends TestCase $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(false); Passport::actingAs($this->emptyUser()); - $response = $this->get('/api/v1/configuration'); + $response = $this->get(route('api.v1.configuration.index')); $response->assertStatus(500); $response->assertSee('No access to method.'); } @@ -156,8 +156,7 @@ class ConfigurationControllerTest extends TestCase 'single_user_mode' => true, ], ]; - - $response = $this->post('/api/v1/configuration/permission_update_check', $data); + $response = $this->post(route('api.v1.configuration.update', ['permission_update_check']), $data); $response->assertStatus(200); $response->assertExactJson($expected); } @@ -210,7 +209,7 @@ class ConfigurationControllerTest extends TestCase ], ]; - $response = $this->post('/api/v1/configuration/single_user_mode', $data); + $response = $this->post(route('api.v1.configuration.update', ['single_user_mode']), $data); $response->assertStatus(200); $response->assertExactJson($expected); } @@ -243,7 +242,7 @@ class ConfigurationControllerTest extends TestCase $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(false); Passport::actingAs($this->emptyUser()); - $response = $this->post('/api/v1/configuration/single_user_mode'); + $response = $this->post(route('api.v1.configuration.update', ['single_user_mode'])); $response->assertStatus(500); $response->assertSee('No access to method.'); } diff --git a/tests/Api/V1/Controllers/CurrencyControllerTest.php b/tests/Api/V1/Controllers/CurrencyControllerTest.php index 995723233a..f107daac00 100644 --- a/tests/Api/V1/Controllers/CurrencyControllerTest.php +++ b/tests/Api/V1/Controllers/CurrencyControllerTest.php @@ -25,10 +25,12 @@ namespace Tests\Api\V1\Controllers; use FireflyIII\Helpers\Collector\TransactionCollectorInterface; -use FireflyIII\Models\Account; +use FireflyIII\Models\AvailableBudget; use FireflyIII\Models\Bill; use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\Preference; +use FireflyIII\Models\Recurrence; +use FireflyIII\Models\Rule; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface; @@ -39,6 +41,15 @@ use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface; use FireflyIII\Repositories\Rule\RuleRepositoryInterface; use FireflyIII\Repositories\User\UserRepositoryInterface; +use FireflyIII\Transformers\AccountTransformer; +use FireflyIII\Transformers\AvailableBudgetTransformer; +use FireflyIII\Transformers\BillTransformer; +use FireflyIII\Transformers\BudgetLimitTransformer; +use FireflyIII\Transformers\CurrencyExchangeRateTransformer; +use FireflyIII\Transformers\CurrencyTransformer; +use FireflyIII\Transformers\RecurrenceTransformer; +use FireflyIII\Transformers\RuleTransformer; +use FireflyIII\Transformers\TransactionTransformer; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Laravel\Passport\Passport; @@ -70,33 +81,33 @@ class CurrencyControllerTest extends TestCase */ public function testAccounts(): void { - // create stuff - $accounts = factory(Account::class, 10)->create(); - // mock stuff: + $currency = TransactionCurrency::first(); + $account = $this->getRandomAsset(); + $collection = new Collection([$account]); $repository = $this->mock(AccountRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(AccountTransformer::class); + + // mock 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('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(); + $currencyRepos->shouldReceive('setUser')->atLeast()->once(); + $repository->shouldReceive('getAccountsByType')->withAnyArgs()->andReturn($collection)->atLeast()->once(); + $repository->shouldReceive('getMetaValue')->atLeast()->once()->andReturn('1'); // test API - $currency = TransactionCurrency::first(); + $response = $this->get(route('api.v1.currencies.accounts', [$currency->code])); $response->assertStatus(200); $response->assertJson(['data' => [],]); - $response->assertJson(['meta' => ['pagination' => ['total' => 10, 'count' => 10, 'per_page' => true, 'current_page' => 1, 'total_pages' => 1]],]); + $response->assertJson(['meta' => ['pagination' => ['total' => 1, 'count' => 1, 'per_page' => true, 'current_page' => 1, 'total_pages' => 1]],]); $response->assertJson( ['links' => ['self' => true, 'first' => true, 'last' => true,],] ); @@ -111,22 +122,25 @@ class CurrencyControllerTest extends TestCase */ public function testAvailableBudgets(): void { - $availableBudgets = $this->user()->availableBudgets()->get(); // mock stuff: $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(AvailableBudgetTransformer::class); + $collection = new Collection([AvailableBudget::first()]); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // mock calls: - $currencyRepos->shouldReceive('setUser')->once(); - $budgetRepos->shouldReceive('setUser')->once(); - $budgetRepos->shouldReceive('getAvailableBudgets')->once()->andReturn($availableBudgets); + $currencyRepos->shouldReceive('setUser')->atLeast()->once(); + $budgetRepos->shouldReceive('setUser')->atLeast()->once(); + $budgetRepos->shouldReceive('getAvailableBudgets')->once()->andReturn($collection); // call API $currency = TransactionCurrency::first(); $response = $this->get(route('api.v1.currencies.available_budgets', [$currency->code])); $response->assertStatus(200); - $response->assertSee($availableBudgets->first()->id); } /** @@ -137,26 +151,26 @@ class CurrencyControllerTest extends TestCase public function testBills(): void { // create stuff - $bills = factory(Bill::class, 10)->create(); - $paginator = new LengthAwarePaginator($bills, 10, 50, 1); + $paginator = new LengthAwarePaginator(new Collection([Bill::first()]), 0, 50, 1); // mock stuff: $repository = $this->mock(BillRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(BillTransformer::class); + + // mock calls to transformer: + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // mock calls: - $repository->shouldReceive('setUser'); - $currencyRepos->shouldReceive('setUser')->once(); + $currencyRepos->shouldReceive('setUser')->atLeast()->once(); $repository->shouldReceive('getPaginator')->withAnyArgs()->andReturn($paginator)->once(); - $repository->shouldReceive('getRulesForBill')->withAnyArgs()->andReturn(new Collection()); - $repository->shouldReceive('getNoteText')->andReturn('Hi there'); // test API $currency = TransactionCurrency::first(); $response = $this->get(route('api.v1.currencies.bills', [$currency->code])); $response->assertStatus(200); $response->assertJson(['data' => [],]); - $response->assertJson(['meta' => ['pagination' => ['total' => 10, 'count' => 10, 'per_page' => true, 'current_page' => 1, 'total_pages' => 1]],]); + $response->assertJson(['meta' => ['pagination' => ['total' => 0, 'count' => 0, 'per_page' => true, 'current_page' => 1, 'total_pages' => 1]],]); $response->assertJson(['links' => ['self' => true, 'first' => true, 'last' => true,],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -169,11 +183,19 @@ class CurrencyControllerTest extends TestCase $repository = $this->mock(BudgetRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(BudgetLimitTransformer::class); $currency = TransactionCurrency::first(); $budgetLimit = BudgetLimit::first(); $budgetLimit->transaction_currency_id = $currency->id; $collection = new Collection([$budgetLimit]); + // mock 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('getAllBudgetLimits')->once()->andReturn($collection); $currencyRepos->shouldReceive('setUser')->once(); @@ -188,9 +210,15 @@ class CurrencyControllerTest extends TestCase */ public function testCer(): void { - $repository = $this->mock(CurrencyRepositoryInterface::class); - $userRepos = $this->mock(UserRepositoryInterface::class); - $repository->shouldReceive('setUser')->once(); + $repository = $this->mock(CurrencyRepositoryInterface::class); + $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(CurrencyExchangeRateTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + + // mock calls + $repository->shouldReceive('setUser')->once()->atLeast()->once(); $repository->shouldReceive('getExchangeRates')->once()->andReturn(new Collection); @@ -233,9 +261,17 @@ class CurrencyControllerTest extends TestCase public function testDisable(): void { // create stuff - $currency = TransactionCurrency::first(); - $repository = $this->mock(CurrencyRepositoryInterface::class); - $userRepos = $this->mock(UserRepositoryInterface::class); + $currency = TransactionCurrency::first(); + $repository = $this->mock(CurrencyRepositoryInterface::class); + $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(CurrencyTransformer::class); + + // mock 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(); @@ -248,7 +284,6 @@ class CurrencyControllerTest extends TestCase $response->assertJson( ['data' => [ 'type' => 'currencies', - 'id' => $currency->id, ],] ); $response->assertHeader('Content-Type', 'application/vnd.api+json'); @@ -260,9 +295,10 @@ class CurrencyControllerTest extends TestCase public function testDisableInUse(): void { // create stuff - $currency = TransactionCurrency::first(); - $repository = $this->mock(CurrencyRepositoryInterface::class); - $userRepos = $this->mock(UserRepositoryInterface::class); + $currency = TransactionCurrency::first(); + $repository = $this->mock(CurrencyRepositoryInterface::class); + $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(CurrencyTransformer::class); // mock calls: $repository->shouldReceive('setUser')->once(); @@ -281,9 +317,17 @@ class CurrencyControllerTest extends TestCase public function testEnable(): void { // create stuff - $currency = TransactionCurrency::first(); - $repository = $this->mock(CurrencyRepositoryInterface::class); - $userRepos = $this->mock(UserRepositoryInterface::class); + $currency = TransactionCurrency::first(); + $repository = $this->mock(CurrencyRepositoryInterface::class); + $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(CurrencyTransformer::class); + + // mock 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(); @@ -295,7 +339,6 @@ class CurrencyControllerTest extends TestCase $response->assertJson( ['data' => [ 'type' => 'currencies', - 'id' => $currency->id, ],] ); $response->assertHeader('Content-Type', 'application/vnd.api+json'); @@ -308,14 +351,17 @@ class CurrencyControllerTest extends TestCase */ public function testIndex(): void { - $collection = TransactionCurrency::get(); // mock stuff: - $repository = $this->mock(CurrencyRepositoryInterface::class); - $userRepos = $this->mock(UserRepositoryInterface::class); + $repository = $this->mock(CurrencyRepositoryInterface::class); + $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(CurrencyTransformer::class); // mock calls: $repository->shouldReceive('setUser')->once(); - $repository->shouldReceive('getAll')->withNoArgs()->andReturn($collection)->once(); + $repository->shouldReceive('getAll')->withNoArgs()->andReturn(new Collection)->once(); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // test API $response = $this->get('/api/v1/currencies'); @@ -325,8 +371,8 @@ class CurrencyControllerTest extends TestCase [ 'meta' => [ 'pagination' => [ - 'total' => $collection->count(), - 'count' => $collection->count(), + 'total' => 0, + 'count' => 0, 'per_page' => true, // depends on user preference. 'current_page' => 1, 'total_pages' => 1, @@ -346,21 +392,28 @@ class CurrencyControllerTest extends TestCase public function testMakeDefault(): void { // create stuff - $currency = TransactionCurrency::first(); - $repository = $this->mock(CurrencyRepositoryInterface::class); - $userRepos = $this->mock(UserRepositoryInterface::class); + $currency = TransactionCurrency::first(); + $repository = $this->mock(CurrencyRepositoryInterface::class); + $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(CurrencyTransformer::class); // mock calls: $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('enable')->once(); + // mock 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]); + // test API $response = $this->post(route('api.v1.currencies.default', [$currency->code])); $response->assertStatus(200); $response->assertJson( ['data' => [ 'type' => 'currencies', - 'id' => $currency->id, ],] ); $response->assertHeader('Content-Type', 'application/vnd.api+json'); @@ -371,23 +424,22 @@ class CurrencyControllerTest extends TestCase */ public function testRecurrences(): void { - $recurrences = $this->user()->recurrences()->get(); - // mock stuff: + $recurrence = Recurrence::first(); $repository = $this->mock(RecurringRepositoryInterface::class); $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::class); // mock calls: $currencyRepos->shouldReceive('setUser')->once(); $repository->shouldReceive('setUser'); - $repository->shouldReceive('getAll')->once()->andReturn($recurrences); - $repository->shouldReceive('getNoteText')->andReturn('Notes.'); - $repository->shouldReceive('repetitionDescription')->andReturn('Some description.'); - $repository->shouldReceive('getXOccurrences')->andReturn([]); + $repository->shouldReceive('getAll')->once()->andReturn(new Collection([$recurrence])); + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // call API $currency = TransactionCurrency::first(); @@ -401,12 +453,15 @@ class CurrencyControllerTest extends TestCase */ public function testRules(): void { - $rules = $this->user()->rules()->get(); - $ruleRepos = $this->mock(RuleRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class); - $ruleRepos->shouldReceive('getAll')->once()->andReturn($rules); + $transformer = $this->mock(RuleTransformer::class); + + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + $ruleRepos->shouldReceive('getAll')->once()->andReturn(new Collection([Rule::first()])); $currencyRepos->shouldReceive('setUser')->once(); // call API @@ -425,20 +480,27 @@ class CurrencyControllerTest extends TestCase public function testShow(): void { // create stuff - $currency = TransactionCurrency::first(); - $repository = $this->mock(CurrencyRepositoryInterface::class); - $userRepos = $this->mock(UserRepositoryInterface::class); + $currency = TransactionCurrency::first(); + $repository = $this->mock(CurrencyRepositoryInterface::class); + $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(CurrencyTransformer::class); // mock calls: $repository->shouldReceive('setUser')->once(); + // mock 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]); + // test API $response = $this->get('/api/v1/currencies/' . $currency->code); $response->assertStatus(200); $response->assertJson( ['data' => [ 'type' => 'currencies', - 'id' => $currency->id, ],] ); $response->assertHeader('Content-Type', 'application/vnd.api+json'); @@ -453,9 +515,17 @@ class CurrencyControllerTest extends TestCase public function testStore(): void { - $currency = TransactionCurrency::first(); - $repository = $this->mock(CurrencyRepositoryInterface::class); - $userRepos = $this->mock(UserRepositoryInterface::class); + $currency = TransactionCurrency::first(); + $repository = $this->mock(CurrencyRepositoryInterface::class); + $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(CurrencyTransformer::class); + + // mock 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(); @@ -476,7 +546,6 @@ class CurrencyControllerTest extends TestCase $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'currencies', 'links' => true],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); - $response->assertSee($currency->name); } /** @@ -487,9 +556,17 @@ class CurrencyControllerTest extends TestCase */ public function testStoreWithDefault(): void { - $currency = TransactionCurrency::first(); - $repository = $this->mock(CurrencyRepositoryInterface::class); - $userRepos = $this->mock(UserRepositoryInterface::class); + $currency = TransactionCurrency::first(); + $repository = $this->mock(CurrencyRepositoryInterface::class); + $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(CurrencyTransformer::class); + + // mock 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]); $preference = new Preference; $preference->data = 'EUR'; @@ -516,7 +593,6 @@ class CurrencyControllerTest extends TestCase $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'currencies', 'links' => true],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); - $response->assertSee($currency->name); } /** @@ -527,8 +603,11 @@ class CurrencyControllerTest extends TestCase $currency = TransactionCurrency::first(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(TransactionTransformer::class); $accountRepos->shouldReceive('setUser'); + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $paginator = new LengthAwarePaginator(new Collection, 0, 50); $repository = $this->mock(JournalRepositoryInterface::class); @@ -566,8 +645,12 @@ class CurrencyControllerTest extends TestCase $currency = TransactionCurrency::first(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(TransactionTransformer::class); $accountRepos->shouldReceive('setUser'); + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + $paginator = new LengthAwarePaginator(new Collection, 0, 50); $repository = $this->mock(JournalRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); @@ -607,9 +690,17 @@ class CurrencyControllerTest extends TestCase */ public function testUpdate(): void { - $currency = TransactionCurrency::first(); - $repository = $this->mock(CurrencyRepositoryInterface::class); - $userRepos = $this->mock(UserRepositoryInterface::class); + $currency = TransactionCurrency::first(); + $repository = $this->mock(CurrencyRepositoryInterface::class); + $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(CurrencyTransformer::class); + + // mock 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(); @@ -630,7 +721,6 @@ class CurrencyControllerTest extends TestCase $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'currencies', 'links' => true],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); - $response->assertSee($currency->name); } /** @@ -644,9 +734,17 @@ class CurrencyControllerTest extends TestCase $currency = TransactionCurrency::first(); $repository = $this->mock(CurrencyRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(CurrencyTransformer::class); $preference = new Preference; $preference->data = 'EUR'; + // mock 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(); $repository->shouldReceive('update')->andReturn($currency); @@ -670,6 +768,5 @@ class CurrencyControllerTest extends TestCase $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'currencies', 'links' => true],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); - $response->assertSee($currency->name); } } diff --git a/tests/Api/V1/Controllers/CurrencyExchangeRateControllerTest.php b/tests/Api/V1/Controllers/CurrencyExchangeRateControllerTest.php index 2020a058ea..661a127d08 100644 --- a/tests/Api/V1/Controllers/CurrencyExchangeRateControllerTest.php +++ b/tests/Api/V1/Controllers/CurrencyExchangeRateControllerTest.php @@ -29,6 +29,7 @@ use FireflyIII\Models\CurrencyExchangeRate; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Services\Currency\ExchangeRateInterface; +use FireflyIII\Transformers\CurrencyExchangeRateTransformer; use Laravel\Passport\Passport; use Log; use Tests\TestCase; @@ -56,8 +57,9 @@ class CurrencyExchangeRateControllerTest extends TestCase public function testIndex(): void { // mock repository - $repository = $this->mock(CurrencyRepositoryInterface::class); - $service = $this->mock(ExchangeRateInterface::class); + $repository = $this->mock(CurrencyRepositoryInterface::class); + $service = $this->mock(ExchangeRateInterface::class); + $transformer = $this->mock(CurrencyExchangeRateTransformer::class); $rate = new CurrencyExchangeRate(); $rate->date = new Carbon(); @@ -67,6 +69,13 @@ class CurrencyExchangeRateControllerTest extends TestCase $rate->to_currency_id = 1; $rate->from_currency_id = 2; + // mock 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(); $repository->shouldReceive('findByCodeNull')->withArgs(['EUR'])->andReturn(TransactionCurrency::whereCode('EUR')->first())->once(); @@ -86,17 +95,6 @@ class CurrencyExchangeRateControllerTest extends TestCase $response->assertJson( ['data' => [ 'type' => 'currency_exchange_rates', - 'id' => '0', - 'attributes' => [ - 'rate' => 0.5, - ], - 'links' => [ - - [ - 'rel' => 'self', - 'uri' => '/currency_exchange_rates/', - ], - ], ], ] ); @@ -109,8 +107,9 @@ class CurrencyExchangeRateControllerTest extends TestCase public function testIndexBadDestination(): void { // mock repository - $repository = $this->mock(CurrencyRepositoryInterface::class); - $service = $this->mock(ExchangeRateInterface::class); + $repository = $this->mock(CurrencyRepositoryInterface::class); + $service = $this->mock(ExchangeRateInterface::class); + $transformer = $this->mock(CurrencyExchangeRateTransformer::class); $rate = new CurrencyExchangeRate(); $rate->date = new Carbon(); @@ -143,8 +142,9 @@ class CurrencyExchangeRateControllerTest extends TestCase public function testIndexBadSource(): void { // mock repository - $repository = $this->mock(CurrencyRepositoryInterface::class); - $service = $this->mock(ExchangeRateInterface::class); + $repository = $this->mock(CurrencyRepositoryInterface::class); + $service = $this->mock(ExchangeRateInterface::class); + $transformer = $this->mock(CurrencyExchangeRateTransformer::class); $rate = new CurrencyExchangeRate(); $rate->date = new Carbon(); diff --git a/tests/Api/V1/Controllers/ImportControllerTest.php b/tests/Api/V1/Controllers/ImportControllerTest.php index 48eedd065d..15dcd0618e 100644 --- a/tests/Api/V1/Controllers/ImportControllerTest.php +++ b/tests/Api/V1/Controllers/ImportControllerTest.php @@ -26,6 +26,8 @@ namespace Tests\Api\V1\Controllers; use FireflyIII\Helpers\Collector\TransactionCollectorInterface; use FireflyIII\Models\ImportJob; use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; +use FireflyIII\Transformers\ImportJobTransformer; +use FireflyIII\Transformers\TransactionTransformer; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Laravel\Passport\Passport; @@ -52,12 +54,16 @@ class ImportControllerTest extends TestCase */ public function testListAll(): void { - $repository = $this->mock(ImportJobRepositoryInterface::class); + $repository = $this->mock(ImportJobRepositoryInterface::class); + $transformer = $this->mock(ImportJobTransformer::class); $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('get')->once()->andReturn(new Collection); + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); - $response = $this->get(route('api.v1.import.list')); + + $response = $this->get(route('api.v1.import.list'), ['Accept' => 'application/json']); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -67,10 +73,19 @@ class ImportControllerTest extends TestCase public function testShow(): void { /** @var ImportJob $job */ - $job = $this->user()->importJobs()->first(); - $repository = $this->mock(ImportJobRepositoryInterface::class); + $job = $this->user()->importJobs()->first(); + $repository = $this->mock(ImportJobRepositoryInterface::class); + $transformer = $this->mock(ImportJobTransformer::class); $repository->shouldReceive('setUser')->once(); + // mock 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]); + + $response = $this->get(route('api.v1.import.show', [$job->key]), ['accept' => 'application/json']); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -85,9 +100,14 @@ class ImportControllerTest extends TestCase $tag = $this->user()->tags()->first(); $job->tag_id = $tag->id; $job->save(); - $repository = $this->mock(ImportJobRepositoryInterface::class); + $repository = $this->mock(ImportJobRepositoryInterface::class); + $transformer = $this->mock(TransactionTransformer::class); $repository->shouldReceive('setUser')->once(); + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + + // paginator: $paginator = new LengthAwarePaginator(new Collection, 0, 50); diff --git a/tests/Api/V1/Controllers/LinkTypeControllerTest.php b/tests/Api/V1/Controllers/LinkTypeControllerTest.php index 268bce73bc..1cc01fea5d 100644 --- a/tests/Api/V1/Controllers/LinkTypeControllerTest.php +++ b/tests/Api/V1/Controllers/LinkTypeControllerTest.php @@ -27,6 +27,8 @@ use FireflyIII\Helpers\Collector\TransactionCollectorInterface; use FireflyIII\Models\LinkType; use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface; use FireflyIII\Repositories\User\UserRepositoryInterface; +use FireflyIII\Transformers\LinkTypeTransformer; +use FireflyIII\Transformers\TransactionTransformer; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Laravel\Passport\Passport; @@ -114,20 +116,19 @@ class LinkTypeControllerTest extends TestCase */ public function testIndex(): void { - $linkTypes = LinkType::get(); - - // mock stuff: $repository = $this->mock(LinkTypeRepositoryInterface::class); $userRepository = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(LinkTypeTransformer::class); + // mock calls to transformer: + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // mock calls: $repository->shouldReceive('setUser')->once(); - $repository->shouldReceive('get')->once()->andReturn($linkTypes); + $repository->shouldReceive('get')->once()->andReturn(new Collection); // call API $response = $this->get('/api/v1/link_types'); $response->assertStatus(200); - $response->assertSee($linkTypes->first()->created_at->toAtomString()); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -141,6 +142,14 @@ class LinkTypeControllerTest extends TestCase // mock stuff: $repository = $this->mock(LinkTypeRepositoryInterface::class); $userRepository = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(LinkTypeTransformer::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(); @@ -148,7 +157,6 @@ class LinkTypeControllerTest extends TestCase // call API $response = $this->get('/api/v1/link_types/' . $linkType->id); $response->assertStatus(200); - $response->assertSee($linkType->first()->created_at->toAtomString()); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -163,6 +171,14 @@ class LinkTypeControllerTest extends TestCase // mock stuff: $repository = $this->mock(LinkTypeRepositoryInterface::class); $userRepository = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(LinkTypeTransformer::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(); @@ -182,7 +198,6 @@ class LinkTypeControllerTest extends TestCase // test API $response = $this->post('/api/v1/link_types', $data); $response->assertStatus(200); - $response->assertSee($linkType->created_at->toAtomString()); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -197,6 +212,7 @@ class LinkTypeControllerTest extends TestCase // mock stuff: $repository = $this->mock(LinkTypeRepositoryInterface::class); $userRepository = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(LinkTypeTransformer::class); // mock calls: $repository->shouldReceive('setUser')->once(); @@ -229,7 +245,12 @@ class LinkTypeControllerTest extends TestCase $linkTypeRepos = $this->mock(LinkTypeRepositoryInterface::class); $userRepos = $this->mock(UserRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); - $journalIds = $linkTypeRepos->shouldReceive('getJournalIds')->once()->andReturn([1, 2, 3]); + $transformer = $this->mock(TransactionTransformer::class); + + // mock calls to transformer: + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + + $journalIds = $linkTypeRepos->shouldReceive('getJournalIds')->once()->andReturn([1, 2, 3]); $collector->shouldReceive('setUser')->once()->andReturnSelf(); $collector->shouldReceive('withOpposingAccount')->once()->andReturnSelf(); $collector->shouldReceive('withCategoryInformation')->once()->andReturnSelf(); @@ -260,6 +281,15 @@ class LinkTypeControllerTest extends TestCase // mock stuff: $repository = $this->mock(LinkTypeRepositoryInterface::class); $userRepository = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(LinkTypeTransformer::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]); + $userRepository->shouldReceive('hasRole')->once()->andReturn(true); // create editable link type: @@ -290,7 +320,6 @@ class LinkTypeControllerTest extends TestCase $response = $this->put('/api/v1/link_types/' . $linkType->id, $data, ['Accept' => 'application/json']); $response->assertStatus(200); $response->assertHeader('Content-Type', 'application/vnd.api+json'); - $response->assertSee($linkType->created_at->toAtomString()); } /** @@ -302,6 +331,7 @@ class LinkTypeControllerTest extends TestCase // mock stuff: $repository = $this->mock(LinkTypeRepositoryInterface::class); $userRepository = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(LinkTypeTransformer::class); // create editable link type: $linkType = LinkType::create( @@ -341,6 +371,8 @@ class LinkTypeControllerTest extends TestCase // mock stuff: $repository = $this->mock(LinkTypeRepositoryInterface::class); $userRepository = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(LinkTypeTransformer::class); + $userRepository->shouldReceive('hasRole')->once()->andReturn(false); // create editable link type: diff --git a/tests/Api/V1/Controllers/PiggyBankControllerTest.php b/tests/Api/V1/Controllers/PiggyBankControllerTest.php index 60609c5f6d..64bde3964a 100644 --- a/tests/Api/V1/Controllers/PiggyBankControllerTest.php +++ b/tests/Api/V1/Controllers/PiggyBankControllerTest.php @@ -23,11 +23,12 @@ declare(strict_types=1); namespace Tests\Api\V1\Controllers; -use FireflyIII\Models\PiggyBank; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; +use FireflyIII\Transformers\PiggyBankEventTransformer; +use FireflyIII\Transformers\PiggyBankTransformer; use Illuminate\Support\Collection; use Laravel\Passport\Passport; use Log; @@ -76,28 +77,29 @@ class PiggyBankControllerTest extends TestCase public function testIndex(): void { // create stuff - $piggies = factory(PiggyBank::class, 10)->create(); $repository = $this->mock(PiggyBankRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(PiggyBankTransformer::class); + + // mock calls to transformer: + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // mock calls: $repository->shouldReceive('setUser'); - $repository->shouldReceive('getPiggyBanks')->withAnyArgs()->andReturn($piggies)->once(); - $repository->shouldReceive('getCurrentAmount')->andReturn('12'); - $repository->shouldReceive('getSuggestedMonthlyAmount')->andReturn('12'); + $repository->shouldReceive('getPiggyBanks')->withAnyArgs()->andReturn(new Collection())->once(); $accountRepos->shouldReceive('setUser'); - $accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1'); + //$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1'); $currencyRepos->shouldReceive('setUser'); - $currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::first()); + //$currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::first()); // test API $response = $this->get('/api/v1/piggy_banks'); $response->assertStatus(200); $response->assertJson(['data' => [],]); - $response->assertJson(['meta' => ['pagination' => ['total' => 10, 'count' => 10, 'per_page' => true, 'current_page' => 1, 'total_pages' => 1]],]); + $response->assertJson(['meta' => ['pagination' => ['total' => 0, 'count' => 0, 'per_page' => true, 'current_page' => 1, 'total_pages' => 1]],]); $response->assertJson( ['links' => ['self' => true, 'first' => true, 'last' => true,],] ); @@ -109,8 +111,14 @@ class PiggyBankControllerTest extends TestCase */ public function testPiggyBankEvents(): void { - $piggyBank = $this->user()->piggyBanks()->first(); - $repository = $this->mock(PiggyBankRepositoryInterface::class); + $piggyBank = $this->user()->piggyBanks()->first(); + $repository = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(PiggyBankEventTransformer::class); + + // mock calls to transformer: + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + + $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('getEvents')->once()->andReturn(new Collection); @@ -131,17 +139,25 @@ class PiggyBankControllerTest extends TestCase $repository = $this->mock(PiggyBankRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::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]); // mock calls: $repository->shouldReceive('setUser'); - $currencyRepos->shouldReceive('setUser')->once(); - $accountRepos->shouldReceive('setUser')->once(); + //$currencyRepos->shouldReceive('setUser')->once(); + //$accountRepos->shouldReceive('setUser')->once(); $repository->shouldReceive('getCurrentAmount')->andReturn('12'); $repository->shouldReceive('getSuggestedMonthlyAmount')->andReturn('12'); - $accountRepos->shouldReceive('setUser'); - $accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1'); + //$accountRepos->shouldReceive('setUser'); + //$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1'); $currencyRepos->shouldReceive('setUser'); $currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::first()); @@ -166,19 +182,27 @@ class PiggyBankControllerTest extends TestCase $repository = $this->mock(PiggyBankRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::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]); // mock calls: $repository->shouldReceive('setUser'); - $accountRepos->shouldReceive('setUser')->once(); + //$accountRepos->shouldReceive('setUser')->once(); $repository->shouldReceive('store')->once()->andReturn($piggy); - $repository->shouldReceive('getCurrentAmount')->andReturn('12')->once(); - $repository->shouldReceive('getSuggestedMonthlyAmount')->andReturn('12')->once(); + //$repository->shouldReceive('getCurrentAmount')->andReturn('12')->once(); + //$repository->shouldReceive('getSuggestedMonthlyAmount')->andReturn('12')->once(); - $accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1')->once(); + //$accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1')->once(); - $currencyRepos->shouldReceive('setUser')->once(); - $currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::first())->once(); + //$currencyRepos->shouldReceive('setUser')->once(); + //$currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::first())->once(); $data = [ 'name' => 'New piggy #' . random_int(1, 100000), @@ -204,6 +228,7 @@ class PiggyBankControllerTest extends TestCase $repository = $this->mock(PiggyBankRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::class); + $transformer = $this->mock(PiggyBankTransformer::class); // mock calls: $repository->shouldReceive('setUser'); @@ -237,18 +262,27 @@ class PiggyBankControllerTest extends TestCase $repository = $this->mock(PiggyBankRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $currencyRepos = $this->mock(CurrencyRepositoryInterface::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]); + // mock calls: $repository->shouldReceive('setUser'); - $currencyRepos->shouldReceive('setUser')->once(); - $accountRepos->shouldReceive('setUser')->once(); + //$currencyRepos->shouldReceive('setUser')->once(); + //$accountRepos->shouldReceive('setUser')->once(); $repository->shouldReceive('update')->once()->andReturn($piggy); $repository->shouldReceive('getCurrentAmount')->andReturn('12'); $repository->shouldReceive('getSuggestedMonthlyAmount')->andReturn('12'); - $accountRepos->shouldReceive('setUser'); + //$accountRepos->shouldReceive('setUser'); $accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1'); $currencyRepos->shouldReceive('setUser'); diff --git a/tests/Api/V1/Controllers/PreferencesControllerTest.php b/tests/Api/V1/Controllers/PreferencesControllerTest.php index be03426e87..28324d0ed8 100644 --- a/tests/Api/V1/Controllers/PreferencesControllerTest.php +++ b/tests/Api/V1/Controllers/PreferencesControllerTest.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace Tests\Api\V1\Controllers; use FireflyIII\Models\Preference; +use FireflyIII\Transformers\PreferenceTransformer; use Laravel\Passport\Passport; use Log; use Mockery; @@ -52,14 +53,18 @@ class PreferencesControllerTest extends TestCase */ public function testIndex(): void { - - $available = ['language', 'customFiscalYear', 'fiscalYearStart', 'currencyPreference', 'transaction_journal_optional_fields', 'frontPageAccounts', - 'viewRange', 'listPageSize, twoFactorAuthEnabled',]; + $transformer = $this->mock(PreferenceTransformer::class); + $available = ['language', 'customFiscalYear', 'fiscalYearStart', 'currencyPreference', 'transaction_journal_optional_fields', 'frontPageAccounts', + 'viewRange', 'listPageSize, twoFactorAuthEnabled',]; foreach ($available as $pref) { Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), $pref])->once(); } + // mock calls to transformer: + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + + // call API $response = $this->get('/api/v1/preferences'); @@ -72,11 +77,19 @@ class PreferencesControllerTest extends TestCase */ public function testUpdateArray(): void { + $transformer = $this->mock(PreferenceTransformer::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]); + /** @var Preference $preference */ $preference = Preferences::setForUser($this->user(), 'frontPageAccounts', [1, 2, 3]); $data = ['data' => '4,5,6']; $response = $this->put('/api/v1/preferences/' . $preference->name, $data, ['Accept' => 'application/json']); - $response->assertSee($preference->name); $response->assertStatus(200); } @@ -87,11 +100,19 @@ class PreferencesControllerTest extends TestCase */ public function testUpdateBoolean(): void { + $transformer = $this->mock(PreferenceTransformer::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]); + /** @var Preference $preference */ $preference = Preferences::setForUser($this->user(), 'twoFactorAuthEnabled', false); $data = ['data' => '1']; $response = $this->put('/api/v1/preferences/' . $preference->name, $data, ['Accept' => 'application/json']); - $response->assertSee($preference->name); $response->assertStatus(200); } @@ -102,11 +123,19 @@ class PreferencesControllerTest extends TestCase */ public function testUpdateDefault(): void { + $transformer = $this->mock(PreferenceTransformer::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]); + /** @var Preference $preference */ $preference = Preferences::setForUser($this->user(), 'currencyPreference', false); $data = ['data' => 'EUR']; $response = $this->put('/api/v1/preferences/' . $preference->name, $data, ['Accept' => 'application/json']); - $response->assertSee($preference->name); $response->assertStatus(200); } @@ -117,11 +146,19 @@ class PreferencesControllerTest extends TestCase */ public function testUpdateInteger(): void { + $transformer = $this->mock(PreferenceTransformer::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]); + /** @var Preference $preference */ $preference = Preferences::setForUser($this->user(), 'listPageSize', 13); $data = ['data' => '434']; $response = $this->put('/api/v1/preferences/' . $preference->name, $data, ['Accept' => 'application/json']); - $response->assertSee($preference->name); $response->assertStatus(200); } diff --git a/tests/Api/V1/Controllers/RecurrenceControllerTest.php b/tests/Api/V1/Controllers/RecurrenceControllerTest.php index 3a25b53e3d..c8327596c5 100644 --- a/tests/Api/V1/Controllers/RecurrenceControllerTest.php +++ b/tests/Api/V1/Controllers/RecurrenceControllerTest.php @@ -35,6 +35,8 @@ use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface; use FireflyIII\Repositories\User\UserRepositoryInterface; use FireflyIII\Support\Cronjobs\RecurringCronjob; +use FireflyIII\Transformers\RecurrenceTransformer; +use FireflyIII\Transformers\TransactionTransformer; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Laravel\Passport\Passport; @@ -86,35 +88,24 @@ class RecurrenceControllerTest extends TestCase */ public function testIndex(): void { - /** @var Recurrence $recurrences */ - $recurrences = $this->user()->recurrences()->get(); - - // mock stuff: $repository = $this->mock(RecurringRepositoryInterface::class); $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); $categoryFactory = $this->mock(CategoryFactory::class); + $transformer = $this->mock(RecurrenceTransformer::class); - $budgetRepos->shouldReceive('findNull')->atLeast()->once()->withAnyArgs() - ->andReturn($this->user()->budgets()->first()); - $piggyRepos->shouldReceive('findNull')->atLeast()->once()->withAnyArgs() - ->andReturn($this->user()->piggyBanks()->first()); + // mock calls to transformer: + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // mock calls: - $categoryFactory->shouldReceive('setUser')->atLeast()->once(); - $categoryFactory->shouldReceive('findOrCreate')->atLeast()->once()->andReturn(null); - $repository->shouldReceive('setUser'); - $repository->shouldReceive('getAll')->once()->andReturn($recurrences); - $repository->shouldReceive('getNoteText')->andReturn('Notes.'); - $repository->shouldReceive('repetitionDescription')->andReturn('Some description.'); - $repository->shouldReceive('getXOccurrences')->andReturn([]); + $repository->shouldReceive('setUser')->atLeast()->once(); + $repository->shouldReceive('getAll')->once()->andReturn(new Collection); // call API $response = $this->get('/api/v1/recurrences'); $response->assertStatus(200); - $response->assertSee($recurrences->first()->title); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -131,23 +122,22 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); $categoryFactory = $this->mock(CategoryFactory::class); + $transformer = $this->mock(RecurrenceTransformer::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]); - $budgetRepos->shouldReceive('findNull')->atLeast()->once()->withAnyArgs() - ->andReturn($this->user()->budgets()->first()); // mock calls: - $categoryFactory->shouldReceive('setUser')->atLeast()->once(); - $categoryFactory->shouldReceive('findOrCreate')->atLeast()->once()->andReturn(null); - $repository->shouldReceive('setUser'); - $repository->shouldReceive('getNoteText')->andReturn('Notes.'); - $repository->shouldReceive('repetitionDescription')->andReturn('Some description.'); - $repository->shouldReceive('getXOccurrences')->andReturn([]); + $repository->shouldReceive('setUser')->atLeast()->once(); // call API $response = $this->get('/api/v1/recurrences/' . $recurrence->id); $response->assertStatus(200); - $response->assertSee($recurrence->title); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -168,9 +158,16 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); - + $transformer = $this->mock(RecurrenceTransformer::class); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); + // 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'); $factory->shouldReceive('setUser'); @@ -223,10 +220,8 @@ class RecurrenceControllerTest extends TestCase // test API $response = $this->post('/api/v1/recurrences', $data, ['Accept' => 'application/json']); - $response->assertSee($recurrence->title); - $response->assertStatus(200); - $response->assertHeader('Content-Type', 'application/vnd.api+json'); + $response->assertStatus(200); } /** @@ -246,6 +241,14 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::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]); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); @@ -301,7 +304,6 @@ class RecurrenceControllerTest extends TestCase // test API $response = $this->post('/api/v1/recurrences', $data, ['Accept' => 'application/json']); - $response->assertSee($recurrence->title); $response->assertStatus(200); $response->assertHeader('Content-Type', 'application/vnd.api+json'); @@ -325,6 +327,14 @@ class RecurrenceControllerTest extends TestCase $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); + $transformer = $this->mock(RecurrenceTransformer::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'); @@ -380,7 +390,6 @@ class RecurrenceControllerTest extends TestCase // test API $response = $this->post('/api/v1/recurrences', $data, ['Accept' => 'application/json']); - $response->assertSee($recurrence->title); $response->assertStatus(200); $response->assertHeader('Content-Type', 'application/vnd.api+json'); @@ -403,6 +412,14 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::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]); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); @@ -463,7 +480,6 @@ class RecurrenceControllerTest extends TestCase // test API $response = $this->post('/api/v1/recurrences', $data, ['Accept' => 'application/json']); - $response->assertSee($recurrence->title); $response->assertStatus(200); $response->assertHeader('Content-Type', 'application/vnd.api+json'); @@ -486,6 +502,14 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::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]); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); $expenseAccount = $this->user()->accounts()->where('account_type_id', 4)->first(); @@ -543,7 +567,7 @@ class RecurrenceControllerTest extends TestCase // test API $response = $this->post('/api/v1/recurrences', $data, ['Accept' => 'application/json']); - $response->assertSee($recurrence->title); + $response->assertStatus(200); $response->assertHeader('Content-Type', 'application/vnd.api+json'); @@ -566,6 +590,7 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::class); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); @@ -658,6 +683,7 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::class); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); @@ -734,6 +760,7 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::class); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); @@ -809,6 +836,7 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::class); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); @@ -897,6 +925,7 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::class); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); @@ -971,6 +1000,7 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::class); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); @@ -1045,6 +1075,7 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::class); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); @@ -1120,6 +1151,7 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::class); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); @@ -1194,6 +1226,7 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::class); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); @@ -1268,6 +1301,7 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::class); // mock calls: $repository->shouldReceive('setUser'); @@ -1339,6 +1373,7 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::class); // mock calls: $repository->shouldReceive('setUser'); @@ -1413,6 +1448,7 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::class); // mock calls: $repository->shouldReceive('setUser'); @@ -1491,6 +1527,7 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::class); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); @@ -1557,6 +1594,7 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::class); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); @@ -1621,6 +1659,14 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::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]); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); $otherAssetAccount = $this->user()->accounts()->where('account_type_id', 3)->where('id', '!=', $assetAccount->id)->first(); @@ -1679,7 +1725,6 @@ class RecurrenceControllerTest extends TestCase // test API $response = $this->post('/api/v1/recurrences', $data, ['Accept' => 'application/json']); - $response->assertSee($recurrence->title); $response->assertStatus(200); $response->assertHeader('Content-Type', 'application/vnd.api+json'); @@ -1697,7 +1742,11 @@ class RecurrenceControllerTest extends TestCase $userRepos = $this->mock(UserRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); $categoryFactory = $this->mock(CategoryFactory::class); - $journalIds = $recurringRepos->shouldReceive('getJournalIds')->once()->andReturn([1, 2, 3]); + $transformer = $this->mock(TransactionTransformer::class); + + $transformer->shouldReceive('setParameters')->atLeast()->once(); + + $journalIds = $recurringRepos->shouldReceive('getJournalIds')->once()->andReturn([1, 2, 3]); $collector->shouldReceive('setUser')->once()->andReturnSelf(); $collector->shouldReceive('withOpposingAccount')->once()->andReturnSelf(); $collector->shouldReceive('withCategoryInformation')->once()->andReturnSelf(); @@ -1727,6 +1776,7 @@ class RecurrenceControllerTest extends TestCase $repository = $this->mock(RecurringRepositoryInterface::class); $cronjob = $this->mock(RecurringCronjob::class); $categoryFactory = $this->mock(CategoryFactory::class); + $transformer = $this->mock(RecurrenceTransformer::class); $cronjob->shouldReceive('fire')->andThrow(FireflyException::class); $repository->shouldReceive('setUser')->atLeast()->once(); @@ -1744,6 +1794,7 @@ class RecurrenceControllerTest extends TestCase $repository = $this->mock(RecurringRepositoryInterface::class); $cronjob = $this->mock(RecurringCronjob::class); $categoryFactory = $this->mock(CategoryFactory::class); + $transformer = $this->mock(RecurrenceTransformer::class); $cronjob->shouldReceive('fire')->once()->andReturnFalse(); $repository->shouldReceive('setUser')->atLeast()->once(); @@ -1759,6 +1810,7 @@ class RecurrenceControllerTest extends TestCase $repository = $this->mock(RecurringRepositoryInterface::class); $cronjob = $this->mock(RecurringCronjob::class); $categoryFactory = $this->mock(CategoryFactory::class); + $transformer = $this->mock(RecurrenceTransformer::class); $cronjob->shouldReceive('fire')->once()->andReturnTrue(); $repository->shouldReceive('setUser')->atLeast()->once(); @@ -1783,6 +1835,14 @@ class RecurrenceControllerTest extends TestCase $budgetRepos = $this->mock(BudgetRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $piggyRepos = $this->mock(PiggyBankRepositoryInterface::class); + $transformer = $this->mock(RecurrenceTransformer::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]); $assetAccount = $this->user()->accounts()->where('account_type_id', 3)->first(); @@ -1839,9 +1899,7 @@ class RecurrenceControllerTest extends TestCase // test API $response = $this->put('/api/v1/recurrences/' . $recurrence->id, $data, ['Accept' => 'application/json']); - $response->assertSee($recurrence->title); $response->assertStatus(200); - $response->assertHeader('Content-Type', 'application/vnd.api+json'); } diff --git a/tests/Api/V1/Controllers/RuleControllerTest.php b/tests/Api/V1/Controllers/RuleControllerTest.php index f6699adb06..2bcc8321e9 100644 --- a/tests/Api/V1/Controllers/RuleControllerTest.php +++ b/tests/Api/V1/Controllers/RuleControllerTest.php @@ -31,6 +31,8 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\Rule\RuleRepositoryInterface; use FireflyIII\TransactionRules\TransactionMatcher; +use FireflyIII\Transformers\RuleTransformer; +use FireflyIII\Transformers\TransactionTransformer; use Illuminate\Support\Collection; use Laravel\Passport\Passport; use Log; @@ -79,20 +81,21 @@ class RuleControllerTest extends TestCase */ public function testIndex(): void { - $rules = $this->user()->rules()->get(); - $ruleRepos = $this->mock(RuleRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); + $transformer = $this->mock(RuleTransformer::class); + + // mock calls to transformer: + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $accountRepos->shouldReceive('setUser')->once(); $ruleRepos->shouldReceive('setUser')->once(); - $ruleRepos->shouldReceive('getAll')->once()->andReturn($rules); + $ruleRepos->shouldReceive('getAll')->once()->andReturn(new Collection); // call API $response = $this->get('/api/v1/rules'); $response->assertStatus(200); - $response->assertSee($rules->first()->title); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -104,6 +107,14 @@ class RuleControllerTest extends TestCase $rule = $this->user()->rules()->first(); $ruleRepos = $this->mock(RuleRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); + $transformer = $this->mock(RuleTransformer::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]); $accountRepos->shouldReceive('setUser')->once(); $ruleRepos->shouldReceive('setUser')->once(); @@ -112,7 +123,6 @@ class RuleControllerTest extends TestCase // call API $response = $this->get('/api/v1/rules/' . $rule->id); $response->assertStatus(200); - $response->assertSee($rule->title); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -124,6 +134,14 @@ class RuleControllerTest extends TestCase { $ruleRepos = $this->mock(RuleRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); + $transformer = $this->mock(RuleTransformer::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]); $ruleRepos->shouldReceive('setUser')->once(); $rule = $this->user()->rules()->first(); @@ -136,14 +154,14 @@ class RuleControllerTest extends TestCase 'active' => 1, 'triggers' => [ [ - 'name' => 'description_is', + 'type' => 'description_is', 'value' => 'Hello', 'stop_processing' => 1, ], ], 'actions' => [ [ - 'name' => 'add_tag', + 'type' => 'add_tag', 'value' => 'A', 'stop_processing' => 1, ], @@ -167,6 +185,7 @@ class RuleControllerTest extends TestCase { $ruleRepos = $this->mock(RuleRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); + $transformer = $this->mock(RuleTransformer::class); $accountRepos->shouldReceive('setUser')->once(); $ruleRepos->shouldReceive('setUser')->once(); @@ -202,6 +221,7 @@ class RuleControllerTest extends TestCase { $ruleRepos = $this->mock(RuleRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); + $transformer = $this->mock(RuleTransformer::class); $accountRepos->shouldReceive('setUser')->once(); $ruleRepos->shouldReceive('setUser')->once(); @@ -241,6 +261,9 @@ class RuleControllerTest extends TestCase $matcher = $this->mock(TransactionMatcher::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); $ruleRepos = $this->mock(RuleRepositoryInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + $transformer->shouldReceive('setParameters')->atLeast()->once(); $asset = $this->getRandomAsset(); $repository->shouldReceive('setUser')->once(); @@ -276,8 +299,9 @@ class RuleControllerTest extends TestCase $matcher = $this->mock(TransactionMatcher::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); $ruleRepos = $this->mock(RuleRepositoryInterface::class); + $transformer = $this->mock(RuleTransformer::class); - $asset = $this->getRandomAsset(); + $asset = $this->getRandomAsset(); $repository->shouldReceive('setUser')->once(); $ruleRepos->shouldReceive('setUser')->once(); $repository->shouldReceive('findNull')->withArgs([1])->andReturn($asset); @@ -306,6 +330,14 @@ class RuleControllerTest extends TestCase { $ruleRepos = $this->mock(RuleRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); + $transformer = $this->mock(RuleTransformer::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]); $accountRepos->shouldReceive('setUser')->once(); $ruleRepos->shouldReceive('setUser')->once(); @@ -320,14 +352,14 @@ class RuleControllerTest extends TestCase 'active' => 1, 'triggers' => [ [ - 'name' => 'description_is', + 'type' => 'description_is', 'value' => 'Hello', 'stop_processing' => 1, ], ], 'actions' => [ [ - 'name' => 'add_tag', + 'type' => 'add_tag', 'value' => 'A', 'stop_processing' => 1, ], diff --git a/tests/Api/V1/Controllers/RuleGroupControllerTest.php b/tests/Api/V1/Controllers/RuleGroupControllerTest.php index 222e797e58..51c537320c 100644 --- a/tests/Api/V1/Controllers/RuleGroupControllerTest.php +++ b/tests/Api/V1/Controllers/RuleGroupControllerTest.php @@ -31,6 +31,9 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface; use FireflyIII\TransactionRules\TransactionMatcher; +use FireflyIII\Transformers\RuleGroupTransformer; +use FireflyIII\Transformers\RuleTransformer; +use FireflyIII\Transformers\TransactionTransformer; use Illuminate\Support\Collection; use Laravel\Passport\Passport; use Log; @@ -64,6 +67,7 @@ class RuleGroupControllerTest extends TestCase // mock stuff: $ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); + $transformer = $this->mock(RuleGroupTransformer::class); // mock calls: $accountRepos->shouldReceive('setUser')->once(); @@ -79,19 +83,20 @@ class RuleGroupControllerTest extends TestCase */ public function testIndex(): void { - $ruleGroups = $this->user()->ruleGroups()->get(); $ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); + $transformer = $this->mock(RuleGroupTransformer::class); $accountRepos->shouldReceive('setUser')->once(); $ruleGroupRepos->shouldReceive('setUser')->once(); - $ruleGroupRepos->shouldReceive('get')->once()->andReturn($ruleGroups); + $ruleGroupRepos->shouldReceive('get')->once()->andReturn(new Collection); + + $transformer->shouldReceive('setParameters')->atLeast()->once(); // call API $response = $this->get('/api/v1/rule_groups'); $response->assertStatus(200); - $response->assertSee($ruleGroups->first()->title); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -102,6 +107,11 @@ class RuleGroupControllerTest extends TestCase { $ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); + $transformer = $this->mock(RuleTransformer::class); + + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $accountRepos->shouldReceive('setUser')->once(); $ruleGroupRepos->shouldReceive('setUser')->once(); @@ -122,15 +132,22 @@ class RuleGroupControllerTest extends TestCase $ruleGroup = $this->user()->ruleGroups()->first(); $ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); + $transformer = $this->mock(RuleGroupTransformer::class); $accountRepos->shouldReceive('setUser')->once(); $ruleGroupRepos->shouldReceive('setUser')->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]); + // call API $response = $this->get('/api/v1/rule_groups/' . $ruleGroup->id); $response->assertStatus(200); - $response->assertSee($ruleGroup->title); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -142,6 +159,14 @@ class RuleGroupControllerTest extends TestCase { $ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); + $transformer = $this->mock(RuleGroupTransformer::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]); $accountRepos->shouldReceive('setUser')->once(); $ruleGroupRepos->shouldReceive('setUser')->once(); @@ -172,6 +197,11 @@ class RuleGroupControllerTest extends TestCase $repository = $this->mock(AccountRepositoryInterface::class); $matcher = $this->mock(TransactionMatcher::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + $ruleGroupRepos->shouldReceive('setUser')->once(); $ruleGroupRepos->shouldReceive('getActiveRules')->once()->andReturn(new Collection([$rule])); @@ -205,6 +235,7 @@ class RuleGroupControllerTest extends TestCase $ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); + $transformer = $this->mock(RuleGroupTransformer::class); $accountRepos->shouldReceive('setUser')->once(); $ruleGroupRepos->shouldReceive('setUser')->once(); @@ -260,6 +291,14 @@ class RuleGroupControllerTest extends TestCase { $ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); + $transformer = $this->mock(RuleGroupTransformer::class); + + // mock 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]); $accountRepos->shouldReceive('setUser')->once(); $ruleGroupRepos->shouldReceive('setUser')->once(); diff --git a/tests/Api/V1/Controllers/TagControllerTest.php b/tests/Api/V1/Controllers/TagControllerTest.php index 7921b8422a..fd1651d3db 100644 --- a/tests/Api/V1/Controllers/TagControllerTest.php +++ b/tests/Api/V1/Controllers/TagControllerTest.php @@ -26,6 +26,8 @@ namespace Tests\Api\V1\Controllers; use FireflyIII\Helpers\Collector\TransactionCollectorInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\Tag\TagRepositoryInterface; +use FireflyIII\Transformers\TagTransformer; +use FireflyIII\Transformers\TransactionTransformer; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Laravel\Passport\Passport; @@ -98,7 +100,11 @@ class TagControllerTest extends TestCase public function testIndex(): void { // mock stuff: - $tagRepos = $this->mock(TagRepositoryInterface::class); + $tagRepos = $this->mock(TagRepositoryInterface::class); + $transformer = $this->mock(TagTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // mock calls: $tagRepos->shouldReceive('setUser')->times(1); @@ -117,8 +123,16 @@ class TagControllerTest extends TestCase public function testShow(): void { // mock stuff: - $tagRepos = $this->mock(TagRepositoryInterface::class); - $tag = $this->user()->tags()->inRandomOrder()->first(); + $tagRepos = $this->mock(TagRepositoryInterface::class); + $tag = $this->user()->tags()->inRandomOrder()->first(); + $transformer = $this->mock(TagTransformer::class); + + // mock 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: $tagRepos->shouldReceive('setUser')->times(2); @@ -139,8 +153,17 @@ class TagControllerTest extends TestCase public function testShowByTag(): void { // mock stuff: - $tagRepos = $this->mock(TagRepositoryInterface::class); - $tag = $this->user()->tags()->inRandomOrder()->first(); + $tagRepos = $this->mock(TagRepositoryInterface::class); + $tag = $this->user()->tags()->inRandomOrder()->first(); + $transformer = $this->mock(TagTransformer::class); + + // mock 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: $tagRepos->shouldReceive('setUser')->times(2); $tagRepos->shouldReceive('findByTag')->once()->withArgs([(string)$tag->tag])->andReturn($tag); @@ -158,9 +181,18 @@ class TagControllerTest extends TestCase */ public function testStore(): void { - $tagRepos = $this->mock(TagRepositoryInterface::class); - $tag = $this->user()->tags()->inRandomOrder()->first(); - $data = ['tag' => 'Some tag' . random_int(1, 10000),]; + $tagRepos = $this->mock(TagRepositoryInterface::class); + $tag = $this->user()->tags()->inRandomOrder()->first(); + $data = ['tag' => 'Some tag' . random_int(1, 10000),]; + $transformer = $this->mock(TagTransformer::class); + + // mock 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]); + $tagRepos->shouldReceive('setUser')->times(1); $tagRepos->shouldReceive('store')->times(1)->andReturn($tag); @@ -182,6 +214,10 @@ class TagControllerTest extends TestCase $tag = $this->user()->tags()->inRandomOrder()->first(); $collector = $this->mock(TransactionCollectorInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $paginator = new LengthAwarePaginator([], 0, 50); @@ -217,9 +253,18 @@ class TagControllerTest extends TestCase */ public function testUpdate(): void { - $tagRepos = $this->mock(TagRepositoryInterface::class); - $tag = $this->user()->tags()->inRandomOrder()->first(); - $data = ['tag' => 'Some tag' . random_int(1, 10000),]; + $tagRepos = $this->mock(TagRepositoryInterface::class); + $tag = $this->user()->tags()->inRandomOrder()->first(); + $data = ['tag' => 'Some tag' . random_int(1, 10000),]; + $transformer = $this->mock(TagTransformer::class); + + // mock 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]); + $tagRepos->shouldReceive('setUser')->times(2); $tagRepos->shouldReceive('update')->times(1)->andReturn($tag); diff --git a/tests/Api/V1/Controllers/TransactionControllerTest.php b/tests/Api/V1/Controllers/TransactionControllerTest.php index d09ff42157..003b80f743 100644 --- a/tests/Api/V1/Controllers/TransactionControllerTest.php +++ b/tests/Api/V1/Controllers/TransactionControllerTest.php @@ -32,6 +32,9 @@ use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; +use FireflyIII\Transformers\AttachmentTransformer; +use FireflyIII\Transformers\PiggyBankEventTransformer; +use FireflyIII\Transformers\TransactionTransformer; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Laravel\Passport\Passport; @@ -62,6 +65,11 @@ class TransactionControllerTest extends TestCase $repository = $this->mock(JournalRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); $attachmentRepos = $this->mock(AttachmentRepositoryInterface::class); + $transformer = $this->mock(AttachmentTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + // mock calls: $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('getAttachmentsByTr')->once()->andReturn(new Collection); @@ -1253,6 +1261,7 @@ class TransactionControllerTest extends TestCase */ public function testIndex(): void { + $transformer = $this->mock(TransactionTransformer::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $accountRepos->shouldReceive('setUser'); $accountRepos->shouldReceive('getAccountsByType') @@ -1271,6 +1280,8 @@ class TransactionControllerTest extends TestCase $collector->shouldReceive('setPage')->andReturnSelf(); $collector->shouldReceive('setTypes')->andReturnSelf(); + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $collector->shouldReceive('getPaginatedTransactions')->andReturn($paginator); @@ -1293,6 +1304,7 @@ class TransactionControllerTest extends TestCase */ public function testIndexWithRange(): void { + $transformer = $this->mock(TransactionTransformer::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $accountRepos->shouldReceive('setUser'); $accountRepos->shouldReceive('getAccountsByType') @@ -1315,6 +1327,8 @@ class TransactionControllerTest extends TestCase $collector->shouldReceive('setRange')->andReturnSelf(); $collector->shouldReceive('getPaginatedTransactions')->andReturn($paginator); + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // mock some calls: @@ -1347,13 +1361,18 @@ class TransactionControllerTest extends TestCase public function testPiggyBankEvents(): void { // mock stuff: - $repository = $this->mock(JournalRepositoryInterface::class); - $collector = $this->mock(TransactionCollectorInterface::class); + $repository = $this->mock(JournalRepositoryInterface::class); + $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(PiggyBankEventTransformer::class); // mock calls: $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('getPiggyBankEventsbyTr')->once()->andReturn(new Collection); + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + + // get account: $transaction = $this->user()->transactions()->first(); @@ -1368,9 +1387,10 @@ class TransactionControllerTest extends TestCase * * @covers \FireflyIII\Api\V1\Controllers\TransactionController */ - public function testShowWithdrawal(): void + public function testShowDeposit(): void { - $withdrawal = $this->getRandomWithdrawal(); + $transformer = $this->mock(TransactionTransformer::class); + $deposit = $this->getRandomDeposit(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $accountRepos->shouldReceive('setUser'); $accountRepos->shouldReceive('getAccountsByType') @@ -1387,8 +1407,11 @@ class TransactionControllerTest extends TestCase $collector->shouldReceive('addFilter')->andReturnSelf()->once(); $collector->shouldReceive('getTransactions')->andReturn(new Collection); + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + // test API - $response = $this->get('/api/v1/transactions/' . $withdrawal->id); + $response = $this->get('/api/v1/transactions/' . $deposit->id); $response->assertStatus(200); $response->assertJson( [ @@ -1407,9 +1430,10 @@ class TransactionControllerTest extends TestCase * * @covers \FireflyIII\Api\V1\Controllers\TransactionController */ - public function testShowDeposit(): void + public function testShowWithdrawal(): void { - $deposit =$this->getRandomDeposit(); + $transformer = $this->mock(TransactionTransformer::class); + $withdrawal = $this->getRandomWithdrawal(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $accountRepos->shouldReceive('setUser'); $accountRepos->shouldReceive('getAccountsByType') @@ -1426,8 +1450,11 @@ class TransactionControllerTest extends TestCase $collector->shouldReceive('addFilter')->andReturnSelf()->once(); $collector->shouldReceive('getTransactions')->andReturn(new Collection); + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + // test API - $response = $this->get('/api/v1/transactions/' . $deposit->id); + $response = $this->get('/api/v1/transactions/' . $withdrawal->id); $response->assertStatus(200); $response->assertJson( [ @@ -1455,6 +1482,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // collector stuff: $collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf(); @@ -1513,6 +1544,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser')->once(); @@ -1569,6 +1604,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -1626,6 +1665,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -1681,6 +1724,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -1736,6 +1783,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -1793,6 +1844,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -1848,6 +1903,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -1904,6 +1963,11 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -1961,6 +2025,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -2017,6 +2085,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -2074,6 +2146,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); try { $this->expectsEvents(StoredTransactionJournal::class); @@ -2130,12 +2206,16 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); $accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account])); $journalRepos->shouldReceive('store')->andReturn($journal)->once(); + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + // collector stuff: $collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf(); $collector->shouldReceive('withOpposingAccount')->atLeast()->once()->andReturnSelf(); @@ -2193,12 +2273,16 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); $accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account])); $journalRepos->shouldReceive('store')->andReturn($journal)->once(); + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + // collector stuff: $collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf(); $collector->shouldReceive('withOpposingAccount')->atLeast()->once()->andReturnSelf(); @@ -2250,6 +2334,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -2307,6 +2395,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -2363,6 +2455,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -2419,6 +2515,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -2475,12 +2575,16 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); $accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection([$account])); $journalRepos->shouldReceive('store')->andReturn($journal)->once(); + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + // collector stuff: $collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf(); $collector->shouldReceive('withOpposingAccount')->atLeast()->once()->andReturnSelf(); @@ -2532,6 +2636,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -2588,6 +2696,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -2642,6 +2754,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -2697,6 +2813,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -2766,6 +2886,10 @@ class TransactionControllerTest extends TestCase $journalRepos = $this->mock(JournalRepositoryInterface::class)->makePartial(); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $journalRepos->shouldReceive('setUser')->once(); $accountRepos->shouldReceive('setUser'); @@ -2821,6 +2945,11 @@ class TransactionControllerTest extends TestCase $repository = $this->mock(JournalRepositoryInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); + $accountRepos->shouldReceive('setUser'); $accountRepos->shouldReceive('getAccountsById')->withArgs([[$account->id]])->andReturn(new Collection([$account])); @@ -2874,6 +3003,10 @@ class TransactionControllerTest extends TestCase $repository = $this->mock(JournalRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); $accountRepos = $this->mock(AccountRepositoryInterface::class); + $transformer = $this->mock(TransactionTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); $accountRepos->shouldReceive('setUser'); $accountRepos->shouldReceive('getAccountsById')->withArgs([[$account->id]])->andReturn(new Collection([$account])); diff --git a/tests/Api/V1/Controllers/TransactionLinkControllerTest.php b/tests/Api/V1/Controllers/TransactionLinkControllerTest.php index 9fea1a324a..8b8340bfd5 100644 --- a/tests/Api/V1/Controllers/TransactionLinkControllerTest.php +++ b/tests/Api/V1/Controllers/TransactionLinkControllerTest.php @@ -30,6 +30,7 @@ use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournalLink; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface; +use FireflyIII\Transformers\TransactionLinkTransformer; use Illuminate\Support\Collection; use Laravel\Passport\Passport; use Log; @@ -80,7 +81,6 @@ class TransactionLinkControllerTest extends TestCase */ public function testIndex(): void { - $journalLinks = TransactionJournalLink::get(); $transaction = Transaction::first(); $transaction->date = new Carbon; $transaction->transaction_type_type = 'Withdrawal'; @@ -88,25 +88,21 @@ class TransactionLinkControllerTest extends TestCase $repository = $this->mock(LinkTypeRepositoryInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionLinkTransformer::class); + + // mock transformer + $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); // mock calls: $repository->shouldReceive('setUser')->once(); $repository->shouldReceive('findByName')->once()->andReturn(null); - $repository->shouldReceive('getJournalLinks')->once()->andReturn($journalLinks); + $repository->shouldReceive('getJournalLinks')->once()->andReturn(new Collection); $journalRepos->shouldReceive('setUser')->once(); - $collector->shouldReceive('setUser')->withAnyArgs(); - $collector->shouldReceive('withOpposingAccount')->andReturnSelf(); - $collector->shouldReceive('withCategoryInformation')->andReturnSelf(); - $collector->shouldReceive('withBudgetInformation')->andReturnSelf(); - $collector->shouldReceive('setJournals')->andReturnSelf(); - $collector->shouldReceive('getTransactions')->andReturn(new Collection([$transaction])); - // call API $response = $this->get('/api/v1/transaction_links'); $response->assertStatus(200); - $response->assertSee($journalLinks->first()->id); } /** @@ -119,20 +115,24 @@ class TransactionLinkControllerTest extends TestCase $transaction->date = new Carbon; $transaction->transaction_type_type = 'Withdrawal'; + // mock stuff: $repository = $this->mock(LinkTypeRepositoryInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionLinkTransformer::class); + + // mock 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(); $journalRepos->shouldReceive('setUser')->once(); - $collector->shouldReceive('setUser')->withAnyArgs(); - $collector->shouldReceive('withOpposingAccount')->andReturnSelf(); - $collector->shouldReceive('withCategoryInformation')->andReturnSelf(); - $collector->shouldReceive('withBudgetInformation')->andReturnSelf(); - $collector->shouldReceive('setJournals')->andReturnSelf(); - $collector->shouldReceive('getTransactions')->andReturn(new Collection([$transaction])); + // call API $response = $this->get('/api/v1/transaction_links/' . $journalLink->id); @@ -156,18 +156,19 @@ class TransactionLinkControllerTest extends TestCase $repository = $this->mock(LinkTypeRepositoryInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionLinkTransformer::class); // mock calls: $repository->shouldReceive('setUser'); $journalRepos->shouldReceive('setUser'); $collector->shouldReceive('setUser')->withAnyArgs(); - $collector->shouldReceive('setUser')->withAnyArgs(); - $collector->shouldReceive('withOpposingAccount')->andReturnSelf(); - $collector->shouldReceive('withCategoryInformation')->andReturnSelf(); - $collector->shouldReceive('withBudgetInformation')->andReturnSelf(); - $collector->shouldReceive('setJournals')->andReturnSelf(); - $collector->shouldReceive('getTransactions')->andReturn(new Collection([$transaction])); + // mock 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]); $journalRepos->shouldReceive('findNull')->andReturn($journal); $repository->shouldReceive('storeLink')->once()->andReturn($journalLink); @@ -185,7 +186,6 @@ class TransactionLinkControllerTest extends TestCase // test API $response = $this->post('/api/v1/transaction_links', $data); $response->assertStatus(200); - $response->assertSee($journalLink->created_at->toAtomString()); // the creation moment. $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -448,6 +448,7 @@ class TransactionLinkControllerTest extends TestCase $repository = $this->mock(LinkTypeRepositoryInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); $collector = $this->mock(TransactionCollectorInterface::class); + $transformer = $this->mock(TransactionLinkTransformer::class); $journalLink = TransactionJournalLink::first(); $journal = $this->user()->transactionJournals()->find(1); @@ -455,6 +456,13 @@ class TransactionLinkControllerTest extends TestCase $transaction->date = new Carbon; $transaction->transaction_type_type = 'Withdrawal'; + // mock 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'); @@ -483,7 +491,6 @@ class TransactionLinkControllerTest extends TestCase // test API $response = $this->put('/api/v1/transaction_links/' . $journalLink->id, $data, ['Accept' => 'application/json']); $response->assertStatus(200); - $response->assertSee($journalLink->created_at->toAtomString()); // the creation moment. $response->assertHeader('Content-Type', 'application/vnd.api+json'); } diff --git a/tests/Api/V1/Controllers/UserControllerTest.php b/tests/Api/V1/Controllers/UserControllerTest.php index 5d207592e3..0c6a9de414 100644 --- a/tests/Api/V1/Controllers/UserControllerTest.php +++ b/tests/Api/V1/Controllers/UserControllerTest.php @@ -25,7 +25,9 @@ namespace Tests\Api\V1\Controllers; use FireflyIII\Repositories\User\UserRepositoryInterface; +use FireflyIII\Transformers\UserTransformer; use FireflyIII\User; +use Illuminate\Support\Collection; use Laravel\Passport\Passport; use Log; use Mockery; @@ -95,6 +97,7 @@ class UserControllerTest extends TestCase { $userRepository = $this->mock(UserRepositoryInterface::class); $userRepository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true); + // create a user first: // call API $response = $this->delete('/api/v1/users/' . $this->user()->id, [], ['Accept' => 'application/json']); @@ -109,20 +112,20 @@ class UserControllerTest extends TestCase */ public function testIndex(): void { - // create stuff - $users = factory(User::class, 10)->create(); // mock stuff: - $repository = $this->mock(UserRepositoryInterface::class); - $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true); + $repository = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(UserTransformer::class); - // mock calls: - $repository->shouldReceive('all')->withAnyArgs()->andReturn($users)->once(); + + $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true); + $repository->shouldReceive('all')->withAnyArgs()->andReturn(new Collection)->once(); + $transformer->shouldReceive('setParameters')->atLeast()->once(); // test API $response = $this->get('/api/v1/users', ['Accept' => 'application/json']); $response->assertStatus(200); $response->assertJson(['data' => [],]); - $response->assertJson(['meta' => ['pagination' => ['total' => 10, 'count' => 10, 'current_page' => 1, 'total_pages' => 1]],]); + $response->assertJson(['meta' => ['pagination' => ['total' => 0, 'count' => 0, 'current_page' => 1, 'total_pages' => 1]],]); $response->assertJson(['links' => ['self' => true, 'first' => true, 'last' => true,],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); } @@ -136,12 +139,19 @@ class UserControllerTest extends TestCase { $user = User::first(); $repository = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(UserTransformer::class); $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true); + // mock 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]); + // test API $response = $this->get('/api/v1/users/' . $user->id, ['Accept' => 'application/json']); $response->assertStatus(200); - $response->assertSee($user->email); } /** @@ -158,13 +168,20 @@ class UserControllerTest extends TestCase // mock $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(UserTransformer::class); $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true); $userRepos->shouldReceive('store')->once()->andReturn($this->user()); + // mock 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]); + // test API $response = $this->post('/api/v1/users', $data, ['Content-Type' => 'application/x-www-form-urlencoded']); $response->assertStatus(200); - $response->assertSee($this->user()->email); } /** @@ -183,13 +200,20 @@ class UserControllerTest extends TestCase // mock $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer= $this->mock(UserTransformer::class); $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true); $userRepos->shouldReceive('store')->once()->andReturn($this->user()); + // mock 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]); + // test API $response = $this->postJson('/api/v1/users', $data, ['Accept' => 'application/json']); $response->assertStatus(200); - $response->assertSee($this->user()->email); } /** @@ -273,9 +297,17 @@ class UserControllerTest extends TestCase // mock $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(UserTransformer::class); $userRepos->shouldReceive('update')->once()->andReturn($user); $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true); + // mock 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]); + // call API $response = $this->put('/api/v1/users/' . $user->id, $data, ['Accept' => 'application/json']); $response->assertStatus(200); @@ -300,9 +332,17 @@ class UserControllerTest extends TestCase // mock $userRepos = $this->mock(UserRepositoryInterface::class); + $transformer = $this->mock(UserTransformer::class); $userRepos->shouldReceive('update')->once()->andReturn($user); $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true); + // mock 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]); + // call API $response = $this->putJson('/api/v1/users/' . $user->id, $data, ['Accept' => 'application/json']); $response->assertStatus(200); diff --git a/tests/Feature/Controllers/PiggyBankControllerTest.php b/tests/Feature/Controllers/PiggyBankControllerTest.php index 093ca8c679..51578fe963 100644 --- a/tests/Feature/Controllers/PiggyBankControllerTest.php +++ b/tests/Feature/Controllers/PiggyBankControllerTest.php @@ -79,6 +79,7 @@ class PiggyBankControllerTest extends TestCase $piggyRepos->shouldReceive('getCurrentAmount')->andReturn('0'); $piggyRepos->shouldReceive('leftOnAccount')->andReturn('0'); + $this->be($this->user()); $response = $this->get(route('piggy-banks.add', [1])); $response->assertStatus(200); @@ -253,7 +254,8 @@ class PiggyBankControllerTest extends TestCase $accountRepos->shouldReceive('getNoteText')->withAnyArgs()->andReturn('Hello')->atLeast()->once(); $currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::find(1))->atLeast()->once(); - + $accountRepos->shouldReceive('getAccountType')->andReturn('Asset account')->atLeast()->once(); + $accountRepos->shouldReceive('getAccountCurrency')->andReturn(TransactionCurrency::find(1))->atLeast()->once(); $first = $this->user()->transactionJournals()->inRandomOrder()->first(); $piggies = $this->user()->piggyBanks()->take(2)->get();