Skip to content

Commit 70a2e83

Browse files
authored
Implement migration generator (#22)
* Rename test code method names for better readability * Implement migration generator * Add a clean command to simplify testing * Remove `multiLineString()` migration row * Create `GenerateFactories` and `GenerateMigrations` commands * Class comments and commands message better to look nicer * Add unused resolvers to be compatible old laravel versions * Create test code for commands
1 parent 17f4a03 commit 70a2e83

33 files changed

+623
-17
lines changed

README.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ bin/console database
6363
# Generate database seed from database in `dist/database/seeders` folder
6464
```
6565

66+
### Generate `Factory`s
67+
68+
\[TODO]
69+
70+
### Generate `migration`
71+
72+
\[TODO]
73+
6674
## How to contribute
6775

6876
### Development
@@ -127,7 +135,6 @@ Schema::create('xeeds', function (Blueprint $table) {
127135
$table->mediumInteger('medium_integer');
128136
$table->mediumText('medium_text');
129137
$table->morphs('morphs');
130-
$table->multiLineString('multi_line_string');
131138
$table->multiPoint('multi_point');
132139
$table->multiPolygon('multi_polygon');
133140
$table->nullableMorphs('nullable_morphs');
@@ -164,6 +171,23 @@ Schema::create('xeeds', function (Blueprint $table) {
164171
}
165172
```
166173

174+
### Helpful commands
175+
176+
If you are going to test this package yourself, then you would use the following commands to clean up generated files.
177+
178+
```sh
179+
bin/console clean
180+
# Clean generated files, seeders, models, factories and migration files.
181+
#=> Refer the below
182+
Please select directory for you to want to clean.
183+
[0] seeder
184+
[1] model
185+
[2] factory
186+
[3] migration
187+
[4] all
188+
[5] exit
189+
```
190+
167191
### Issue a bug report and Pull Request
168192
169193
The opportunity for you to create issues and pull requests makes me happy. Feel free to contribute and submit pull requests whenever you need.
@@ -184,6 +208,10 @@ It uses the built-in SQLite database, not your own database. It will never cause
184208
185209
```sh
186210
composer test
211+
# All tests
212+
213+
composer testgen
214+
# All generator tests
187215
```
188216
189217
## License

bin/console

+6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
require __DIR__.'/../vendor/autoload.php';
55

6+
use Cable8mm\Xeed\Command\CleanCommand;
67
use Cable8mm\Xeed\Command\GenerateDatabaseSeederCommand;
8+
use Cable8mm\Xeed\Command\GenerateFactoriesCommand;
9+
use Cable8mm\Xeed\Command\GenerateMigrationsCommand;
710
use Cable8mm\Xeed\Command\GenerateModelsCommand;
811
use Cable8mm\Xeed\Command\GenerateSeedersCommand;
912
use Cable8mm\Xeed\Command\ImportXeedCommand;
@@ -17,5 +20,8 @@ $application->add(new GenerateModelsCommand());
1720
$application->add(new GenerateSeedersCommand());
1821
$application->add(new GenerateDatabaseSeederCommand());
1922
$application->add(new ImportXeedCommand());
23+
$application->add(new CleanCommand());
24+
$application->add(new GenerateFactoriesCommand());
25+
$application->add(new GenerateMigrationsCommand());
2026

2127
$application->run();

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
4747
],
4848
"test": "./vendor/bin/phpunit tests",
49+
"testgen": "./vendor/bin/phpunit --testsuite Generator",
4950
"lint": "./vendor/bin/pint",
5051
"inspect": "./vendor/bin/pint --test",
5152
"apidoc": "rm -rf build; doctum.phar update doctum.php --output-format=github --no-ansi --no-progress;"

phpunit.xml.dist

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
<testsuite name="Feature">
1818
<directory>tests/Feature</directory>
1919
</testsuite>
20+
<testsuite name="Generator">
21+
<directory>tests/Unit/Generators</directory>
22+
</testsuite>
2023
</testsuites>
2124

2225
<source>

resources/tests/2024_03_15_034619_create_xeeds_table.php.test

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ return new class extends Migration
3939
$table->mediumInteger('medium_integer');
4040
$table->mediumText('medium_text');
4141
$table->morphs('morphs');
42-
$table->multiLineString('multi_line_string');
4342
$table->multiPoint('multi_point');
4443
$table->multiPolygon('multi_polygon');
4544
$table->nullableMorphs('nullable_morphs');

