Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danieltrolezi committed Oct 15, 2024
1 parent d728503 commit db51da5
Show file tree
Hide file tree
Showing 29 changed files with 237 additions and 280 deletions.
2 changes: 1 addition & 1 deletion app/Http/Requests/RegisterAccountRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];
}
}
4 changes: 2 additions & 2 deletions app/Http/Requests/UpdateAccountRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
];
Expand Down
28 changes: 23 additions & 5 deletions app/Models/Firestore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
Expand Down
10 changes: 3 additions & 7 deletions app/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
6 changes: 5 additions & 1 deletion app/Swagger/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ class Application
name: 'account',
description: 'account management routes'
)]
public function tags()
/**
* @return null
*/
public function tags(): null
{
return null;
}
}
19 changes: 16 additions & 3 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
];
}
}
30 changes: 0 additions & 30 deletions database/factories/UserSettingFactory.php

This file was deleted.

34 changes: 0 additions & 34 deletions database/migrations/0001_01_01_000000_create_users_table.php

This file was deleted.

46 changes: 0 additions & 46 deletions database/migrations/2024_08_14_210442_create_health_tables.php

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
10 changes: 5 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_STORE" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="FIRESTORE_DATABASE" value="test"/>
<env name="MAIL_MAILER" value="array"/>
<env name="PULSE_ENABLED" value="false"/>
<env name="QUEUE_CONNECTION" value="sync"/>
Expand Down
Loading

0 comments on commit db51da5

Please sign in to comment.