Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Blueprint\Blueprint;
use Blueprint\Contracts\Generator;
use Blueprint\Models\Model;
use Blueprint\Models\Controller;
use Blueprint\Models\Statements\DispatchStatement;
use Blueprint\Models\Statements\EloquentStatement;
Expand All @@ -27,6 +28,8 @@ class ControllerGenerator implements Generator

private $imports = [];

private $models = [];

public function __construct($files)
{
$this->files = $files;
Expand All @@ -38,6 +41,8 @@ public function output(array $tree): array

$stub = $this->files->stub('controller/class.stub');

$this->registerModels($tree);

/** @var \Blueprint\Models\Controller $controller */
foreach ($tree['controllers'] as $controller) {
$this->addImport($controller, 'Illuminate\\Http\\Request');
Expand Down Expand Up @@ -198,6 +203,34 @@ private function fullyQualifyModelReference(string $sub_namespace, string $model
// TODO: get model_name from tree.
// If not found, assume parallel namespace as controller.
// Use respond-statement.php as test case.

/** @var \Blueprint\Models\Model $model */
$model = $this->modelForContext($model_name);

if (isset($this->models[Str::studly($model_name)])) {
return $model->fullyQualifiedClassName();
}

return config('blueprint.namespace') . '\\' . ($sub_namespace ? $sub_namespace . '\\' : '') . $model_name;
}

private function modelForContext(string $context)
{
if (isset($this->models[Str::studly($context)])) {
return $this->models[Str::studly($context)];
}

$matches = array_filter(array_keys($this->models), function ($key) use ($context) {
return Str::endsWith($key, '/'.Str::studly($context));
});

if (count($matches) === 1) {
return $this->models[$matches[0]];
}
}

private function registerModels(array $tree)
{
$this->models = array_merge($tree['cache'] ?? [], $tree['models'] ?? []);
}
}
2 changes: 1 addition & 1 deletion src/Generators/RouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function buildRoutes(Controller $controller)
$routes = '';
$methods = array_keys($controller->methods());

$className = $controller->className();
$className = str_replace('App\Http\Controllers\\', '', $controller->fullyQualifiedClassName());
$slug = Str::kebab($controller->prefix());

$resource_methods = array_intersect($methods, Controller::$resourceMethods);
Expand Down
11 changes: 10 additions & 1 deletion src/Generators/TestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected function buildTestCases(Controller $controller)
} elseif ($statement instanceof ValidateStatement) {
$this->addTestAssertionsTrait($controller);

$class = $controller->name() . Str::studly($name) . 'Request';
$class = $this->buildFormRequestName($controller, $name);
$test_case = $this->buildFormRequestTestCase($controller->fullyQualifiedClassName(), $name, config('blueprint.namespace') . '\\Http\\Requests\\' . $class) . PHP_EOL . PHP_EOL . $test_case;

if ($statement->data()) {
Expand Down Expand Up @@ -438,6 +438,15 @@ private function determineModel(string $prefix, ?string $reference)
return Str::studly($reference);
}

private function buildFormRequestName(Controller $controller, string $name)
{
if (empty($controller->namespace())) {
return $controller->name() . Str::studly($name) . 'Request';
}

return $controller->namespace() .'\\'. $controller->name() . Str::studly($name) . 'Request';
}

private function buildFormRequestTestCase(string $controller, string $action, string $form_request)
{
return <<< END
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Generator/ControllerGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected function setUp(): void
$this->subject = new ControllerGenerator($this->files);

$this->blueprint = new Blueprint();
$this->blueprint->registerLexer(new \Blueprint\Lexers\ModelLexer());
$this->blueprint->registerLexer(new \Blueprint\Lexers\ControllerLexer(new StatementLexer()));
$this->blueprint->registerGenerator($this->subject);
}
Expand Down Expand Up @@ -66,7 +67,6 @@ public function output_writes_migration_for_controller_tree($definition, $path,

$tokens = $this->blueprint->parse($this->fixture($definition));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
}

Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Generator/RouteGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function controllerTreeDataProvider()
['definitions/readme-example.bp', 'routes/readme-example.php'],
['definitions/cruddy.bp', 'routes/cruddy.php'],
['definitions/non-cruddy.bp', 'routes/non-cruddy.php'],
['definitions/respond-statements.bp', 'routes/respond-statements.php'],
];
}
}
7 changes: 4 additions & 3 deletions tests/fixtures/controllers/respond-statements.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace App\Http\Controllers\Api;

use App\Api\Post;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\PostStoreRequest;
use App\Post;
use Illuminate\Http\Request;

class PostController extends Controller
Expand All @@ -20,10 +21,10 @@ public function index(Request $request)
}

/**
* @param \Illuminate\Http\Request $request
* @param \App\Http\Requests\Api\PostStoreRequest $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
public function store(PostStoreRequest $request)
{
return response()->noContent();
}
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/definitions/respond-statements.bp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ controllers:
query: all
respond: posts
store:
validate: title
respond: 204
error:
respond: 400
4 changes: 4 additions & 0 deletions tests/fixtures/routes/respond-statements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@


Route::resource('post', 'Api\PostController')->only('index', 'store');
Route::get('post/error', 'Api\PostController@error');
22 changes: 20 additions & 2 deletions tests/fixtures/tests/respond-statements.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

use App\Post;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use JMac\Testing\Traits\AdditionalAssertions;
use Tests\TestCase;

/**
* @see \App\Http\Controllers\Api\PostController
*/
class PostControllerTest extends TestCase
{
use RefreshDatabase;
use AdditionalAssertions, RefreshDatabase, WithFaker;

/**
* @test
Expand All @@ -27,12 +29,28 @@ public function index_responds_with()
}


/**
* @test
*/
public function store_uses_form_request_validation()
{
$this->assertActionUsesFormRequest(
\App\Http\Controllers\Api\PostController::class,
'store',
\App\Http\Requests\Api\PostStoreRequest::class
);
}

/**
* @test
*/
public function store_responds_with()
{
$response = $this->post(route('post.store'));
$title = $this->faker->sentence(4);

$response = $this->post(route('post.store'), [
'title' => $title,
]);

$response->assertNoContent();
}
Expand Down