Skip to content

Commit ff3006b

Browse files
authored
Merge pull request #26 from Lomkit/feature/test-commands
✅ commands tests
2 parents 21eb4a2 + 973966f commit ff3006b

19 files changed

+269
-30
lines changed

src/Console/Commands/ActionCommand.php

+9-11
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@ public function handle()
4040
parent::handle();
4141
}
4242

43-
/**
44-
* Build the class with the given name.
45-
*
46-
* @param string $name
47-
* @return string
48-
*/
49-
protected function buildClass($name)
50-
{
51-
return parent::buildClass($name);
52-
}
53-
5443
/**
5544
* Get the stub file for the generator.
5645
*
@@ -61,6 +50,15 @@ protected function getStub()
6150
return $this->resolveStubPath('/stubs/rest/action.stub');
6251
}
6352

53+
protected function getPath($name)
54+
{
55+
if ($this->hasOption('path')) {
56+
return $this->option('path').'/'.$this->argument('name').'.php';
57+
}
58+
59+
return parent::getPath($name);
60+
}
61+
6462
/**
6563
* Get the default namespace for the class.
6664
*

src/Console/Commands/BaseControllerCommand.php

+9
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ protected function getStub()
6262
return $this->resolveStubPath('/stubs/rest/base-controller.stub');
6363
}
6464

65+
protected function getPath($name)
66+
{
67+
if ($this->hasOption('path')) {
68+
return $this->option('path').'/'.$this->argument('name').'.php';
69+
}
70+
71+
return parent::getPath($name);
72+
}
73+
6574
/**
6675
* Get the default namespace for the class.
6776
*

src/Console/Commands/BaseResourceCommand.php

+9-10
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,6 @@ class BaseResourceCommand extends GeneratorCommand implements PromptsForMissingI
4242
*/
4343
protected $description = 'Create a new base resource class';
4444

45-
/**
46-
* Execute the console command.
47-
*
48-
* @return bool|null
49-
*/
50-
public function handle()
51-
{
52-
parent::handle();
53-
}
54-
5545
/**
5646
* Get the stub file for the generator.
5747
*
@@ -62,6 +52,15 @@ protected function getStub()
6252
return $this->resolveStubPath('/stubs/rest/base-resource.stub');
6353
}
6454

55+
protected function getPath($name)
56+
{
57+
if ($this->hasOption('path')) {
58+
return $this->option('path').'/'.$this->argument('name').'.php';
59+
}
60+
61+
return parent::getPath($name);
62+
}
63+
6564
/**
6665
* Get the default namespace for the class.
6766
*

src/Console/Commands/ControllerCommand.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ public function handle()
4141
{
4242
parent::handle();
4343

44-
$this->callSilent('rest:base-controller', [
45-
'name' => 'Controller',
46-
]);
44+
if (!$this->hasOption('path')) {
45+
$this->callSilent('rest:base-controller', [
46+
'name' => 'Controller',
47+
]);
48+
}
4749
}
4850

4951
/**
@@ -87,6 +89,15 @@ protected function getStub()
8789
return $this->resolveStubPath('/stubs/rest/controller.stub');
8890
}
8991

92+
protected function getPath($name)
93+
{
94+
if ($this->hasOption('path')) {
95+
return $this->option('path').'/'.$this->argument('name').'.php';
96+
}
97+
98+
return parent::getPath($name);
99+
}
100+
90101
/**
91102
* Get the default namespace for the class.
92103
*

src/Console/Commands/DocumentationCommand.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public function handle()
5555
*/
5656
protected function getPath($name)
5757
{
58-
return public_path('vendor/rest/'.$name.'.json');
58+
59+
return $this->hasOption('path') ? $this->option('path').'/'.$name.'.json' : public_path('vendor/rest/'.$name.'.json');
5960
}
6061

6162
protected function getStub()

src/Console/Commands/InstructionCommand.php

+9
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ protected function getStub()
6161
return $this->resolveStubPath('/stubs/rest/instruction.stub');
6262
}
6363

