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
47 changes: 47 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Run Tests

on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: [8.1, 8.2, 8.3, 8.4]
laravel: [10.*, 11.*, 12.*, 13.*]
exclude:
# Laravel 11+ requires PHP 8.2+
- php: 8.1
laravel: 11.*
- php: 8.1
laravel: 12.*
- php: 8.1
laravel: 13.*
# Laravel 12+ requires PHP 8.2+
# Laravel 13+ requires PHP 8.2+ (or 8.3+ depending on final specs, but excluding 8.1 is safe)

name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}

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

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:^8.0 || ^9.0 || ^10.0 || ^11.0" --no-interaction --no-update
composer update --prefer-dist --no-interaction

- name: Execute tests
run: vendor/bin/phpunit
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
.phpunit.result.cache
composer.lock
19 changes: 15 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@
"php": "^8.1",
"laravel/framework": "^10.0 || ^11.0 || ^12.0 || ^13.0"
},
"require-dev": {
"phpunit/phpunit": "^10.0"
"autoload-dev": {
"psr-4": {
"Nosleepman\\ArchCLI\\Tests\\": "tests/"
}
},
"minimum-stability": "stable",
"prefer-stable": true
}
"prefer-stable": true,
"require-dev": {
"phpunit/phpunit": "^10.0",
"orchestra/testbench": "^8.0 || ^9.0 || ^10.0 || ^11.0"
},
"config": {
"audit": {
"block-insecure": false
}
}
}
11 changes: 11 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
157 changes: 157 additions & 0 deletions tests/GenerateModuleCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

namespace Nosleepman\ArchCLI\Tests;

use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\File;

