Skip to content

Commit

Permalink
Create import xeed command for testing (#14)
Browse files Browse the repository at this point in the history
* Create import xeed command for testing

* Fix some namespace typo and add method comments
  • Loading branch information
cable8mm authored Mar 15, 2024
1 parent 2b23d99 commit f51d82d
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 2 deletions.
2 changes: 2 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
File renamed without changes.
50 changes: 50 additions & 0 deletions src/Command/ImportXeedCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Cable8mm\Xeed\Command;

use Cable8mm\Xeed\DB;
use Cable8mm\Xeed\Support\Path;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'import-xeed',
description: 'Import xeed sql for testing. run `bin/console import-xeed`',
hidden: false,
aliases: ['xeed']
)]
class ImportXeedCommand extends Command
{
protected function configure()
{
$dotenv = \Dotenv\Dotenv::createImmutable(getcwd());
$dotenv->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;
}
}
6 changes: 6 additions & 0 deletions src/Support/Inflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions src/Support/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Generators/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Cable8mm\Xeed\Tests\Unit;
namespace Cable8mm\Xeed\Tests\Unit\Generators;

use Cable8mm\Xeed\Generators\ModelGenerator;
use Cable8mm\Xeed\Support\Path;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Generators/SeederGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Cable8mm\Xeed\Tests\Unit\Generagers;
namespace Cable8mm\Xeed\Tests\Unit\Generators;

use Cable8mm\Xeed\Generators\SeederGenerator;
use Cable8mm\Xeed\Support\Path;
Expand Down

0 comments on commit f51d82d

Please sign in to comment.