Skip to content

Commit 5394d98

Browse files
committed
make controller and test commands
1 parent 4abae5a commit 5394d98

15 files changed

+692
-225
lines changed

phpcs.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@
66
<rule ref="PEAR">
77
<exclude name="PEAR.NamingConventions.ValidVariableName.PrivateNoUnderscore" />
88
<exclude name="PEAR.NamingConventions.ValidFunctionName.PrivateNoUnderscore" />
9+
<exclude name="Generic.Files.LineEndings"/>
10+
</rule>
11+
<rule ref="Generic.Files.LineLength">
12+
<properties>
13+
<property name="lineLimit" value="200"/>
14+
<property name="absoluteLineLimit" value="0"/>
15+
<property name="ignoreComments" value="true"/>
16+
</properties>
917
</rule>
1018
<rule ref="Generic.PHP.RequireStrictTypes" />
1119
<file>src/</file>
12-
<file>tests/</file>
20+
<exclude-pattern>tests/</exclude-pattern>
1321
<exclude-pattern>vendor</exclude-pattern>
1422
<exclude-pattern>resources</exclude-pattern>
1523
<exclude-pattern>database/</exclude-pattern>

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
</include>
2626
<exclude>
2727
<file>src/Exceptions/Handler.php</file>
28+
<directory suffix=".php">src/Commands</directory>
2829
<directory suffix=".php">src/Libraries</directory>
2930
<directory suffix=".php">src/Providers</directory>
3031
<directory suffix=".php">src/Validations</directory>
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
/**
4+
* PHP version 8
5+
*
6+
* @category Library
7+
* @package Middlewares
8+
* @author Made Mas Adi Winata <m45adiwinata@gmail.com>
9+
* @license https://mit-license.org/ MIT License
10+
* @version GIT: 0.0.6
11+
* @link https://github.com/spotlibs
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Spotlibs\PhpLib\Commands;
17+
18+
use Illuminate\Console\GeneratorCommand;
19+
use Symfony\Component\Console\Input\InputOption;
20+
use Illuminate\Support\Str;
21+
22+
/**
23+
* ControllerMakeCommand
24+
*
25+
* Standard command
26+
*
27+
* @category Console
28+
* @package Commands
29+
* @author Made Mas Adi Winata <m45adiwinata@gmail.com>
30+
* @license https://mit-license.org/ MIT License
31+
* @link https://github.com/spotlibs
32+
*/
33+
class ControllerMakeCommand extends GeneratorCommand
34+
{
35+
/**
36+
* The console command name.
37+
*
38+
* @var string
39+
*/
40+
protected $name = 'make:controller';
41+
/**
42+
* The console command description.
43+
*
44+
* @var string
45+
*/
46+
protected $description = 'Create a new controller class';
47+
/**
48+
* The type of class being generated.
49+
*
50+
* @var string
51+
*/
52+
protected $type = 'Controller';
53+
/**
54+
* Execute the console command.
55+
*
56+
* @return void
57+
*/
58+
public function handle()
59+
{
60+
parent::handle();
61+
$this->createSwagger();
62+
$this->createTest();
63+
}
64+
/**
65+
* Create a swagger file controller.
66+
*
67+
* @return void
68+
*/
69+
protected function createSwagger()
70+
{
71+
$className = class_basename($this->argument('name'));
72+
$this->call(
73+
'make:controller-swagger',
74+
[
75+
'name' => $className
76+
]
77+
);
78+
}
79+
/**
80+
* Create a unit test file.
81+
*
82+
* @return void
83+
*/
84+
protected function createTest()
85+
{
86+
$className = class_basename($this->argument('name'));
87+
$this->call(
88+
'make:test',
89+
[
90+
'name' => $className
91+
]
92+
);
93+
}
94+
/**
95+
* Get the destination class path.
96+
*
97+
* @param string $name name of the type
98+
*
99+
* @return string
100+
*/
101+
protected function getPath($name)
102+
{
103+
return parent::getPath($name . 'Controller');
104+
}
105+
/**
106+
* ReplaceClass
107+
*
108+
* @param string $stub filename of stub file
109+
* @param string $name name of the type
110+
*
111+
* @return string|string[]
112+
*/
113+
protected function replaceClass($stub, $name)
114+
{
115+
$stub = parent::replaceClass($stub, $name);
116+
$stub = str_replace('DummyUsecase', Str::ucfirst($this->argument('name')) . 'Usecase', $stub);
117+
$stub = str_replace('dummyUsecase', Str::lower($this->argument('name')) . 'Usecase', $stub);
118+
return $stub;
119+
}
120+
/**
121+
* Get the stub file for the generator.
122+
*
123+
* @return string
124+
*/
125+
protected function getStub()
126+
{
127+
if ($this->option('resource')) {
128+
return __DIR__ . '/stubs/controller.stub';
129+
}
130+
return __DIR__ . '/stubs/controller.plain.stub';
131+
}
132+
/**
133+
* Get the default namespace for the class.
134+
*
135+
* @param string $rootNamespace namespace of root (generally App)
136+
*
137+
* @return string
138+
*/
139+
protected function getDefaultNamespace($rootNamespace)
140+
{
141+
return $rootNamespace . '\Http\Controllers';
142+
}
143+
/**
144+
* Get the console command options.
145+
*
146+
* @return array
147+
*/
148+
protected function getOptions()
149+
{
150+
return [
151+
['resource', null, InputOption::VALUE_NONE, 'Generate a resource controller class.'],
152+
];
153+
}
154+
}

