diff --git a/CHANGELOG.md b/CHANGELOG.md index a8476e6a..2838c32a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,6 +65,8 @@ Sending an invalid content type header now returns a JSON API error object. Return a 404 JSON API error object and allow this to be overridden. - [#155](https://github.com/cloudcreativity/laravel-json-api/issues/155) Return a JSON API error when the request content cannot be JSON decoded. +- [#169](https://github.com/cloudcreativity/laravel-json-api/issues/169) +Generating a resource when the `by-resource` option was set to `false` had the wrong class name in the generated file. ## [0.12.0] - 2018-02-08 diff --git a/src/Console/Commands/AbstractGeneratorCommand.php b/src/Console/Commands/AbstractGeneratorCommand.php index 035f49dd..5ba1ea8c 100644 --- a/src/Console/Commands/AbstractGeneratorCommand.php +++ b/src/Console/Commands/AbstractGeneratorCommand.php @@ -142,9 +142,10 @@ protected function buildClass($name) $stub = $this->files->get($this->getStub()); $this->replaceNamespace($stub, $name) + ->replaceClassName($stub, $name) ->replaceResourceType($stub, $this->getResourceName()) ->replaceApplicationNamespace($stub) - ->replaceModel($stub, $this->getResourceName()); + ->replaceRecord($stub, $this->getResourceName()); return $stub; } @@ -234,9 +235,9 @@ private function replaceResourceType(&$stub, $resource) * @param $resource * @return $this */ - private function replaceModel(&$stub, $resource) + private function replaceRecord(&$stub, $resource) { - $stub = str_replace('DummyModel', Str::classify(str_singular($resource)), $stub); + $stub = str_replace('DummyRecord', Str::classify(str_singular($resource)), $stub); return $this; } @@ -255,6 +256,19 @@ private function replaceApplicationNamespace(&$stub) return $this; } + /** + * Replace the class name. + * + * @param $stub + * @return $this + */ + private function replaceClassName(&$stub, $name) + { + $stub = $this->replaceClass($stub, $name); + + return $this; + } + /** * Get the stub for specific generator type * diff --git a/stubs/abstract/adapter.stub b/stubs/abstract/adapter.stub index 882cb69a..c1d51fd4 100644 --- a/stubs/abstract/adapter.stub +++ b/stubs/abstract/adapter.stub @@ -8,7 +8,7 @@ use CloudCreativity\LaravelJsonApi\Contracts\Object\ResourceObjectInterface; use CloudCreativity\Utils\Object\StandardObjectInterface; use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface; -class Adapter extends AbstractResourceAdapter +class DummyClass extends AbstractResourceAdapter { /** diff --git a/stubs/abstract/schema.stub b/stubs/abstract/schema.stub index 9922350b..df9fc5fd 100644 --- a/stubs/abstract/schema.stub +++ b/stubs/abstract/schema.stub @@ -3,8 +3,9 @@ namespace DummyNamespace; use Neomerx\JsonApi\Schema\SchemaProvider; +use DummyApplicationNamespace\DummyRecord; -class Schema extends SchemaProvider +class DummyClass extends SchemaProvider { /** @@ -13,8 +14,8 @@ class Schema extends SchemaProvider protected $resourceType = 'dummyResourceType'; /** - * @param object $resource - * @return mixed + * @param DummyRecord $resource + * @return string */ public function getId($resource) { @@ -22,8 +23,8 @@ class Schema extends SchemaProvider } /** - * @param object $resource - * @return mixed + * @param DummyRecord $resource + * @return array */ public function getAttributes($resource) { diff --git a/stubs/eloquent/adapter.stub b/stubs/eloquent/adapter.stub index c7cf9ec0..493e05b6 100644 --- a/stubs/eloquent/adapter.stub +++ b/stubs/eloquent/adapter.stub @@ -6,9 +6,9 @@ use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter; use CloudCreativity\LaravelJsonApi\Pagination\StandardStrategy; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Collection; -use DummyApplicationNamespace\DummyModel; +use DummyApplicationNamespace\DummyRecord; -class Adapter extends AbstractAdapter +class DummyClass extends AbstractAdapter { /** @@ -25,7 +25,7 @@ class Adapter extends AbstractAdapter */ public function __construct(StandardStrategy $paging) { - parent::__construct(new DummyModel(), $paging); + parent::__construct(new DummyRecord(), $paging); } /** diff --git a/stubs/eloquent/schema.stub b/stubs/eloquent/schema.stub index 56e4fbb0..8c42bc10 100644 --- a/stubs/eloquent/schema.stub +++ b/stubs/eloquent/schema.stub @@ -4,7 +4,7 @@ namespace DummyNamespace; use CloudCreativity\LaravelJsonApi\Eloquent\AbstractSchema; -class Schema extends AbstractSchema +class DummyClass extends AbstractSchema { /** diff --git a/stubs/independent/validators.stub b/stubs/independent/validators.stub index 98f8a423..9cc9ea25 100644 --- a/stubs/independent/validators.stub +++ b/stubs/independent/validators.stub @@ -4,8 +4,9 @@ namespace DummyNamespace; use CloudCreativity\LaravelJsonApi\Contracts\Validators\RelationshipsValidatorInterface; use CloudCreativity\LaravelJsonApi\Validators\AbstractValidatorProvider; +use DummyApplicationNamespace\DummyRecord; -class Validators extends AbstractValidatorProvider +class DummyClass extends AbstractValidatorProvider { /** @@ -16,7 +17,7 @@ class Validators extends AbstractValidatorProvider /** * Get the validation rules for the resource attributes. * - * @param object|null $record + * @param DummyRecord|null $record * the record being updated, or null if it is a create request. * @return array */ @@ -31,7 +32,7 @@ class Validators extends AbstractValidatorProvider * Define the validation rules for the resource relationships. * * @param RelationshipsValidatorInterface $relationships - * @param object|null $record + * @param DummyRecord|null $record * the record being updated, or null if it is a create request. * @return void */ diff --git a/tests/lib/Integration/GeneratorsTest.php b/tests/lib/Integration/GeneratorsTest.php index 81c888d8..e00e80c1 100644 --- a/tests/lib/Integration/GeneratorsTest.php +++ b/tests/lib/Integration/GeneratorsTest.php @@ -32,6 +32,11 @@ class GeneratorsTest extends TestCase */ private $files; + /** + * @var bool + */ + private $byResource = true; + /** * @return void */ @@ -49,8 +54,17 @@ protected function tearDown() { parent::tearDown(); - if ($this->files->exists($dir = "{$this->path}/app/JsonApi/Companies")) { - $this->files->deleteDirectory($dir); + $directories = [ + "{$this->path}/app/JsonApi/Companies", + "{$this->path}/app/JsonApi/Adapters", + "{$this->path}/app/JsonApi/Schemas", + "{$this->path}/app/JsonApi/Validators", + ]; + + foreach ($directories as $dir) { + if ($this->files->exists($dir)) { + $this->files->deleteDirectory($dir); + } } if ($this->files->exists($file = "{$this->path}/config/json-api-v1.php")) { @@ -107,6 +121,21 @@ public function testForceEloquentResource() $this->assertEloquentResource(); } + /** + * Test generating an Eloquent resource with the `by-resource` option set to `false`. + */ + public function testEloquentResourceNotByResource() + { + $this->withEloquent()->notByResource(); + + $result = $this->artisan('make:json-api:resource', [ + 'resource' => 'companies', + ]); + + $this->assertSame(0, $result); + $this->assertEloquentResource(); + } + /** * If Eloquent is not the default, running the generator without specifying * anything will create generic classes. @@ -140,6 +169,21 @@ public function testForceGenericResource() $this->assertGenericResource(); } + /** + * Test generating generic resources with the `by-resource` option set to `false`. + */ + public function testGenericResourceNotByResource() + { + $this->withoutEloquent()->notByResource(); + + $result = $this->artisan('make:json-api:resource', [ + 'resource' => 'companies', + ]); + + $this->assertSame(0, $result); + $this->assertGenericResource(); + } + /** * @return $this */ @@ -160,6 +204,17 @@ private function withoutEloquent() return $this; } + /** + * @return $this + */ + private function notByResource() + { + $this->byResource = false; + config()->set('json-api-default.by-resource', false); + + return $this; + } + /** * @return void */ @@ -206,9 +261,20 @@ private function assertGenericAdapter() */ private function assertAdapter() { - $this->assertFileExists($file = "{$this->path}/app/JsonApi/Companies/Adapter.php"); + $file = $this->byResource ? + "{$this->path}/app/JsonApi/Companies/Adapter.php" : + "{$this->path}/app/JsonApi/Adapters/Company.php"; + + $this->assertFileExists($file); $content = $this->files->get($file); - $this->assertContains('namespace DummyApp\JsonApi\Companies;', $content); + + if ($this->byResource) { + $this->assertContains('namespace DummyApp\JsonApi\Companies;', $content); + $this->assertContains('class Adapter extends', $content); + } else { + $this->assertContains('namespace DummyApp\JsonApi\Adapters;', $content); + $this->assertContains('class Company extends', $content); + } return $content; } @@ -230,6 +296,8 @@ private function assertGenericSchema() { $content = $this->assertSchema(); $this->assertContains('Schema\SchemaProvider', $content); + $this->assertContains('use DummyApp\Company;', $content); + $this->assertContains('@param Company $resource', $content); } /** @@ -237,11 +305,22 @@ private function assertGenericSchema() */ private function assertSchema() { - $this->assertFileExists($file = "{$this->path}/app/JsonApi/Companies/Schema.php"); + $file = $this->byResource ? + "{$this->path}/app/JsonApi/Companies/Schema.php" : + "{$this->path}/app/JsonApi/Schemas/Company.php"; + + $this->assertFileExists($file); $content = $this->files->get($file); - $this->assertContains('namespace DummyApp\JsonApi\Companies;', $content); $this->assertContains("protected \$resourceType = 'companies';", $content); + if ($this->byResource) { + $this->assertContains('namespace DummyApp\JsonApi\Companies;', $content); + $this->assertContains('class Schema extends', $content); + } else { + $this->assertContains('namespace DummyApp\JsonApi\Schemas;', $content); + $this->assertContains('class Company extends', $content); + } + return $content; } @@ -250,11 +329,23 @@ private function assertSchema() */ private function assertValidators() { - $this->assertFileExists($file = "{$this->path}/app/JsonApi/Companies/Validators.php"); + $file = $this->byResource ? + "{$this->path}/app/JsonApi/Companies/Validators.php" : + "{$this->path}/app/JsonApi/Validators/Company.php"; + $this->assertFileExists($file); $content = $this->files->get($file); - $this->assertContains('namespace DummyApp\JsonApi\Companies;', $content); $this->assertContains("protected \$resourceType = 'companies';", $content); + $this->assertContains('use DummyApp\Company;', $content); + $this->assertContains('@param Company|null $record', $content); + + if ($this->byResource) { + $this->assertContains('namespace DummyApp\JsonApi\Companies;', $content); + $this->assertContains('class Validators extends', $content); + } else { + $this->assertContains('namespace DummyApp\JsonApi\Validators;', $content); + $this->assertContains('class Company extends', $content); + } } }