From 328c9609509c953bdee2fb544f82d67969e4cc2f Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 15 Mar 2020 15:32:09 +0100 Subject: [PATCH] Fix API tests --- .../V1/Controllers/AccountControllerTest.php | 2 + .../Controllers/AttachmentControllerTest.php | 17 +++--- .../AvailableBudgetControllerTest.php | 1 + .../Api/V1/Controllers/BillControllerTest.php | 1 + .../V1/Controllers/BudgetControllerTest.php | 3 +- .../Controllers/BudgetLimitControllerTest.php | 1 + .../V1/Controllers/CategoryControllerTest.php | 1 + .../Chart/AccountControllerTest.php | 1 + .../Chart/AvailableBudgetControllerTest.php | 1 + .../Chart/CategoryControllerTest.php | 1 + .../V1/Controllers/CurrencyControllerTest.php | 1 + .../V1/Controllers/LinkTypeControllerTest.php | 1 + .../Controllers/PiggyBankControllerTest.php | 52 +++++++++++++++++++ .../Controllers/PreferencesControllerTest.php | 9 ++-- .../Controllers/RecurrenceControllerTest.php | 1 + .../Api/V1/Controllers/RuleControllerTest.php | 3 +- .../Controllers/RuleGroupControllerTest.php | 1 + .../V1/Controllers/SummaryControllerTest.php | 1 + .../Api/V1/Controllers/TagControllerTest.php | 5 +- .../Controllers/TransactionControllerTest.php | 1 + .../TransactionLinkControllerTest.php | 1 + .../Api/V1/Controllers/UserControllerTest.php | 1 + tests/Support/TestDataTrait.php | 22 ++++++-- tests/TestCase.php | 4 ++ 24 files changed, 112 insertions(+), 20 deletions(-) diff --git a/tests/Api/V1/Controllers/AccountControllerTest.php b/tests/Api/V1/Controllers/AccountControllerTest.php index 932e2c9e55..a8f354d2b3 100644 --- a/tests/Api/V1/Controllers/AccountControllerTest.php +++ b/tests/Api/V1/Controllers/AccountControllerTest.php @@ -46,6 +46,7 @@ class AccountControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } @@ -59,6 +60,7 @@ class AccountControllerTest extends TestCase */ public function testStoreInvalidBalance(): void { + // mock repositories $repository = $this->mock(AccountRepositoryInterface::class); diff --git a/tests/Api/V1/Controllers/AttachmentControllerTest.php b/tests/Api/V1/Controllers/AttachmentControllerTest.php index 6a40908a79..7dd3cd9c0e 100644 --- a/tests/Api/V1/Controllers/AttachmentControllerTest.php +++ b/tests/Api/V1/Controllers/AttachmentControllerTest.php @@ -48,6 +48,7 @@ class AttachmentControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } @@ -114,10 +115,10 @@ class AttachmentControllerTest extends TestCase // data to submit $data = [ - 'filename' => 'Some new att', - 'description' => sprintf('Attempt #%d', $this->randomInt()), - 'model' => 'TransactionJournal', - 'model_id' => $journal->id, + 'filename' => 'Some new att', + 'description' => sprintf('Attempt #%d', $this->randomInt()), + 'attachable_type' => 'TransactionJournal', + 'attachable_id' => $journal->id, ]; @@ -158,10 +159,10 @@ class AttachmentControllerTest extends TestCase $repository->shouldReceive('getNoteText')->andReturn('Hi There'); // data to submit $data = [ - 'filename' => $attachment->filename, - 'description' => sprintf('Attempt #%d', $this->randomInt()), - 'model' => 'TransactionJournal', - 'model_id' => 1, + 'filename' => $attachment->filename, + 'description' => sprintf('Attempt #%d', $this->randomInt()), + 'attachable_type' => 'TransactionJournal', + 'attachable_id' => 1, ]; // test API diff --git a/tests/Api/V1/Controllers/AvailableBudgetControllerTest.php b/tests/Api/V1/Controllers/AvailableBudgetControllerTest.php index 4e64dc5dc2..c23af29971 100644 --- a/tests/Api/V1/Controllers/AvailableBudgetControllerTest.php +++ b/tests/Api/V1/Controllers/AvailableBudgetControllerTest.php @@ -50,6 +50,7 @@ class AvailableBudgetControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } diff --git a/tests/Api/V1/Controllers/BillControllerTest.php b/tests/Api/V1/Controllers/BillControllerTest.php index 876000f6bb..bda9c04e5e 100644 --- a/tests/Api/V1/Controllers/BillControllerTest.php +++ b/tests/Api/V1/Controllers/BillControllerTest.php @@ -46,6 +46,7 @@ class BillControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } diff --git a/tests/Api/V1/Controllers/BudgetControllerTest.php b/tests/Api/V1/Controllers/BudgetControllerTest.php index 1f88c18b8d..e19b4dd450 100644 --- a/tests/Api/V1/Controllers/BudgetControllerTest.php +++ b/tests/Api/V1/Controllers/BudgetControllerTest.php @@ -50,6 +50,7 @@ class BudgetControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } @@ -86,7 +87,7 @@ class BudgetControllerTest extends TestCase ]; // test API - $response = $this->post(route('api.v1.budgets.store'), $data); + $response = $this->post(route('api.v1.budgets.store'), $data, ['Accept' => 'application/json']); $response->assertStatus(200); $response->assertJson(['data' => ['type' => 'budgets', 'links' => true],]); $response->assertHeader('Content-Type', 'application/vnd.api+json'); diff --git a/tests/Api/V1/Controllers/BudgetLimitControllerTest.php b/tests/Api/V1/Controllers/BudgetLimitControllerTest.php index 7eae543c25..ac0fcff587 100644 --- a/tests/Api/V1/Controllers/BudgetLimitControllerTest.php +++ b/tests/Api/V1/Controllers/BudgetLimitControllerTest.php @@ -48,6 +48,7 @@ class BudgetLimitControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } diff --git a/tests/Api/V1/Controllers/CategoryControllerTest.php b/tests/Api/V1/Controllers/CategoryControllerTest.php index e846c8298e..00b1421713 100644 --- a/tests/Api/V1/Controllers/CategoryControllerTest.php +++ b/tests/Api/V1/Controllers/CategoryControllerTest.php @@ -48,6 +48,7 @@ class CategoryControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } diff --git a/tests/Api/V1/Controllers/Chart/AccountControllerTest.php b/tests/Api/V1/Controllers/Chart/AccountControllerTest.php index 0b4014b7a0..24b73c3aad 100644 --- a/tests/Api/V1/Controllers/Chart/AccountControllerTest.php +++ b/tests/Api/V1/Controllers/Chart/AccountControllerTest.php @@ -51,6 +51,7 @@ class AccountControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } diff --git a/tests/Api/V1/Controllers/Chart/AvailableBudgetControllerTest.php b/tests/Api/V1/Controllers/Chart/AvailableBudgetControllerTest.php index 6d59378aa2..a303d40b19 100644 --- a/tests/Api/V1/Controllers/Chart/AvailableBudgetControllerTest.php +++ b/tests/Api/V1/Controllers/Chart/AvailableBudgetControllerTest.php @@ -46,6 +46,7 @@ class AvailableBudgetControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } diff --git a/tests/Api/V1/Controllers/Chart/CategoryControllerTest.php b/tests/Api/V1/Controllers/Chart/CategoryControllerTest.php index 04b21190fb..a8291e3ae8 100644 --- a/tests/Api/V1/Controllers/Chart/CategoryControllerTest.php +++ b/tests/Api/V1/Controllers/Chart/CategoryControllerTest.php @@ -49,6 +49,7 @@ class CategoryControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } diff --git a/tests/Api/V1/Controllers/CurrencyControllerTest.php b/tests/Api/V1/Controllers/CurrencyControllerTest.php index 94a0c9ef7b..13f5a89a74 100644 --- a/tests/Api/V1/Controllers/CurrencyControllerTest.php +++ b/tests/Api/V1/Controllers/CurrencyControllerTest.php @@ -50,6 +50,7 @@ class CurrencyControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } diff --git a/tests/Api/V1/Controllers/LinkTypeControllerTest.php b/tests/Api/V1/Controllers/LinkTypeControllerTest.php index 6a53894469..a9c431d8c8 100644 --- a/tests/Api/V1/Controllers/LinkTypeControllerTest.php +++ b/tests/Api/V1/Controllers/LinkTypeControllerTest.php @@ -48,6 +48,7 @@ class LinkTypeControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } diff --git a/tests/Api/V1/Controllers/PiggyBankControllerTest.php b/tests/Api/V1/Controllers/PiggyBankControllerTest.php index 40815f54e9..b2f14c897f 100644 --- a/tests/Api/V1/Controllers/PiggyBankControllerTest.php +++ b/tests/Api/V1/Controllers/PiggyBankControllerTest.php @@ -51,6 +51,7 @@ class PiggyBankControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } @@ -171,5 +172,56 @@ class PiggyBankControllerTest extends TestCase $response->assertHeader('Content-Type', 'application/vnd.api+json'); } + /** + * @covers \FireflyIII\Api\V1\Controllers\PiggyBankController + * @throws Exception + */ + public function testUpdateWithAmount(): void + { + // create stuff + $piggy = $this->getRandomPiggyBank(); + + // mock stuff: + $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'); + + $repository->shouldReceive('update')->once()->andReturn($piggy); + $repository->shouldReceive('setCurrentAmount')->once(); + $repository->shouldReceive('getCurrentAmount')->andReturn('0'); + $repository->shouldReceive('getSuggestedMonthlyAmount')->andReturn('12'); + + //$accountRepos->shouldReceive('setUser'); + $accountRepos->shouldReceive('getMetaValue')->withArgs([Mockery::any(), 'currency_id'])->andReturn('1'); + + $currencyRepos->shouldReceive('setUser'); + $currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn($this->getEuro()); + + $data = [ + 'name' => 'new pigy bank ' . $this->randomInt(), + 'account_id' => 1, + 'current_amount' => '5', + 'target_amount' => '100', + ]; + + // test API + $response = $this->put(route('api.v1.piggy_banks.update', [$piggy->id]), $data, ['Accept' => 'application/json']); + $response->assertStatus(200); + $response->assertJson(['data' => ['type' => 'piggy_banks', 'links' => true],]); + $response->assertHeader('Content-Type', 'application/vnd.api+json'); + } + } diff --git a/tests/Api/V1/Controllers/PreferencesControllerTest.php b/tests/Api/V1/Controllers/PreferencesControllerTest.php index 25ed6964ff..aa091364fc 100644 --- a/tests/Api/V1/Controllers/PreferencesControllerTest.php +++ b/tests/Api/V1/Controllers/PreferencesControllerTest.php @@ -49,6 +49,7 @@ class PreferencesControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } @@ -99,12 +100,12 @@ class PreferencesControllerTest extends TestCase $saved = new Preference; $saved->user_id = $this->user()->id; - $saved->name = 'twoFactorAuthEnabled'; + $saved->name = 'customFiscalYear'; $saved->data = false; $saved->save(); Preferences::shouldReceive('getForUser')->atLeast()->once()->withArgs([Mockery::any(), 'frontPageAccounts', []])->andReturn($countable); - Preferences::shouldReceive('set')->atLeast()->once()->withArgs(['twoFactorAuthEnabled', '1'])->atLeast()->once()->andReturn($pref); + Preferences::shouldReceive('set')->atLeast()->once()->withArgs(['customFiscalYear', '1'])->atLeast()->once()->andReturn($pref); // mock calls to transformer: $transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once(); @@ -116,7 +117,7 @@ class PreferencesControllerTest extends TestCase /** @var Preference $preference */ $data = ['data' => '1']; - $response = $this->put(route('api.v1.preferences.update', ['twoFactorAuthEnabled']), $data, ['Accept' => 'application/json']); + $response = $this->put(route('api.v1.preferences.update', ['customFiscalYear']), $data, ['Accept' => 'application/json']); $response->assertStatus(200); @@ -147,7 +148,7 @@ class PreferencesControllerTest extends TestCase $saved = new Preference; $saved->user_id = $this->user()->id; - $saved->name = 'twoFactorEnabled'; + $saved->name = 'customFiscalYear'; $saved->data = false; $saved->save(); diff --git a/tests/Api/V1/Controllers/RecurrenceControllerTest.php b/tests/Api/V1/Controllers/RecurrenceControllerTest.php index 661a206436..9534a49a34 100644 --- a/tests/Api/V1/Controllers/RecurrenceControllerTest.php +++ b/tests/Api/V1/Controllers/RecurrenceControllerTest.php @@ -49,6 +49,7 @@ class RecurrenceControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } diff --git a/tests/Api/V1/Controllers/RuleControllerTest.php b/tests/Api/V1/Controllers/RuleControllerTest.php index 9be548e1ba..0725debeaf 100644 --- a/tests/Api/V1/Controllers/RuleControllerTest.php +++ b/tests/Api/V1/Controllers/RuleControllerTest.php @@ -35,9 +35,9 @@ use FireflyIII\Transformers\RuleTransformer; use FireflyIII\Transformers\TransactionGroupTransformer; use Laravel\Passport\Passport; use Log; +use Mockery; use Preferences; use Tests\TestCase; -use Mockery; /** * @@ -55,6 +55,7 @@ class RuleControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } diff --git a/tests/Api/V1/Controllers/RuleGroupControllerTest.php b/tests/Api/V1/Controllers/RuleGroupControllerTest.php index 7333ca03af..e6867368c6 100644 --- a/tests/Api/V1/Controllers/RuleGroupControllerTest.php +++ b/tests/Api/V1/Controllers/RuleGroupControllerTest.php @@ -60,6 +60,7 @@ class RuleGroupControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } diff --git a/tests/Api/V1/Controllers/SummaryControllerTest.php b/tests/Api/V1/Controllers/SummaryControllerTest.php index e789fa35ce..5486bd0ba9 100644 --- a/tests/Api/V1/Controllers/SummaryControllerTest.php +++ b/tests/Api/V1/Controllers/SummaryControllerTest.php @@ -56,6 +56,7 @@ class SummaryControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } diff --git a/tests/Api/V1/Controllers/TagControllerTest.php b/tests/Api/V1/Controllers/TagControllerTest.php index 9339462aaa..7494f3d994 100644 --- a/tests/Api/V1/Controllers/TagControllerTest.php +++ b/tests/Api/V1/Controllers/TagControllerTest.php @@ -44,6 +44,7 @@ class TagControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } @@ -51,7 +52,7 @@ class TagControllerTest extends TestCase * Create Tag over API. * * @covers \FireflyIII\Api\V1\Controllers\TagController - * @covers \FireflyIII\Api\V1\Requests\TagRequest + * @covers \FireflyIII\Api\V1\Requests\TagStoreRequest */ public function testStore(): void { @@ -120,7 +121,7 @@ class TagControllerTest extends TestCase * Update Tag over API. * * @covers \FireflyIII\Api\V1\Controllers\TagController - * @covers \FireflyIII\Api\V1\Requests\TagRequest + * @covers \FireflyIII\Api\V1\Requests\TagUpdateRequest */ public function testUpdate(): void { diff --git a/tests/Api/V1/Controllers/TransactionControllerTest.php b/tests/Api/V1/Controllers/TransactionControllerTest.php index 70f54172bc..a647108edc 100644 --- a/tests/Api/V1/Controllers/TransactionControllerTest.php +++ b/tests/Api/V1/Controllers/TransactionControllerTest.php @@ -53,6 +53,7 @@ class TransactionControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } diff --git a/tests/Api/V1/Controllers/TransactionLinkControllerTest.php b/tests/Api/V1/Controllers/TransactionLinkControllerTest.php index 720f379f79..83e89b52ce 100644 --- a/tests/Api/V1/Controllers/TransactionLinkControllerTest.php +++ b/tests/Api/V1/Controllers/TransactionLinkControllerTest.php @@ -51,6 +51,7 @@ class TransactionLinkControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } diff --git a/tests/Api/V1/Controllers/UserControllerTest.php b/tests/Api/V1/Controllers/UserControllerTest.php index 85f9511c0b..282cde7c4a 100644 --- a/tests/Api/V1/Controllers/UserControllerTest.php +++ b/tests/Api/V1/Controllers/UserControllerTest.php @@ -48,6 +48,7 @@ class UserControllerTest extends TestCase { parent::setUp(); Passport::actingAs($this->user()); + $this->mockDefaultConfiguration(); Log::info(sprintf('Now in %s.', get_class($this))); } diff --git a/tests/Support/TestDataTrait.php b/tests/Support/TestDataTrait.php index 707f8ea7a5..959612b4ec 100644 --- a/tests/Support/TestDataTrait.php +++ b/tests/Support/TestDataTrait.php @@ -422,11 +422,18 @@ trait TestDataTrait 'currency_symbol' => $currency->symbol, 'currency_code' => $currency->code, 'currency_decimal_places' => $currency->decimal_places, - 'transaction_journals' => [], + 'categories' => [ + 0 => [ + 'id' => 0, + 'name' => 'no cat', + 'transaction_journals' => [], + ], + ], ]; + // add two random amounts: for ($i = 0; $i < 2; $i++) { - $data[$currency->id]['transaction_journals'][$i] = [ + $data[$currency->id]['categories'][0]['transaction_journals'][$i] = [ 'amount' => $amount, 'date' => $date, ]; @@ -464,11 +471,18 @@ trait TestDataTrait 'currency_symbol' => $currency->symbol, 'currency_code' => $currency->code, 'currency_decimal_places' => $currency->decimal_places, - 'transaction_journals' => [], + 'categories' => [ + 0 => [ + 'id' => 0, + 'name' => 'no cat', + 'transaction_journals' => [], + ], + ], ]; + // add two random amounts: for ($i = 0; $i < 2; $i++) { - $data[$currency->id]['transaction_journals'][$i] = [ + $data[$currency->id]['categories'][0]['transaction_journals'][$i] = [ 'amount' => $amount, 'date' => $date, ]; diff --git a/tests/TestCase.php b/tests/TestCase.php index fdaf689aef..057517b80c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -231,7 +231,11 @@ abstract class TestCase extends BaseTestCase $falseConfig = new Configuration; $falseConfig->data = false; + $idConfig = new Configuration; + $idConfig->data = 'abc'; + FireflyConfig::shouldReceive('get')->withArgs(['is_demo_site', false])->andReturn($falseConfig); + FireflyConfig::shouldReceive('get')->withArgs(['installation_id', null])->andReturn($idConfig); } /**