diff --git a/tests/integration/Api/Autocomplete/CurrencyControllerTest.php b/tests/integration/Api/Autocomplete/CurrencyControllerTest.php new file mode 100644 index 0000000000..0ee08f220a --- /dev/null +++ b/tests/integration/Api/Autocomplete/CurrencyControllerTest.php @@ -0,0 +1,172 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\integration\Api\Autocomplete; + +use FireflyIII\Models\TransactionCurrency; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Tests\integration\TestCase; +use FireflyIII\User; +use FireflyIII\Models\UserGroup; + + +/** + * Class CurrencyControllerTest + * + * @internal + * + * @coversNothing + */ +final class CurrencyControllerTest extends TestCase { + /** + * @covers \FireflyIII\Api\V1\Controllers\Autocomplete\CurrencyController + */ + use RefreshDatabase; + + protected function createAuthenticatedUser(): User + { + $userGroup = UserGroup::create(['title' => 'Test Group']); + + + $user= User::create([ + 'email' => 'test@email.com', + 'password' => 'password', + ]); + $user->user_group_id = $userGroup->id; + $user->save(); + return $user; + } + + private function createTestCurrencies(int $count, bool $enabled): void + { + for ($i = 1; $i <= $count; ++$i) { + $currency = TransactionCurrency::create([ + 'name' => 'Currency '.$i, + 'code' => 'CUR'.$i, + 'symbol' => 'C'.$i, + 'decimal_places' => $i, + 'enabled' => $enabled + ]); + } + } + + public function testGivenAnUnauthenticatedRequestWhenCallingTheCurrenciesEndpointThenReturns401HttpCode(): void + { + // test API + $response = $this->get(route('api.v1.autocomplete.currencies'), ['Accept' => 'application/json']); + $response->assertStatus(401); + $response->assertHeader('Content-Type', 'application/json'); + $response->assertContent('{"message":"Unauthenticated","exception":"AuthenticationException"}'); + } + + public function testGivenAuthenticatedRequestWhenCallingTheCurrenciesEndpointThenReturns200HttpCode(): void + { + // act as a user + $user = $this->createAuthenticatedUser(); + $this->actingAs($user); + + // test API + $response = $this->get(route('api.v1.autocomplete.currencies'), ['Accept' => 'application/json']); + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + } + + public function testGivenAuthenticatedRequestWhenCallingTheCurrenciesEndpointThenReturnsACollectionOfEnabledCurrencies(): void + { + // act as a user + $user = $this->createAuthenticatedUser(); + $this->actingAs($user); + + // create test data + $this->createTestCurrencies(10, true); + $this->createTestCurrencies(5, false); + + // test API + $response = $this->get(route('api.v1.autocomplete.currencies'), ['Accept' => 'application/json']); + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + $response->assertJsonFragment(['name' => 'Currency 1']); + $response->assertJsonFragment(['code' => 'CUR1']); + $response->assertJsonStructure([ + '*' => [ + 'id', + 'name', + 'code', + 'symbol', + 'decimal_places', + ], + ]); + + $response->assertJsonCount(10); + } + + public function testGivenAuthenticatedRequestWhenCallingTheCurrenciesEndpointWithQueryThenReturnsCurrenciesWithLimit(): void + { + // act as a user + $user = $this->createAuthenticatedUser(); + $this->actingAs($user); + + // create test data + $this->createTestCurrencies(5, true); + + // test API + $response = $this->get(route('api.v1.autocomplete.currencies', ['query' => 'Currency 1']), ['Accept' => 'application/json']); + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + $response->assertJsonFragment(['name' => 'Currency 1']); + $response->assertJsonStructure([ + '*' => [ + 'id', + 'name', + 'code', + 'symbol', + 'decimal_places', + ], + ]); + + $response->assertJsonCount(1); + +} + + + + public function testGivenAuthenticatedRequestWhenCallingTheCurrenciesEndpointWithQueryThenReturnsCurrenciesThatMatchQuery(): void + { + $user = $this->createAuthenticatedUser(); + $this->actingAs($user); + + $this->createTestCurrencies(20, true); + $response = $this->get(route('api.v1.autocomplete.currencies', [ + 'query' => 'Currency 1', + 'limit' => 20, + ]), ['Accept' => 'application/json']); + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + // Currency 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 (11) + $response->assertJsonCount(11); + } + + + +} \ No newline at end of file