class GenerateModuleCommandTest extends TestCase
{
protected function getPackageProviders($app)
{
return [
\Nosleepman\ArchCLI\ArchCLIServiceProvider::class,
];
}

protected function setUp(): void
{
parent::setUp();
$this->cleanUp();
}

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

private function cleanUp()
{
File::deleteDirectory(app_path('Models'));
File::deleteDirectory(app_path('Http'));
File::deleteDirectory(app_path('Services'));
File::deleteDirectory(app_path('Repositories'));
File::deleteDirectory(app_path('Events'));
File::deleteDirectory(app_path('Listeners'));
File::deleteDirectory(app_path('Notifications'));
File::deleteDirectory(app_path('Policies'));
File::deleteDirectory(database_path('migrations'));
File::delete(app_path('Product.php'));
File::delete(app_path('Category.php'));
}

/**
* Test full module generation with all components, including Service and Repository.
*/
public function test_it_generates_complete_module_with_service_and_repository()
{
$this->artisan('make:module', ['name' => 'Product'])
->expectsQuestion('Field (or empty to finish)', 'title:string:unique')
->expectsQuestion('Field (or empty to finish)', 'price:integer:nullable')
->expectsQuestion('Field (or empty to finish)', '')
->expectsChoice('Controller version', 'v1', ['v1', 'v2', 'v3'])
->expectsConfirmation('Include policies?', 'yes')
->expectsConfirmation('Include service layer?', 'yes')
->expectsConfirmation('Include repository layer?', 'yes')
->expectsConfirmation('Include events and listeners?', 'yes')
->assertExitCode(0);

// Assert Model and fillable fields
$modelPath = app_path('Models/Product.php');
if (!File::exists($modelPath)) {
$modelPath = app_path('Product.php');
}
$this->assertTrue(File::exists($modelPath));
$modelContent = File::get($modelPath);
$this->assertStringContainsString("protected \$fillable = ['title', 'price'];", $modelContent);

// Assert Migration files
$migrations = File::files(database_path('migrations'));
$this->assertCount(1, $migrations);
$migrationContent = File::get($migrations[0]);
$this->assertStringContainsString("\$table->string('title')->unique();", $migrationContent);
$this->assertStringContainsString("\$table->integer('price')->nullable();", $migrationContent);

// Assert Repository
$repositoryPath = app_path('Repositories/ProductRepository.php');
$this->assertTrue(File::exists($repositoryPath));
$repositoryContent = File::get($repositoryPath);
$this->assertStringContainsString('class ProductRepository', $repositoryContent);
$this->assertStringContainsString('Product::create($data)', $repositoryContent);

// Assert Service (uses Repository)
$servicePath = app_path('Services/ProductService.php');
$this->assertTrue(File::exists($servicePath));
$serviceContent = File::get($servicePath);
$this->assertStringContainsString('protected ProductRepository $repository', $serviceContent);
$this->assertStringContainsString('$this->repository->create($data)', $serviceContent);
$this->assertStringContainsString('use App\Events\ProductCreated;', $serviceContent);
$this->assertStringContainsString('event(new ProductCreated($model));', $serviceContent);

// Assert Controller
$controllerPath = app_path('Http/Controllers/Api/V1/ProductController.php');
$this->assertTrue(File::exists($controllerPath));
$controllerContent = File::get($controllerPath);
$this->assertStringContainsString('protected ProductService $service', $controllerContent);

// Assert Form Requests and dynamic validation rules
$storeRequestPath = app_path('Http/Requests/Product/StoreProductRequest.php');
$this->assertTrue(File::exists($storeRequestPath));
$storeRequestContent = File::get($storeRequestPath);
$this->assertStringContainsString("'title' => 'required|string|max:255|unique:titles'", $storeRequestContent);
$this->assertStringContainsString("'price' => 'required|integer'", $storeRequestContent);

$updateRequestPath = app_path('Http/Requests/Product/UpdateProductRequest.php');
$this->assertTrue(File::exists($updateRequestPath));
$updateRequestContent = File::get($updateRequestPath);
$this->assertStringContainsString("'title' => 'required|string|max:255|unique:titles'", $updateRequestContent);

// Assert API Resource
$resourcePath = app_path('Http/Resources/ProductResource.php');
$this->assertTrue(File::exists($resourcePath));
$resourceContent = File::get($resourcePath);
$this->assertStringContainsString("'title' => \$this->title", $resourceContent);
$this->assertStringContainsString("'price' => \$this->price", $resourceContent);

// Assert Event, Listener, Notification, Policy
$this->assertTrue(File::exists(app_path('Events/ProductCreated.php')));
$this->assertTrue(File::exists(app_path('Listeners/ProductCreatedListener.php')));
$this->assertTrue(File::exists(app_path('Notifications/ProductCreatedNotification.php')));
$this->assertTrue(File::exists(app_path('Policies/ProductPolicy.php')));
}

/**
* Test module generation without Service Layer and without Repository.
*/
public function test_it_generates_module_without_service_and_repository()
{
$this->artisan('make:module', ['name' => 'Category'])
->expectsQuestion('Field (or empty to finish)', 'name:string')
->expectsQuestion('Field (or empty to finish)', '')
->expectsChoice('Controller version', 'v2', ['v1', 'v2', 'v3'])
->expectsConfirmation('Include policies?', 'no')
->expectsConfirmation('Include service layer?', 'no')
->expectsConfirmation('Include repository layer?', 'no')
->expectsConfirmation('Include events and listeners?', 'no')
->assertExitCode(0);

// Assert Category Model exists
$modelPath = app_path('Models/Category.php');
if (!File::exists($modelPath)) {
$modelPath = app_path('Category.php');
}
$this->assertTrue(File::exists($modelPath));

// Assert Category Controller exists and uses Model directly
$controllerPath = app_path('Http/Controllers/Api/V2/CategoryController.php');
$this->assertTrue(File::exists($controllerPath));
$controllerContent = File::get($controllerPath);
$this->assertStringNotContainsString('protected CategoryService $service', $controllerContent);
$this->assertStringContainsString('Category::all()', $controllerContent);

// Assert Service & Repository do NOT exist
$this->assertFalse(File::exists(app_path('Services/CategoryService.php')));
$this->assertFalse(File::exists(app_path('Repositories/CategoryRepository.php')));
}
}
Loading