Skip to content

Commit

Permalink
[Bugfix] Fix generating generic authorizer with multiple servers
Browse files Browse the repository at this point in the history
As the authorizer is generic, it does not need a server to be
specified.

Closes #34
  • Loading branch information
lindyhopchris committed Feb 25, 2021
1 parent 7608731 commit 0839fea
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ All notable changes to this project will be documented in this file. This projec
iterating over a resource's relationships.
- [#26](https://github.com/laravel-json-api/laravel/issues/26) Fix parsing the `fields` query parameter to field set
value objects.
- [#34](https://github.com/laravel-json-api/laravel/issues/34) Do not require server option when generating a generic
authorizer with multiple servers present.

## [1.0.0-alpha.3] - 2021-02-09

Expand Down
18 changes: 16 additions & 2 deletions src/Console/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ abstract class GeneratorCommand extends BaseGeneratorCommand
*/
public function handle()
{
if (!$server = $this->getServerInput()) {
$server = $this->getServerInput();

if ($this->doesRequireServer() && empty($server)) {
$this->error('You must use the server option when you have more than one API.');
return 1;
}

if (is_null($this->getServerNamespace($server))) {
if (!empty($server) && is_null($this->getServerNamespace($server))) {
$this->error("Server {$server} does not exist in your jsonapi.servers configuration.");
return 1;
}
Expand Down Expand Up @@ -136,6 +138,18 @@ protected function getClassType(): string
return $this->classType;
}

/**
* Does the generator require a server to be specified?
*
* Child classes can overload this method if a server is not required.
*
* @return bool
*/
protected function doesRequireServer(): bool
{
return true;
}

/**
* Get the console command arguments.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Console/MakeAuthorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ protected function getStub()
return $this->resolveStubPath('authorizer.stub');
}

/**
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
if ($this->option('resource')) {
Expand All @@ -63,6 +67,14 @@ protected function getDefaultNamespace($rootNamespace)
return $rootNamespace . '\\' . $jsonApi . '\\' . 'Authorizers';
}

/**
* @inheritDoc
*/
protected function doesRequireServer(): bool
{
return true === $this->option('resource');
}

/**
* @inheritDoc
*/
Expand Down
22 changes: 15 additions & 7 deletions tests/lib/Integration/Console/MakeAuthorizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function tearDown(): void
$files->deleteDirectory(app_path('JsonApi'));
}

public function test(): void
public function testGeneric(): void
{
config()->set('jsonapi.servers', [
'v1' => Server::class,
Expand All @@ -57,20 +57,27 @@ public function test(): void
$result = $this->artisan('jsonapi:authorizer blog');

$this->assertSame(0, $result);
$this->assertAuthorizerCreated();
$this->assertGenericAuthorizerCreated();
}

public function testWithServer(): void
/**
* As a generic authorizer is not created in a server namespace, the
* developer shouldn't have to provide a server argument even if there
* are multiple servers.
*
* @see https://github.com/laravel-json-api/laravel/issues/34
*/
public function testGenericWithMultipleServers(): void
{
config()->set('jsonapi.servers', [
'beta' => 'App\JsonApi\Beta\Server',
'v1' => Server::class,
]);

$result = $this->artisan('jsonapi:authorizer blog --server v1');
$result = $this->artisan('jsonapi:authorizer blog');

$this->assertSame(0, $result);
$this->assertAuthorizerCreated();
$this->assertGenericAuthorizerCreated();
}

public function testResource(): void
Expand Down Expand Up @@ -121,8 +128,9 @@ public function testInvalidServer(): void
]);

$result = $this->artisan('jsonapi:authorizer', [
'name' => 'blog',
'name' => 'posts',
'--server' => 'v2',
'--resource' => true,
]);

$this->assertSame(1, $result);
Expand All @@ -132,7 +140,7 @@ public function testInvalidServer(): void
/**
* @return void
*/
private function assertAuthorizerCreated(): void
private function assertGenericAuthorizerCreated(): void
{
$this->assertFileExists($path = app_path('JsonApi/Authorizers/BlogAuthorizer.php'));
$content = file_get_contents($path);
Expand Down

0 comments on commit 0839fea

Please sign in to comment.