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
4 changes: 2 additions & 2 deletions src/Generators/TestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ protected function buildTestCases(Controller $controller)
$assertions['sanity'][] = '$this->assertCount(1, $' . $plural . ');';
$assertions['sanity'][] = sprintf('$%s = $%s->first();', $variable, $plural);
} else {
$assertions['generic'][] = '$this->assertDatabaseHas(' . Str::lower(Str::plural($model)) . ', [ /* ... */ ]);';
$assertions['generic'][] = '$this->assertDatabaseHas(' . Str::camel(Str::plural($model)) . ', [ /* ... */ ]);';
}
} elseif ($statement->operation() === 'find') {
$setup['data'][] = sprintf('$%s = factory(%s::class)->create();', $variable, $model);
Expand All @@ -351,7 +351,7 @@ protected function buildTestCases(Controller $controller)
}
}

$call = sprintf('$response = $this->%s(route(\'%s.%s\'', $this->httpMethodForAction($name), Str::lower($context), $name);
$call = sprintf('$response = $this->%s(route(\'%s.%s\'', $this->httpMethodForAction($name), Str::kebab($context), $name);

if (in_array($name, ['edit', 'update', 'show', 'destroy'])) {
$call .= ', $' . Str::camel($context);
Expand Down
2 changes: 1 addition & 1 deletion src/Lexers/ControllerLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private function generateResourceTokens(Controller $controller, array $methods)

return str_replace(
['[singular]', '[plural]'],
[Str::lower($model), Str::lower(Str::plural($model))],
[Str::camel($model), Str::camel(Str::plural($model))],
$statement
);
}),
Expand Down
8 changes: 4 additions & 4 deletions src/Models/Statements/EloquentStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public function output(string $controller_prefix, string $context): string

if ($this->operation() == 'save') {
if ($context === 'store') {
$code = "$" . Str::lower($model);
$code = "$" . Str::camel($model);
$code .= ' = ';
$code .= $model;
$code .= '::create($request->all());';
} else {
$code = "$" . Str::lower($model) . '->save();';
$code = "$" . Str::camel($model) . '->save();';
}
}

Expand All @@ -54,7 +54,7 @@ public function output(string $controller_prefix, string $context): string
$model = $this->extractModel();
}

