Skip to content

Commit

Permalink
Exclude softDeletes columns from factory definition (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary authored May 22, 2022
1 parent cb8db08 commit 7291204
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected function buildDefinition(Model $model)
* @var \Blueprint\Models\Column $column
*/
foreach ($fillable as $column) {
if ($column->name() === 'id') {
if (in_array($column->name(), ['id', 'softdeletes', 'softdeletestz'])) {
continue;
}

Expand Down Expand Up @@ -185,6 +185,10 @@ protected function buildDefinition(Model $model)
}
}

if (empty($definition)) {
return '//';
}

return trim($definition);
}

Expand All @@ -194,10 +198,15 @@ private function fillableColumns(array $columns): array
return $columns;
}

return array_filter(
$nonNullableColumns = array_filter(
$columns,
fn (Column $column) => !in_array('nullable', $column->modifiers())
);

return array_filter(
$nonNullableColumns,
fn (Column $column) => $column->dataType() !== 'softDeletes'
);
}

private function fullyQualifyModelReference(string $model_name)
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Generators/FactoryGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Blueprint\Blueprint;
use Blueprint\Generators\FactoryGenerator;
use Blueprint\Tree;
use Illuminate\Support\Facades\App;
use Tests\TestCase;

/**
Expand Down Expand Up @@ -215,6 +214,7 @@ public function modelTreeDataProvider()
['drafts/resource-statements.yaml', 'database/factories/UserFactory.php', 'factories/resource-statements.php'],
['drafts/factory-smallint-and-tinyint.yaml', 'database/factories/ModelFactory.php', 'factories/factory-smallint-and-tinyint.php'],
['drafts/all-column-types.yaml', 'database/factories/AllTypeFactory.php', 'factories/all-column-types.php'],
['drafts/shorthands.yaml', 'database/factories/NameFactory.php', 'factories/shorthands.php'],
];
}
}
29 changes: 29 additions & 0 deletions tests/fixtures/factories/shorthands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use App\Models\Name;

class NameFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Name::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
//
];
}
}

0 comments on commit 7291204

Please sign in to comment.