Skip to content

Commit

Permalink
Merge pull request #26 from Lomkit/feature/test-commands
Browse files Browse the repository at this point in the history
✅ commands tests
  • Loading branch information
GautierDele authored Aug 30, 2023
2 parents 21eb4a2 + 973966f commit ff3006b
Show file tree
Hide file tree
Showing 19 changed files with 269 additions and 30 deletions.
20 changes: 9 additions & 11 deletions src/Console/Commands/ActionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@ public function handle()
parent::handle();
}

/**
* Build the class with the given name.
*
* @param string $name
* @return string
*/
protected function buildClass($name)
{
return parent::buildClass($name);
}

/**
* Get the stub file for the generator.
*
Expand All @@ -61,6 +50,15 @@ protected function getStub()
return $this->resolveStubPath('/stubs/rest/action.stub');
}

protected function getPath($name)
{
if ($this->hasOption('path')) {
return $this->option('path').'/'.$this->argument('name').'.php';
}

return parent::getPath($name);
}

/**
* Get the default namespace for the class.
*
Expand Down
9 changes: 9 additions & 0 deletions src/Console/Commands/BaseControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ protected function getStub()
return $this->resolveStubPath('/stubs/rest/base-controller.stub');
}

protected function getPath($name)
{
if ($this->hasOption('path')) {
return $this->option('path').'/'.$this->argument('name').'.php';
}

return parent::getPath($name);
}

/**
* Get the default namespace for the class.
*
Expand Down
19 changes: 9 additions & 10 deletions src/Console/Commands/BaseResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ class BaseResourceCommand extends GeneratorCommand implements PromptsForMissingI
*/
protected $description = 'Create a new base resource class';

/**
* Execute the console command.
*
* @return bool|null
*/
public function handle()
{
parent::handle();
}

/**
* Get the stub file for the generator.
*
Expand All @@ -62,6 +52,15 @@ protected function getStub()
return $this->resolveStubPath('/stubs/rest/base-resource.stub');
}

protected function getPath($name)
{
if ($this->hasOption('path')) {
return $this->option('path').'/'.$this->argument('name').'.php';
}

return parent::getPath($name);
}

/**
* Get the default namespace for the class.
*
Expand Down
17 changes: 14 additions & 3 deletions src/Console/Commands/ControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ public function handle()
{
parent::handle();

$this->callSilent('rest:base-controller', [
'name' => 'Controller',
]);
if (!$this->hasOption('path')) {
$this->callSilent('rest:base-controller', [
'name' => 'Controller',
]);
}
}

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

protected function getPath($name)
{
if ($this->hasOption('path')) {
return $this->option('path').'/'.$this->argument('name').'.php';
}

return parent::getPath($name);
}

/**
* Get the default namespace for the class.
*
Expand Down
3 changes: 2 additions & 1 deletion src/Console/Commands/DocumentationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public function handle()
*/
protected function getPath($name)
{
return public_path('vendor/rest/'.$name.'.json');

return $this->hasOption('path') ? $this->option('path').'/'.$name.'.json' : public_path('vendor/rest/'.$name.'.json');
}

protected function getStub()
Expand Down
9 changes: 9 additions & 0 deletions src/Console/Commands/InstructionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ protected function getStub()
return $this->resolveStubPath('/stubs/rest/instruction.stub');
}

protected function getPath($name)
{
if ($this->hasOption('path')) {
return $this->option('path').'/'.$this->argument('name').'.php';
}

return parent::getPath($name);
}

