From db51da593671c18468551fba7c0ad6d636f79986 Mon Sep 17 00:00:00 2001 From: Daniel Trolezi Date: Tue, 15 Oct 2024 17:32:48 -0300 Subject: [PATCH] Fix tests --- app/Http/Requests/RegisterAccountRequest.php | 2 +- app/Http/Requests/UpdateAccountRequest.php | 4 +- app/Models/Firestore.php | 28 ++++++-- app/Models/User.php | 2 + app/Repositories/UserRepository.php | 10 +-- app/Swagger/Application.php | 6 +- database/factories/UserFactory.php | 19 ++++- database/factories/UserSettingFactory.php | 30 -------- .../0001_01_01_000000_create_users_table.php | 34 --------- ...2024_08_14_210442_create_health_tables.php | 46 ------------ ...8_18_053353_create_user_settings_table.php | 34 --------- database/seeders/DatabaseSeeder.php | 1 - docker-compose.yml | 10 +-- phpunit.xml | 3 +- .../Controllers/AccountControllerTest.php | 51 ++++++------- .../Http/Controllers/AuthControllerTest.php | 3 - .../Controllers/RawgDomainControllerTest.php | 3 - .../Controllers/RawgGamesControllerTest.php | 3 - tests/TestCase.php | 72 +++++++++++++++++-- .../Unit/Console/Commands/CreateRootTest.php | 4 -- tests/Unit/Guards/JwtGuardTest.php | 12 ++-- tests/Unit/Models/PaginationTest.php | 2 +- tests/Unit/Models/UserSettingTest.php | 21 ------ tests/Unit/Models/UserTest.php | 7 +- .../Unit/Repositories/UserRepositoryTest.php | 37 +++++----- tests/Unit/Services/AuthServiceTest.php | 3 - .../Services/Rawg/RawgGamesServiceTest.php | 5 +- tests/Unit/Swagger/ApplicationTest.php | 2 - tests/Utils/FirestoreTestUtils.php | 63 ++++++++++++++++ 29 files changed, 237 insertions(+), 280 deletions(-) delete mode 100644 database/factories/UserSettingFactory.php delete mode 100755 database/migrations/0001_01_01_000000_create_users_table.php delete mode 100644 database/migrations/2024_08_14_210442_create_health_tables.php delete mode 100644 database/migrations/2024_08_18_053353_create_user_settings_table.php delete mode 100644 tests/Unit/Models/UserSettingTest.php create mode 100644 tests/Utils/FirestoreTestUtils.php diff --git a/app/Http/Requests/RegisterAccountRequest.php b/app/Http/Requests/RegisterAccountRequest.php index 787e661..c471df5 100644 --- a/app/Http/Requests/RegisterAccountRequest.php +++ b/app/Http/Requests/RegisterAccountRequest.php @@ -27,7 +27,7 @@ public function rules(): array 'email' => ['required', 'email', new Unique('users', 'email')], 'password' => 'required|string|min:6|max:18', 'discord' => 'sometimes|array', - 'discord.user_id' => 'required|string' + 'discord.user_id' => 'sometimes|string' ]; } } diff --git a/app/Http/Requests/UpdateAccountRequest.php b/app/Http/Requests/UpdateAccountRequest.php index 8ea0cb4..be9bd35 100644 --- a/app/Http/Requests/UpdateAccountRequest.php +++ b/app/Http/Requests/UpdateAccountRequest.php @@ -32,9 +32,9 @@ public function rules(): array 'password' => 'sometimes|string|min:6|max:18', 'settings' => 'sometimes|array', 'settings.platforms' => ['sometimes', 'array'], - 'settings.platforms.*' => ['required', 'string', 'in:' . Platform::valuesAsString()], + 'settings.platforms.*' => ['sometimes', 'string', 'in:' . Platform::valuesAsString()], 'settings.genres' => ['sometimes', 'array'], - 'settings.genres.*' => ['required', 'string', 'in:' . RawgGenre::valuesAsString()], + 'settings.genres.*' => ['sometimes', 'string', 'in:' . RawgGenre::valuesAsString()], 'settings.period' => ['sometimes', 'string', 'in:' . Period::valuesAsString()], 'settings.frequency' => ['sometimes', 'string', 'in:' . Frequency::valuesAsString()], ]; diff --git a/app/Models/Firestore.php b/app/Models/Firestore.php index f74659f..ae39b7f 100644 --- a/app/Models/Firestore.php +++ b/app/Models/Firestore.php @@ -46,7 +46,7 @@ public function jsonSerialize(): mixed private static function applyPersistFilter(array $attributes): array { $attributes = array_filter($attributes, function ($key) { - return in_array($key, [...static::$persist, 'created_at', 'updated_at']); + return in_array($key, ['id', ...static::$persist, 'created_at', 'updated_at']); }, ARRAY_FILTER_USE_KEY); return $attributes; @@ -298,7 +298,7 @@ public function update(array $attributes): self $attributes = $this->prepareAttributes($attributes, true); $document->set($attributes, ['merge' => true]); - $this->fill($attributes); + $this->fill($document->snapshot()->data()); return $this; } @@ -308,13 +308,31 @@ public function update(array $attributes): self */ public function save(): self { - $data = get_object_vars($this); + $attributes = get_object_vars($this); if (!empty($this->id)) { - return $this->update($data); + return $this->update($attributes); } - return $this->create($data); + return $this->createSelf($attributes); + } + + /** + * @param array $attributes + * @return self + */ + private function createSelf(array $attributes): self + { + $attributes = $this->prepareAttributes($attributes); + $newDocument = $this->getFirestoreCollection()->add($attributes); + $snapshot = $newDocument->snapshot(); + + $this->fill(array_merge( + ['id' => $snapshot->id()], + $snapshot->data() + )); + + return $this; } /** diff --git a/app/Models/User.php b/app/Models/User.php index 3b0ab48..52dfe10 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -5,12 +5,14 @@ use App\Enums\Scope; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Auth\Authenticatable as AutenticatableTrait; +use Illuminate\Database\Eloquent\Factories\HasFactory; use OpenApi\Attributes as OA; #[OA\Schema()] class User extends Firestore implements Authenticatable { use AutenticatableTrait; + use HasFactory; #[OA\Property(property: 'id', type: 'string')] #[OA\Property(property: 'name', type: 'string')] diff --git a/app/Repositories/UserRepository.php b/app/Repositories/UserRepository.php index 1819f7e..149c026 100644 --- a/app/Repositories/UserRepository.php +++ b/app/Repositories/UserRepository.php @@ -44,9 +44,8 @@ public function create(array $data): User } $user->settings = $this->getDefaultSettings(); - $user->save(); - return $user; + return $user->save(); } public function createRoot(): bool @@ -81,9 +80,8 @@ public function createDiscord(array $data): User $user->scopes = [Scope::Default->value]; $user->discord = $data['discord']; $user->settings = $this->getDefaultSettings(); - $user->save(); - return $user; + return $user->save(); } public function findById(string $id): ?User @@ -131,8 +129,6 @@ public function update(User $user, array $data): User $data['password'] = bcrypt($data['password']); } - $user->update($data); - - return $user; + return $user->update($data); } } diff --git a/app/Swagger/Application.php b/app/Swagger/Application.php index 959c6a6..611e0d7 100644 --- a/app/Swagger/Application.php +++ b/app/Swagger/Application.php @@ -44,7 +44,11 @@ class Application name: 'account', description: 'account management routes' )] - public function tags() + /** + * @return null + */ + public function tags(): null { + return null; } } diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 8a83c65..bb69c5f 100755 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -2,7 +2,13 @@ namespace Database\Factories; +use App\Enums\Frequency; +use App\Enums\Period; +use App\Enums\Platform; +use App\Enums\Rawg\RawgGenre; use App\Enums\Scope; +use DateTime; +use Google\Cloud\Core\Timestamp; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Facades\Hash; @@ -23,14 +29,21 @@ class UserFactory extends Factory */ public function definition(): array { + $now = new Timestamp(new DateTime()); + return [ 'name' => fake()->name(), 'email' => fake()->unique()->safeEmail(), 'password' => static::$password ??= Hash::make('password'), 'scopes' => [Scope::Default->value], - 'discord_user_id' => null, - 'discord_username' => null, - 'discord_channel_id' => null + 'settings' => [ + 'platforms' => Platform::values(), + 'genres' => RawgGenre::values(), + 'period' => Period::Next_30_Days->value, + 'frequency' => Frequency::Monthly->value, + ], + 'created_at' => $now, + 'updated_at' => $now, ]; } } diff --git a/database/factories/UserSettingFactory.php b/database/factories/UserSettingFactory.php deleted file mode 100644 index cbd3f02..0000000 --- a/database/factories/UserSettingFactory.php +++ /dev/null @@ -1,30 +0,0 @@ - - */ -class UserSettingFactory extends Factory -{ - /** - * Define the model's default state. - * - * @return array - */ - public function definition(): array - { - return [ - 'platforms' => Platform::values(), - 'genres' => RawgGenre::values(), - 'period' => Period::Next_30_Days->value, - 'frequency' => Frequency::Monthly->value - ]; - } -} diff --git a/database/migrations/0001_01_01_000000_create_users_table.php b/database/migrations/0001_01_01_000000_create_users_table.php deleted file mode 100755 index 6db8871..0000000 --- a/database/migrations/0001_01_01_000000_create_users_table.php +++ /dev/null @@ -1,34 +0,0 @@ -id(); - $table->string('name'); - $table->string('email')->unique()->nullable(); - $table->string('password')->nullable(); - $table->string('scopes'); - $table->string('discord_user_id')->unique()->nullable(); - $table->string('discord_username')->unique()->nullable(); - $table->string('discord_channel_id')->unique()->nullable(); - $table->timestamps(); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::dropIfExists('users'); - } -}; diff --git a/database/migrations/2024_08_14_210442_create_health_tables.php b/database/migrations/2024_08_14_210442_create_health_tables.php deleted file mode 100644 index d0f0f80..0000000 --- a/database/migrations/2024_08_14_210442_create_health_tables.php +++ /dev/null @@ -1,46 +0,0 @@ -getConnectionName(); - $tableName = EloquentHealthResultStore::getHistoryItemInstance()->getTable(); - - Schema::connection($connection)->create($tableName, function (Blueprint $table) { - $table->id(); - - $table->string('check_name'); - $table->string('check_label'); - $table->string('status'); - $table->text('notification_message')->nullable(); - $table->string('short_summary')->nullable(); - $table->json('meta'); - $table->timestamp('ended_at'); - $table->uuid('batch'); - - $table->timestamps(); - }); - - Schema::connection($connection)->table($tableName, function(Blueprint $table) { - $table->index('created_at'); - $table->index('batch'); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - $tableName = EloquentHealthResultStore::getHistoryItemInstance()->getTable(); - - Schema::dropIfExists($tableName); - } -}; diff --git a/database/migrations/2024_08_18_053353_create_user_settings_table.php b/database/migrations/2024_08_18_053353_create_user_settings_table.php deleted file mode 100644 index 40f5696..0000000 --- a/database/migrations/2024_08_18_053353_create_user_settings_table.php +++ /dev/null @@ -1,34 +0,0 @@ -id(); - $table->unsignedBigInteger('user_id'); - $table->string('platforms'); - $table->string('genres'); - $table->string('period'); - $table->string('frequency'); - $table->timestamps(); - - $table->foreign('user_id')->references('id')->on('users'); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::dropIfExists('user_settings'); - } -}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 4953ee1..307fcb2 100755 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -5,7 +5,6 @@ use App\Models\User; // use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; -use Illuminate\Support\Facades\Artisan; class DatabaseSeeder extends Seeder { diff --git a/docker-compose.yml b/docker-compose.yml index 1a1f28c..427e97e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,11 +18,11 @@ services: depends_on: firestore: condition: service_healthy - #healthcheck: - # test: ["CMD", "curl", "-f", "http://localhost/api/up"] - # interval: 5s - # timeout: 10s - # retries: 5 + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost/api/up"] + interval: 5s + timeout: 10s + retries: 5 networks: - bridge diff --git a/phpunit.xml b/phpunit.xml index a26fc41..d5e032b 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -22,8 +22,7 @@ - - + diff --git a/tests/Feature/Http/Controllers/AccountControllerTest.php b/tests/Feature/Http/Controllers/AccountControllerTest.php index 7527b4b..726d3b5 100644 --- a/tests/Feature/Http/Controllers/AccountControllerTest.php +++ b/tests/Feature/Http/Controllers/AccountControllerTest.php @@ -2,15 +2,12 @@ namespace Tests\Feature\Http\Controllers; -use App\Enums\Platform; +use App\Enums\Frequency; use App\Models\User; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Tests\TestCase; class AccountControllerTest extends TestCase { - use DatabaseMigrations; - private User $user; public function setUp(): void @@ -26,9 +23,14 @@ private function getUserJsonStructure(): array 'name', 'email', 'scopes', + 'settings' => [ + 'platforms', + 'genres', + 'period', + 'frequency' + ], 'created_at', - 'updated_at', - 'settings' + 'updated_at' ]; } @@ -63,46 +65,45 @@ public function test_should_register_user() public function test_should_update_user() { - $response = $this->actingAs($this->user)->put('/api/account/update', [ + $data = [ 'name' => 'Test User Updated', - ]); + ]; + + $response = $this->actingAs($this->user)->put('/api/account/update', $data); $response->assertStatus(200); $response->assertJsonStructure( $this->getUserJsonStructure() ); - $response->assertJson([ - 'name' => 'Test User Updated' - ]); + $response->assertJson($data); $this->assertDatabaseHas('users', [ 'id' => $this->user->id, - 'name' => 'Test User Updated' + 'name' => $data['name'] ]); } - public function test_should_updated_user_settings() + public function test_should_update_settings() { - $platforms = [Platform::PC->value]; - $response = $this->actingAs($this->user)->put('/api/account/settings', [ - 'platforms' => $platforms - ]); + $data = [ + 'settings' => [ + 'frequency' => Frequency::Weekly->value + ], + ]; + + $response = $this->actingAs($this->user)->put('/api/account/update', $data); $response->assertStatus(200); $response->assertJsonStructure( $this->getUserJsonStructure() ); - $response->assertJson([ - 'settings' => [ - 'platforms' => $platforms - ] - ]); + $response->assertJson($data); - $this->assertDatabaseHas('user_settings', [ - 'user_id' => $this->user->id, - 'platforms' => json_encode([Platform::PC->value]) + $this->assertDatabaseHas('users', [ + 'id' => $this->user->id, + 'settings.frequency' => $data['settings']['frequency'] ]); } } diff --git a/tests/Feature/Http/Controllers/AuthControllerTest.php b/tests/Feature/Http/Controllers/AuthControllerTest.php index c616f2d..4b287ad 100644 --- a/tests/Feature/Http/Controllers/AuthControllerTest.php +++ b/tests/Feature/Http/Controllers/AuthControllerTest.php @@ -2,14 +2,11 @@ namespace Tests\Feature\Http\Controllers; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Support\Facades\Config; use Tests\TestCase; class AuthControllerTest extends TestCase { - use DatabaseMigrations; - public function setUp(): void { parent::setUp(); diff --git a/tests/Feature/Http/Controllers/RawgDomainControllerTest.php b/tests/Feature/Http/Controllers/RawgDomainControllerTest.php index e1e2665..f0d7da4 100644 --- a/tests/Feature/Http/Controllers/RawgDomainControllerTest.php +++ b/tests/Feature/Http/Controllers/RawgDomainControllerTest.php @@ -4,13 +4,10 @@ use App\Models\User; use GuzzleHttp\Client; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Tests\TestCase; class RawgDomainControllerTest extends TestCase { - use DatabaseMigrations; - private User $user; public function setUp(): void diff --git a/tests/Feature/Http/Controllers/RawgGamesControllerTest.php b/tests/Feature/Http/Controllers/RawgGamesControllerTest.php index a953db0..f5ff176 100644 --- a/tests/Feature/Http/Controllers/RawgGamesControllerTest.php +++ b/tests/Feature/Http/Controllers/RawgGamesControllerTest.php @@ -4,13 +4,10 @@ use App\Models\User; use GuzzleHttp\Client; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Tests\TestCase; class RawgGamesControllerTest extends TestCase { - use DatabaseMigrations; - private User $user; public function setUp(): void diff --git a/tests/TestCase.php b/tests/TestCase.php index bbf14d9..7fa40e8 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -7,25 +7,40 @@ use App\Models\User; use Faker\Factory; use Faker\Generator; -use Illuminate\Foundation\Testing\TestCase as BaseTestCase; +use Illuminate\Foundation\Testing\TestCase as LaravelTestCase; use Illuminate\Support\Collection; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Stream; +use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Redis; use Mockery; +use Override; +use Tests\Utils\FirestoreTestUtils; -abstract class TestCase extends BaseTestCase +abstract class TestCase extends LaravelTestCase { + protected FirestoreTestUtils $firestore; protected Generator $faker; protected function setUp(): void { parent::setUp(); + $this->firestore = $this->app->make(FirestoreTestUtils::class); $this->faker = Factory::create(); } + protected function tearDown(): void + { + parent::tearDown(); + $this->firestore->clearData(); + } + + /** + * @param string|null $password + * @return User|Authenticatable + */ protected function createUser(?string $password = null) { $data = !empty($password) @@ -33,16 +48,22 @@ protected function createUser(?string $password = null) : []; return User::factory() - ->hasSettings(1) - ->create($data) - ->load('settings'); + ->make($data) + ->save(); } + /** + * @return User||Authenticatable + */ protected function createRootUser() { - return User::factory()->hasSettings(1)->create([ + $data = [ 'scopes' => Scope::values() - ])->load('settings'); + ]; + + return User::factory() + ->make($data) + ->save(); } protected function createGameCollection(int $total): Collection @@ -92,4 +113,41 @@ protected function createClientMock(string $file) return $client; } + + #[Override] + public function actingAs(Authenticatable $user, $guard = null) + { + if ($this->firestore->findById('users', $user->id)) { + $this->be($user, $guard); + } else { + throw new \Exception('User not found in Firestore'); + } + + return $this; + } + + #[Override] + protected function assertDatabaseHas($collection, array $data = [], $connection = null) + { + $found = $this->firestore->findByConditions($collection, $data); + + $this->assertTrue( + $found, + "Found no record in the [{$collection}] " . + "collection matching the attributes." + ); + } + + #[Override] + protected function assertDatabaseCount($collection, int $count, $connection = null) + { + $countInDb = $this->firestore->countRows($collection); + + $this->assertEquals( + $count, + $countInDb, + "Failed asserting that Firestore has [{$count}] " . + "records in the [{$collection}] collection." + ); + } } diff --git a/tests/Unit/Console/Commands/CreateRootTest.php b/tests/Unit/Console/Commands/CreateRootTest.php index 55c2498..c923147 100644 --- a/tests/Unit/Console/Commands/CreateRootTest.php +++ b/tests/Unit/Console/Commands/CreateRootTest.php @@ -2,15 +2,11 @@ namespace Tests\Unit\Console\Commands; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Support\Facades\Artisan; -use PHPUnit\Framework\Attributes\Depends; use Tests\TestCase; class CreateRootTest extends TestCase { - use DatabaseMigrations; - public function test_should_create_root() { Artisan::call('app:create-root'); diff --git a/tests/Unit/Guards/JwtGuardTest.php b/tests/Unit/Guards/JwtGuardTest.php index 543691b..f1a3da1 100644 --- a/tests/Unit/Guards/JwtGuardTest.php +++ b/tests/Unit/Guards/JwtGuardTest.php @@ -3,17 +3,13 @@ namespace Tests\Unit\Guards; use App\Guards\JwtGuard; -use App\Models\User; use App\Services\AuthService; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Support\Facades\Auth; use Mockery; use Tests\TestCase; class JwtGuardTest extends TestCase { - use DatabaseMigrations; - private JwtGuard $guard; public function setUp(): void @@ -98,8 +94,8 @@ public function test_user_should_return_user() $this->guard->setUser($user); $this->assertEquals( - $user->toArray(), - $this->guard->user()->toArray() + $user->jsonSerialize(), + $this->guard->user()->jsonSerialize() ); } @@ -123,8 +119,8 @@ public function test_user_should_return_user_using_jwt() ); $this->assertEquals( - $user->toArray(), - $guard->user()->toArray() + $user->jsonSerialize(), + $guard->user()->jsonSerialize() ); } diff --git a/tests/Unit/Models/PaginationTest.php b/tests/Unit/Models/PaginationTest.php index b6ebd68..237249a 100644 --- a/tests/Unit/Models/PaginationTest.php +++ b/tests/Unit/Models/PaginationTest.php @@ -29,7 +29,7 @@ public function test_should_get_contents( } $Pagination = new Pagination($games, $pageSize, $currentPage, $total); - $contents = $Pagination->getContents(); + $contents = $Pagination->jsonSerialize(); $this->assertEquals($total, $contents['total']); $this->assertEquals($pageSize, $contents['page_size']); diff --git a/tests/Unit/Models/UserSettingTest.php b/tests/Unit/Models/UserSettingTest.php deleted file mode 100644 index fdd40fe..0000000 --- a/tests/Unit/Models/UserSettingTest.php +++ /dev/null @@ -1,21 +0,0 @@ -createUser(); - $settings = UserSetting::first(); - - $this->assertInstanceOf(User::class, $settings->user); - } -} diff --git a/tests/Unit/Models/UserTest.php b/tests/Unit/Models/UserTest.php index eda5f9d..6df5b6c 100644 --- a/tests/Unit/Models/UserTest.php +++ b/tests/Unit/Models/UserTest.php @@ -4,20 +4,17 @@ use App\Enums\Scope; use App\Models\User; -use Illuminate\Foundation\Testing\DatabaseMigrations; use PHPUnit\Framework\Attributes\DataProvider; use Tests\TestCase; class UserTest extends TestCase { - use DatabaseMigrations; - #[DataProvider('provider_scopes')] public function test_should_return_is_root(array $scopes, bool $expected) { - $user = User::factory()->create([ + $user = User::factory()->make([ 'scopes' => $scopes - ]); + ])->save(); $this->assertEquals($expected, $user->isRoot()); } diff --git a/tests/Unit/Repositories/UserRepositoryTest.php b/tests/Unit/Repositories/UserRepositoryTest.php index 5fac23c..bd967b0 100644 --- a/tests/Unit/Repositories/UserRepositoryTest.php +++ b/tests/Unit/Repositories/UserRepositoryTest.php @@ -9,13 +9,10 @@ use App\Enums\Scope; use App\Models\User; use App\Repositories\UserRepository; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Tests\TestCase; class UserRepositoryTest extends TestCase { - use DatabaseMigrations; - private UserRepository $repository; public function setUp(): void @@ -29,24 +26,20 @@ public function test_should_create_user(): void $user = $this->repository->create([ 'name' => 'Test User', 'email' => 'test@example.com', - 'password' => 'password', + 'password' => $this->faker->password(), ]); $this->assertInstanceOf(User::class, $user); $this->assertDatabaseHas('users', [ - 'id' => $user->id, - 'name' => $user->name, - 'email' => $user->email, - 'scopes' => json_encode([Scope::Default->value]) - ]); - - $this->assertDatabaseHas('user_settings', [ - 'user_id' => $user->id, - 'platforms' => json_encode(Platform::values()), - 'genres' => json_encode(RawgGenre::values()), - 'period' => Period::Next_30_Days->value, - 'frequency' => Frequency::Monthly->value + 'id' => $user->id, + 'name' => $user->name, + 'email' => $user->email, + 'scopes' => [Scope::Default->value], + 'settings.platforms' => Platform::values(), + 'settings.genres' => RawgGenre::values(), + 'settings.period' => Period::Next_30_Days->value, + 'settings.frequency' => Frequency::Monthly->value ]); } @@ -82,13 +75,15 @@ public function test_should_update_user_settings() $user = $this->createUser(); $platforms = [Platform::PC->value]; - $result = $this->repository->updateSettings($user, [ - 'platforms' => $platforms + $result = $this->repository->update($user, [ + 'settings' => [ + 'platforms' => $platforms + ] ]); - $this->assertDatabaseHas('user_settings', [ - 'user_id' => $user->id, - 'platforms' => json_encode($platforms) + $this->assertDatabaseHas('users', [ + 'id' => $user->id, + 'settings.platforms' => $platforms ]); $this->assertInstanceOf(User::class, $result); diff --git a/tests/Unit/Services/AuthServiceTest.php b/tests/Unit/Services/AuthServiceTest.php index c4d0081..2ba1f10 100644 --- a/tests/Unit/Services/AuthServiceTest.php +++ b/tests/Unit/Services/AuthServiceTest.php @@ -9,7 +9,6 @@ use Firebase\JWT\JWT; use Firebase\JWT\SignatureInvalidException; use Illuminate\Auth\AuthenticationException; -use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Config; use PHPUnit\Framework\Attributes\DataProvider; @@ -18,8 +17,6 @@ class AuthServiceTest extends TestCase { - use DatabaseMigrations; - private AuthService $service; public function setUp(): void diff --git a/tests/Unit/Services/Rawg/RawgGamesServiceTest.php b/tests/Unit/Services/Rawg/RawgGamesServiceTest.php index 7f7b9c5..2088d7a 100644 --- a/tests/Unit/Services/Rawg/RawgGamesServiceTest.php +++ b/tests/Unit/Services/Rawg/RawgGamesServiceTest.php @@ -9,7 +9,6 @@ use App\Models\Pagination; use App\Services\Rawg\RawgFilterService; use App\Services\Rawg\RawgGamesService; -use Illuminate\Support\Facades\Config; use Mockery; use Tests\TestCase; @@ -60,7 +59,7 @@ public function test_should_get_recommendations() $result = $service->getRecommendations($genre, $filters); $this->assertInstanceOf(Pagination::class, $result); - $this->assertInstanceOf(Game::class, $result->getContents()['data']->first()); + $this->assertInstanceOf(Game::class, $result->jsonSerialize()['data']->first()); } public function test_should_get_upcoming_releases() @@ -88,6 +87,6 @@ public function test_should_get_upcoming_releases() $result = $service->getUpcomingReleases($period, $filters); $this->assertInstanceOf(Pagination::class, $result); - $this->assertInstanceOf(Game::class, $result->getContents()['data']->first()); + $this->assertInstanceOf(Game::class, $result->jsonSerialize()['data']->first()); } } diff --git a/tests/Unit/Swagger/ApplicationTest.php b/tests/Unit/Swagger/ApplicationTest.php index a0c3b9b..7c535a4 100644 --- a/tests/Unit/Swagger/ApplicationTest.php +++ b/tests/Unit/Swagger/ApplicationTest.php @@ -12,7 +12,5 @@ public function test_should_create_instance() $app = new Application(); $this->assertInstanceOf(Application::class, $app); $this->assertNull($app->tags()); - $this->assertNull($app->up()); - $this->assertNull($app->apiUp()); } } diff --git a/tests/Utils/FirestoreTestUtils.php b/tests/Utils/FirestoreTestUtils.php new file mode 100644 index 0000000..e8347bb --- /dev/null +++ b/tests/Utils/FirestoreTestUtils.php @@ -0,0 +1,63 @@ +firestore = app()->make(FirestoreClient::class); + } + + public function clearData(): void + { + $collections = ['users']; + + foreach ($collections as $collection) { + $documents = $this->firestore->collection($collection)->documents(); + foreach ($documents as $document) { + $document->reference()->delete(); + } + } + } + + public function findById(string $collection, string $id): bool + { + return $this->firestore->collection($collection) + ->document($id) + ->snapshot() + ->exists(); + } + + public function findByConditions($collection, array $conditions = []): bool + { + $query = $this->firestore->collection($collection); + + foreach ($conditions as $field => $value) { + if ($field == 'id') { + $field = '__name__'; + } + + $query = $query->where($field, '=', $value); + } + + $documents = $query->limit(1)->documents(); + + return !$documents->isEmpty(); + } + + public function countRows($collection, array $conditions = []): int + { + $query = $this->firestore->collection($collection); + + foreach ($conditions as $field => $value) { + $query = $query->where($field, '=', $value); + } + + return $query->documents()->size(); + } +}