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
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
)
);
}
}
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_from_inline_configuration(): 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));
}
}
16 changes: 16 additions & 0 deletions tests/fixtures/drafts/controller-configured.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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
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