Skip to content
1 change: 1 addition & 0 deletions src/BlueprintServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function register()

$this->app->singleton(Blueprint::class, function ($app) {
$blueprint = new Blueprint();
$blueprint->registerLexer(new \Blueprint\Lexers\ConfigLexer($app));
$blueprint->registerLexer(new \Blueprint\Lexers\ModelLexer());
$blueprint->registerLexer(new \Blueprint\Lexers\SeederLexer());
$blueprint->registerLexer(new \Blueprint\Lexers\ControllerLexer(new \Blueprint\Lexers\StatementLexer()));
Expand Down
36 changes: 36 additions & 0 deletions src/Lexers/ConfigLexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Blueprint\Lexers;

use Blueprint\Contracts\Lexer;
use Illuminate\Container\Container;

class ConfigLexer implements Lexer
{
private $app;

public function __construct(Container $app = null)
{
$this->app = $app ?? Container::getInstance();
}

public function analyze(array $tokens): array
{
if (array_key_exists('config', $tokens) && is_array($tokens['config'])) {
$this->analyzeValue($tokens['config']);
}

return [];
}

private function analyzeValue(array $config): void
{
$this->app['config']->set(
'blueprint',
array_merge(
$this->app['config']->get('blueprint'),
$config
)
);
}
}
8 changes: 8 additions & 0 deletions tests/Feature/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ public function it_parses_the_readme_example()
],
],
],
'config' => [
'namespace' => 'MyAppNamespace',
'models_namespace' => 'MyAppModels'
]
], $this->subject->parse($blueprint));
}

Expand Down Expand Up @@ -276,6 +280,10 @@ public function it_parses_the_readme_example_with_different_platform_eols()
],
],
],
'config' => [
'namespace' => 'MyAppNamespace',
'models_namespace' => 'MyAppModels'
]
];

$this->assertEquals($expected, $this->subject->parse($definition_mac_eol));
Expand Down
4 changes: 1 addition & 3 deletions tests/Feature/Generators/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected function setUp(): void
$this->subject = new MigrationGenerator($this->files);