/**
* Get the default namespace for the class.
*
Expand Down
17 changes: 14 additions & 3 deletions src/Console/Commands/ResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,20 @@ public function handle()
{
parent::handle();

$this->callSilent('rest:base-resource', [
'name' => 'Resource',
]);
if (!$this->hasOption('path')) {
$this->callSilent('rest:base-resource', [
'name' => 'Resource',
]);
}
}

protected function getPath($name)
{
if ($this->hasOption('path')) {
return $this->option('path').'/'.$this->argument('name').'.php';
}

return parent::getPath($name);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/Console/Commands/ResponseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ protected function getStub()
return $this->resolveStubPath('/stubs/rest/response.stub');
}

protected function getPath($name)
{
if ($this->hasOption('path')) {
return $this->option('path').'/'.$this->argument('name').'.php';
}

return parent::getPath($name);
}

/**
* Get the default namespace for the class.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function toResponse($request) {
'data' => $data ?? $this->map($this->responsable, $this->modelToResponse($this->responsable, $this->resource, $request->input())),
'meta' => [
config('rest.automatic_gates.key') => [
config('rest.automatic_gates.names.authorized_to_create') => Gate::allows('create', $this->responsable->first()::class),
config('rest.automatic_gates.names.authorized_to_create') => Gate::allows('create', $this->resource::newModel()::class),
]
]
];
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/Commands/ActionCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Lomkit\Rest\Tests\Feature\Commands;

use Illuminate\Bus\PendingBatch;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Gate;
use Lomkit\Rest\Actions\CallRestApiAction;
use Lomkit\Rest\Tests\Feature\TestCase;
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
use Lomkit\Rest\Tests\Support\Models\Model;
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;

class ActionCommandTest extends TestCase
{
public function test_create_action_class(): void
{
$this->artisan('rest:action', ['name' => 'TestAction', '--path' => './.phpunit.cache'])->assertOk();

$this->assertFileExists('./.phpunit.cache/TestAction.php');
}
}
22 changes: 22 additions & 0 deletions tests/Feature/Commands/BaseControllerCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Lomkit\Rest\Tests\Feature\Commands;

use Illuminate\Bus\PendingBatch;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Gate;
use Lomkit\Rest\Actions\CallRestApiAction;
use Lomkit\Rest\Tests\Feature\TestCase;
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
use Lomkit\Rest\Tests\Support\Models\Model;
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;

class BaseControllerCommandTest extends TestCase
{
public function test_create_base_controller_class(): void
{
$this->artisan('rest:base-controller', ['name' => 'TestBaseController', '--path' => './.phpunit.cache'])->assertOk();

$this->assertFileExists('./.phpunit.cache/TestBaseController.php');
}
}
22 changes: 22 additions & 0 deletions tests/Feature/Commands/BaseResourceCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Lomkit\Rest\Tests\Feature\Commands;

use Illuminate\Bus\PendingBatch;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Gate;
use Lomkit\Rest\Actions\CallRestApiAction;
use Lomkit\Rest\Tests\Feature\TestCase;
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
use Lomkit\Rest\Tests\Support\Models\Model;
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;

class BaseResourceCommandTest extends TestCase
{
public function test_create_base_resource_class(): void
{
$this->artisan('rest:base-resource', ['name' => 'TestBaseResource', '--path' => './.phpunit.cache'])->assertOk();

$this->assertFileExists('./.phpunit.cache/TestBaseResource.php');
}
}
22 changes: 22 additions & 0 deletions tests/Feature/Commands/ControllerCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Lomkit\Rest\Tests\Feature\Commands;

use Illuminate\Bus\PendingBatch;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Gate;
use Lomkit\Rest\Actions\CallRestApiAction;
use Lomkit\Rest\Tests\Feature\TestCase;
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
use Lomkit\Rest\Tests\Support\Models\Model;
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;

class ControllerCommandTest extends TestCase
{
public function test_create_controller_class(): void
{
$this->artisan('rest:controller', ['name' => 'TestController', '--path' => './.phpunit.cache'])->assertOk();

$this->assertFileExists('./.phpunit.cache/TestController.php');
}
}
22 changes: 22 additions & 0 deletions tests/Feature/Commands/DocumentationCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Lomkit\Rest\Tests\Feature\Commands;

use Illuminate\Bus\PendingBatch;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Gate;
use Lomkit\Rest\Actions\CallRestApiAction;
use Lomkit\Rest\Tests\Feature\TestCase;
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
use Lomkit\Rest\Tests\Support\Models\Model;
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;

class DocumentationCommandTest extends TestCase
{
public function test_create_documentation_class(): void
{
$this->artisan('rest:documentation', ['--path' => './.phpunit.cache'])->assertOk();

$this->assertFileExists('./.phpunit.cache/openapi.json');
}
}
22 changes: 22 additions & 0 deletions tests/Feature/Commands/InstructionCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Lomkit\Rest\Tests\Feature\Commands;

use Illuminate\Bus\PendingBatch;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Gate;
use Lomkit\Rest\Actions\CallRestApiAction;
use Lomkit\Rest\Tests\Feature\TestCase;
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
use Lomkit\Rest\Tests\Support\Models\Model;
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;

class InstructionCommandTest extends TestCase
{
public function test_create_instruction_class(): void
{
$this->artisan('rest:instruction', ['name' => 'TestInstruction', '--path' => './.phpunit.cache'])->assertOk();

$this->assertFileExists('./.phpunit.cache/TestInstruction.php');
}
}
22 changes: 22 additions & 0 deletions tests/Feature/Commands/ResourceCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Lomkit\Rest\Tests\Feature\Commands;

use Illuminate\Bus\PendingBatch;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Gate;
use Lomkit\Rest\Actions\CallRestApiAction;
use Lomkit\Rest\Tests\Feature\TestCase;
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
use Lomkit\Rest\Tests\Support\Models\Model;
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;

class ResourceCommandTest extends TestCase
{
public function test_create_resource_class(): void
{
$this->artisan('rest:resource', ['name' => 'TestResource', '--path' => './.phpunit.cache'])->assertOk();

$this->assertFileExists('./.phpunit.cache/TestResource.php');
}
}
22 changes: 22 additions & 0 deletions tests/Feature/Commands/ResponseCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Lomkit\Rest\Tests\Feature\Commands;

use Illuminate\Bus\PendingBatch;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Gate;
use Lomkit\Rest\Actions\CallRestApiAction;
use Lomkit\Rest\Tests\Feature\TestCase;
use Lomkit\Rest\Tests\Support\Database\Factories\ModelFactory;
use Lomkit\Rest\Tests\Support\Models\Model;
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;

class ResponseCommandTest extends TestCase
{
public function test_create_response_class(): void
{
$this->artisan('rest:response', ['name' => 'TestResponse', '--path' => './.phpunit.cache'])->assertOk();

$this->assertFileExists('./.phpunit.cache/TestResponse.php');
}
}
Loading

0 comments on commit ff3006b

Please sign in to comment.