Skip to content

Commit

Permalink
make controller and test commands
Browse files Browse the repository at this point in the history
  • Loading branch information
m45adiwinata committed Sep 11, 2024
1 parent 4abae5a commit 5394d98
Show file tree
Hide file tree
Showing 15 changed files with 692 additions and 225 deletions.
10 changes: 9 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
<rule ref="PEAR">
<exclude name="PEAR.NamingConventions.ValidVariableName.PrivateNoUnderscore" />
<exclude name="PEAR.NamingConventions.ValidFunctionName.PrivateNoUnderscore" />
<exclude name="Generic.Files.LineEndings"/>
</rule>
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="200"/>
<property name="absoluteLineLimit" value="0"/>
<property name="ignoreComments" value="true"/>
</properties>
</rule>
<rule ref="Generic.PHP.RequireStrictTypes" />
<file>src/</file>
<file>tests/</file>
<exclude-pattern>tests/</exclude-pattern>
<exclude-pattern>vendor</exclude-pattern>
<exclude-pattern>resources</exclude-pattern>
<exclude-pattern>database/</exclude-pattern>
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</include>
<exclude>
<file>src/Exceptions/Handler.php</file>
<directory suffix=".php">src/Commands</directory>
<directory suffix=".php">src/Libraries</directory>
<directory suffix=".php">src/Providers</directory>
<directory suffix=".php">src/Validations</directory>
Expand Down
154 changes: 154 additions & 0 deletions src/Commands/ControllerMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

/**
* PHP version 8
*
* @category Library
* @package Middlewares
* @author Made Mas Adi Winata <m45adiwinata@gmail.com>
* @license https://mit-license.org/ MIT License
* @version GIT: 0.0.6
* @link https://github.com/spotlibs
*/

declare(strict_types=1);

namespace Spotlibs\PhpLib\Commands;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;
use Illuminate\Support\Str;

/**
* ControllerMakeCommand
*
* Standard command
*
* @category Console
* @package Commands
* @author Made Mas Adi Winata <m45adiwinata@gmail.com>
* @license https://mit-license.org/ MIT License
* @link https://github.com/spotlibs
*/
class ControllerMakeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:controller';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new controller class';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Controller';
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
parent::handle();
$this->createSwagger();
$this->createTest();
}
/**
* Create a swagger file controller.
*
* @return void
*/
protected function createSwagger()
{
$className = class_basename($this->argument('name'));
$this->call(
'make:controller-swagger',
[
'name' => $className
]
);
}
/**
* Create a unit test file.
*
* @return void
*/
protected function createTest()
{
$className = class_basename($this->argument('name'));
$this->call(
'make:test',
[
'name' => $className
]
);
}
/**
* Get the destination class path.
*
* @param string $name name of the type
*
* @return string
*/
protected function getPath($name)
{
return parent::getPath($name . 'Controller');
}
/**
* ReplaceClass
*
* @param string $stub filename of stub file
* @param string $name name of the type
*
* @return string|string[]
*/
protected function replaceClass($stub, $name)
{
$stub = parent::replaceClass($stub, $name);
$stub = str_replace('DummyUsecase', Str::ucfirst($this->argument('name')) . 'Usecase', $stub);
$stub = str_replace('dummyUsecase', Str::lower($this->argument('name')) . 'Usecase', $stub);
return $stub;
}
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
if ($this->option('resource')) {
return __DIR__ . '/stubs/controller.stub';
}
return __DIR__ . '/stubs/controller.plain.stub';
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace namespace of root (generally App)
*
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace . '\Http\Controllers';
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['resource', null, InputOption::VALUE_NONE, 'Generate a resource controller class.'],
];
}
}
88 changes: 88 additions & 0 deletions src/Commands/TestMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/**
* PHP version 8.0.30
*
* @category Application
* @package Commands
* @author Made Mas Adi Winata <m45adiwinata@gmail.com>
* @license https://mit-license.org/ MIT License
* @version GIT: 0.0.1
* @link https://brispot.bri.co.id/
*/

declare(strict_types=1);

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;
use Illuminate\Support\Str;

/**
* CollectionMakeCommand
*
* Custom command
*
* @category Console
* @package Commands
* @author Made Mas Adi Winata <m45adiwinata@gmail.com>
* @license https://mit-license.org/ MIT License
* @link https://brispot.bri.co.id/
*/
class TestMakeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:test';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new test class';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Test';
/**
* Get the destination class path.
*
* @param string $name name of the type
*
* @return string
*/
protected function getPath($name)
{
$name = Str::replaceFirst($this->laravel->getNamespace(), '', $name);
return $this->laravel->basePath() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $name . 'Test') . '.php';
}
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
if ($this->option('resource')) {
return __DIR__ . '/stubs/test.stub';
}
return __DIR__ . '/stubs/test.plain.stub';
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['resource', null, InputOption::VALUE_NONE, 'Generate a resource test class.'],
];
}
}
49 changes: 49 additions & 0 deletions src/Commands/stubs/controller.plain.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* PHP version 8.0.30
*
* @category Application
* @package Controllers
* @author
* @license https://mit-license.org/ MIT License
* @version GIT: 0.0.1
* @link https://brispot.bri.co.id/
*/

declare(strict_types=1);

namespace DummyNamespace;

use stdClass;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Spotlibs\PhpLib\Exceptions\ParameterException;
use App\Usecases\DummyUsecase;

/**
* DummyClassController
*
* Request and response handler
*
* @category Collections
* @package Collections
* @author
* @license https://mit-license.org/ MIT License
* @link https://brispot.bri.co.id/
*/
class DummyClassController extends Controller
{
private $dummyUsecase;
private $output;

public function __construct(DummyUsecase $dummyUsecase)
{
parent::__construct();
$this->dummyUsecase = $dummyUsecase;
$this->output = new stdClass();
$this->output->responseCode = '';
$this->output->responseDesc = '';
}
}
36 changes: 36 additions & 0 deletions src/Commands/stubs/test.plain.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* PHP version 8.0.30
*
* @category Application
* @package Controllers
* @author
* @license https://mit-license.org/ MIT License
* @version GIT: 0.0.1
* @link https://brispot.bri.co.id/
*/

declare(strict_types=1);

use Tests\TestCase;

class DummyClassTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function createApplication()
{
return require realpath(env('LARAVEL_BASE_PATH'));
}

/** @test */
/** @runInSeparateProcess */
public function testDummyClassMethod()
{
$this->assertTrue(true);
}
}
Loading

0 comments on commit 5394d98

Please sign in to comment.