# HG changeset patch # User Luka Sitas # Date 1758856573 14400 # Node ID b5c6ebd3354725c77892b24390568f95bb1cbed5 # Parent c062f013fd19aa93b1cf0eed38ba462e7ee0f675 Improving the factories, tests, and requests diff -r c062f013fd19 -r b5c6ebd33547 src/Generator/Factory/FactoryGenerator.php --- 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; } diff -r c062f013fd19 -r b5c6ebd33547 src/Generator/Factory/snippets/value.stub --- a/src/Generator/Factory/snippets/value.stub Thu Sep 25 20:24:13 2025 -0400 +++ b/src/Generator/Factory/snippets/value.stub Thu Sep 25 23:16:13 2025 -0400 @@ -1,2 +1,1 @@ '{{column_name}}' => {{value}}, - diff -r c062f013fd19 -r b5c6ebd33547 src/Generator/Requests/FilterRequestGenerator.php --- a/src/Generator/Requests/FilterRequestGenerator.php Thu Sep 25 20:24:13 2025 -0400 +++ b/src/Generator/Requests/FilterRequestGenerator.php Thu Sep 25 23:16:13 2025 -0400 @@ -74,4 +74,19 @@ { return str_replace(['App\\', '\\'], ['app/', '/'], $this->getRequestNamespace($this->getTableInput()).'/'.$this->filter_request_name($this->getTableInput()).'.php'); } + + /** + * Get available insertions including model relationships. + * + * @return array + */ + public function get_available_inserts(): array + { + // Merge parent insertions (attributes, fillable, etc.) + $inserts = parent::get_available_inserts(); + + $inserts['// {{ valuesForValidation }}'] = str_replace("'required'", "'nullable'", $inserts['// {{ valuesForValidation }}']); + + return $inserts; + } } diff -r c062f013fd19 -r b5c6ebd33547 src/Generator/Test/stubs/test.stub --- a/src/Generator/Test/stubs/test.stub Thu Sep 25 20:24:13 2025 -0400 +++ b/src/Generator/Test/stubs/test.stub Thu Sep 25 23:16:13 2025 -0400 @@ -105,7 +105,7 @@ ->assertRedirect(route('{{ tableName }}.edit', $item)); // Make sure no entry was added - $this->assertDatabaseHas('games', array_intersect(array_flip($item->getFillable()), $item->toArray())); + $this->assertDatabaseHas('{{ tableName }}', array_intersect_key($item->toArray(), array_flip($item->getFillable()))); } public function test_{{ modelVariable }}_crud_delete_usage(): void diff -r c062f013fd19 -r b5c6ebd33547 src/Replacer/TableReplacer.php --- a/src/Replacer/TableReplacer.php Thu Sep 25 20:24:13 2025 -0400 +++ b/src/Replacer/TableReplacer.php Thu Sep 25 23:16:13 2025 -0400 @@ -51,15 +51,15 @@ if (in_array($column['name'], $this->columns_to_ignore)) { continue; } - $type = $column['type_name']; - //date - if(in_array($type, ['date'])) { - $insert .= sprintf("'%s' => 'date:Y-m-d',", $column['name'])."\n"; - } - //time - if(in_array($type, ['timestamp'])) { - $insert .= sprintf("'%s' => 'date:Y-m-d H:i',", $column['name'])."\n"; - } + $type = $column['type_name']; + // date + if (in_array($type, ['date'])) { + $insert .= sprintf("'%s' => 'date:Y-m-d',", $column['name'])."\n"; + } + // time + if (in_array($type, ['timestamp'])) { + $insert .= sprintf("'%s' => 'date:Y-m-d H:i',", $column['name'])."\n"; + } } return $insert; @@ -104,10 +104,81 @@ { $insert = ''; foreach ($this->get_columns() as $column) { + // Don't do validation on some of the basic columns if (in_array($column['name'], $this->columns_to_ignore)) { continue; } - $insert .= sprintf("'%s' => 'nullable',", $column['name'])."\n"; + + $options = []; + + // Add default value checks if needed + if (is_null($column['default']) && ! $column['nullable']) { + $options[] = "'required'"; + } + else { + $options[] = "'nullable'"; + } + + // Determine the validations based on column type + $type = strtolower($column['type_name']); + $size = (preg_match('/\((\d+)\)/', $column['type'], $matches)) ? $matches[1] : null; + + switch ($type) { + case 'varchar': + case 'char': + $options[] = "'string'"; + + // Extract length if defined + if (! is_null($size)) { + $options[] = "'max:$size'"; + } + break; + + case 'text': + $options[] = "'string'"; + break; + + case 'int': + case 'tinyint': + case 'smallint': + case 'mediumint': + case 'bigint': + $options[] = "'integer'"; + if (strpos($column['type'], 'unsigned') !== false) { + $options[] = "'min:0'"; + } + if (! is_null($size)) { + $options[] = "'max_digits:".$size."'"; + } + break; + + case 'decimal': + case 'double': + case 'float': + $options[] = "'numeric'"; + // Extract precision and scale if defined + if (preg_match('/\((\d+),(\d+)\)/', $column['type'], $matches)) { + $precision = $matches[1]; + $scale = isset($matches[2]) ? ','.$matches[2] : ''; + + $options[] = "'decimal:$precision$scale'"; + } + break; + + case 'boolean': + $options[] = "'boolean'"; + break; + + case 'date': + case 'datetime': + case 'timestamp': + $options[] = "'date'"; + break; + + // Add other types as needed + } + + $insert .= sprintf("'%s' => [%s],\n", $column['name'], implode(',', $options)); } return $insert;