From 6ad35a3a72ce2971bbfa77fcb97a53ee2284e03b Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Wed, 19 Jan 2022 18:46:11 -0500 Subject: [PATCH 01/11] Add tests Signed-off-by: Nathanael Esayeas --- .../Generators/MigrationGeneratorTest.php | 6 ++-- tests/Feature/Lexers/ConfigLexerTest.php | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 tests/Feature/Lexers/ConfigLexerTest.php diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index 5e159045..dfcb8e1c 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -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); } @@ -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')); @@ -221,8 +220,7 @@ public function output_creates_constraints_for_unconventional_foreign_reference_ ->with($model_migration, $this->fixture('migrations/relationships-constraints.php')); $tokens = $this->blueprint->parse($this->fixture('drafts/relationships.yaml')); - $tree = $this->blueprint->analyze($tokens); - + $tree = $this->blueprint->analyze($tokens + ['config' => ['use_constraints' => true]]); $this->assertEquals(['created' => [$model_migration]], $this->subject->output($tree)); } diff --git a/tests/Feature/Lexers/ConfigLexerTest.php b/tests/Feature/Lexers/ConfigLexerTest.php new file mode 100644 index 00000000..24a1c0ad --- /dev/null +++ b/tests/Feature/Lexers/ConfigLexerTest.php @@ -0,0 +1,36 @@ +subject = new ConfigLexer(); + } + + /** + * @test + */ + public function it_updates_config(): void + { + $tokens = ['config' => ['key' => 'value']]; + + $this->subject->analyze($tokens); + + $this->assertSame($tokens['config']['key'], config('blueprint.key')); + } +} From 6a01f592a9802c21bc8052d4618ba73bbfb864df Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Wed, 19 Jan 2022 18:46:30 -0500 Subject: [PATCH 02/11] Create ConfigLexer Signed-off-by: Nathanael Esayeas --- src/Lexers/ConfigLexer.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/Lexers/ConfigLexer.php diff --git a/src/Lexers/ConfigLexer.php b/src/Lexers/ConfigLexer.php new file mode 100644 index 00000000..f1b90146 --- /dev/null +++ b/src/Lexers/ConfigLexer.php @@ -0,0 +1,35 @@ +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 + ) + ); + } +} From 4a9c3342a0d32ba235db826c6284fff0df1b93e7 Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Wed, 19 Jan 2022 18:46:54 -0500 Subject: [PATCH 03/11] Register ConfigLexer Signed-off-by: Nathanael Esayeas --- src/BlueprintServiceProvider.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/BlueprintServiceProvider.php b/src/BlueprintServiceProvider.php index f088cd26..8c2a98f7 100644 --- a/src/BlueprintServiceProvider.php +++ b/src/BlueprintServiceProvider.php @@ -75,6 +75,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())); From 6ad099e1b0de120d0d2f5d08eacc53c9032ee69c Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Wed, 19 Jan 2022 20:27:01 -0500 Subject: [PATCH 04/11] Update test fixture Signed-off-by: Nathanael Esayeas --- tests/fixtures/drafts/readme-example.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/fixtures/drafts/readme-example.yaml b/tests/fixtures/drafts/readme-example.yaml index 7721ac72..61752f35 100644 --- a/tests/fixtures/drafts/readme-example.yaml +++ b/tests/fixtures/drafts/readme-example.yaml @@ -19,3 +19,7 @@ controllers: fire: NewPost with:post flash: post.title redirect: post.index + +config: + namespace: MyAppNamespace + models_namespace: MyAppModels From 93c9d42e70e937c256c9dc416aa4159f339308fb Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Wed, 19 Jan 2022 20:27:26 -0500 Subject: [PATCH 05/11] Additional test Signed-off-by: Nathanael Esayeas --- tests/Feature/BlueprintTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Feature/BlueprintTest.php b/tests/Feature/BlueprintTest.php index 70e7d318..1e155183 100644 --- a/tests/Feature/BlueprintTest.php +++ b/tests/Feature/BlueprintTest.php @@ -233,6 +233,10 @@ public function it_parses_the_readme_example() ], ], ], + 'config' => [ + 'namespace' => 'MyAppNamespace', + 'models_namespace' => 'MyAppModels' + ] ], $this->subject->parse($blueprint)); } @@ -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)); From 15c85ce5f4bc6ca67a6461b140e08e9c1b3aa3f2 Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Wed, 19 Jan 2022 20:43:17 -0500 Subject: [PATCH 06/11] Refactor Signed-off-by: Nathanael Esayeas --- src/Lexers/ConfigLexer.php | 1 + tests/Feature/Generators/MigrationGeneratorTest.php | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Lexers/ConfigLexer.php b/src/Lexers/ConfigLexer.php index f1b90146..1f49bc57 100644 --- a/src/Lexers/ConfigLexer.php +++ b/src/Lexers/ConfigLexer.php @@ -19,6 +19,7 @@ public function analyze(array $tokens): array if (array_key_exists('config', $tokens) && is_array($tokens['config'])) { $this->analyzeValue($tokens['config']); } + return []; } diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index dfcb8e1c..1f806072 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -205,6 +205,7 @@ 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')); @@ -220,7 +221,7 @@ public function output_creates_constraints_for_unconventional_foreign_reference_ ->with($model_migration, $this->fixture('migrations/relationships-constraints.php')); $tokens = $this->blueprint->parse($this->fixture('drafts/relationships.yaml')); - $tree = $this->blueprint->analyze($tokens + ['config' => ['use_constraints' => true]]); + $tree = $this->blueprint->analyze($tokens); $this->assertEquals(['created' => [$model_migration]], $this->subject->output($tree)); } From 60483faf51ae7919f8e59a5f7d2249a12e923182 Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Wed, 19 Jan 2022 21:08:04 -0500 Subject: [PATCH 07/11] Use inline config Signed-off-by: Nathanael Esayeas --- tests/Feature/Generators/MigrationGeneratorTest.php | 1 - tests/fixtures/drafts/relationships.yaml | 3 +++ tests/fixtures/migrations/relationships.php | 8 ++++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index 1f806072..e3fdf478 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -205,7 +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')); diff --git a/tests/fixtures/drafts/relationships.yaml b/tests/fixtures/drafts/relationships.yaml index 87037510..ec772a5b 100644 --- a/tests/fixtures/drafts/relationships.yaml +++ b/tests/fixtures/drafts/relationships.yaml @@ -2,3 +2,6 @@ models: Comment: post_id: id author_id: id:user + +config: + use_constraints: true diff --git a/tests/fixtures/migrations/relationships.php b/tests/fixtures/migrations/relationships.php index 8632b31f..bc1f18ff 100644 --- a/tests/fixtures/migrations/relationships.php +++ b/tests/fixtures/migrations/relationships.php @@ -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(); } /** From 61b6c29c136421ba4f3189ef50de200eae54c358 Mon Sep 17 00:00:00 2001 From: Ben James Date: Thu, 10 Feb 2022 23:32:42 +0000 Subject: [PATCH 08/11] Add some further tests Signed-off-by: Ben James --- tests/Feature/Lexers/ConfigLexerTest.php | 81 +++++++++++++++++++ .../drafts/controller-configured.yaml | 15 ++++ .../drafts/relationships-configured.yaml | 9 +++ 3 files changed, 105 insertions(+) create mode 100644 tests/fixtures/drafts/controller-configured.yaml create mode 100644 tests/fixtures/drafts/relationships-configured.yaml diff --git a/tests/Feature/Lexers/ConfigLexerTest.php b/tests/Feature/Lexers/ConfigLexerTest.php index 24a1c0ad..7993d61f 100644 --- a/tests/Feature/Lexers/ConfigLexerTest.php +++ b/tests/Feature/Lexers/ConfigLexerTest.php @@ -2,7 +2,13 @@ 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; /** @@ -10,6 +16,8 @@ */ class ConfigLexerTest extends TestCase { + private $blueprint; + /** * @var ConfigLexer */ @@ -20,6 +28,16 @@ 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); + } /** @@ -33,4 +51,67 @@ public function it_updates_config(): void $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)); + + } } diff --git a/tests/fixtures/drafts/controller-configured.yaml b/tests/fixtures/drafts/controller-configured.yaml new file mode 100644 index 00000000..e223d235 --- /dev/null +++ b/tests/fixtures/drafts/controller-configured.yaml @@ -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 diff --git a/tests/fixtures/drafts/relationships-configured.yaml b/tests/fixtures/drafts/relationships-configured.yaml new file mode 100644 index 00000000..39d28ba7 --- /dev/null +++ b/tests/fixtures/drafts/relationships-configured.yaml @@ -0,0 +1,9 @@ +models: + Comment: + post_id: id + author_id: id:user + +config: + use_constraints: true + app_path: atum + namespace: Some\App From 3e22bf5ef3732ea49952e3982d64afce0050e6ea Mon Sep 17 00:00:00 2001 From: Ben James Date: Thu, 10 Feb 2022 23:47:19 +0000 Subject: [PATCH 09/11] fix: code style Signed-off-by: Ben James --- tests/Feature/Lexers/ConfigLexerTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/Feature/Lexers/ConfigLexerTest.php b/tests/Feature/Lexers/ConfigLexerTest.php index 7993d61f..05ba002d 100644 --- a/tests/Feature/Lexers/ConfigLexerTest.php +++ b/tests/Feature/Lexers/ConfigLexerTest.php @@ -37,7 +37,6 @@ protected function setUp(): void $this->blueprint->registerLexer(new ConfigLexer()); $this->blueprint->registerLexer(new ControllerLexer(new StatementLexer())); $this->blueprint->registerGenerator($this->modelGenerator); - } /** @@ -85,7 +84,6 @@ public function it_uses_app_path_and_namespace_config_from_yaml_override(): void $tree = $this->blueprint->analyze($tokens); $this->assertEquals(['created' => ['atum/Models/Comment.php']], $this->modelGenerator->output($tree)); - } /** @@ -112,6 +110,5 @@ public function it_uses_controller_namespace_config_from_yaml_override() $tree = $this->blueprint->analyze($tokens); $this->assertEquals(['created' => ['shift/Other/Http/UserController.php']], $this->controllerGenerator->output($tree)); - } } From e0207ed7faa90c4028e2289210f43cde3d2f231f Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Wed, 5 Oct 2022 13:52:46 -0400 Subject: [PATCH 10/11] Revert changes to exsisting test cases --- tests/Feature/BlueprintTest.php | 8 -------- tests/Feature/Generators/MigrationGeneratorTest.php | 4 +++- tests/fixtures/drafts/readme-example.yaml | 4 ---- tests/fixtures/drafts/relationships.yaml | 3 --- tests/fixtures/migrations/relationships.php | 8 ++------ 5 files changed, 5 insertions(+), 22 deletions(-) diff --git a/tests/Feature/BlueprintTest.php b/tests/Feature/BlueprintTest.php index 1e155183..70e7d318 100644 --- a/tests/Feature/BlueprintTest.php +++ b/tests/Feature/BlueprintTest.php @@ -233,10 +233,6 @@ public function it_parses_the_readme_example() ], ], ], - 'config' => [ - 'namespace' => 'MyAppNamespace', - 'models_namespace' => 'MyAppModels' - ] ], $this->subject->parse($blueprint)); } @@ -280,10 +276,6 @@ 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)); diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index a373407b..86d49f9e 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -28,7 +28,6 @@ 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); } @@ -205,6 +204,8 @@ 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')); @@ -221,6 +222,7 @@ 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)); } diff --git a/tests/fixtures/drafts/readme-example.yaml b/tests/fixtures/drafts/readme-example.yaml index 61752f35..7721ac72 100644 --- a/tests/fixtures/drafts/readme-example.yaml +++ b/tests/fixtures/drafts/readme-example.yaml @@ -19,7 +19,3 @@ controllers: fire: NewPost with:post flash: post.title redirect: post.index - -config: - namespace: MyAppNamespace - models_namespace: MyAppModels diff --git a/tests/fixtures/drafts/relationships.yaml b/tests/fixtures/drafts/relationships.yaml index ec772a5b..87037510 100644 --- a/tests/fixtures/drafts/relationships.yaml +++ b/tests/fixtures/drafts/relationships.yaml @@ -2,6 +2,3 @@ models: Comment: post_id: id author_id: id:user - -config: - use_constraints: true diff --git a/tests/fixtures/migrations/relationships.php b/tests/fixtures/migrations/relationships.php index 881bc17c..c6990a0e 100644 --- a/tests/fixtures/migrations/relationships.php +++ b/tests/fixtures/migrations/relationships.php @@ -13,16 +13,12 @@ */ public function up() { - Schema::disableForeignKeyConstraints(); - Schema::create('comments', function (Blueprint $table) { $table->id(); - $table->foreignId('post_id')->constrained()->cascadeOnDelete()->cascadeOnUpdate(); - $table->foreignId('author_id')->constrained('users')->cascadeOnDelete()->cascadeOnUpdate(); + $table->unsignedBigInteger('post_id'); + $table->unsignedBigInteger('author_id'); $table->timestamps(); }); - - Schema::enableForeignKeyConstraints(); } /** From 5c727865a494a462a9c9d0b5d7f56234cd0707c6 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Wed, 5 Oct 2022 13:55:34 -0400 Subject: [PATCH 11/11] Formatting --- tests/Feature/Lexers/ConfigLexerTest.php | 2 +- tests/fixtures/drafts/controller-configured.yaml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Feature/Lexers/ConfigLexerTest.php b/tests/Feature/Lexers/ConfigLexerTest.php index 05ba002d..1df029d3 100644 --- a/tests/Feature/Lexers/ConfigLexerTest.php +++ b/tests/Feature/Lexers/ConfigLexerTest.php @@ -54,7 +54,7 @@ public function it_updates_config(): void /** * @test */ - public function it_uses_app_path_and_namespace_config_from_yaml_override(): void + public function it_uses_app_path_and_namespace_from_inline_configuration(): void { $this->filesystem->expects('stub') ->with('model.class.stub') diff --git a/tests/fixtures/drafts/controller-configured.yaml b/tests/fixtures/drafts/controller-configured.yaml index e223d235..f7cf6b87 100644 --- a/tests/fixtures/drafts/controller-configured.yaml +++ b/tests/fixtures/drafts/controller-configured.yaml @@ -9,6 +9,7 @@ controllers: save: user flash: user.name redirect: post.index + config: app_path: shift namespace: Some\App