Skip to content

Commit

Permalink
Add type coverage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WendellAdriel committed Apr 26, 2024
1 parent 6112e17 commit c6be37f
Show file tree
Hide file tree
Showing 17 changed files with 22 additions and 18 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
* API Documentation with Swagger
* Laravel Pint configuration (very opinionated)
* Pest v2 for Tests
* Type Coverage Tests
* Base classes to speed up the development
* DTOs with [Laravel Validated DTO](https://github.com/WendellAdriel/laravel-validated-dto)
* Slack Client for notifications
* API structured in modules
* Laravel Sanctum for Authentication
* JWT for Authentication
* Users management out-of-the-box with simple roles system
* Logs on DB for user logins and for actions made on models
* [Strictus](https://github.com/php-strictus/strictus) for enforcing local variable types
Expand Down
4 changes: 2 additions & 2 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function register(): void
});
}

public function render($request, Throwable $exception)
public function render($request, Throwable $exception) // @pest-ignore-type
{
if ($exception instanceof \Illuminate\Auth\AuthenticationException) {
return $this->error($exception, Response::HTTP_UNAUTHORIZED, 'Unauthenticated');
Expand All @@ -81,7 +81,7 @@ public function render($request, Throwable $exception)
return $this->error($exception, Response::HTTP_INTERNAL_SERVER_ERROR);
}

protected function unauthenticated($request, \Illuminate\Auth\AuthenticationException $exception)
protected function unauthenticated($request, \Illuminate\Auth\AuthenticationException $exception) // @pest-ignore-type
{
return $this->error($exception, Response::HTTP_UNAUTHORIZED, 'Unauthenticated');
}
Expand Down
1 change: 0 additions & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class Kernel extends HttpKernel
],

'api' => [
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class . ':api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Exa\Http\Middlewares\BlockViewerUsers::class,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Middleware/ValidateSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ValidateSignature extends Middleware
*
* @var array<int, string>
*/
protected $except = [
protected array $except = [
// 'fbclid',
// 'utm_campaign',
// 'utm_content',
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@
"test": [
"pest"
],
"test:types": [
"pest --type-coverage --min=100 app/ exa/ modules/"
],
"swagger": [
"sh ./tools/swagger.sh"
],
"prepare": [
"pint --dirty",
"@test:types",
"@swagger"
],
"post-autoload-dump": [
Expand Down
2 changes: 1 addition & 1 deletion config/cors.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
|
*/

'paths' => ['api/*', 'sanctum/csrf-cookie'],
'paths' => ['*'],

'allowed_methods' => ['*'],

Expand Down
2 changes: 1 addition & 1 deletion exa/Http/Middlewares/BlockViewerUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

final class BlockViewerUsers
{
public function handle(Request $request, Closure $next)
public function handle(Request $request, Closure $next): Closure
{
$user = Auth::user();
if (is_null($user)) {
Expand Down
2 changes: 1 addition & 1 deletion exa/Http/Middlewares/HasRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(private HasRoleAction $action)
/**
* @throws AccessDeniedException
*/
public function handle(Request $request, Closure $next, string $role)
public function handle(Request $request, Closure $next, string $role): Closure
{
$user = Auth::user();
if (is_null($user)) {
Expand Down
2 changes: 1 addition & 1 deletion exa/Http/Responses/ApiErrorResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(
) {
}

public function toResponse($request): JsonResponse
public function toResponse($request): JsonResponse // @pest-ignore-type
{
$response = ['message' => $this->message];

Expand Down
2 changes: 1 addition & 1 deletion exa/Http/Responses/ApiSuccessResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
) {
}

public function toResponse($request): JsonResponse
public function toResponse($request): JsonResponse // @pest-ignore-type
{
return response()->json(
$this->resource->toArray($request),
Expand Down
2 changes: 1 addition & 1 deletion exa/Http/Responses/NoContentResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(
) {
}

public function toResponse($request): Response
public function toResponse($request): Response // @pest-ignore-type
{
return response()->noContent($this->code, $this->headers);
}
Expand Down
2 changes: 1 addition & 1 deletion exa/Services/SlackClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private function formatUsersToMention(array $users): string
return '';
}

$formatted = array_map(fn ($user) => "<@{$user}>", $users);
$formatted = array_map(fn (string $user) => "<@{$user}>", $users);

return implode(', ', $formatted);
}
Expand Down
2 changes: 1 addition & 1 deletion modules/Auth/Actions/UpdateUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function handle(string $uuid, UpdateUserDTO $dto): User
{
$authUser = Auth::user();
$user = $this->fetchUser->handle($uuid);
$updateData = collect($dto->toArray())->filter(fn ($item) => ! is_null($item))->toArray();
$updateData = collect($dto->toArray())->filter(fn (string|bool|null $item) => ! is_null($item))->toArray();

if (! $authUser->is_admin) {
unset($updateData['role'], $updateData['active']);
Expand Down
4 changes: 1 addition & 3 deletions modules/Auth/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Modules\Auth\Support\Role;
use Modules\Auth\Traits\HasRole;

class User extends Authenticatable
{
use CommonQueries,
HasApiTokens,
HasFactory,
HasRole,
HasUuidField,
Expand Down Expand Up @@ -49,7 +47,7 @@ class User extends Authenticatable
'role' => Role::REGULAR->value,
];

protected static function booted()
protected static function booted(): void
{
static::addGlobalScope('active-users', fn (Builder $builder) => $builder->where('active', true));
}
Expand Down
2 changes: 1 addition & 1 deletion modules/Auth/Routes/v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Route::post('auth/login', [AuthController::class, 'login']);

// Protected Routes
Route::middleware('auth:sanctum')->group(function () {
Route::middleware('auth')->group(function () {
Route::prefix('auth')->group(function () {
Route::post('logout', [AuthController::class, 'logout']);
Route::get('me', [AuthController::class, 'user']);
Expand Down
2 changes: 1 addition & 1 deletion modules/Common/Routes/v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
// Public Routes

// Protected Routes
Route::middleware('auth:sanctum')->group(function () {
Route::middleware('auth')->group(function () {
//
});
2 changes: 2 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<source>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./exa</directory>
<directory suffix=".php">./modules</directory>
</include>
</source>
</phpunit>

0 comments on commit c6be37f

Please sign in to comment.