Mercurial > packages > magicforger
diff src/Generator/Factory/FactoryGenerator.php @ 39:b5c6ebd33547 ls_dev_2025_09
Improving the factories, tests, and requests
| author | Luka Sitas <sitas.luka.97@gmail.com> |
|---|---|
| date | Thu, 25 Sep 2025 23:16:13 -0400 |
| parents | 76584181267a |
| children | 2cf26b593f4a |
line wrap: on
line diff
--- a/src/Generator/Factory/FactoryGenerator.php Thu Sep 25 20:24:13 2025 -0400 +++ b/src/Generator/Factory/FactoryGenerator.php Thu Sep 25 23:16:13 2025 -0400 @@ -2,10 +2,10 @@ namespace Wizard\MagicForger\Generator\Factory; +use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; use Wizard\MagicForger\Generator\BaseGenerator; use Wizard\MagicForger\Helpers\RelationshipNavigator; -use Illuminate\Support\Str; #[AsCommand(name: 'mf:factory')] class FactoryGenerator extends BaseGenerator @@ -82,43 +82,45 @@ return str_replace(['App\\', '\\'], ['app/', '/'], $this->getFactoryNamespace().'/'.$this->factory_name($this->getTableInput()).'.php'); } - protected function gatherRelations() { - $relations = RelationshipNavigator::getRelations($this->getCurrentTable()); + protected function gatherRelations() + { + $relations = RelationshipNavigator::getRelations($this->getCurrentTable()); + + return $relations; - return $relations; - - } + } - protected function renderColumns() { + protected function renderColumns() + { $insert = ''; - $values = []; + $values = []; foreach ($this->get_columns() as $column) { if (in_array($column['name'], $this->columns_to_ignore)) { continue; } $type = $column['type_name']; - $nullable = ($column['nullable'] ? '->optional($weight = 0.5)' : '' ); - $value = 'fake()' . $nullable; - $name = $column['name']; + $nullable = ($column['nullable'] ? '->optional($weight = 0.5)' : ''); + $value = 'fake()'.$nullable; + $name = $column['name']; // Get the expected header name $replacements = [ - '{{value}}' => $value . '->text()', + '{{value}}' => $value.'->text()', '{{column_name}}' => $name, ]; // date if (in_array($type, ['date'])) { - $replacements['{{value}}'] = $value . '->date()'; + $replacements['{{value}}'] = $value.'->date()'; } // time if (in_array($type, ['timestamp'])) { - $replacements['{{value}}'] = $value . '->timestamp()'; + $replacements['{{value}}'] = $value.'->timestamp()'; } // checkbox if (in_array($type, ['tinyint'])) { - $replacements['{{value}}'] = $value . '->boolean()'; + $replacements['{{value}}'] = $value.'->numberBetween(0,1)'; } // select elseif (in_array($type, ['bigint']) && array_key_exists($name, $selects)) { @@ -127,10 +129,9 @@ } // bigint, float elseif (in_array($type, ['bigint', 'int'])) { - $replacements['{{value}}'] = $value . '->randomNumber()'; - } - elseif (in_array($type, ['float'])) { - $replacements['{{value}}'] = $value . '->randomFloat()'; + $replacements['{{value}}'] = $value.'->randomNumber()'; + } elseif (in_array($type, ['float'])) { + $replacements['{{value}}'] = $value.'->randomFloat()'; } else { // text area // varchar, , etc @@ -144,17 +145,15 @@ $snippet ); - // Replace placeholders with actual values + // Replace placeholders with actual values } - $insert = implode("\n", $values); + $insert = implode("\n", $values); - return $insert; - } + return $insert; + } /** * Get available insertions including model relationships. - * - * @return array */ public function get_available_inserts(): array { @@ -162,10 +161,10 @@ $inserts = parent::get_available_inserts(); // Gather and render relationships for this model - $columns = $this->renderColumns(); + $columns = $this->renderColumns(); // Assign to stub placeholders - $inserts['# {{ factoryInsertPoint }}'] = $columns; + $inserts['# {{ factoryInsertPoint }}'] = $columns; return $inserts; }
