Skip to content

Commit

Permalink
Merge pull request #1 from ColoredCow/feature/ci-workflow
Browse files Browse the repository at this point in the history
Add Laravel CI workflow
  • Loading branch information
nishant22029 authored Mar 1, 2025
2 parents 75e5458 + d6f3caa commit 609d471
Show file tree
Hide file tree
Showing 9 changed files with 982 additions and 93 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Laravel CI Test

on: [push, pull_request]

jobs:
laravel-tests:
runs-on: ubuntu-latest

services:
mysql:
image: mysql:8
env:
MYSQL_DATABASE: testing_db
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping --host=localhost --user=root --password=root"
--health-interval=10s
--health-timeout=5s
--health-retries=3
steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
extensions: mbstring, bcmath, pdo, pdo_mysql
coverage: none

- name: Install Composer Dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader

- name: Setup Laravel Environment
run: |
cp .env.example .env
php artisan key:generate
- name: Run Migrations
env:
DB_CONNECTION: mysql
DB_DATABASE: testing_db
DB_USERNAME: user
DB_PASSWORD: password
DB_HOST: 127.0.0.1
run: php artisan migrate --force

# Check PHP code formatting with PHPFMT
- name: Run PHPFMT
run: ./vendor/bin/phpmd app,routes text codesize,controversial,design,naming,unusedcode

# Static analysis using Larastan
- name: Run Larastan
run: ./vendor/bin/phpstan analyse app routes --memory-limit=2G

# Run PHPUnit Tests
- name: Run PHPUnit Tests
env:
DB_CONNECTION: mysql
DB_DATABASE: testing_db
DB_USERNAME: user
DB_PASSWORD: password
DB_HOST: 127.0.0.1
run: |
php artisan config:clear
php artisan cache:clear
php artisan test --testsuite=Feature --stop-on-failure
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;

class AuthenticatedSessionController extends Controller
Expand Down
12 changes: 6 additions & 6 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public function register(): void
public function boot(): void
{
ResetPassword::createUrlUsing(function (object $notifiable, string $token) {
return config('app.frontend_url')."/password-reset/$token?email={$notifiable->getEmailForPasswordReset()}";
return config('app.frontend_url') . "/password-reset/$token?email={$notifiable->getEmailForPasswordReset()}";
});

Gate::after(function ($user, $ability) {
if($user->hasRole('admin')){
return true;
}
return false;
Gate::after(function ($user) {
if ($user->hasRole('admin')) {
return true;
}
return false;
});
}
}
20 changes: 9 additions & 11 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
api: __DIR__.'/../routes/api.php',
commands: __DIR__.'/../routes/console.php',
web: __DIR__ . '/../routes/web.php',
api: __DIR__ . '/../routes/api.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
Expand All @@ -40,50 +39,49 @@
]);
})
->withExceptions(function (Exceptions $exceptions) {
$exceptions->render(function(ModelNotFoundException $e) {
$exceptions->render(function (ModelNotFoundException $e) {
return response()->json([
'success' => false,
'message' => 'Resource not found'
], 404);
});
$exceptions->render(function(NotFoundHttpException $e) {
$exceptions->render(function (NotFoundHttpException $e) {
return response()->json([
'success' => false,
'message' => 'Resource not found'
], 404);
});
$exceptions->render(function(AuthorizationException $e) {
$exceptions->render(function (AuthorizationException $e) {
return response()->json([
'success' => false,
'message' => 'You are not authorized to perform this action'
], 403);
});

$exceptions->render(function(ValidationException $e) {
$exceptions->render(function (ValidationException $e) {
return response()->json([
'success' => false,
'message' => 'Validation failed',
'errors' => $e->errors()
], 422);
});
$exceptions->render(function(MethodNotAllowedHttpException $e) {
$exceptions->render(function (MethodNotAllowedHttpException $e) {
return response()->json([
'success' => false,
'message' => 'Method is not allowed for the requested route'
], 405);
});
$exceptions->render(function(AccessDeniedHttpException $e) {
$exceptions->render(function (AccessDeniedHttpException $e) {
return response()->json([
'success' => false,
'message' => 'You are not authorized to perform this action'
], 403);
});
$exceptions->render(function(\Throwable $e) {
$exceptions->render(function (\Throwable $e) {
return response()->json([
'success' => false,
'message' => 'An error occurred',
'error' => $e->getMessage()
], 500);
});

})->create();
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
},
"require-dev": {
"fakerphp/faker": "^1.23",
"larastan/larastan": "^3.1",
"laravel/breeze": "^2.3",
"laravel/pail": "^1.1",
"laravel/pint": "^1.13",
"laravel/sail": "^1.26",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.1",
"pestphp/pest": "^3.7",
"pestphp/pest-plugin-laravel": "^3.1"
"pestphp/pest-plugin-laravel": "^3.1",
"phpmd/phpmd": "^2.15"
},
"autoload": {
"psr-4": {
Expand Down
Loading

0 comments on commit 609d471

Please sign in to comment.