From 2178bba0ee8654efc7f868ca51d058a18421e974 Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Thu, 5 Jan 2023 17:03:01 -0600 Subject: [PATCH 1/7] Add draft files Signed-off-by: Nathanael Esayeas --- tests/fixtures/drafts/form-requests-softdeletes.yaml | 8 ++++++++ tests/fixtures/drafts/form-requests-softdeletestz.yaml | 8 ++++++++ 2 files changed, 16 insertions(+) create mode 100644 tests/fixtures/drafts/form-requests-softdeletes.yaml create mode 100644 tests/fixtures/drafts/form-requests-softdeletestz.yaml diff --git a/tests/fixtures/drafts/form-requests-softdeletes.yaml b/tests/fixtures/drafts/form-requests-softdeletes.yaml new file mode 100644 index 00000000..b3bddd7a --- /dev/null +++ b/tests/fixtures/drafts/form-requests-softdeletes.yaml @@ -0,0 +1,8 @@ +models: + Project: + title: string + softDeletes + +controllers: + Project: + resource diff --git a/tests/fixtures/drafts/form-requests-softdeletestz.yaml b/tests/fixtures/drafts/form-requests-softdeletestz.yaml new file mode 100644 index 00000000..6556061e --- /dev/null +++ b/tests/fixtures/drafts/form-requests-softdeletestz.yaml @@ -0,0 +1,8 @@ +models: + Repo: + title: string + softDeletestz + +controllers: + Repo: + resource From 65c8e6a3ade4911e6417cddc845b46c55402aa91 Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Thu, 5 Jan 2023 17:04:17 -0600 Subject: [PATCH 2/7] Add test fixtures Signed-off-by: Nathanael Esayeas --- .../form-requests-softdeletes.php | 30 +++++++++++++++++++ .../form-requests-softdeletestz.php | 30 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 tests/fixtures/form-requests/form-requests-softdeletes.php create mode 100644 tests/fixtures/form-requests/form-requests-softdeletestz.php diff --git a/tests/fixtures/form-requests/form-requests-softdeletes.php b/tests/fixtures/form-requests/form-requests-softdeletes.php new file mode 100644 index 00000000..fbd1ebb9 --- /dev/null +++ b/tests/fixtures/form-requests/form-requests-softdeletes.php @@ -0,0 +1,30 @@ + ['required', 'string'], + ]; + } +} diff --git a/tests/fixtures/form-requests/form-requests-softdeletestz.php b/tests/fixtures/form-requests/form-requests-softdeletestz.php new file mode 100644 index 00000000..6291d9a1 --- /dev/null +++ b/tests/fixtures/form-requests/form-requests-softdeletestz.php @@ -0,0 +1,30 @@ + ['required', 'string'], + ]; + } +} From 29a6d57725d72d5b76d652151aa60ab71116bb23 Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Thu, 5 Jan 2023 17:09:58 -0600 Subject: [PATCH 3/7] Add tests Signed-off-by: Nathanael Esayeas --- .../Statements/FormRequestGeneratorTest.php | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tests/Feature/Generators/Statements/FormRequestGeneratorTest.php b/tests/Feature/Generators/Statements/FormRequestGeneratorTest.php index 49ceaf65..1356857c 100644 --- a/tests/Feature/Generators/Statements/FormRequestGeneratorTest.php +++ b/tests/Feature/Generators/Statements/FormRequestGeneratorTest.php @@ -280,4 +280,68 @@ public function output_generates_test_for_controller_tree_using_cached_model() $this->assertEquals(['created' => ['app/Http/Requests/UserStoreRequest.php']], $this->subject->output($tree)); } + + public function testOutputGeneratesFormRequestWithoutSoftdeletes(): void + { + $this->filesystem->expects('stub') + ->with('request.stub') + ->andReturn($this->stub('request.stub')); + $this->filesystem->expects('exists') + ->twice() + ->with('app/Http/Requests') + ->andReturnFalse(); + $this->filesystem->expects('exists') + ->with('app/Http/Requests/ProjectStoreRequest.php') + ->andReturnFalse(); + $this->filesystem->expects('exists') + ->with('app/Http/Requests/ProjectUpdateRequest.php') + ->andReturnFalse(); + $this->filesystem->expects('makeDirectory') + ->twice() + ->with('app/Http/Requests', 0755, true); + $this->filesystem->expects('put') + ->with('app/Http/Requests/ProjectStoreRequest.php', $this->fixture('form-requests/form-requests-softdeletes.php')); + + $tokens = $this->blueprint->parse($this->fixture('drafts/form-requests-softdeletes.yaml')); + $tree = $this->blueprint->analyze($tokens); + + self::assertSame([ + 'created' => [ + 'app/Http/Requests/ProjectStoreRequest.php', + 'app/Http/Requests/ProjectUpdateRequest.php', + ], + ], $this->subject->output($tree)); + } + + public function testOutputGeneratesFormRequestWithoutSoftdeletestz(): void + { + $this->filesystem->expects('stub') + ->with('request.stub') + ->andReturn($this->stub('request.stub')); + $this->filesystem->expects('exists') + ->twice() + ->with('app/Http/Requests') + ->andReturnFalse(); + $this->filesystem->expects('exists') + ->with('app/Http/Requests/RepoStoreRequest.php') + ->andReturnFalse(); + $this->filesystem->expects('exists') + ->with('app/Http/Requests/RepoUpdateRequest.php') + ->andReturnFalse(); + $this->filesystem->expects('makeDirectory') + ->twice() + ->with('app/Http/Requests', 0755, true); + $this->filesystem->expects('put') + ->with('app/Http/Requests/RepoUpdateRequest.php', $this->fixture('form-requests/form-requests-softdeletestz.php')); + + $tokens = $this->blueprint->parse($this->fixture('drafts/form-requests-softdeletestz.yaml')); + $tree = $this->blueprint->analyze($tokens); + + self::assertSame([ + 'created' => [ + 'app/Http/Requests/RepoStoreRequest.php', + 'app/Http/Requests/RepoUpdateRequest.php', + ], + ], $this->subject->output($tree)); + } } From 2dd2752591a7551b6ce1d173390e0ae151198d5d Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Thu, 5 Jan 2023 17:16:12 -0600 Subject: [PATCH 4/7] Drop softdeletes and softdeletestz columns Signed-off-by: Nathanael Esayeas --- src/Lexers/ModelLexer.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Lexers/ModelLexer.php b/src/Lexers/ModelLexer.php index 8f2fcb7d..b4b13ab0 100644 --- a/src/Lexers/ModelLexer.php +++ b/src/Lexers/ModelLexer.php @@ -162,8 +162,10 @@ private function buildModel(string $name, array $columns) if (isset($columns['softdeletes'])) { $model->enableSoftDeletes(); + unset($columns['softdeletes']); } elseif (isset($columns['softdeletestz'])) { $model->enableSoftDeletes(true); + unset($columns['softdeletestz']); } if (isset($columns['relationships'])) { From 87716df10254d4a3474160a9700b935eccc1a433 Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Thu, 5 Jan 2023 17:16:36 -0600 Subject: [PATCH 5/7] Fix doc comment order Signed-off-by: Nathanael Esayeas --- src/Generators/ModelGenerator.php | 8 +++++--- tests/fixtures/models/soft-deletes-phpdoc.php | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 2ae65f55..8660aaa5 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -94,9 +94,6 @@ protected function buildClassPhpDoc(Model $model) $phpDoc .= PHP_EOL; $phpDoc .= ' * @property string|null $' . $column->name() . '_type'; $phpDoc .= PHP_EOL; - } elseif (in_array($column->dataType(), ['softDeletesTz', 'softDeletes'])) { - $phpDoc .= ' * @property \Carbon\Carbon $deleted_at'; - $phpDoc .= PHP_EOL; } else { $phpDoc .= sprintf(' * @property %s $%s', $this->phpDataType($column->dataType()), $column->name()); $phpDoc .= PHP_EOL; @@ -110,6 +107,11 @@ protected function buildClassPhpDoc(Model $model) $phpDoc .= PHP_EOL; } + if ($model->usesSoftDeletes()) { + $phpDoc .= ' * @property \Carbon\Carbon $deleted_at'; + $phpDoc .= PHP_EOL; + } + $phpDoc .= ' */'; return $phpDoc; diff --git a/tests/fixtures/models/soft-deletes-phpdoc.php b/tests/fixtures/models/soft-deletes-phpdoc.php index b2f68cea..97802ab3 100644 --- a/tests/fixtures/models/soft-deletes-phpdoc.php +++ b/tests/fixtures/models/soft-deletes-phpdoc.php @@ -9,9 +9,9 @@ /** * @property int $id * @property int $post_id - * @property \Carbon\Carbon $deleted_at * @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $updated_at + * @property \Carbon\Carbon $deleted_at */ class Comment extends Model { From 1f7337e6217f2e16dc614db0e7932223919ea20e Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Thu, 5 Jan 2023 17:17:54 -0600 Subject: [PATCH 6/7] Output softdeletes columns last Signed-off-by: Nathanael Esayeas --- src/Generators/MigrationGenerator.php | 8 ++++---- tests/Feature/Generators/MigrationGeneratorTest.php | 2 +- tests/fixtures/migrations/soft-deletes-respect-order.php | 2 +- tests/fixtures/migrations/soft-deletes.php | 2 +- .../migrations/with-path-prefix-table-name-region.php | 2 +- tests/fixtures/migrations/with-timezones.php | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index b7a85274..6e87f080 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -182,10 +182,6 @@ protected function buildDefinition(Model $model) $column_definition .= '$table->id('; } elseif ($dataType === 'rememberToken') { $column_definition .= '$table->rememberToken('; - } elseif ($dataType === 'softDeletes') { - $column_definition .= '$table->softDeletes('; - } elseif ($dataType === 'softDeletesTz') { - $column_definition .= '$table->softDeletesTz('; } else { $column_definition .= '$table->' . $dataType . "('{$column->name()}'"; } @@ -279,6 +275,10 @@ protected function buildDefinition(Model $model) $definition .= self::INDENT . '$table->' . $model->timestampsDataType() . '();' . PHP_EOL; } + if ($model->usesSoftDeletes()) { + $definition .= self::INDENT . '$table->' . $model->softDeletesDataType() . '();' . PHP_EOL; + } + return trim($definition); } diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index 5ca2e673..d5c0826b 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -637,7 +637,7 @@ public function output_generates_constraint_for_uuid() /** * @test */ - public function output_respects_softdelete_order() + public function output_softdelete_column_last() { $this->app->config->set('blueprint.use_constraints', true); diff --git a/tests/fixtures/migrations/soft-deletes-respect-order.php b/tests/fixtures/migrations/soft-deletes-respect-order.php index 7796571e..4148a71f 100644 --- a/tests/fixtures/migrations/soft-deletes-respect-order.php +++ b/tests/fixtures/migrations/soft-deletes-respect-order.php @@ -16,9 +16,9 @@ public function up() Schema::create('comments', function (Blueprint $table) { $table->id(); $table->string('title'); - $table->softDeletes(); $table->string('name'); $table->timestamps(); + $table->softDeletes(); }); } diff --git a/tests/fixtures/migrations/soft-deletes.php b/tests/fixtures/migrations/soft-deletes.php index 8b2f1a69..11c6284d 100644 --- a/tests/fixtures/migrations/soft-deletes.php +++ b/tests/fixtures/migrations/soft-deletes.php @@ -16,8 +16,8 @@ public function up() Schema::create('comments', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('post_id'); - $table->softDeletes(); $table->timestamps(); + $table->softDeletes(); }); } diff --git a/tests/fixtures/migrations/with-path-prefix-table-name-region.php b/tests/fixtures/migrations/with-path-prefix-table-name-region.php index dbeec666..e35a5d21 100644 --- a/tests/fixtures/migrations/with-path-prefix-table-name-region.php +++ b/tests/fixtures/migrations/with-path-prefix-table-name-region.php @@ -16,8 +16,8 @@ public function up() Schema::create('regions', function (Blueprint $table) { $table->id(); $table->string('name_en', 255)->nullable(); - $table->softDeletes(); $table->timestamps(); + $table->softDeletes(); }); } diff --git a/tests/fixtures/migrations/with-timezones.php b/tests/fixtures/migrations/with-timezones.php index b1bfb17f..d6c1ec9a 100644 --- a/tests/fixtures/migrations/with-timezones.php +++ b/tests/fixtures/migrations/with-timezones.php @@ -15,8 +15,8 @@ public function up() { Schema::create('comments', function (Blueprint $table) { $table->id(); - $table->softDeletesTz(); $table->timestampsTz(); + $table->softDeletesTz(); }); } From 9196e26adb5d78a96ad4fc65805187d7ea0090de Mon Sep 17 00:00:00 2001 From: Nathanael Esayeas Date: Thu, 5 Jan 2023 17:19:39 -0600 Subject: [PATCH 7/7] Refactor old test Signed-off-by: Nathanael Esayeas --- tests/Feature/Lexers/ModelLexerTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/Feature/Lexers/ModelLexerTest.php b/tests/Feature/Lexers/ModelLexerTest.php index da28f488..73c08d73 100644 --- a/tests/Feature/Lexers/ModelLexerTest.php +++ b/tests/Feature/Lexers/ModelLexerTest.php @@ -384,11 +384,9 @@ public function it_enables_soft_deletes() $this->assertTrue($model->usesSoftDeletes()); $columns = $model->columns(); - $this->assertCount(2, $columns); + $this->assertCount(1, $columns); $this->assertEquals('id', $columns['id']->name()); $this->assertEquals('id', $columns['id']->dataType()); - $this->assertEquals('softdeletes', $columns['softdeletes']->name()); - $this->assertEquals('softDeletes', $columns['softdeletes']->dataType()); $this->assertEquals([], $columns['id']->modifiers()); }