$code = "$" . Str::lower($model);
$code = "$" . Str::camel($model);
$code .= ' = ';
$code .= $model;
$code .= '::find($' . $this->columnName($this->reference()) . ');';
Expand All @@ -66,7 +66,7 @@ public function output(string $controller_prefix, string $context): string
$code .= '::destroy($' . str_replace('.', '->', $this->reference()) . ');';
} else {
// TODO: only for certain contexts or no matter what given simple reference?
$code = "$" . Str::lower($model) . '->delete();';
$code = "$" . Str::camel($model) . '->delete();';
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Models/Statements/QueryStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public function output(string $controller): string

if ($this->operation() === 'all') {
if (is_null($this->model())) {
return '$' . Str::lower(Str::plural($model)) . ' = ' . $model . '::all();';
return '$' . Str::camel(Str::plural($model)) . ' = ' . $model . '::all();';
} else {
return '$' . Str::lower($this->clauses()[0]) . ' = ' . $this->model() . '::all();';
return '$' . Str::camel($this->clauses()[0]) . ' = ' . $this->model() . '::all();';
}
}

Expand All @@ -82,9 +82,9 @@ public function output(string $controller): string
if ($this->operation() === 'pluck') {
$variable_name = $this->pluckName($pluck_field);
} elseif ($this->operation() === 'count') {
$variable_name = Str::lower($model) . '_count';
$variable_name = Str::camel($model) . '_count';
} else {
$variable_name = Str::lower(Str::plural($model));
$variable_name = Str::camel(Str::plural($model));
}

$code = '$' . $variable_name . ' = ' . $model . '::';
Expand Down
37 changes: 35 additions & 2 deletions tests/Feature/Generator/ControllerGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public function output_writes_migration_for_controller_tree($definition, $path,
}

/**
* @test
*/
* @test
*/
public function output_generates_controllers_with_models_with_custom_namespace_correctly()
{
$definition = 'definitions/custom-models-namespace.bp';
Expand All @@ -99,6 +99,39 @@ public function output_generates_controllers_with_models_with_custom_namespace_c

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

/**
* @test
*/
public function output_works_for_pascal_case_definition()
{
$this->files->expects('stub')
->with('controller/class.stub')
->andReturn(file_get_contents('stubs/controller/class.stub'));
$this->files->expects('stub')
->with('controller/method.stub')
->andReturn(file_get_contents('stubs/controller/method.stub'))
->twice();

$certificateController = 'app/Http/Controllers/CertificateController.php';
$certificateTypeController = 'app/Http/Controllers/CertificateTypeController.php';

$this->files->expects('exists')
->with(dirname($certificateController))
->andReturnTrue();
$this->files->expects('put')
->with($certificateController, $this->fixture('controllers/certificate-controller.php'));

$this->files->expects('exists')
->with(dirname($certificateTypeController))
->andReturnTrue();
$this->files->expects('put')
->with($certificateTypeController, $this->fixture('controllers/certificate-type-controller.php'));

$tokens = $this->blueprint->parse($this->fixture('definitions/pascal-case.bp'));
$tree = $this->blueprint->analyze($tokens);
$this->assertEquals(['created' => [$certificateController, $certificateTypeController]], $this->subject->output($tree));
}

/**
* @test
Expand Down
42 changes: 42 additions & 0 deletions tests/Feature/Generator/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,48 @@ public function output_generates_models($definition, $path, $model)
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
}

/**
* @test
*/
public function output_works_for_pascal_case_definition()
{
$this->files->expects('stub')
->with('model/class.stub')
->andReturn(file_get_contents('stubs/model/class.stub'));
$this->files->expects('stub')
->with('model/fillable.stub')
->andReturn(file_get_contents('stubs/model/fillable.stub'))
->twice();
$this->files->expects('stub')
->with('model/casts.stub')
->andReturn(file_get_contents('stubs/model/casts.stub'))
->twice();
$this->files->expects('stub')
->with('model/method.stub')
->andReturn(file_get_contents('stubs/model/method.stub'))
->twice();

$certificateModel = 'app/Certificate.php';
$certificateTypeModel = 'app/CertificateType.php';

$this->files->expects('exists')
->with(dirname($certificateModel))
->andReturnTrue();
$this->files->expects('put')
->with($certificateModel, $this->fixture('models/certificate-pascal-case-example.php'));

$this->files->expects('exists')
->with(dirname($certificateTypeModel))
->andReturnTrue();
$this->files->expects('put')
->with($certificateTypeModel, $this->fixture('models/certificate-type-pascal-case-example.php'));

$tokens = $this->blueprint->parse($this->fixture('definitions/pascal-case.bp'));
$tree = $this->blueprint->analyze($tokens);

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

/**
* @test
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/Feature/Generator/TestGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,39 @@ public function output_generates_test_for_controller_tree($definition, $path, $t
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
}

/**
* @test
*/
public function output_works_for_pascal_case_definition()
{
$this->files->expects('get')
->with('stubs/test/class.stub')
->andReturn(file_get_contents('stubs/test/class.stub'));

$this->files->expects('get')
->with('stubs/test/case.stub')
->andReturn(file_get_contents('stubs/test/case.stub'));

$certificateControllerTest = 'tests/Feature/Http/Controllers/CertificateControllerTest.php';
$certificateTypeControllerTest = 'tests/Feature/Http/Controllers/CertificateTypeControllerTest.php';

$this->files->expects('exists')
->with(dirname($certificateControllerTest))
->andReturnTrue();
$this->files->expects('put')
->with($certificateControllerTest, $this->fixture('tests/certificate-pascal-case-example.php'));

$this->files->expects('exists')
->with(dirname($certificateTypeControllerTest))
->andReturnTrue();
$this->files->expects('put')
->with($certificateTypeControllerTest, $this->fixture('tests/certificate-type-pascal-case-example.php'));

$tokens = $this->blueprint->parse($this->fixture('definitions/pascal-case.bp'));
$tree = $this->blueprint->analyze($tokens);
$this->assertEquals(['created' => [$certificateControllerTest, $certificateTypeControllerTest]], $this->subject->output($tree));
}

/**
* @test
*/
Expand Down
67 changes: 67 additions & 0 deletions tests/fixtures/controllers/certificate-controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Http\Controllers;

use App\Certificate;
use App\Http\Requests\CertificateStoreRequest;
use App\Http\Requests\CertificateUpdateRequest;
use App\Http\Resources\Certificate as CertificateResource;
use App\Http\Resources\CertificateCollection;
use Illuminate\Http\Request;

class CertificateController extends Controller
{
/**
* @param \Illuminate\Http\Request $request
* @return \App\Http\Resources\CertificateCollection
*/
public function index(Request $request)
{
$certificates = Certificate::all();

return new CertificateCollection($certificates);
}

/**
* @param \App\Http\Requests\CertificateStoreRequest $request
* @return \App\Http\Resources\Certificate
*/
public function store(CertificateStoreRequest $request)
{
$certificate = Certificate::create($request->all());

return new CertificateResource($certificate);
}

/**
* @param \Illuminate\Http\Request $request
* @param \App\Certificate $certificate
* @return \App\Http\Resources\Certificate
*/
public function show(Request $request, Certificate $certificate)
{
return new CertificateResource($certificate);
}

/**
* @param \App\Http\Requests\CertificateUpdateRequest $request
* @param \App\Certificate $certificate
* @return \App\Http\Resources\Certificate
*/
public function update(CertificateUpdateRequest $request, Certificate $certificate)
{
return new CertificateResource($certificate);
}

/**
* @param \Illuminate\Http\Request $request
* @param \App\Certificate $certificate
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request, Certificate $certificate)
{
$certificate->delete();

return response()->noContent(200);
}
}
67 changes: 67 additions & 0 deletions tests/fixtures/controllers/certificate-type-controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Http\Controllers;

use App\CertificateType;
use App\Http\Requests\CertificateTypeStoreRequest;
use App\Http\Requests\CertificateTypeUpdateRequest;
use App\Http\Resources\CertificateType as CertificateTypeResource;
use App\Http\Resources\CertificateTypeCollection;
use Illuminate\Http\Request;

class CertificateTypeController extends Controller
{
/**
* @param \Illuminate\Http\Request $request
* @return \App\Http\Resources\CertificateTypeCollection
*/
public function index(Request $request)
{
$certificateTypes = CertificateType::all();

return new CertificateTypeCollection($certificateTypes);
}

/**
* @param \App\Http\Requests\CertificateTypeStoreRequest $request
* @return \App\Http\Resources\CertificateType
*/
public function store(CertificateTypeStoreRequest $request)
{
$certificateType = CertificateType::create($request->all());

return new CertificateTypeResource($certificateType);
}

/**
* @param \Illuminate\Http\Request $request
* @param \App\CertificateType $certificateType
* @return \App\Http\Resources\CertificateType
*/
public function show(Request $request, CertificateType $certificateType)
{
return new CertificateTypeResource($certificateType);
}

/**
* @param \App\Http\Requests\CertificateTypeUpdateRequest $request
* @param \App\CertificateType $certificateType
* @return \App\Http\Resources\CertificateType
*/
public function update(CertificateTypeUpdateRequest $request, CertificateType $certificateType)
{
return new CertificateTypeResource($certificateType);
}

/**
* @param \Illuminate\Http\Request $request
* @param \App\CertificateType $certificateType
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request, CertificateType $certificateType)
{
$certificateType->delete();

return response()->noContent(200);
}
}
18 changes: 18 additions & 0 deletions tests/fixtures/definitions/pascal-case.bp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
models:
Certificate:
name: string
certificate_type_id: id
reference: string
document: string
expiry_date: date
remarks: nullable text
CertificateType:
name: string
relationships:
hasMany: Certificate

controllers:
Certificate:
resource: api
CertificateType:
resource: api
Loading