Skip to content

Commit 3185ee4

Browse files
authored
Ignore SoftDelete columns in Resources and Form Requests (#592)
1 parent 8db6637 commit 3185ee4

15 files changed

+158
-16
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,6 @@ protected function buildDefinition(Model $model)
182182
$column_definition .= '$table->id(';
183183
} elseif ($dataType === 'rememberToken') {
184184
$column_definition .= '$table->rememberToken(';
185-
} elseif ($dataType === 'softDeletes') {
186-
$column_definition .= '$table->softDeletes(';
187-
} elseif ($dataType === 'softDeletesTz') {
188-
$column_definition .= '$table->softDeletesTz(';
189185
} else {
190186
$column_definition .= '$table->' . $dataType . "('{$column->name()}'";
191187
}
@@ -279,6 +275,10 @@ protected function buildDefinition(Model $model)
279275
$definition .= self::INDENT . '$table->' . $model->timestampsDataType() . '();' . PHP_EOL;
280276
}
281277

278+
if ($model->usesSoftDeletes()) {
279+
$definition .= self::INDENT . '$table->' . $model->softDeletesDataType() . '();' . PHP_EOL;
280+
}
281+
282282
return trim($definition);
283283
}
284284

src/Generators/ModelGenerator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ protected function buildClassPhpDoc(Model $model)
9494
$phpDoc .= PHP_EOL;
9595
$phpDoc .= ' * @property string|null $' . $column->name() . '_type';
9696
$phpDoc .= PHP_EOL;
97-
} elseif (in_array($column->dataType(), ['softDeletesTz', 'softDeletes'])) {
98-
$phpDoc .= ' * @property \Carbon\Carbon $deleted_at';
99-
$phpDoc .= PHP_EOL;
10097
} else {
10198
$phpDoc .= sprintf(' * @property %s $%s', $this->phpDataType($column->dataType()), $column->name());
10299
$phpDoc .= PHP_EOL;
@@ -110,6 +107,11 @@ protected function buildClassPhpDoc(Model $model)
110107
$phpDoc .= PHP_EOL;
111108
}
112109

110+
if ($model->usesSoftDeletes()) {
111+
$phpDoc .= ' * @property \Carbon\Carbon $deleted_at';
112+
$phpDoc .= PHP_EOL;
113+
}
114+
113115
$phpDoc .= ' */';
114116

115117
return $phpDoc;

src/Lexers/ModelLexer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ private function buildModel(string $name, array $columns)
162162

163163
if (isset($columns['softdeletes'])) {
164164
$model->enableSoftDeletes();
165+
unset($columns['softdeletes']);
165166
} elseif (isset($columns['softdeletestz'])) {
166167
$model->enableSoftDeletes(true);
168+
unset($columns['softdeletestz']);
167169
}
168170

169171
if (isset($columns['relationships'])) {

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ public function output_generates_constraint_for_uuid()
637637
/**
638638
* @test
639639
*/
640-
public function output_respects_softdelete_order()
640+
public function output_softdelete_column_last()
641641
{
642642
$this->app->config->set('blueprint.use_constraints', true);
643643

tests/Feature/Generators/Statements/FormRequestGeneratorTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,68 @@ public function output_generates_test_for_controller_tree_using_cached_model()
280280

281281
$this->assertEquals(['created' => ['app/Http/Requests/UserStoreRequest.php']], $this->subject->output($tree));
282282
}
283+
284+
public function testOutputGeneratesFormRequestWithoutSoftdeletes(): void
285+
{
286+
$this->filesystem->expects('stub')
287+
->with('request.stub')
288+
->andReturn($this->stub('request.stub'));
289+
$this->filesystem->expects('exists')
290+
->twice()
291+
->with('app/Http/Requests')
292+
->andReturnFalse();
293+
$this->filesystem->expects('exists')
294+
->with('app/Http/Requests/ProjectStoreRequest.php')
295+
->andReturnFalse();
296+
$this->filesystem->expects('exists')
297+
->with('app/Http/Requests/ProjectUpdateRequest.php')
298+
->andReturnFalse();
299+
$this->filesystem->expects('makeDirectory')
300+
->twice()
301+
->with('app/Http/Requests', 0755, true);
302+
$this->filesystem->expects('put')
303+
->with('app/Http/Requests/ProjectStoreRequest.php', $this->fixture('form-requests/form-requests-softdeletes.php'));
304+
305+
$tokens = $this->blueprint->parse($this->fixture('drafts/form-requests-softdeletes.yaml'));
306+
$tree = $this->blueprint->analyze($tokens);
307+
308+
self::assertSame([
309+
'created' => [
310+
'app/Http/Requests/ProjectStoreRequest.php',
311+
'app/Http/Requests/ProjectUpdateRequest.php',
312+
],
313+
], $this->subject->output($tree));
314+
}
315+
316+
public function testOutputGeneratesFormRequestWithoutSoftdeletestz(): void
317+
{
318+
$this->filesystem->expects('stub')
319+
->with('request.stub')
320+
->andReturn($this->stub('request.stub'));
321+
$this->filesystem->expects('exists')
322+
->twice()
323+
->with('app/Http/Requests')
324+
->andReturnFalse();
325+
$this->filesystem->expects('exists')
326+
->with('app/Http/Requests/RepoStoreRequest.php')
327+
->andReturnFalse();
328+
$this->filesystem->expects('exists')
329+
->with('app/Http/Requests/RepoUpdateRequest.php')
330+
->andReturnFalse();
331+
$this->filesystem->expects('makeDirectory')
332+
->twice()
333+
->with('app/Http/Requests', 0755, true);
334+
$this->filesystem->expects('put')
335+
->with('app/Http/Requests/RepoUpdateRequest.php', $this->fixture('form-requests/form-requests-softdeletestz.php'));
336+
337+
$tokens = $this->blueprint->parse($this->fixture('drafts/form-requests-softdeletestz.yaml'));
338+
$tree = $this->blueprint->analyze($tokens);
339+
340+
self::assertSame([
341+
'created' => [
342+
'app/Http/Requests/RepoStoreRequest.php',
343+
'app/Http/Requests/RepoUpdateRequest.php',
344+
],
345+
], $this->subject->output($tree));
346+
}
283347
}

tests/Feature/Lexers/ModelLexerTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,9 @@ public function it_enables_soft_deletes()
384384
$this->assertTrue($model->usesSoftDeletes());
385385

386386
$columns = $model->columns();
387-
$this->assertCount(2, $columns);
387+
$this->assertCount(1, $columns);
388388
$this->assertEquals('id', $columns['id']->name());
389389
$this->assertEquals('id', $columns['id']->dataType());
390-
$this->assertEquals('softdeletes', $columns['softdeletes']->name());
391-
$this->assertEquals('softDeletes', $columns['softdeletes']->dataType());
392390
$this->assertEquals([], $columns['id']->modifiers());
393391
}
394392

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
models:
2+
Project:
3+
title: string
4+
softDeletes
5+
6+
controllers:
7+
Project:
8+
resource
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
models:
2+
Repo:
3+
title: string
4+
softDeletestz
5+
6+
controllers:
7+
Repo:
8+
resource
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class ProjectStoreRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
'title' => ['required', 'string'],
28+
];
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class RepoUpdateRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
'title' => ['required', 'string'],
28+
];
29+
}
30+
}

0 commit comments

Comments
 (0)