From eccc58e75a73bd07e0dbe9cc2836d177634b8b2b Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 16 Aug 2025 19:31:59 +0200 Subject: [PATCH] Add piggy bank tests. --- .../Autocomplete/PiggyBankControllerTest.php | 164 ++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 tests/integration/Api/Autocomplete/PiggyBankControllerTest.php diff --git a/tests/integration/Api/Autocomplete/PiggyBankControllerTest.php b/tests/integration/Api/Autocomplete/PiggyBankControllerTest.php new file mode 100644 index 0000000000..0144d0777c --- /dev/null +++ b/tests/integration/Api/Autocomplete/PiggyBankControllerTest.php @@ -0,0 +1,164 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\integration\Api\Autocomplete; + +use FireflyIII\Enums\AccountTypeEnum; +use FireflyIII\Models\Account; +use FireflyIII\Models\AccountType; +use FireflyIII\Models\PiggyBank; +use FireflyIII\Models\TransactionCurrency; +use FireflyIII\User; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Tests\integration\TestCase; + +/** + * Class BudgetControllerTest + * + * @internal + * + * @coversNothing + */ +final class PiggyBankControllerTest extends TestCase +{ + /** + * @covers \FireflyIII\Api\V1\Controllers\Autocomplete\BudgetController + */ + use RefreshDatabase; + + private function createTestPiggyBanks(int $count, User $user): void + { + $type = AccountType::whereType(AccountTypeEnum::DEFAULT->value)->first(); + if (null === $type) { + $type = AccountType::create(['type' => AccountTypeEnum::DEFAULT->value]); + } + $currency = TransactionCurrency::whereCode('EUR')->first(); + if (null === $currency) { + $currency = TransactionCurrency::create( + [ + 'code' => 'EUR', + 'name' => 'Euro', + 'symbol' => '€', + ] + ); + } + for ($i = 1; $i <= $count; ++$i) { + $piggyBank = PiggyBank::create( + [ + 'user_id' => $user->id, + 'name' => 'Piggy bank ' . $i, + 'target_amount' => 1000, + 'transaction_currency_id' => $currency->id, + 'target_date' => now()->addDays(30), + 'user_group_id' => $user->user_group_id, + 'active' => 1, + ] + ); + $account = Account::create( + [ + 'user_id' => $user->id, + 'name' => 'Account ' . $i, + 'user_group_id' => $user->user_group_id, + 'account_type_id' => $type->id, + 'active' => 1, + ] + ); + $piggyBank->accounts()->save($account); + } + } + + public function testGivenAnUnauthenticatedRequestWhenCallingTheBudgetsEndpointThenReturns401HttpCode(): void + { + // test API + $response = $this->get(route('api.v1.autocomplete.piggy-banks'), ['Accept' => 'application/json']); + $response->assertStatus(401); + $response->assertHeader('Content-Type', 'application/json'); + $response->assertContent('{"message":"Unauthenticated.","exception":"AuthenticationException"}'); + } + + public function testGivenAuthenticatedRequestWhenCallingTheBudgetsEndpointThenReturns200HttpCode(): void + { + // act as a user + $user = $this->createAuthenticatedUser(); + $this->actingAs($user); + + $response = $this->get(route('api.v1.autocomplete.piggy-banks'), ['Accept' => 'application/json']); + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + + } + + public function testGivenAuthenticatedRequestWhenCallingTheBudgetsEndpointThenReturnsBudgets(): void + { + $user = $this->createAuthenticatedUser(); + $this->actingAs($user); + + $this->createTestPiggyBanks(5, $user); + $response = $this->get(route('api.v1.autocomplete.piggy-banks'), ['Accept' => 'application/json']); + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + $response->assertJsonCount(5); + $response->assertJsonFragment(['name' => 'Piggy bank 1']); + $response->assertJsonStructure([ + '*' => [ + 'id', + 'name', + ], + ]); + } + + public function testGivenAuthenticatedRequestWhenCallingTheBudgetsEndpointWithQueryThenReturnsBudgetsWithLimit(): void + { + $user = $this->createAuthenticatedUser(); + $this->actingAs($user); + + $this->createTestPiggyBanks(5, $user); + $response = $this->get(route('api.v1.autocomplete.piggy-banks', [ + 'query' => 'Piggy', + 'limit' => 3, + ]), ['Accept' => 'application/json']); + + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + $response->assertJsonCount(3); + } + + public function testGivenAuthenticatedRequestWhenCallingTheBudgetsEndpointWithQueryThenReturnsBudgetsThatMatchQuery(): void + { + $user = $this->createAuthenticatedUser(); + $this->actingAs($user); + + $this->createTestPiggyBanks(20, $user); + $response = $this->get(route('api.v1.autocomplete.piggy-banks', [ + 'query' => 'Piggy bank 1', + 'limit' => 20, + ]), ['Accept' => 'application/json']); + + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + // Budget 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 (11) + $response->assertJsonCount(11); + $response->assertJsonMissing(['name' => 'Piggy bank 2']); + } +}