resources/tests/xeeds.mysql.sql

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ CREATE TABLE `xeeds` (
4848
`medium_text` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL,
4949
`morphs_type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
5050
`morphs_id` bigint unsigned NOT NULL,
51-
`multi_line_string` multilinestring NOT NULL,
5251
`multi_point` multipoint NOT NULL,
5352
`multi_polygon` multipolygon NOT NULL,
5453
`nullable_morphs_type` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,

resources/tests/xeeds.sqlite.sql

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ CREATE TABLE "xeeds" (
4545
"medium_text" text not null,
4646
"morphs_type" varchar not null,
4747
"morphs_id" integer not null,
48-
"multi_line_string" multilinestring not null,
4948
"multi_point" multipoint not null,
5049
"multi_polygon" multipolygon not null,
5150
"nullable_morphs_type" varchar,

src/Column.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Cable8mm\Xeed;
44

5+
use Stringable;
6+
57
/**
68
* Database Column Object.
79
*/
8-
final class Column
10+
final class Column implements Stringable
911
{
1012
/**
1113
* Column constructor.
@@ -95,4 +97,14 @@ public function fake(): string
9597
{
9698
return ResolverSelector::of($this)->fake();
9799
}
100+
101+
public function migration(): string
102+
{
103+
return ResolverSelector::of($this)->migration();
104+
}
105+
106+
public function __toString()
107+
{
108+
return 'Field is '.$this->field.' and type is '.$this->type.'.';
109+
}
98110
}

src/Command/CleanCommand.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Cable8mm\Xeed\Command;
4+
5+
use Cable8mm\Xeed\Support\Path;
6+
use Symfony\Component\Console\Attribute\AsCommand;
7+
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use Symfony\Component\Console\Question\ChoiceQuestion;
11+
12+
/**
13+
* Clean generated files, seeders, models, factories and migration files.
14+
*
15+
* run `bin/console clean`
16+
*/
17+
#[AsCommand(
18+
name: 'clean',
19+
description: 'Clean generated files, seeders, models, factories and migration files. run `bin/console clean`',
20+
hidden: false
21+
)]
22+
class CleanCommand extends Command
23+
{
24+
/**
25+
* Run the console command.
26+
*/
27+
protected function execute(InputInterface $input, OutputInterface $output): int
28+
{
29+
$helper = $this->getHelper('question');
30+
31+
$question = new ChoiceQuestion(
32+
'Please select directory for you to want to clean.',
33+
['seeder', 'model', 'factory', 'migration', 'all', 'exit'],
34+
'exit'
35+
);
36+
37+
$question->setErrorMessage('Type %s is invalid.');
38+
39+
/** @disregard P1013 */
40+
$thing = $helper->ask($input, $output, $question);
41+
42+
$output->writeln('You have just selected: '.$thing);
43+
44+
$path = match ($thing) {
45+
'seeder' => [Path::seeder()],
46+
'model' => [Path::model()],
47+
'factory' => [Path::factory()],
48+
'migration' => [Path::migration()],
49+
'all' => [
50+
Path::seeder(),
51+
Path::model(),
52+
Path::factory(),
53+
Path::migration(),
54+
],
55+
default => null,
56+
};
57+
58+
if ($path === null) {
59+
$output->writeln('See you later!');
60+
61+
return Command::SUCCESS;
62+
}
63+
64+
foreach ($path as $path) {
65+
array_map('unlink', array_filter((array) glob($path.'*.php')));
66+
$output->writeln($path.' was cleaned. Enjoy it.');
67+
}
68+
69+
return Command::SUCCESS;
70+
}
71+
}

src/Command/GenerateDatabaseSeederCommand.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Cable8mm\Xeed\DB;
66
use Cable8mm\Xeed\Generators\DatabaseSeederGenerator;
7-
use Cable8mm\Xeed\Support\Inflector;
87
use Symfony\Component\Console\Attribute\AsCommand;
98
use Symfony\Component\Console\Command\Command;
109
use Symfony\Component\Console\Input\InputInterface;
@@ -13,11 +12,11 @@
1312
/**
1413
* Generate database seeder.
1514
*
16-
* Run `bin/console generate-seeders` or `bin/console seeders`
15+
* Run `bin/console generate-database-seeders` or `bin/console database`
1716
*/
1817
#[AsCommand(
1918
name: 'generate-database-seeder',
20-
description: 'Generate seeders. run `bin/console generate-database-seeder`',
19+
description: 'Generate seeders. run `bin/console generate-database-seeder` or `bin/console database`',
2120
hidden: false,
2221
aliases: ['database']
2322
)]
@@ -42,11 +41,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4241
$classes = [];
4342

4443
foreach ($tables as $table) {
45-
$classes[] = Inflector::classify($table);
44+
$classes[] = $table;
4645
}
4746

4847
DatabaseSeederGenerator::make($classes)->run();
4948

49+
$output->writeln('Database seeder have been generated.');
50+
5051
return Command::SUCCESS;
5152
}
5253
}
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Cable8mm\Xeed\Command;
4+
5+
use Cable8mm\Xeed\DB;
6+
use Cable8mm\Xeed\Generators\FactoryGenerator;
7+
use Symfony\Component\Console\Attribute\AsCommand;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
/**
13+
* Generate factories.
14+
*
15+
* Run `bin/console generate-factories` or `bin/console factories`
16+
*/
17+
#[AsCommand(
18+
name: 'generate-factories',
19+
description: 'Generate factories. run `bin/console generate-factories` or `bin/console factories`',
20+
hidden: false,
21+
aliases: ['factories']
22+
)]
23+
class GenerateFactoriesCommand extends Command
24+
{
25+
/**
26+
* Configure the command.
27+
*/
28+
protected function configure(): void
29+
{
30+
$dotenv = \Dotenv\Dotenv::createImmutable(getcwd());
31+
$dotenv->safeLoad();
32+
}
33+
34+
/**
35+
* Run the console command.
36+
*/
37+
protected function execute(InputInterface $input, OutputInterface $output): int
38+
{
39+
$tables = DB::getInstance()->attach()->getTables();
40+
41+
foreach ($tables as $table) {
42+
FactoryGenerator::make($table)->run();
43+
}
44+
45+
$output->writeln('Factories have been generated.');
46+
47+
return Command::SUCCESS;
48+
}
49+
}
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Cable8mm\Xeed\Command;
4+
5+
use Cable8mm\Xeed\DB;
6+
use Cable8mm\Xeed\Generators\MigrationGenerator;
7+
use Symfony\Component\Console\Attribute\AsCommand;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
/**
13+
* Generate migrations.
14+
*
15+
* Run `bin/console generate-migrations` or `bin/console migrations`
16+
*/
17+
#[AsCommand(
18+
name: 'generate-migrations',
19+
description: 'Generate migrations. run `bin/console generate-migrations` or `bin/console migrations`',
20+
hidden: false,
21+
aliases: ['migrations']
22+
)]
23+
class GenerateMigrationsCommand extends Command
24+
{
25+
/**
26+
* Configure the command.
27+
*/
28+
protected function configure(): void
29+
{
30+
$dotenv = \Dotenv\Dotenv::createImmutable(getcwd());
31+
$dotenv->safeLoad();
32+
}
33+
34+
/**
35+
* Run the console command.
36+
*/
37+
protected function execute(InputInterface $input, OutputInterface $output): int
38+
{
39+
$tables = DB::getInstance()->attach()->getTables();
40+
41+
foreach ($tables as $table) {
42+
MigrationGenerator::make($table)->run();
43+
}
44+
45+
$output->writeln('Migrations have been generated.');
46+
47+
return Command::SUCCESS;
48+
}
49+
}

src/Command/GenerateModelsCommand.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
#[AsCommand(
1818
name: 'generate-models',
19-
description: 'Generate models. run `bin/console generate-models`',
19+
description: 'Generate models. run `bin/console generate-models` or `bin/console models`',
2020
hidden: false,
2121
aliases: ['models']
2222
)]
@@ -42,6 +42,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4242
ModelGenerator::make($table)->run();
4343
}
4444

45+
$output->writeln('Models have been generated.');
46+
4547
return Command::SUCCESS;
4648
}
4749
}

src/Command/GenerateSeedersCommand.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
#[AsCommand(
1818
name: 'generate-seeders',
19-
description: 'Generate seeders. run `bin/console generate-seeders`',
19+
description: 'Generate seeders. run `bin/console generate-seeders` or `bin/console seeders`',
2020
hidden: false,
2121
aliases: ['seeders']
2222
)]
@@ -42,6 +42,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4242
SeederGenerator::make($table)->run();
4343
}
4444

45+
$output->writeln('Seeders have been generated.');
46+
4547
return Command::SUCCESS;
4648
}
4749
}

0 commit comments

Comments
 (0)