Skip to content

Commit

Permalink
feat: add more information for user auth route
Browse files Browse the repository at this point in the history
  • Loading branch information
otaaaviio committed Jun 21, 2024
1 parent aa5e2f2 commit a398e11
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
24 changes: 24 additions & 0 deletions app/Http/Resources/UserDetailedResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Resources;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
* @mixin User
*/
class UserDetailedResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'guilds_count' => $this->guilds->count(),
'messages_count' => $this->messages->count(),
];
}
}
5 changes: 3 additions & 2 deletions app/Http/Services/AuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Exceptions\AuthException;
use App\Http\Resources\AuthResource;
use App\Http\Resources\UserDetailedResource;
use App\interfaces\Services\IAuthService;
use App\Jobs\SendWelcomeMail;
use App\Models\User;
Expand Down Expand Up @@ -41,8 +42,8 @@ public function register(array $data): array
];
}

public function user(): AuthResource
public function user(): UserDetailedResource
{
return AuthResource::make(Auth::user());
return UserDetailedResource::make(Auth::user());
}
}
3 changes: 2 additions & 1 deletion app/interfaces/Services/IAuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\interfaces\Services;

use App\Http\Resources\AuthResource;
use App\Http\Resources\UserDetailedResource;

interface IAuthService
{
Expand All @@ -12,5 +13,5 @@ public function logout(): void;

public function register(array $data): array;

public function user(): AuthResource;
public function user(): UserDetailedResource;
}
7 changes: 4 additions & 3 deletions tests/Unit/Controllers/AuthControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Exceptions\AuthException;
use App\Http\Resources\AuthResource;
use App\Http\Resources\UserDetailedResource;
use App\interfaces\Services\IAuthService;
use App\Models\User;
use Illuminate\Foundation\Testing\TestCase;
Expand Down Expand Up @@ -60,10 +61,10 @@
test('test get user authenticated', function () {
$user = User::factory()->make();

$authResource = new AuthResource($user);
$userResource = new UserDetailedResource($user);

$this->mock(IAuthService::class, function (MockInterface $mock) use ($authResource) {
$mock->shouldReceive('user')->once()->andReturn($authResource);
$this->mock(IAuthService::class, function (MockInterface $mock) use ($userResource) {
$mock->shouldReceive('user')->once()->andReturn($userResource);
});

$res = $this->actingAs($user)->getJson('api/auth/user');
Expand Down

0 comments on commit a398e11

Please sign in to comment.