Skip to content

Commit

Permalink
[Bugfix] Ensure correct class name in generated file
Browse files Browse the repository at this point in the history
When generating a file when the `by-resource` option was set to
false, the wrong class name was used in the generated file.
This commit adds tests and the fix.

Fixes #169
  • Loading branch information
lindyhopchris committed Apr 27, 2018
1 parent a8b896a commit f319b67
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 17 additions & 3 deletions src/Console/Commands/AbstractGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion stubs/abstract/adapter.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

/**
Expand Down
11 changes: 6 additions & 5 deletions stubs/abstract/schema.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace DummyNamespace;

use Neomerx\JsonApi\Schema\SchemaProvider;
use DummyApplicationNamespace\DummyRecord;

class Schema extends SchemaProvider
class DummyClass extends SchemaProvider
{

/**
Expand All @@ -13,17 +14,17 @@ class Schema extends SchemaProvider
protected $resourceType = 'dummyResourceType';

/**
* @param object $resource
* @return mixed
* @param DummyRecord $resource
* @return string
*/
public function getId($resource)
{
// TODO
}

/**
* @param object $resource
* @return mixed
* @param DummyRecord $resource
* @return array
*/
public function getAttributes($resource)
{
Expand Down
6 changes: 3 additions & 3 deletions stubs/eloquent/adapter.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

/**
Expand All @@ -25,7 +25,7 @@ class Adapter extends AbstractAdapter
*/
public function __construct(StandardStrategy $paging)
{
parent::__construct(new DummyModel(), $paging);
parent::__construct(new DummyRecord(), $paging);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion stubs/eloquent/schema.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace DummyNamespace;

use CloudCreativity\LaravelJsonApi\Eloquent\AbstractSchema;

class Schema extends AbstractSchema
class DummyClass extends AbstractSchema
{

/**
Expand Down
7 changes: 4 additions & 3 deletions stubs/independent/validators.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

/**
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand Down
107 changes: 99 additions & 8 deletions tests/lib/Integration/GeneratorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class GeneratorsTest extends TestCase
*/
private $files;

/**
* @var bool
*/
private $byResource = true;

/**
* @return void
*/
Expand All @@ -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")) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
}
Expand All @@ -230,18 +296,31 @@ private function assertGenericSchema()
{
$content = $this->assertSchema();
$this->assertContains('Schema\SchemaProvider', $content);
$this->assertContains('use DummyApp\Company;', $content);
$this->assertContains('@param Company $resource', $content);
}

/**
* @return string
*/
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;
}

Expand All @@ -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);
}
}
}

0 comments on commit f319b67

Please sign in to comment.