$this->blueprint = new Blueprint();
$this->blueprint->registerLexer(new \Blueprint\Lexers\ConfigLexer());
$this->blueprint->registerLexer(new \Blueprint\Lexers\ModelLexer());
$this->blueprint->registerGenerator($this->subject);
}
Expand Down Expand Up @@ -204,8 +205,6 @@ public function output_proper_pascal_case_model_names()
*/
public function output_creates_constraints_for_unconventional_foreign_reference_migration()
{
$this->app->config->set('blueprint.use_constraints', true);

$this->filesystem->expects('stub')
->with('migration.stub')
->andReturn($this->stub('migration.stub'));
Expand All @@ -222,7 +221,6 @@ public function output_creates_constraints_for_unconventional_foreign_reference_

$tokens = $this->blueprint->parse($this->fixture('drafts/relationships.yaml'));
$tree = $this->blueprint->analyze($tokens);

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

Expand Down
114 changes: 114 additions & 0 deletions tests/Feature/Lexers/ConfigLexerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace Tests\Feature\Lexers;

use Blueprint\Blueprint;
use Blueprint\Generators\ControllerGenerator;
use Blueprint\Generators\ModelGenerator;
use Blueprint\Lexers\ConfigLexer;
use Blueprint\Lexers\ControllerLexer;
use Blueprint\Lexers\ModelLexer;
use Blueprint\Lexers\StatementLexer;
use Tests\TestCase;

/**
* @see ConfigLexer
*/
class ConfigLexerTest extends TestCase
{
private $blueprint;

/**
* @var ConfigLexer
*/
private $subject;

protected function setUp(): void
{
parent::setUp();

$this->subject = new ConfigLexer();

$this->modelGenerator = new ModelGenerator($this->filesystem);
$this->controllerGenerator = new ControllerGenerator($this->filesystem);

$this->blueprint = new Blueprint();
$this->blueprint->registerLexer(new ModelLexer());
$this->blueprint->registerLexer(new ConfigLexer());
$this->blueprint->registerLexer(new ControllerLexer(new StatementLexer()));
$this->blueprint->registerGenerator($this->modelGenerator);
}

/**
* @test
*/
public function it_updates_config(): void
{
$tokens = ['config' => ['key' => 'value']];

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

$this->assertSame($tokens['config']['key'], config('blueprint.key'));
}

/**
* @test
*/
public function it_uses_app_path_and_namespace_config_from_yaml_override(): void
{
$this->filesystem->expects('stub')
->with('model.class.stub')
->andReturn($this->stub('model.class.stub'));

$this->filesystem->expects('stub')
->with('model.fillable.stub')
->andReturn($this->stub('model.fillable.stub'));

$this->filesystem->expects('stub')
->with('model.casts.stub')
->andReturn($this->stub('model.casts.stub'));

$this->filesystem->expects('stub')
->with('model.method.stub')
->andReturn($this->stub('model.method.stub'));

$this->filesystem->expects('exists')
->with('atum/Models')
->andReturnFalse();
$this->filesystem->expects('makeDirectory')
->with('atum/Models', 0755, true);
$this->filesystem->expects('put')
->with('atum/Models/Comment.php', $this->fixture('models/model-configured.php'));

$tokens = $this->blueprint->parse($this->fixture('drafts/relationships-configured.yaml'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['created' => ['atum/Models/Comment.php']], $this->modelGenerator->output($tree));
}

/**
* @test
*/
public function it_uses_controller_namespace_config_from_yaml_override()
{
$this->filesystem->expects('stub')
->with('controller.class.stub')
->andReturn($this->stub('controller.class.stub'));
$this->filesystem->expects('stub')
->with('controller.method.stub')
->andReturn($this->stub('controller.method.stub'));

$this->filesystem->expects('exists')
->with('shift/Other/Http')
->andReturnFalse();
$this->filesystem->expects('makeDirectory')
->with('shift/Other/Http', 0755, true);
$this->filesystem->expects('put')
->with('shift/Other/Http/UserController.php', $this->fixture('controllers/controller-configured.php'));

$tokens = $this->blueprint->parse($this->fixture('drafts/controller-configured.yaml'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['created' => ['shift/Other/Http/UserController.php']], $this->controllerGenerator->output($tree));
}
}
15 changes: 15 additions & 0 deletions tests/fixtures/drafts/controller-configured.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
controllers:
User:
index:
query: all:users
render: user.index with:users

store:
validate: name
save: user
flash: user.name
redirect: post.index
config:
app_path: shift
namespace: Some\App
controllers_namespace: Other\Http
4 changes: 4 additions & 0 deletions tests/fixtures/drafts/readme-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ controllers:
fire: NewPost with:post
flash: post.title
redirect: post.index

config:
namespace: MyAppNamespace
models_namespace: MyAppModels
9 changes: 9 additions & 0 deletions tests/fixtures/drafts/relationships-configured.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
models:
Comment:
post_id: id
author_id: id:user

config:
use_constraints: true
app_path: atum
namespace: Some\App
3 changes: 3 additions & 0 deletions tests/fixtures/drafts/relationships.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ models:
Comment:
post_id: id
author_id: id:user

config:
use_constraints: true
8 changes: 6 additions & 2 deletions tests/fixtures/migrations/relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ class CreateCommentsTable extends Migration
*/
public function up()
{
Schema::disableForeignKeyConstraints();

Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('post_id');
$table->unsignedBigInteger('author_id');
$table->foreignId('post_id')->constrained()->cascadeOnDelete()->cascadeOnUpdate();
$table->foreignId('author_id')->constrained('users')->cascadeOnDelete()->cascadeOnUpdate();
$table->timestamps();
});

Schema::enableForeignKeyConstraints();
}

/**
Expand Down