Skip to content

Commit

Permalink
Ignore SoftDelete columns in Resources and Form Requests (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostwriter authored Jan 5, 2023
1 parent 8db6637 commit 3185ee4
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()}'";
}
Expand Down Expand Up @@ -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);
}

Expand Down
8 changes: 5 additions & 3 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/Lexers/ModelLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Generators/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
64 changes: 64 additions & 0 deletions tests/Feature/Generators/Statements/FormRequestGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
4 changes: 1 addition & 3 deletions tests/Feature/Lexers/ModelLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/drafts/form-requests-softdeletes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
models:
Project:
title: string
softDeletes

controllers:
Project:
resource
8 changes: 8 additions & 0 deletions tests/fixtures/drafts/form-requests-softdeletestz.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
models:
Repo:
title: string
softDeletestz

controllers:
Repo:
resource
30 changes: 30 additions & 0 deletions tests/fixtures/form-requests/form-requests-softdeletes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ProjectStoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => ['required', 'string'],
];
}
}
30 changes: 30 additions & 0 deletions tests/fixtures/form-requests/form-requests-softdeletestz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class RepoUpdateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => ['required', 'string'],
];
}
}
2 changes: 1 addition & 1 deletion tests/fixtures/migrations/soft-deletes-respect-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/migrations/soft-deletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/migrations/with-timezones.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->softDeletesTz();
$table->timestampsTz();
$table->softDeletesTz();
});
}

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/models/soft-deletes-phpdoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 3185ee4

Please sign in to comment.