From f51d82d5ea578b1058be2635b33fc2c028016119 Mon Sep 17 00:00:00 2001 From: Samgu Lee Date: Fri, 15 Mar 2024 22:30:38 +1300 Subject: [PATCH] Create import xeed command for testing (#14) * Create import xeed command for testing * Fix some namespace typo and add method comments --- bin/console | 2 + resources/tests/{xeeds.sql.test => xeeds.sql} | 0 src/Command/ImportXeedCommand.php | 50 +++++++++++++++++++ src/Support/Inflector.php | 6 +++ src/Support/Path.php | 24 +++++++++ tests/Unit/Generators/ModelGeneratorTest.php | 2 +- tests/Unit/Generators/SeederGeneratorTest.php | 2 +- 7 files changed, 84 insertions(+), 2 deletions(-) rename resources/tests/{xeeds.sql.test => xeeds.sql} (100%) create mode 100644 src/Command/ImportXeedCommand.php diff --git a/bin/console b/bin/console index 9bd5903..f105af1 100755 --- a/bin/console +++ b/bin/console @@ -6,6 +6,7 @@ require __DIR__.'/../vendor/autoload.php'; use Cable8mm\Xeed\Command\GenerateDatabaseSeederCommand; use Cable8mm\Xeed\Command\GenerateModelsCommand; use Cable8mm\Xeed\Command\GenerateSeedersCommand; +use Cable8mm\Xeed\Command\ImportXeedCommand; use Symfony\Component\Console\Application; $application = new Application(); @@ -15,5 +16,6 @@ $application = new Application(); $application->add(new GenerateModelsCommand()); $application->add(new GenerateSeedersCommand()); $application->add(new GenerateDatabaseSeederCommand()); +$application->add(new ImportXeedCommand()); $application->run(); diff --git a/resources/tests/xeeds.sql.test b/resources/tests/xeeds.sql similarity index 100% rename from resources/tests/xeeds.sql.test rename to resources/tests/xeeds.sql diff --git a/src/Command/ImportXeedCommand.php b/src/Command/ImportXeedCommand.php new file mode 100644 index 0000000..9a5f64e --- /dev/null +++ b/src/Command/ImportXeedCommand.php @@ -0,0 +1,50 @@ +safeLoad(); + + $this + ->addArgument('drop', InputArgument::OPTIONAL, 'Drop xeeds table?'); + } + + /** + * Generate models. + * + * Run `bin/console generate-seeders` or `bin/console seeders` + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + + $drop = $input->getArgument('drop'); + + if ($drop) { + DB::getInstance()->prepare('DROP TABLE xeeds')->execute(); + } else { + $sql = file_get_contents(Path::resourceTest().'xeeds.sql'); + + DB::getInstance()->exec($sql); + } + + return Command::SUCCESS; + } +} diff --git a/src/Support/Inflector.php b/src/Support/Inflector.php index 2e0bdac..b2c8737 100644 --- a/src/Support/Inflector.php +++ b/src/Support/Inflector.php @@ -15,6 +15,12 @@ final class Inflector */ private static ?DoctrineInflector $inflector = null; + /** + * To get Class name as Laravel style. + * + * @param string $string raw table name + * @return string Model class name as Laravel style + */ public static function classify(string $string): string { if (self::$inflector === null) { diff --git a/src/Support/Path.php b/src/Support/Path.php index 0d35596..860a11e 100644 --- a/src/Support/Path.php +++ b/src/Support/Path.php @@ -66,4 +66,28 @@ public static function migration(): string { return getcwd().DIRECTORY_SEPARATOR.'dist'.DIRECTORY_SEPARATOR.'database'.DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR; } + + /** + * To get `resource` folder path. + * + * @return string The resource folder path + * + * @example ./resources/ + */ + public static function resource(): string + { + return getcwd().DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR; + } + + /** + * To get `resource/test` folder path. + * + * @return string The resource/test folder path + * + * @example ./resources/tests/ + */ + public static function resourceTest(): string + { + return getcwd().DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR; + } } diff --git a/tests/Unit/Generators/ModelGeneratorTest.php b/tests/Unit/Generators/ModelGeneratorTest.php index 9eb8a08..c33fa98 100644 --- a/tests/Unit/Generators/ModelGeneratorTest.php +++ b/tests/Unit/Generators/ModelGeneratorTest.php @@ -1,6 +1,6 @@