Skip to content

Commit

Permalink
Fix resource always generating API routes (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary authored May 22, 2020
1 parent 3c638a5 commit cb93d7c
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 32 deletions.
21 changes: 11 additions & 10 deletions src/Generators/RouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@ public function output(array $tree): array
return [];
}

$routes = '';
$path = 'routes/web.php';
$routes = ['api' => '', 'web' => ''];

/** @var \Blueprint\Models\Controller $controller */
foreach ($tree['controllers'] as $controller) {
$routes .= PHP_EOL . PHP_EOL . $this->buildRoutes($controller);

if ($controller->isApiResource()) {
$path = 'routes/api.php';
}
$type = $controller->isApiResource() ? 'api' : 'web';
$routes[$type] .= PHP_EOL . PHP_EOL . $this->buildRoutes($controller);
}
$routes .= PHP_EOL;

$this->files->append($path, $routes);
$paths = [];
foreach (array_filter($routes) as $type => $definitions) {
$path = 'routes/' . $type . '.php';
$this->files->append($path, $definitions . PHP_EOL);
$paths[] = $path;
}

return ['updated' => [$path]];
return ['updated' => $paths];
}

protected function buildRoutes(Controller $controller)
Expand Down
23 changes: 10 additions & 13 deletions src/Lexers/ControllerLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ public function analyze(array $tokens): array
foreach ($tokens['controllers'] as $name => $definition) {
$controller = new Controller($name);

if ($this->isResource($definition)) {
$original = $definition;
$definition = $this->generateResourceTokens($controller, $this->methodsForResource($definition['resource']));
// unset shorthand
unset($original['resource']);
// this gives the ability to both use a shorthand and override some methods
$definition = array_merge($definition, $original);
$controller->setApiResource(true);
if (isset($definition['resource'])) {
$resource_definition = $this->generateResourceTokens($controller, $this->methodsForResource($definition['resource']));

if ($definition['resource'] === 'api') {
$controller->setApiResource(true);
}

unset($definition['resource']);

$definition = array_merge($resource_definition, $definition);
}

foreach ($definition as $method => $body) {
Expand All @@ -49,11 +51,6 @@ public function analyze(array $tokens): array
return $registry;
}

private function isResource(array $definition)
{
return isset($definition['resource']) && is_string($definition['resource']);
}

private function generateResourceTokens(Controller $controller, array $methods)
{
return collect($this->resourceTokens())
Expand Down
30 changes: 21 additions & 9 deletions tests/Feature/Generator/RouteGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function setUp(): void
/**
* @test
*/
public function output_writes_nothing_for_empty_tree()
public function output_generates_nothing_for_empty_tree()
{
$this->files->shouldNotHaveReceived('append');

Expand All @@ -45,7 +45,7 @@ public function output_writes_nothing_for_empty_tree()
* @test
* @dataProvider controllerTreeDataProvider
*/
public function output_writes_migration_for_route_tree($definition, $routes)
public function output_generates_web_routes($definition, $routes)
{
$path = 'routes/web.php';
$this->files->expects('append')
Expand All @@ -60,19 +60,31 @@ public function output_writes_migration_for_route_tree($definition, $routes)
/**
* @test
*/
public function output_writes_migration_for_route_tree_api_routes()
public function output_generates_api_routes()
{
$definition = "definitions/api-routes-example.bp";
$routes = "routes/api-routes.php";
$path = 'routes/api.php';
$this->files->expects('append')
->with('routes/api.php', $this->fixture('routes/api-routes.php'));

$tokens = $this->blueprint->parse($this->fixture('definitions/api-routes-example.bp'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['updated' => ['routes/api.php']], $this->subject->output($tree));
}

/**
* @test
*/
public function output_generates_routes_for_mixed_resources()
{
$this->files->expects('append')
->with($path, $this->fixture($routes));
->with('routes/api.php', $this->fixture('routes/multiple-resource-controllers-api.php'));
$this->files->expects('append')
->with('routes/web.php', $this->fixture('routes/multiple-resource-controllers-web.php'));

$tokens = $this->blueprint->parse($this->fixture($definition));
$tokens = $this->blueprint->parse($this->fixture('definitions/multiple-resource-controllers.bp'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['updated' => [$path]], $this->subject->output($tree));
$this->assertEquals(['updated' => ['routes/api.php', 'routes/web.php']], $this->subject->output($tree));
}

public function controllerTreeDataProvider()
Expand Down
49 changes: 49 additions & 0 deletions tests/Feature/Lexers/ControllerLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,53 @@ public function it_returns_a_resource_controller_with_overrides()
$this->assertCount(1, $methods['custom']);
$this->assertEquals('custom-statements', $methods['custom'][0]);
}

/**
* @test
*/
public function it_returns_a_resource_controllers_with_api_flag_set()
{
$tokens = [
'controllers' => [
'Page' => [
'resource' => 'web',
],
'File' => [
'resource' => 'api',
],
'Category' => [
'resource' => 'web',
],
'Gallery' => [
'resource' => 'api',
],
]
];

$this->statementLexer->shouldReceive('analyze');

$actual = $this->subject->analyze($tokens);

$this->assertCount(4, $actual['controllers']);

$controller = $actual['controllers']['Page'];
$this->assertEquals('PageController', $controller->className());
$this->assertCount(7, $controller->methods());
$this->assertFalse($controller->isApiResource());

$controller = $actual['controllers']['File'];
$this->assertEquals('FileController', $controller->className());
$this->assertCount(5, $controller->methods());
$this->assertTrue($controller->isApiResource());

$controller = $actual['controllers']['Category'];
$this->assertEquals('CategoryController', $controller->className());
$this->assertCount(7, $controller->methods());
$this->assertFalse($controller->isApiResource());

$controller = $actual['controllers']['Gallery'];
$this->assertEquals('GalleryController', $controller->className());
$this->assertCount(5, $controller->methods());
$this->assertTrue($controller->isApiResource());
}
}
35 changes: 35 additions & 0 deletions tests/fixtures/definitions/multiple-resource-controllers.bp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
models:
Page:
page_name: string
page_slug: string
page_content: longtext
order: integer nullable

File:
file_name: string
file_path: text
relationships:
belongsToMany: Category

Category:
category_name: string
relationships:
belongsToMany: File

Gallery:
gallery_name: string
images: longtext
order: integer nullable

controllers:
Page:
resource

File:
resource: api

Category:
resource

Gallery:
resource: api
5 changes: 5 additions & 0 deletions tests/fixtures/routes/multiple-resource-controllers-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


Route::apiResource('file', 'FileController');

Route::apiResource('gallery', 'GalleryController');
5 changes: 5 additions & 0 deletions tests/fixtures/routes/multiple-resource-controllers-web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


Route::resource('page', 'PageController');

Route::resource('category', 'CategoryController');

0 comments on commit cb93d7c

Please sign in to comment.