64+
protected function getPath($name)
65+
{
66+
if ($this->hasOption('path')) {
67+
return $this->option('path').'/'.$this->argument('name').'.php';
68+
}
69+
70+
return parent::getPath($name);
71+
}
72+
6473
/**
6574
* Get the default namespace for the class.
6675
*

src/Console/Commands/ResourceCommand.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,20 @@ public function handle()
4040
{
4141
parent::handle();
4242

43-
$this->callSilent('rest:base-resource', [
44-
'name' => 'Resource',
45-
]);
43+
if (!$this->hasOption('path')) {
44+
$this->callSilent('rest:base-resource', [
45+
'name' => 'Resource',
46+
]);
47+
}
48+
}
49+
50+
protected function getPath($name)
51+
{
52+
if ($this->hasOption('path')) {
53+
return $this->option('path').'/'.$this->argument('name').'.php';
54+
}
55+
56+
return parent::getPath($name);
4657
}
4758

4859
/**

src/Console/Commands/ResponseCommand.php

+9
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ protected function getStub()
6565
return $this->resolveStubPath('/stubs/rest/response.stub');
6666
}
6767

68+
protected function getPath($name)
69+
{
70+
if ($this->hasOption('path')) {
71+
return $this->option('path').'/'.$this->argument('name').'.php';
72+
}
73+
74+
return parent::getPath($name);
75+
}
76+
6877
/**
6978
* Get the default namespace for the class.
7079
*

src/Http/Response.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function toResponse($request) {
146146
'data' => $data ?? $this->map($this->responsable, $this->modelToResponse($this->responsable, $this->resource, $request->input())),
147147
'meta' => [
148148
config('rest.automatic_gates.key') => [
149-
config('rest.automatic_gates.names.authorized_to_create') => Gate::allows('create', $this->responsable->first()::class),
149+
config('rest.automatic_gates.names.authorized_to_create') => Gate::allows('create', $this->resource::newModel()::class),
150150
]
151151
]
152152
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Tests\Feature\Commands;
4+
5+
use Illuminate\Bus\PendingBatch;
6+
use Illuminate\Support\Facades\Bus;
7+
use Illuminate\Support\Facades\Gate;
8+
use Lomkit\Rest\Actions\CallRestApiAction;
9+
use Lomkit\Rest\Tests\Feature\TestCase;
10+
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
11+
use Lomkit\Rest\Tests\Support\Models\Model;
12+
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;
13+
14+
class ActionCommandTest extends TestCase
15+
{
16+
public function test_create_action_class(): void
17+
{
18+
$this->artisan('rest:action', ['name' => 'TestAction', '--path' => './.phpunit.cache'])->assertOk();
19+
20+
$this->assertFileExists('./.phpunit.cache/TestAction.php');
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Tests\Feature\Commands;
4+
5+
use Illuminate\Bus\PendingBatch;
6+
use Illuminate\Support\Facades\Bus;
7+
use Illuminate\Support\Facades\Gate;
8+
use Lomkit\Rest\Actions\CallRestApiAction;
9+
use Lomkit\Rest\Tests\Feature\TestCase;
10+
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
11+
use Lomkit\Rest\Tests\Support\Models\Model;
12+
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;
13+
14+
class BaseControllerCommandTest extends TestCase
15+
{
16+
public function test_create_base_controller_class(): void
17+
{
18+
$this->artisan('rest:base-controller', ['name' => 'TestBaseController', '--path' => './.phpunit.cache'])->assertOk();
19+
20+
$this->assertFileExists('./.phpunit.cache/TestBaseController.php');
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Tests\Feature\Commands;
4+
5+
use Illuminate\Bus\PendingBatch;
6+
use Illuminate\Support\Facades\Bus;
7+
use Illuminate\Support\Facades\Gate;
8+
use Lomkit\Rest\Actions\CallRestApiAction;
9+
use Lomkit\Rest\Tests\Feature\TestCase;
10+
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
11+
use Lomkit\Rest\Tests\Support\Models\Model;
12+
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;
13+
14+
class BaseResourceCommandTest extends TestCase
15+
{
16+
public function test_create_base_resource_class(): void
17+
{
18+
$this->artisan('rest:base-resource', ['name' => 'TestBaseResource', '--path' => './.phpunit.cache'])->assertOk();
19+
20+
$this->assertFileExists('./.phpunit.cache/TestBaseResource.php');
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Tests\Feature\Commands;
4+
5+
use Illuminate\Bus\PendingBatch;
6+
use Illuminate\Support\Facades\Bus;
7+
use Illuminate\Support\Facades\Gate;
8+
use Lomkit\Rest\Actions\CallRestApiAction;
9+
use Lomkit\Rest\Tests\Feature\TestCase;
10+
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
11+
use Lomkit\Rest\Tests\Support\Models\Model;
12+
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;
13+
14+
class ControllerCommandTest extends TestCase
15+
{
16+
public function test_create_controller_class(): void
17+
{
18+
$this->artisan('rest:controller', ['name' => 'TestController', '--path' => './.phpunit.cache'])->assertOk();
19+
20+
$this->assertFileExists('./.phpunit.cache/TestController.php');
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Tests\Feature\Commands;
4+
5+
use Illuminate\Bus\PendingBatch;
6+
use Illuminate\Support\Facades\Bus;
7+
use Illuminate\Support\Facades\Gate;
8+
use Lomkit\Rest\Actions\CallRestApiAction;
9+
use Lomkit\Rest\Tests\Feature\TestCase;
10+
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
11+
use Lomkit\Rest\Tests\Support\Models\Model;
12+
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;
13+
14+
class DocumentationCommandTest extends TestCase
15+
{
16+
public function test_create_documentation_class(): void
17+
{
18+
$this->artisan('rest:documentation', ['--path' => './.phpunit.cache'])->assertOk();
19+
20+
$this->assertFileExists('./.phpunit.cache/openapi.json');
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Tests\Feature\Commands;
4+
5+
use Illuminate\Bus\PendingBatch;
6+
use Illuminate\Support\Facades\Bus;
7+
use Illuminate\Support\Facades\Gate;
8+
use Lomkit\Rest\Actions\CallRestApiAction;
9+
use Lomkit\Rest\Tests\Feature\TestCase;
10+
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
11+
use Lomkit\Rest\Tests\Support\Models\Model;
12+
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;
13+
14+
class InstructionCommandTest extends TestCase
15+
{
16+
public function test_create_instruction_class(): void
17+
{
18+
$this->artisan('rest:instruction', ['name' => 'TestInstruction', '--path' => './.phpunit.cache'])->assertOk();
19+
20+
$this->assertFileExists('./.phpunit.cache/TestInstruction.php');
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Tests\Feature\Commands;
4+
5+
use Illuminate\Bus\PendingBatch;
6+
use Illuminate\Support\Facades\Bus;
7+
use Illuminate\Support\Facades\Gate;
8+
use Lomkit\Rest\Actions\CallRestApiAction;
9+
use Lomkit\Rest\Tests\Feature\TestCase;
10+
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
11+
use Lomkit\Rest\Tests\Support\Models\Model;
12+
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;
13+
14+
class ResourceCommandTest extends TestCase
15+
{
16+
public function test_create_resource_class(): void
17+
{
18+
$this->artisan('rest:resource', ['name' => 'TestResource', '--path' => './.phpunit.cache'])->assertOk();
19+
20+
$this->assertFileExists('./.phpunit.cache/TestResource.php');
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Tests\Feature\Commands;
4+
5+
use Illuminate\Bus\PendingBatch;
6+
use Illuminate\Support\Facades\Bus;
7+
use Illuminate\Support\Facades\Gate;
8+
use Lomkit\Rest\Actions\CallRestApiAction;
9+
use Lomkit\Rest\Tests\Feature\TestCase;
10+
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
11+
use Lomkit\Rest\Tests\Support\Models\Model;
12+
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;
13+
14+
class ResponseCommandTest extends TestCase
15+
{
16+
public function test_create_response_class(): void
17+
{
18+
$this->artisan('rest:response', ['name' => 'TestResponse', '--path' => './.phpunit.cache'])->assertOk();
19+
20+
$this->assertFileExists('./.phpunit.cache/TestResponse.php');
21+
}
22+
}

0 commit comments

Comments
 (0)