Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
norbybaru committed Jun 16, 2024
1 parent e5a5f6a commit fd95524
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 25 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Run Unit Tests

on:
pull_request:
branches:
- main
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php: [8.0, 8.1, 8.2, 8.3]
laravel: [10.*, 11.*]
dependency-version: [prefer-lowest, prefer-stable]
exclude:
- laravel: 10.*
php: 8.0
- laravel: 11.*
php: 8.0
- laravel: 11.*
php: 8.1

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}

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

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{matrix.php}}

- name: Get Composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache Composer dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
# Use composer.json for key, if composer.lock is not committed.
key: php-${{ matrix.php }}-lara-${{ matrix.laravel }}-composer-${{ matrix.dependency-version }}-${{ hashFiles('**/composer.json') }}
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: php-${{ matrix.php }}-lara-${{ matrix.laravel }}-composer-${{ matrix.dependency-version }}-

- name: Install Composer dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
- name: Run PHPUnit tests
run: vendor/bin/phpunit --testdox
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build/
vendor/
composer.lock
composer.lock
*.cache
19 changes: 12 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
"description": "Generate modular structure files for laravel",
"homepage": "https://github.com/norbybaru/modularize",
"keywords": ["laravel", "modular", "modules", "module", "structure", "modular", "laravel-modular", "modularize"],
"require": {
"php": "^8.1",
"illuminate/console": "^10.13",
"illuminate/support": "^8.0|^9.52|^10.0"
},
"license": "MIT",
"version": "2.0",
"authors": [
Expand All @@ -21,6 +16,11 @@
"NorbyBaru\\Modularize\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"NorbyBaru\\Modularize\\Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
Expand All @@ -41,10 +41,15 @@
"config": {
"sort-packages": true
},
"require": {
"php": "^8.1",
"illuminate/console": "^10.13|^11.0",
"illuminate/support": "^10.13|^11.0"
},
"require-dev": {
"laravel/framework": "^10.13",
"laravel/pint": "^1.10",
"nunomaduro/larastan": "^2.0",
"orchestra/testbench": "^8.5"
"orchestra/testbench": "^8.5|^9.0",
"phpunit/phpunit": "^10.13|^11.0"
}
}
23 changes: 23 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing" force="true"/>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
<env name="DB_CONNECTION" value="testing"/>
</php>
</phpunit>
1 change: 1 addition & 0 deletions src/Console/Commands/ModuleMakeModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace NorbyBaru\Modularize\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;

Expand Down
17 changes: 0 additions & 17 deletions src/ModularizeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public function boot()
$this->publishConfig();

$moduleRootPath = base_path(config('modularize.root_path'));
//$moduleRootPath = app_path('/Modules');

//dd($moduleRootPath, app_path());
if (is_dir($moduleRootPath)) {

if (! config('modularize.enable')) {
Expand All @@ -52,21 +50,6 @@ public function boot()
$this->files->directories($moduleRootPath)
);

// foreach ($modules as $key => $module) {
// if (! $this->files->exists("{$moduleRootPath}/{$module}/Controllers")) {
// unset($modules[$key]);

// $directories = array_map(
// 'class_basename',
// $this->files->directories($moduleRootPath.'/'.$module)
// );

// foreach ($directories as $directory) {
// array_push($modules, $module.'/'.$directory);
// }
// }
// }

foreach ($modules as $module) {
$this->autoloadServiceProvider($moduleRootPath, $module);
$this->autoloadConfig($moduleRootPath, $module);
Expand Down
91 changes: 91 additions & 0 deletions tests/Commands/MakeModelCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace NorbyBaru\Modularize\Tests\Commands;

use NorbyBaru\Modularize\Tests\MakeCommandTestCase;

class MakeModelCommandTest extends MakeCommandTestCase
{
public function test_it_creates_a_model()
{
$this->artisan(
command: 'module:make:model',
parameters: [
'name' => 'Post',
'--module' => $this->moduleName,
]
)
->assertSuccessful();

$this->assertFileExists(filename: $this->getModulePath().'/Models/Post.php');
}

public function test_it_creates_a_model_with_migration()
{
$this->artisan(
command: 'module:make:model',
parameters: [
'name' => 'Post',
'--module' => $this->moduleName,
'--migration' => true,
]
)
->assertExitCode(exitCode: 0);

$this->assertFileExists(filename: $this->getModulePath().'/Models/Post.php');
$this->assertFileExists(filename: $this->getModulePath().'/Database/migration/'.date('Y_m_d_His').'_create_posts_table.php');
}

public function test_it_creates_a_model_with_factory()
{
$this->artisan(
command: 'module:make:model',
parameters: [
'name' => 'Post',
'--module' => $this->moduleName,
//'--factory' => true
]
)
->assertExitCode(exitCode: 0);

$this->assertFileExists(filename: $this->getModulePath().'/Models/Post.php');
//$this->assertFileExists($this->getModulePath($this->moduleName).'/Database/Factories/PostFactory.php');
}

public function test_it_creates_a_model_with_migration_and_factory()
{
$this->artisan(
command: 'module:make:model',
parameters: [
'name' => 'Post',
'--module' => $this->moduleName,
'--migration' => true,
//'--factory' => true
]
)
->assertExitCode(exitCode: 0);

$this->assertFileExists(
filename: $this->getModulePath().'/Models/Post.php'
);
$this->assertFileExists(
filename: $this->getModulePath().'/Database/migration/'.date('Y_m_d_His').'_create_posts_table.php'
);
//$this->assertFileExists($this->getModulePath($this->moduleName).'/Database/factories/PostFactory.php');
}

public function test_it_creates_a_model_with_migration_and_factory_and_test()
{
$this->artisan(
command: 'module:make:model',
parameters: [
'name' => 'Post',
'--module' => $this->moduleName,
'--migration' => true,
'--factory' => true,
//'--test' => true
])
->assertExitCode(exitCode: 0);

}
}
26 changes: 26 additions & 0 deletions tests/MakeCommandTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace NorbyBaru\Modularize\Tests;

abstract class MakeCommandTestCase extends TestCase
{
public string $moduleName = 'Blog';

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

public function getModulePath(?string $module = null): string
{
$module = $module ?? $this->moduleName;

return parent::getModulePath($module);
}

public function cleanUp(): void
{
$this->app['files']->deleteDirectory($this->getModulePath(module: $this->moduleName));
}
}
21 changes: 21 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace NorbyBaru\Modularize\Tests;

use NorbyBaru\Modularize\ModularizeServiceProvider;
use Orchestra\Testbench\TestCase as OrchestraTestCase;

abstract class TestCase extends OrchestraTestCase
{
public function getModulePath(string $module): string
{
return base_path(config('modularize.root_path')."/$module");
}

protected function getPackageProviders($app)
{
return [
ModularizeServiceProvider::class,
];
}
}

0 comments on commit fd95524

Please sign in to comment.