Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/static_analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Static Analysis

on:
pull_request:
types: [opened, synchronize]
paths:
- "src/**/*.php"
workflow_dispatch: {}

jobs:
mago:
runs-on: ubuntu-latest
timeout-minutes: 5

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.2"
coverage: none
tools: composer

- name: Cache Composer dependencies
uses: actions/cache@v4
with:
path: ~/.cache/composer
key: ${{ runner.os }}-composer-${{ hashFiles('src/composer.lock') }}

- name: Install dependencies
run: composer install --prefer-dist --no-progress
working-directory: ./src

- name: Setup Mago
uses: nhedger/setup-mago@v1

- name: Run Mago lint
run: ./vendor/bin/mago lint
working-directory: ./src
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.phpunit.cache
/.composer
/node_modules
/public/build
/public/hot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use App\Packages\Features\QueryUseCases\Dto\User\AuthTokenDto;
use InvalidArgumentException;

final class LoginUseCase
class LoginUseCase
{
public function __construct(
private readonly UserRepositroyInterface $userRepository,
Expand Down
49 changes: 49 additions & 0 deletions src/app/Packages/Features/Controller/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Packages\Features\Controller;

use App\Http\Controllers\Controller;
use App\Packages\Features\CommandUseCases\UseCase\User\LoginUseCase;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use InvalidArgumentException;
use Throwable;

class AuthController extends Controller
{
public function login(
Request $request,
LoginUseCase $useCase,
): JsonResponse {
try {
$dto = $useCase->handle(
email: $request->input('email'),
password: $request->input('password'),
);

return response()->json([
'status' => 'success',
'data' => [
'token' => $dto->token,
'token_type' => $dto->tokenType,
],
], 200);
} catch (InvalidArgumentException $exception) {
return response()->json([
'status' => 'error',
'message' => $exception->getMessage(),
], 401);
} catch (Throwable $throw) {
Log::error('Failed to login', [
'message' => $throw->getMessage(),
'trace' => $throw->getTraceAsString(),
]);

return response()->json([
'status' => 'error',
'message' => 'Internal Server Error',
], 500);
}
}
}
109 changes: 109 additions & 0 deletions src/app/Packages/Features/Tests/LoginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace App\Packages\Features\Tests;

use App\Models\User;
use App\Packages\Features\CommandUseCases\UseCase\User\LoginUseCase;
use Illuminate\Support\Facades\DB;
use RuntimeException;
use Tests\TestCase;

class LoginTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->truncate();
}

protected function tearDown(): void
{
$this->truncate();
parent::tearDown();
}

private function truncate(): void
{
if (env('APP_ENV') === 'testing') {
DB::connection('mysql')->statement('SET FOREIGN_KEY_CHECKS=0;');
DB::connection('mysql')->table('personal_access_tokens')->truncate();
User::truncate();
DB::connection('mysql')->statement('SET FOREIGN_KEY_CHECKS=1;');
}
}

private function seedUser(string $email, string $password): User
{
return User::create([
'first_name' => 'John',
'last_name' => 'Doe',
'email' => $email,
'password' => bcrypt($password),
'age_range' => '20s',
'subscription_tier' => 'free',
'subscription_expires_at' => null,
]);
}

public function test_login_returns_200_with_token_on_valid_credentials(): void
{
$this->seedUser('john@example.com', 'secret123');

$response = $this->postJson('/api/v1/user/login', [
'email' => 'john@example.com',
'password' => 'secret123',
]);

$response->assertStatus(200)
->assertJsonStructure([
'status',
'data' => ['token', 'token_type'],
])
->assertJsonFragment([
'status' => 'success',
'token_type' => 'Bearer',
]);
}

public function test_login_returns_401_when_user_not_found(): void
{
$response = $this->postJson('/api/v1/user/login', [
'email' => 'notfound@example.com',
'password' => 'secret123',
]);

$response->assertStatus(401)
->assertJsonFragment(['status' => 'error']);
}

public function test_login_returns_401_on_wrong_password(): void
{
$this->seedUser('john@example.com', 'secret123');

$response = $this->postJson('/api/v1/user/login', [
'email' => 'john@example.com',
'password' => 'wrong-password',
]);

$response->assertStatus(401)
->assertJsonFragment(['status' => 'error']);
}

public function test_login_returns_500_on_unexpected_error(): void
{
$this->mock(LoginUseCase::class)
->shouldReceive('handle')
->andThrow(new RuntimeException('Unexpected error'));

$response = $this->postJson('/api/v1/user/login', [
'email' => 'john@example.com',
'password' => 'secret123',
]);

$response->assertStatus(500)
->assertJsonFragment([
'status' => 'error',
'message' => 'Internal Server Error',
]);
}
}
Loading
Loading