src/Commands/TestMakeCommand.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
/**
4+
* PHP version 8.0.30
5+
*
6+
* @category Application
7+
* @package Commands
8+
* @author Made Mas Adi Winata <m45adiwinata@gmail.com>
9+
* @license https://mit-license.org/ MIT License
10+
* @version GIT: 0.0.1
11+
* @link https://brispot.bri.co.id/
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace App\Console\Commands;
17+
18+
use Illuminate\Console\GeneratorCommand;
19+
use Symfony\Component\Console\Input\InputOption;
20+
use Illuminate\Support\Str;
21+
22+
/**
23+
* CollectionMakeCommand
24+
*
25+
* Custom command
26+
*
27+
* @category Console
28+
* @package Commands
29+
* @author Made Mas Adi Winata <m45adiwinata@gmail.com>
30+
* @license https://mit-license.org/ MIT License
31+
* @link https://brispot.bri.co.id/
32+
*/
33+
class TestMakeCommand extends GeneratorCommand
34+
{
35+
/**
36+
* The console command name.
37+
*
38+
* @var string
39+
*/
40+
protected $name = 'make:test';
41+
/**
42+
* The console command description.
43+
*
44+
* @var string
45+
*/
46+
protected $description = 'Create a new test class';
47+
/**
48+
* The type of class being generated.
49+
*
50+
* @var string
51+
*/
52+
protected $type = 'Test';
53+
/**
54+
* Get the destination class path.
55+
*
56+
* @param string $name name of the type
57+
*
58+
* @return string
59+
*/
60+
protected function getPath($name)
61+
{
62+
$name = Str::replaceFirst($this->laravel->getNamespace(), '', $name);
63+
return $this->laravel->basePath() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $name . 'Test') . '.php';
64+
}
65+
/**
66+
* Get the stub file for the generator.
67+
*
68+
* @return string
69+
*/
70+
protected function getStub()
71+
{
72+
if ($this->option('resource')) {
73+
return __DIR__ . '/stubs/test.stub';
74+
}
75+
return __DIR__ . '/stubs/test.plain.stub';
76+
}
77+
/**
78+
* Get the console command options.
79+
*
80+
* @return array
81+
*/
82+
protected function getOptions()
83+
{
84+
return [
85+
['resource', null, InputOption::VALUE_NONE, 'Generate a resource test class.'],
86+
];
87+
}
88+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* PHP version 8.0.30
5+
*
6+
* @category Application
7+
* @package Controllers
8+
* @author
9+
* @license https://mit-license.org/ MIT License
10+
* @version GIT: 0.0.1
11+
* @link https://brispot.bri.co.id/
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace DummyNamespace;
17+
18+
use stdClass;
19+
use Illuminate\Support\Str;
20+
use Illuminate\Http\Request;
21+
use App\Http\Controllers\Controller;
22+
use Spotlibs\PhpLib\Exceptions\ParameterException;
23+
use App\Usecases\DummyUsecase;
24+
25+
/**
26+
* DummyClassController
27+
*
28+
* Request and response handler
29+
*
30+
* @category Collections
31+
* @package Collections
32+
* @author
33+
* @license https://mit-license.org/ MIT License
34+
* @link https://brispot.bri.co.id/
35+
*/
36+
class DummyClassController extends Controller
37+
{
38+
private $dummyUsecase;
39+
private $output;
40+
41+
public function __construct(DummyUsecase $dummyUsecase)
42+
{
43+
parent::__construct();
44+
$this->dummyUsecase = $dummyUsecase;
45+
$this->output = new stdClass();
46+
$this->output->responseCode = '';
47+
$this->output->responseDesc = '';
48+
}
49+
}

src/Commands/stubs/test.plain.stub

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* PHP version 8.0.30
5+
*
6+
* @category Application
7+
* @package Controllers
8+
* @author
9+
* @license https://mit-license.org/ MIT License
10+
* @version GIT: 0.0.1
11+
* @link https://brispot.bri.co.id/
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
use Tests\TestCase;
17+
18+
class DummyClassTest extends TestCase
19+
{
20+
/**
21+
* A basic test example.
22+
*
23+
* @return void
24+
*/
25+
public function createApplication()
26+
{
27+
return require realpath(env('LARAVEL_BASE_PATH'));
28+
}
29+
30+
/** @test */
31+
/** @runInSeparateProcess */
32+
public function testDummyClassMethod()
33+
{
34+
$this->assertTrue(true);
35+
}
36+
}

0 commit comments

Comments
 (0)