diff --git a/.gitignore b/.gitignore index f3de5e0a..49db7044 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ /tests/App/ /tests/Database/ /tests/lion_database.sqlite +/public/lion_database.sqlite .phpunit.result.cache # Ignore dependency files ---------------------------------------------------------------------------------------------- /vendor/ diff --git a/docker-compose.yml b/compose.yml similarity index 100% rename from docker-compose.yml rename to compose.yml diff --git a/public/connections.php b/public/connections.php new file mode 100644 index 00000000..1cda0bdf --- /dev/null +++ b/public/connections.php @@ -0,0 +1,49 @@ + [ + 'type' => env('DB_TYPE'), + 'host' => env('DB_HOST'), + 'port' => env('DB_PORT'), + 'dbname' => env('DB_NAME'), + 'user' => env('DB_USER'), + 'password' => env('DB_PASSWORD'), + ], + env('DB_NAME_TEST') => [ + 'type' => env('DB_TYPE_TEST'), + 'host' => env('DB_HOST_TEST'), + 'port' => env('DB_PORT_TEST'), + 'dbname' => env('DB_NAME_TEST'), + 'user' => env('DB_USER_TEST'), + 'password' => env('DB_PASSWORD_TEST'), + ], + env('DB_NAME_TEST_POSTGRESQL') => [ + 'type' => env('DB_TYPE_TEST_POSTGRESQL'), + 'host' => env('DB_HOST_TEST_POSTGRESQL'), + 'port' => env('DB_PORT_TEST_POSTGRESQL'), + 'dbname' => env('DB_NAME'), + 'user' => env('DB_USER_TEST_POSTGRESQL'), + 'password' => env('DB_PASSWORD_TEST_POSTGRESQL'), + ], + 'lion_database_sqlite' => [ + 'type' => env('DB_TYPE_TEST_SQLITE'), + 'dbname' => __DIR__ . '/' . env('DB_NAME_TEST_SQLITE'), + ], +]; + +Driver::run([ + 'default' => env('DB_DEFAULT'), + 'connections' => $privateConnections, +]); + +define('NUMBER_OF_ACTIVE_CONNECTIONS', count($privateConnections)); diff --git a/src/LionBundle/Commands/Lion/DB/CrudCommand.php b/src/LionBundle/Commands/Lion/DB/CrudCommand.php index c31ea0a9..81f508b2 100644 --- a/src/LionBundle/Commands/Lion/DB/CrudCommand.php +++ b/src/LionBundle/Commands/Lion/DB/CrudCommand.php @@ -91,12 +91,29 @@ protected function configure(): void { $this ->setName('db:crud') - ->setDescription( - 'Command to generate controller and model of an entity with their respective CRUD functions.' - ) + ->setDescription('Command to generate controller and model of an entity with their respective CRUD functions.') // phpcs:ignore ->addArgument('entity', InputArgument::REQUIRED, 'Entity name.'); } + /** + * Initializes the command after the input has been bound and before the input + * is validated. + * + * This is mainly useful when a lot of commands extends one main command where + * some things need to be initialized based on the input arguments and options. + * + * @param InputInterface $input InputInterface is the interface implemented by + * all input classes. + * @param OutputInterface $output OutputInterface is the interface implemented + * by all Output classes. + * + * @return void + */ + protected function initialize(InputInterface $input, OutputInterface $output): void + { + parent::initialize($input, $output); + } + /** * Executes the current command. * @@ -121,7 +138,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int /** @var string $entity */ $entity = $input->getArgument('entity'); - $selectedConnection = $this->selectConnection($input, $output); + $selectedConnection = $this->selectConnection(); $connectionName = Connection::getConnections()[$selectedConnection][Connection::CONNECTION_DBNAME]; diff --git a/src/LionBundle/Commands/Lion/DB/DBSeedCommand.php b/src/LionBundle/Commands/Lion/DB/DBSeedCommand.php index 812cebc3..a985803a 100644 --- a/src/LionBundle/Commands/Lion/DB/DBSeedCommand.php +++ b/src/LionBundle/Commands/Lion/DB/DBSeedCommand.php @@ -5,40 +5,21 @@ namespace Lion\Bundle\Commands\Lion\DB; use DI\Attribute\Inject; -use InvalidArgumentException; use Lion\Bundle\Helpers\Commands\ClassFactory; use Lion\Bundle\Helpers\Commands\Migrations\Migrations; +use Lion\Bundle\Helpers\Commands\Selection\MenuCommand; use Lion\Bundle\Helpers\DatabaseEngine; use Lion\Bundle\Interface\SeedInterface; -use Lion\Command\Command; use Lion\Database\Connection; -use Lion\Files\Store; -use Lion\Helpers\Str; -use Lion\Request\Http; use LogicException; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** * Execute the database connection seeds. */ -class DBSeedCommand extends Command +class DBSeedCommand extends MenuCommand { - /** - * Manipulate system files. - * - * @var Store $store - */ - private Store $store; - - /** - * Modify and construct strings with different formats. - * - * @var Str $str - */ - private Str $str; - /** * Manages basic database engine processes. * @@ -61,22 +42,6 @@ class DBSeedCommand extends Command */ private ClassFactory $classFactory; - #[Inject] - public function setStore(Store $store): DBSeedCommand - { - $this->store = $store; - - return $this; - } - - #[Inject] - public function setStr(Str $str): DBSeedCommand - { - $this->str = $str; - - return $this; - } - #[Inject] public function setDatabaseEngine(DatabaseEngine $databaseEngine): DBSeedCommand { @@ -110,8 +75,26 @@ protected function configure(): void { $this ->setName('db:seed') - ->setDescription('Run the available seeds.') - ->addOption('connection', 'c', InputOption::VALUE_REQUIRED, 'The connection to run.'); + ->setDescription('Run the available seeds.'); + } + + /** + * Initializes the command after the input has been bound and before the input + * is validated. + * + * This is mainly useful when a lot of commands extends one main command where + * some things need to be initialized based on the input arguments and options. + * + * @param InputInterface $input InputInterface is the interface implemented by + * all input classes. + * @param OutputInterface $output OutputInterface is the interface implemented + * by all Output classes. + * + * @return void + */ + protected function initialize(InputInterface $input, OutputInterface $output): void + { + parent::initialize($input, $output); } /** @@ -132,12 +115,7 @@ protected function configure(): void */ protected function execute(InputInterface $input, OutputInterface $output): int { - /** @var string|null $connectionName */ - $connectionName = $input->getOption('connection'); - - if (!$connectionName) { - throw new InvalidArgumentException("The '--connection' option is required.", Http::INTERNAL_SERVER_ERROR); - } + $connectionName = $this->selectConnection(); $connections = Connection::getConnections(); diff --git a/src/LionBundle/Commands/Lion/Migrations/FreshMigrationsCommand.php b/src/LionBundle/Commands/Lion/Migrations/FreshMigrationsCommand.php index 4ecbde95..4fb822ee 100644 --- a/src/LionBundle/Commands/Lion/Migrations/FreshMigrationsCommand.php +++ b/src/LionBundle/Commands/Lion/Migrations/FreshMigrationsCommand.php @@ -6,7 +6,6 @@ use DI\Attribute\Inject; use Exception; -use InvalidArgumentException; use Lion\Bundle\Helpers\Commands\Migrations\Migrations; use Lion\Bundle\Helpers\Commands\Selection\MenuCommand; use Lion\Bundle\Helpers\DatabaseEngine; @@ -16,7 +15,6 @@ use Lion\Bundle\Interface\Migrations\ViewInterface; use Lion\Bundle\Interface\MigrationUpInterface; use Lion\Database\Connection; -use Lion\Request\Http; use LogicException; use Symfony\Component\Console\Exception\ExceptionInterface; use Symfony\Component\Console\Input\ArrayInput; @@ -69,8 +67,7 @@ protected function configure(): void $this ->setName('migrate:fresh') ->setDescription('Drop all tables and re-run all migrations') - ->addOption('seed', 's', InputOption::VALUE_OPTIONAL, 'Do you want to run the seeds?', 'none') - ->addOption('connection', 'c', InputOption::VALUE_REQUIRED, 'The connection to run.'); + ->addOption('seed', 's', InputOption::VALUE_OPTIONAL, 'Do you want to run the seeds?', 'none'); } /** @@ -113,12 +110,7 @@ protected function initialize(InputInterface $input, OutputInterface $output): v */ protected function execute(InputInterface $input, OutputInterface $output): int { - /** @var string|null $connectionName */ - $connectionName = $input->getOption('connection'); - - if (!$connectionName) { - throw new InvalidArgumentException("The '--connection' option is required.", Http::INTERNAL_SERVER_ERROR); - } + $connectionName = $this->selectConnection(); $connections = Connection::getConnections(); @@ -170,15 +162,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int $seed = $input->getOption('seed'); if ($seed != 'none') { - $output->writeln(''); - - $this - ->getApplication() - /** @phpstan-ignore-next-line */ - ->find('db:seed') - ->run(new ArrayInput([ - '--connection' => $connectionName, - ]), $output); + $this->getApplication() + ?->find('db:seed') + ->run(new ArrayInput([]), $output); } return parent::SUCCESS; diff --git a/src/LionBundle/Commands/Lion/New/MigrationCommand.php b/src/LionBundle/Commands/Lion/New/MigrationCommand.php index 49b5dc54..d3d196f9 100644 --- a/src/LionBundle/Commands/Lion/New/MigrationCommand.php +++ b/src/LionBundle/Commands/Lion/New/MigrationCommand.php @@ -16,7 +16,6 @@ use LogicException; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** @@ -80,8 +79,26 @@ protected function configure(): void $this ->setName('new:migration') ->setDescription('Command required to generate a new migration.') - ->addArgument('migration', InputArgument::REQUIRED, 'Migration name.') - ->addOption('connection', 'c', InputOption::VALUE_REQUIRED, 'The connection to run.'); + ->addArgument('migration', InputArgument::REQUIRED, 'Migration name.'); + } + + /** + * Initializes the command after the input has been bound and before the input + * is validated. + * + * This is mainly useful when a lot of commands extends one main command where + * some things need to be initialized based on the input arguments and options. + * + * @param InputInterface $input InputInterface is the interface implemented by + * all input classes. + * @param OutputInterface $output OutputInterface is the interface implemented + * by all Output classes. + * + * @return void + */ + protected function initialize(InputInterface $input, OutputInterface $output): void + { + parent::initialize($input, $output); } /** @@ -106,16 +123,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int /** @var string $migration */ $migration = $input->getArgument('migration'); - if (STR->of($migration)->test("/.*\//")) { + if ($this->str->of($migration)->test("/.*\//")) { throw new InvalidArgumentException('Migration cannot be inside subfolders.', Http::INTERNAL_SERVER_ERROR); } - /** @var string|null $connectionName */ - $connectionName = $input->getOption('connection'); + $connectionName = $this->selectConnection(); - if (!$connectionName) { - throw new InvalidArgumentException("The '--connection' option is required.", Http::INTERNAL_SERVER_ERROR); - } + $migrationType = $this->selectMigrationType(); $connections = Connection::getConnections(); @@ -128,8 +142,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int $driver = $this->databaseEngine->getDriver($databaseEngineType); - $selectedType = $this->selectMigrationType($input, $output, MigrationFactory::MIGRATIONS_OPTIONS); - /** @var string $migrationClassName */ $migrationClassName = $this->str ->of($migration) @@ -148,7 +160,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int ->trim() ->get(); - $dataMigration = $this->migrationFactory->getBody($migrationClassName, $selectedType, $dbNameFormat, $driver); + $dataMigration = $this->migrationFactory->getBody($migrationClassName, $migrationType, $dbNameFormat, $driver); /** @var string $path */ $path = $dataMigration->path; diff --git a/src/LionBundle/Commands/Lion/New/SeedCommand.php b/src/LionBundle/Commands/Lion/New/SeedCommand.php index 2aa88cac..b28b6900 100644 --- a/src/LionBundle/Commands/Lion/New/SeedCommand.php +++ b/src/LionBundle/Commands/Lion/New/SeedCommand.php @@ -6,40 +6,21 @@ use DI\Attribute\Inject; use Exception; -use InvalidArgumentException; use Lion\Bundle\Helpers\Commands\ClassFactory; use Lion\Bundle\Helpers\Commands\Migrations\Migrations; +use Lion\Bundle\Helpers\Commands\Selection\MenuCommand; use Lion\Bundle\Helpers\DatabaseEngine; -use Lion\Command\Command; use Lion\Database\Connection; -use Lion\Files\Store; -use Lion\Helpers\Str; -use Lion\Request\Http; use LogicException; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** * Generate a seed. */ -class SeedCommand extends Command +class SeedCommand extends MenuCommand { - /** - * Modify and construct strings with different formats. - * - * @var Str $str - */ - private Str $str; - - /** - * Manipulate system files. - * - * @var Store $store - */ - private Store $store; - /** * Manages basic database engine processes. * @@ -55,22 +36,6 @@ class SeedCommand extends Command */ private ClassFactory $classFactory; - #[Inject] - public function setStr(Str $str): SeedCommand - { - $this->str = $str; - - return $this; - } - - #[Inject] - public function setStore(Store $store): SeedCommand - { - $this->store = $store; - - return $this; - } - #[Inject] public function setDatabaseEngine(DatabaseEngine $databaseEngine): SeedCommand { @@ -96,9 +61,27 @@ protected function configure(): void { $this ->setName('new:seed') - ->setDescription('Command required for creating new seeds') - ->addArgument('seed', InputArgument::OPTIONAL, 'Seed name.', 'ExampleSeed') - ->addOption('connection', 'c', InputOption::VALUE_REQUIRED, 'The connection to run.'); + ->setDescription('Command required for creating new seeds.') + ->addArgument('seed', InputArgument::OPTIONAL, 'Seed name.', 'ExampleSeed'); + } + + /** + * Initializes the command after the input has been bound and before the input + * is validated. + * + * This is mainly useful when a lot of commands extends one main command where + * some things need to be initialized based on the input arguments and options. + * + * @param InputInterface $input InputInterface is the interface implemented by + * all input classes. + * @param OutputInterface $output OutputInterface is the interface implemented + * by all Output classes. + * + * @return void + */ + protected function initialize(InputInterface $input, OutputInterface $output): void + { + parent::initialize($input, $output); } /** @@ -120,12 +103,7 @@ protected function configure(): void */ protected function execute(InputInterface $input, OutputInterface $output): int { - /** @var string|null $connectionName */ - $connectionName = $input->getOption('connection'); - - if (!$connectionName) { - throw new InvalidArgumentException("The '--connection' option is required.", Http::INTERNAL_SERVER_ERROR); - } + $connectionName = $this->selectConnection(); $connections = Connection::getConnections(); diff --git a/src/LionBundle/Helpers/Commands/Migrations/MigrationFactory.php b/src/LionBundle/Helpers/Commands/Migrations/MigrationFactory.php index 40842c7f..1749439d 100644 --- a/src/LionBundle/Helpers/Commands/Migrations/MigrationFactory.php +++ b/src/LionBundle/Helpers/Commands/Migrations/MigrationFactory.php @@ -72,20 +72,24 @@ public function setDatabaseEngine(DatabaseEngine $databaseEngine): MigrationFact } /** - * Gets the data to generate the body of the selected migration type + * Gets the data to generate the body of the selected migration type. * - * @param string $className Class name - * @param string $selectedType Type of migration - * @param string $dbPascal Database in PascalCase format - * @param string $driver Database Engine Type + * @param string $className Class name. + * @param string $migrationType Type of migration. + * @param string $dbPascal Database in PascalCase format. + * @param string $driver Database Engine Type. * * @return stdClass * * @throws Exception If the driver is not supported. */ - public function getBody(string $className, string $selectedType, string $dbPascal, string $driver): stdClass + public function getBody(string $className, string $migrationType, string $dbPascal, string $driver): stdClass { - if (self::SCHEMA === $selectedType && 'PostgreSQL' === $driver || 'SQLite' === $driver) { + if ( + self::SCHEMA === $migrationType && + $this->databaseEngine->getDriver(Driver::POSTGRESQL) === $driver || + $this->databaseEngine->getDriver(Driver::SQLITE) === $driver + ) { throw new Exception( 'Currently, the driver does not support this type of migration.', Http::INTERNAL_SERVER_ERROR @@ -116,20 +120,20 @@ public function getBody(string $className, string $selectedType, string $dbPasca self::STORED_PROCEDURE => 'getPostgreSQLStoredProcedureBody', ]; - if (isset($typeFolders[$selectedType])) { - $folder = $typeFolders[$selectedType]; + if (isset($typeFolders[$migrationType])) { + $folder = $typeFolders[$migrationType]; $path = "database/Migrations/{$dbPascal}/{$driver}/{$folder}/"; $namespace = "Database\\Migrations\\{$dbPascal}\\{$driver}\\{$folder}"; if ($this->databaseEngine->getDriver(Driver::MYSQL) === $driver) { - $method = $mysqlMethods[$selectedType]; + $method = $mysqlMethods[$migrationType]; $body = $this->$method($className, $namespace); } elseif ($this->databaseEngine->getDriver(Driver::POSTGRESQL) === $driver) { /** @phpstan-ignore-next-line */ - $method = $pgsqlMethods[$selectedType]; + $method = $pgsqlMethods[$migrationType]; $body = $this->$method($className, $namespace); } diff --git a/src/LionBundle/Helpers/Commands/Selection/MenuCommand.php b/src/LionBundle/Helpers/Commands/Selection/MenuCommand.php index db100762..6fce1417 100644 --- a/src/LionBundle/Helpers/Commands/Selection/MenuCommand.php +++ b/src/LionBundle/Helpers/Commands/Selection/MenuCommand.php @@ -6,6 +6,7 @@ use DI\Attribute\Inject; use Exception; +use Lion\Bundle\Helpers\Commands\Migrations\MigrationFactory; use Lion\Command\Command; use Lion\Database\Connection; use Lion\Database\Driver; @@ -238,46 +239,47 @@ protected function selectedTypes(InputInterface $input, OutputInterface $output, } /** - * Selection menu to select a database - * - * @param InputInterface $input [InputInterface is the interface - * implemented by all input classes] - * @param OutputInterface $output [OutputInterface is the interface - * implemented by all Output classes] + * Selection menu to select a database. * * @return string * * @internal */ - protected function selectConnection(InputInterface $input, OutputInterface $output): string + protected function selectConnection(): string { + if (!empty($_ENV['SELECTED_CONNECTION'])) { + /** @var string $connectionName */ + $connectionName = $_ENV['SELECTED_CONNECTION']; + + return $connectionName; + } + /** @var QuestionHelper $helper */ $helper = $this->getHelper('question'); $connections = Connection::getConnections(); - $connectionKeys = array_keys($connections); - - /** @var string $defaultConnection */ - $defaultConnection = reset($connectionKeys); + $defaultConnection = Connection::getDefaultConnectionName(); if ($this->arr->of($connections)->length() > 1) { /** @var array $choiseConnections */ - $choiseConnections = $this->arr - ->of($connections) - ->keys() - ->get(); + $choiseConnections = []; + + foreach ($connections as $connectionName => $connectionConfig) { + $choiseConnections[$connectionName] = + "{$connectionName}[{$connectionConfig[Connection::CONNECTION_TYPE]}]"; + } $choiseQuestion = new ChoiceQuestion( 'Select a connection ' . $this->warningOutput("(default: {$defaultConnection})"), $choiseConnections, - 0 + $defaultConnection ); /** @var string $selectedConnection */ - $selectedConnection = $helper->ask($input, $output, $choiseQuestion); + $selectedConnection = $helper->ask($this->input, $this->output, $choiseQuestion); } else { - $output->writeln($this->warningOutput("default connection: ({$defaultConnection})")); + $this->output->writeln($this->warningOutput("Default connection: ({$defaultConnection})")); $selectedConnection = $defaultConnection; } @@ -305,37 +307,31 @@ protected function selectConnectionByEnviroment(InputInterface $input, OutputInt return $_ENV['SELECTED_CONNECTION']; } - return $this->selectConnection($input, $output); + return $this->selectConnection(); } /** - * Selection menu to select a database - * - * @param InputInterface $input [InputInterface is the interface - * implemented by all input classes] - * @param OutputInterface $output [OutputInterface is the interface - * implemented by all Output classes] - * @param array $options [List of available migration types] + * Select the migration type. * * @return string * * @internal */ - protected function selectMigrationType(InputInterface $input, OutputInterface $output, array $options): string + protected function selectMigrationType(): string { - $defaultOption = $options[0]; + $defaultOption = MigrationFactory::TABLE; /** @var QuestionHelper $helper */ $helper = $this->getHelper('question'); $choiceQuestion = new ChoiceQuestion( "Select the type of migration {$this->warningOutput("(default: {$defaultOption})")}", - $options, - 0 + MigrationFactory::MIGRATIONS_OPTIONS, + MigrationFactory::TABLE ); /** @var string $migrationType */ - $migrationType = $helper->ask($input, $output, $choiceQuestion); + $migrationType = $helper->ask($this->input, $this->output, $choiceQuestion); return $migrationType; } diff --git a/tests/Commands/Lion/DB/DBCapsuleCommandTest.php b/tests/Commands/Lion/DB/DBCapsuleCommandTest.php index f2832c48..50066aef 100644 --- a/tests/Commands/Lion/DB/DBCapsuleCommandTest.php +++ b/tests/Commands/Lion/DB/DBCapsuleCommandTest.php @@ -10,7 +10,6 @@ use Lion\Bundle\Commands\Lion\New\CapsuleCommand; use Lion\Bundle\Commands\Lion\New\InterfaceCommand; use Lion\Bundle\Helpers\Commands\ClassFactory; -use Lion\Bundle\Helpers\Commands\Selection\MenuCommand; use Lion\Bundle\Helpers\DatabaseEngine; use Lion\Database\Drivers\Schema\MySQL as Schema; use Lion\Database\Interface\DatabaseCapsuleInterface; @@ -104,7 +103,9 @@ public function setDatabaseEngine(): void #[Testing] public function execute(): void { - Schema::connection(getDefaultConnection()) + $connectionName = getDefaultConnection(); + + Schema::connection($connectionName) ->createTable(self::ENTITY, function (): void { Schema::int('id') ->notNull() @@ -117,12 +118,8 @@ public function execute(): void ->execute(); $execute = $this->commandTester - ->setInputs([ - '0', - ]) - ->execute([ - 'entity' => self::ENTITY, - ]); + ->setInputs([$connectionName]) + ->execute(['entity' => self::ENTITY]); $this->assertSame(Command::SUCCESS, $execute); $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTester->getDisplay()); @@ -134,7 +131,7 @@ public function execute(): void $this->assertArrayNotHasKey('SELECTED_CONNECTION', $_ENV); - Schema::connection(getDefaultConnection()) + Schema::connection($connectionName) ->dropTable(self::ENTITY) ->execute(); } @@ -142,7 +139,9 @@ public function execute(): void #[Testing] public function executeWithForeignKeys(): void { - Schema::connection(getDefaultConnection()) + $connectionName = getDefaultConnection(); + + Schema::connection($connectionName) ->createTable('roles', function (): void { Schema::int('idroles') ->notNull() @@ -154,7 +153,7 @@ public function executeWithForeignKeys(): void }) ->execute(); - Schema::connection(getDefaultConnection()) + Schema::connection($connectionName) ->createTable('users', function (): void { Schema::int('idusers') ->notNull() @@ -171,12 +170,8 @@ public function executeWithForeignKeys(): void ->execute(); $execute = $this->commandTester - ->setInputs([ - '0', - ]) - ->execute([ - 'entity' => 'roles', - ]); + ->setInputs([$connectionName]) + ->execute(['entity' => 'roles']); $this->assertSame(Command::SUCCESS, $execute); @@ -195,12 +190,8 @@ public function executeWithForeignKeys(): void EOT, $display); $execute = $this->commandTester - ->setInputs([ - '0', - ]) - ->execute([ - 'entity' => 'users', - ]); + ->setInputs([$connectionName]) + ->execute(['entity' => 'users']); $this->assertSame(Command::SUCCESS, $execute); @@ -218,11 +209,11 @@ public function executeWithForeignKeys(): void >> CAPSULE: Database\Class\LionDatabase\MySQL\Users EOT, $display); - Schema::connection(getDefaultConnection()) + Schema::connection($connectionName) ->dropTable('users') ->execute(); - Schema::connection(getDefaultConnection()) + Schema::connection($connectionName) ->dropTable('roles') ->execute(); } @@ -231,12 +222,8 @@ public function executeWithForeignKeys(): void public function executeWithoutColumns(): void { $execute = $this->commandTester - ->setInputs([ - '0', - ]) - ->execute([ - 'entity' => self::ENTITY, - ]); + ->setInputs([getDefaultConnection()]) + ->execute(['entity' => self::ENTITY]); $this->assertSame(Command::FAILURE, $execute); $this->assertStringContainsString(self::OUTPUT_MESSAGE_ERROR, $this->commandTester->getDisplay()); @@ -245,7 +232,9 @@ public function executeWithoutColumns(): void #[Testing] public function executeWithForeignsReturningErrorObject(): void { - Schema::connection(getDefaultConnection()) + $connectionName = getDefaultConnection(); + + Schema::connection($connectionName) ->createTable('roles', function (): void { Schema::int('idroles') ->notNull() @@ -257,7 +246,7 @@ public function executeWithForeignsReturningErrorObject(): void }) ->execute(); - Schema::connection(getDefaultConnection()) + Schema::connection($connectionName) ->createTable('users', function (): void { Schema::int('idusers') ->notNull() @@ -314,18 +303,18 @@ protected function getTableForeigns( $tester = new CommandTester($application->find('db:capsule')); $exitCode = $tester - ->setInputs(['0']) + ->setInputs([$connectionName]) ->execute(['entity' => 'users']); $this->assertEquals(Command::FAILURE, $exitCode); $this->assertStringContainsString('>> CAPSULE: ERROR MESSAGE', $tester->getDisplay()); - Schema::connection(getDefaultConnection()) + Schema::connection($connectionName) ->dropTable('users') ->execute(); - Schema::connection(getDefaultConnection()) + Schema::connection($connectionName) ->dropTable('roles') ->execute(); } diff --git a/tests/Commands/Lion/DB/DBSeedCommandTest.php b/tests/Commands/Lion/DB/DBSeedCommandTest.php index 79adf719..eae73d83 100644 --- a/tests/Commands/Lion/DB/DBSeedCommandTest.php +++ b/tests/Commands/Lion/DB/DBSeedCommandTest.php @@ -6,7 +6,6 @@ use DI\DependencyException; use DI\NotFoundException; -use InvalidArgumentException; use Lion\Bundle\Commands\Lion\DB\DBSeedCommand; use Lion\Bundle\Commands\Lion\New\SeedCommand; use Lion\Bundle\Helpers\Commands\ClassFactory; @@ -14,9 +13,7 @@ use Lion\Bundle\Helpers\DatabaseEngine; use Lion\Database\Connection; use Lion\Dependency\Injection\Container; -use Lion\Files\Store; use Lion\Helpers\Str; -use Lion\Request\Http; use Lion\Test\Test; use PHPUnit\Framework\Attributes\Test as Testing; use ReflectionException; @@ -65,28 +62,6 @@ protected function setUp(): void $this->initReflection($this->dbSeedCommand); } - /** - * @throws ReflectionException If the property does not exist in the reflected - * class. - */ - #[Testing] - public function setStore(): void - { - $this->assertInstanceOf(DBSeedCommand::class, $this->dbSeedCommand->setStore(new Store())); - $this->assertInstanceOf(Store::class, $this->getPrivateProperty('store')); - } - - /** - * @throws ReflectionException If the property does not exist in the reflected - * class. - */ - #[Testing] - public function setStr(): void - { - $this->assertInstanceOf(DBSeedCommand::class, $this->dbSeedCommand->setStr(new Str())); - $this->assertInstanceOf(Str::class, $this->getPrivateProperty('str')); - } - /** * @throws ReflectionException If the property does not exist in the reflected * class. @@ -120,16 +95,6 @@ public function setClassFactory(): void $this->assertInstanceOf(ClassFactory::class, $this->getPrivateProperty('classFactory')); } - #[Testing] - public function executeWithoutConnection(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage("The '--connection' option is required."); - $this->expectExceptionCode(Http::INTERNAL_SERVER_ERROR); - - $this->commandTester->execute([]); - } - #[Testing] public function execute(): void { @@ -153,17 +118,22 @@ public function execute(): void $seedsPath = Migrations::SEEDS_PATH . "{$dbNamePascal}/{$dbType}/"; - $this->assertSame(Command::SUCCESS, $this->commandTesterNewSeed->execute([ - 'seed' => self::CLASS_NAME, - '--connection' => $connectionName, - ])); + $this->assertSame( + Command::SUCCESS, + $this->commandTesterNewSeed + ->setInputs([Connection::getDefaultConnectionName()]) + ->execute(['seed' => self::CLASS_NAME]) + ); $this->assertStringContainsString(self::OUTPUT_MESSAGE_NEW_SEED, $this->commandTesterNewSeed->getDisplay()); $this->assertFileExists($seedsPath . self::FILE_NAME); - $this->assertSame(Command::SUCCESS, $this->commandTester->execute([ - '--connection' => $connectionName, - ])); + $this->assertSame( + Command::SUCCESS, + $this->commandTester + ->setInputs([Connection::getDefaultConnectionName()]) + ->execute([]) + ); $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTester->getDisplay()); $this->assertFileExists($seedsPath . self::FILE_NAME); @@ -174,9 +144,12 @@ public function execute(): void #[Testing] public function executeIfPathDoesNotExist(): void { - $this->assertSame(Command::FAILURE, $this->commandTester->execute([ - '--connection' => getDefaultConnection(), - ])); + $this->assertSame( + Command::FAILURE, + $this->commandTester + ->setInputs([Connection::getDefaultConnectionName()]) + ->execute([]) + ); $this->assertStringContainsString(self::OUTPUT_MESSAGE_NOT_EXISTS_ERROR, $this->commandTester->getDisplay()); } diff --git a/tests/Commands/Lion/DB/RulesDBCommandTest.php b/tests/Commands/Lion/DB/RulesDBCommandTest.php index 67d4e234..a61172a5 100644 --- a/tests/Commands/Lion/DB/RulesDBCommandTest.php +++ b/tests/Commands/Lion/DB/RulesDBCommandTest.php @@ -167,12 +167,8 @@ public function execute(): void $this->createTables(); $execute = $this->commandTester - ->setInputs([ - '0', - ]) - ->execute([ - 'entity' => self::ENTITY, - ]); + ->setInputs([getDefaultConnection()]) + ->execute(['entity' => self::ENTITY]); $this->assertSame(Command::SUCCESS, $execute); @@ -207,12 +203,8 @@ public function execute(): void public function executeWithoutColumns(): void { $execute = $this->commandTester - ->setInputs([ - '0', - ]) - ->execute([ - 'entity' => self::ENTITY, - ]); + ->setInputs([getDefaultConnection()]) + ->execute(['entity' => self::ENTITY]); $this->assertSame(Command::FAILURE, $execute); $this->assertStringContainsString(self::OUTPUT_MESSAGE_ERROR, $this->commandTester->getDisplay()); @@ -224,12 +216,8 @@ public function executeWithForeign(): void $this->createTables(); $execute = $this->commandTester - ->setInputs([ - '0', - ]) - ->execute([ - 'entity' => self::ENTITY, - ]); + ->setInputs([getDefaultConnection()]) + ->execute(['entity' => self::ENTITY]); $this->assertSame(Command::SUCCESS, $execute); $this->assertStringContainsString(self::OUTPUT_MESSAGE_FOREIGN, $this->commandTester->getDisplay()); diff --git a/tests/Commands/Lion/Migrations/FreshMigrationsCommandTest.php b/tests/Commands/Lion/Migrations/FreshMigrationsCommandTest.php index f95d273c..e3c8a4e4 100644 --- a/tests/Commands/Lion/Migrations/FreshMigrationsCommandTest.php +++ b/tests/Commands/Lion/Migrations/FreshMigrationsCommandTest.php @@ -6,12 +6,12 @@ use DI\DependencyException; use DI\NotFoundException; -use InvalidArgumentException; use Lion\Bundle\Commands\Lion\DB\DBSeedCommand; use Lion\Bundle\Commands\Lion\Migrations\FreshMigrationsCommand; use Lion\Bundle\Commands\Lion\New\MigrationCommand; use Lion\Bundle\Commands\Lion\New\SeedCommand; use Lion\Bundle\Helpers\Commands\ClassFactory; +use Lion\Bundle\Helpers\Commands\Migrations\MigrationFactory; use Lion\Bundle\Helpers\Commands\Migrations\Migrations; use Lion\Bundle\Helpers\DatabaseEngine; use Lion\Bundle\Interface\SeedInterface; @@ -20,7 +20,6 @@ use Lion\Database\Drivers\Schema\MySQL; use Lion\Dependency\Injection\Container; use Lion\Helpers\Str; -use Lion\Request\Http; use Lion\Test\Test; use PHPUnit\Framework\Attributes\Test as Testing; use ReflectionException; @@ -36,7 +35,7 @@ class FreshMigrationsCommandTest extends Test private const string SEED_CLASS = 'ExampleSeed'; private const string SEED_FILE = 'ExampleSeed.php'; - private CommandTester $commandTesterNew; + private CommandTester $commandTesterMigrationNew; private CommandTester $commandTesterSeed; private CommandTester $commandTesterFresh; private FreshMigrationsCommand $freshMigrationsCommand; @@ -80,7 +79,7 @@ protected function setUp(): void $application = $kernel->getApplication(); - $this->commandTesterNew = new CommandTester($application->find('new:migration')); + $this->commandTesterMigrationNew = new CommandTester($application->find('new:migration')); $this->commandTesterSeed = new CommandTester($application->find('new:seed')); @@ -119,22 +118,10 @@ public function setDatabaseEngine(): void $this->assertInstanceOf(DatabaseEngine::class, $this->getPrivateProperty('databaseEngine')); } - #[Testing] - public function executeWithoutConnection(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage("The '--connection' option is required."); - $this->expectExceptionCode(Http::INTERNAL_SERVER_ERROR); - - $this->commandTesterFresh->execute([]); - } - #[Testing] public function executePathDoesNotExist(): void { - $this->assertSame(Command::FAILURE, $this->commandTesterFresh->execute([ - '--connection' => getDefaultConnection(), - ])); + $this->assertSame(Command::FAILURE, $this->commandTesterFresh->execute([])); $this->assertStringContainsString(self::OUTPUT_MESSAGE_ERROR_PATH, $this->commandTesterFresh->getDisplay()); } @@ -144,18 +131,27 @@ public function execute(): void { $this->createDirectory(Migrations::MIGRATIONS_PATH); - $connectionName = 'local'; - - $this->assertSame(Command::SUCCESS, $this->commandTesterNew->execute([ - 'migration' => 'test', - '--connection' => $connectionName, - ])); + $this->assertSame( + Command::SUCCESS, + $this->commandTesterMigrationNew + ->setInputs([ + Connection::getDefaultConnectionName(), + MigrationFactory::TABLE, + ]) + ->execute(['migration' => 'test']) + ); - $this->assertStringContainsString(self::OUTPUT_MIGRATION_CREATE_MESSAGE, $this->commandTesterNew->getDisplay()); + $this->assertStringContainsString( + self::OUTPUT_MIGRATION_CREATE_MESSAGE, + $this->commandTesterMigrationNew->getDisplay() + ); - $this->assertSame(Command::SUCCESS, $this->commandTesterFresh->execute([ - '--connection' => $connectionName, - ])); + $this->assertSame( + Command::SUCCESS, + $this->commandTesterFresh + ->setInputs([Connection::getDefaultConnectionName()]) + ->execute([]) + ); $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTesterFresh->getDisplay()); @@ -194,10 +190,12 @@ public function executeWithSeed(): void $this->classFactory->classFactory($seedsPath, self::SEED_CLASS); - $this->assertSame(Command::SUCCESS, $this->commandTesterSeed->execute([ - 'seed' => self::SEED_CLASS, - '--connection' => $connectionName, - ])); + $this->assertSame( + Command::SUCCESS, + $this->commandTesterSeed + ->setInputs([Connection::getDefaultConnectionName()]) + ->execute(['seed' => self::SEED_CLASS]) + ); $this->assertStringContainsString(self::OUTPUT_SEED_CREATE_MESSAGE, $this->commandTesterSeed->getDisplay()); $this->assertFileExists($seedsPath . self::SEED_FILE); @@ -206,17 +204,27 @@ public function executeWithSeed(): void $this->assertInstanceOf(SeedInterface::class, $objClass); - $this->assertSame(Command::SUCCESS, $this->commandTesterNew->execute([ - 'migration' => 'test', - '--connection' => $connectionName, - ])); + $this->assertSame( + Command::SUCCESS, + $this->commandTesterMigrationNew + ->setInputs([ + Connection::getDefaultConnectionName(), + MigrationFactory::TABLE, + ]) + ->execute(['migration' => 'test']) + ); - $this->assertStringContainsString(self::OUTPUT_MIGRATION_CREATE_MESSAGE, $this->commandTesterNew->getDisplay()); + $this->assertStringContainsString( + self::OUTPUT_MIGRATION_CREATE_MESSAGE, + $this->commandTesterMigrationNew->getDisplay() + ); - $this->assertSame(Command::SUCCESS, $this->commandTesterFresh->execute([ - '--seed' => null, - '--connection' => $connectionName, - ])); + $this->assertSame( + Command::SUCCESS, + $this->commandTesterFresh + ->setInputs([Connection::getDefaultConnectionName()]) + ->execute(['--seed' => null]) + ); $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTesterFresh->getDisplay()); diff --git a/tests/Commands/Lion/New/MigrationCommandTest.php b/tests/Commands/Lion/New/MigrationCommandTest.php index 0305084b..7b87266f 100644 --- a/tests/Commands/Lion/New/MigrationCommandTest.php +++ b/tests/Commands/Lion/New/MigrationCommandTest.php @@ -17,6 +17,7 @@ use Lion\Bundle\Interface\Migrations\TableInterface; use Lion\Bundle\Interface\Migrations\ViewInterface; use Lion\Bundle\Interface\MigrationUpInterface; +use Lion\Database\Connection; use Lion\Dependency\Injection\Container; use Lion\Request\Http; use Lion\Test\Test; @@ -124,24 +125,12 @@ public function setDatabaseEngine(): void public function executeIsInvalid(): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Migration cannot be inside subfolders.'); + $this->expectExceptionMessageIs('Migration cannot be inside subfolders.'); $this->expectExceptionCode(Http::INTERNAL_SERVER_ERROR); - $this->commandTester->execute([ - 'migration' => 'users/create-users', - ]); - } - - #[Testing] - public function executeWithoutConnection(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage("The '--connection' option is required."); - $this->expectExceptionCode(Http::INTERNAL_SERVER_ERROR); - - $this->commandTester->execute([ - 'migration' => self::MIGRATION_NAME, - ]); + $this->commandTester + ->setInputs([Connection::getDefaultConnectionName()]) + ->execute(['migration' => 'users/create-users']); } #[Testing] @@ -150,12 +139,10 @@ public function executeForMySQLSchema(): void { $commandExecute = $this->commandTester ->setInputs([ + Connection::getDefaultConnectionName(), MigrationFactory::SCHEMA, ]) - ->execute([ - 'migration' => self::MIGRATION_NAME, - '--connection' => 'local', - ]); + ->execute(['migration' => self::MIGRATION_NAME]); $this->assertSame(Command::SUCCESS, $commandExecute); $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTester->getDisplay()); @@ -178,16 +165,14 @@ public function executeForPostgreSQLSchema(): void { $this->expectException(Exception::class); $this->expectExceptionCode(Http::INTERNAL_SERVER_ERROR); - $this->expectExceptionMessage('Currently, the driver does not support this type of migration.'); + $this->expectExceptionMessageIs('Currently, the driver does not support this type of migration.'); $this->commandTester ->setInputs([ + env('DB_NAME_TEST_POSTGRESQL'), MigrationFactory::SCHEMA, ]) - ->execute([ - 'migration' => self::MIGRATION_NAME, - '--connection' => 'lion_database_postgres', - ]); + ->execute(['migration' => self::MIGRATION_NAME]); } #[Testing] @@ -196,15 +181,15 @@ public function executeForSQLiteSchema(): void { $this->expectException(Exception::class); $this->expectExceptionCode(Http::INTERNAL_SERVER_ERROR); - $this->expectExceptionMessage('Currently, the driver does not support this type of migration.'); + $this->expectExceptionMessageIs('Currently, the driver does not support this type of migration.'); $this->commandTester ->setInputs([ + 'lion_database_sqlite', MigrationFactory::SCHEMA, ]) ->execute([ 'migration' => self::MIGRATION_NAME, - '--connection' => 'lion_database_sqlite', ]); } @@ -214,11 +199,11 @@ public function executeForMySQLTable(): void { $commandExecute = $this->commandTester ->setInputs([ + Connection::getDefaultConnectionName(), MigrationFactory::TABLE, ]) ->execute([ 'migration' => self::MIGRATION_NAME, - '--connection' => 'local', ]); $this->assertSame(Command::SUCCESS, $commandExecute); @@ -242,11 +227,11 @@ public function executeForMySQLView(): void { $commandExecute = $this->commandTester ->setInputs([ + Connection::getDefaultConnectionName(), MigrationFactory::VIEW, ]) ->execute([ 'migration' => self::MIGRATION_NAME, - '--connection' => 'local', ]); $this->assertSame(Command::SUCCESS, $commandExecute); @@ -270,11 +255,11 @@ public function executeForMySQLStoreProcedure(): void { $commandExecute = $this->commandTester ->setInputs([ + Connection::getDefaultConnectionName(), MigrationFactory::STORED_PROCEDURE, ]) ->execute([ 'migration' => self::MIGRATION_NAME, - '--connection' => 'local', ]); $this->assertSame(Command::SUCCESS, $commandExecute); @@ -298,11 +283,11 @@ public function executeForPostgreSQLTable(): void { $commandExecute = $this->commandTester ->setInputs([ + env('DB_NAME_TEST_POSTGRESQL'), MigrationFactory::TABLE, ]) ->execute([ 'migration' => self::MIGRATION_NAME, - '--connection' => 'lion_database_postgres', ]); $this->assertSame(Command::SUCCESS, $commandExecute); @@ -326,11 +311,11 @@ public function executeForPostgreSQLView(): void { $commandExecute = $this->commandTester ->setInputs([ + env('DB_NAME_TEST_POSTGRESQL'), MigrationFactory::VIEW, ]) ->execute([ 'migration' => self::MIGRATION_NAME, - '--connection' => 'lion_database_postgres', ]); $this->assertSame(Command::SUCCESS, $commandExecute); @@ -354,11 +339,11 @@ public function executeForPostgreSQLStoreProcedure(): void { $commandExecute = $this->commandTester ->setInputs([ + env('DB_NAME_TEST_POSTGRESQL'), MigrationFactory::STORED_PROCEDURE, ]) ->execute([ 'migration' => self::MIGRATION_NAME, - '--connection' => 'lion_database_postgres', ]); $this->assertSame(Command::SUCCESS, $commandExecute); diff --git a/tests/Commands/Lion/New/SeedCommandTest.php b/tests/Commands/Lion/New/SeedCommandTest.php index a1643f7a..12aa4bda 100644 --- a/tests/Commands/Lion/New/SeedCommandTest.php +++ b/tests/Commands/Lion/New/SeedCommandTest.php @@ -6,7 +6,6 @@ use DI\DependencyException; use DI\NotFoundException; -use InvalidArgumentException; use Lion\Bundle\Commands\Lion\New\SeedCommand; use Lion\Bundle\Helpers\Commands\ClassFactory; use Lion\Bundle\Helpers\Commands\Migrations\Migrations; @@ -14,9 +13,7 @@ use Lion\Bundle\Interface\SeedInterface; use Lion\Database\Connection; use Lion\Dependency\Injection\Container; -use Lion\Files\Store; use Lion\Helpers\Str; -use Lion\Request\Http; use Lion\Test\Test; use PHPUnit\Framework\Attributes\Test as Testing; use ReflectionException; @@ -68,28 +65,6 @@ protected function tearDown(): void $this->rmdirRecursively('./database/'); } - /** - * @throws ReflectionException If the property does not exist in the reflected - * class. - */ - #[Testing] - public function setStr(): void - { - $this->assertInstanceOf(SeedCommand::class, $this->seedCommand->setStr(new Str())); - $this->assertInstanceOf(Str::class, $this->getPrivateProperty('str')); - } - - /** - * @throws ReflectionException If the property does not exist in the reflected - * class. - */ - #[Testing] - public function setStore(): void - { - $this->assertInstanceOf(SeedCommand::class, $this->seedCommand->setStore(new Store())); - $this->assertInstanceOf(Store::class, $this->getPrivateProperty('store')); - } - /** * @throws ReflectionException If the property does not exist in the reflected * class. @@ -112,16 +87,6 @@ public function setClassFactory(): void $this->assertInstanceOf(ClassFactory::class, $this->getPrivateProperty('classFactory')); } - #[Testing] - public function executeWithoutConnection(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage("The '--connection' option is required."); - $this->expectExceptionCode(Http::INTERNAL_SERVER_ERROR); - - $this->commandTester->execute([]); - } - #[Testing] public function execute(): void { @@ -143,10 +108,12 @@ public function execute(): void $seedsPath = Migrations::SEEDS_PATH . "{$dbNamePascal}/{$dbType}/"; - $this->assertSame(Command::SUCCESS, $this->commandTester->execute([ - 'seed' => self::CLASS_NAME, - '--connection' => getDefaultConnection(), - ])); + $this->assertSame( + Command::SUCCESS, + $this->commandTester + ->setInputs([Connection::getDefaultConnectionName()]) + ->execute(['seed' => self::CLASS_NAME]) + ); $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTester->getDisplay()); $this->assertFileExists($seedsPath . self::FILE_NAME); diff --git a/tests/Helpers/Commands/Migrations/MigrationsTest.php b/tests/Helpers/Commands/Migrations/MigrationsTest.php index dff8c6d1..c3399b10 100644 --- a/tests/Helpers/Commands/Migrations/MigrationsTest.php +++ b/tests/Helpers/Commands/Migrations/MigrationsTest.php @@ -52,7 +52,7 @@ class MigrationsTest extends Test private const string FILE_NAME = self::CLASS_NAME . '.php'; private const string OUTPUT_MESSAGE = 'The migration was generated successfully.'; - private CommandTester $commandTester; + private CommandTester $commandTesterNewMigration; private Migrations $migrations; private Store $store; @@ -78,7 +78,7 @@ protected function setUp(): void $application->addCommand($migrationCommand); - $this->commandTester = new CommandTester($application->find('new:migration')); + $this->commandTesterNewMigration = new CommandTester($application->find('new:migration')); $this->initReflection($this->migrations); } @@ -102,17 +102,15 @@ public function setStore(): void #[Testing] public function orderList(): void { - $commandExecute = $this->commandTester + $commandExecute = $this->commandTesterNewMigration ->setInputs([ + getDefaultConnection(), MigrationFactory::TABLE, ]) - ->execute([ - 'migration' => self::MIGRATION_NAME, - '--connection' => env('DB_DEFAULT'), - ]); + ->execute(['migration' => self::MIGRATION_NAME]); $this->assertSame(Command::SUCCESS, $commandExecute); - $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTester->getDisplay()); + $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTesterNewMigration->getDisplay()); $this->assertFileExists(self::URL_PATH_MYSQL_TABLE . self::FILE_NAME); /** @phpstan-ignore-next-line */ @@ -147,17 +145,17 @@ public function orderList(): void #[Testing] public function getMigrations(): void { - $commandExecute = $this->commandTester + $connectionName = getDefaultConnection(); + + $commandExecute = $this->commandTesterNewMigration ->setInputs([ + $connectionName, MigrationFactory::SCHEMA, ]) - ->execute([ - 'migration' => self::MIGRATION_NAME, - '--connection' => env('DB_DEFAULT'), - ]); + ->execute(['migration' => self::MIGRATION_NAME]); $this->assertSame(Command::SUCCESS, $commandExecute); - $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTester->getDisplay()); + $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTesterNewMigration->getDisplay()); $this->assertFileExists(self::URL_PATH_MYSQL_SCHEMA . self::FILE_NAME); /** @phpstan-ignore-next-line */ @@ -170,17 +168,15 @@ public function getMigrations(): void SchemaInterface::class, ]); - $commandExecute = $this->commandTester + $commandExecute = $this->commandTesterNewMigration ->setInputs([ + $connectionName, MigrationFactory::TABLE, ]) - ->execute([ - 'migration' => self::MIGRATION_NAME, - '--connection' => env('DB_DEFAULT'), - ]); + ->execute(['migration' => self::MIGRATION_NAME]); $this->assertSame(Command::SUCCESS, $commandExecute); - $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTester->getDisplay()); + $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTesterNewMigration->getDisplay()); $this->assertFileExists(self::URL_PATH_MYSQL_TABLE . self::FILE_NAME); /** @phpstan-ignore-next-line */ @@ -193,17 +189,15 @@ public function getMigrations(): void TableInterface::class, ]); - $commandExecute = $this->commandTester + $commandExecute = $this->commandTesterNewMigration ->setInputs([ + $connectionName, MigrationFactory::VIEW, ]) - ->execute([ - 'migration' => self::MIGRATION_NAME, - '--connection' => env('DB_DEFAULT'), - ]); + ->execute(['migration' => self::MIGRATION_NAME]); $this->assertSame(Command::SUCCESS, $commandExecute); - $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTester->getDisplay()); + $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTesterNewMigration->getDisplay()); $this->assertFileExists(self::URL_PATH_MYSQL_VIEW . self::FILE_NAME); /** @phpstan-ignore-next-line */ @@ -216,17 +210,15 @@ public function getMigrations(): void ViewInterface::class, ]); - $commandExecute = $this->commandTester + $commandExecute = $this->commandTesterNewMigration ->setInputs([ + $connectionName, MigrationFactory::STORED_PROCEDURE, ]) - ->execute([ - 'migration' => self::MIGRATION_NAME, - '--connection' => env('DB_DEFAULT'), - ]); + ->execute(['migration' => self::MIGRATION_NAME]); $this->assertSame(Command::SUCCESS, $commandExecute); - $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTester->getDisplay()); + $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTesterNewMigration->getDisplay()); $this->assertFileExists(self::URL_PATH_MYSQL_STORED_PROCEDURE . self::FILE_NAME); /** @phpstan-ignore-next-line */ @@ -291,19 +283,17 @@ public function getMigrations(): void #[Testing] public function executeMigrationsGroup(): void { - $connectionName = 'local'; + $connectionName = getDefaultConnection(); - $commandExecute = $this->commandTester + $commandExecute = $this->commandTesterNewMigration ->setInputs([ + $connectionName, MigrationFactory::TABLE, ]) - ->execute([ - 'migration' => self::MIGRATION_NAME, - '--connection' => $connectionName, - ]); + ->execute(['migration' => self::MIGRATION_NAME]); $this->assertSame(Command::SUCCESS, $commandExecute); - $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTester->getDisplay()); + $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTesterNewMigration->getDisplay()); $this->assertFileExists(self::URL_PATH_MYSQL_TABLE . self::FILE_NAME); /** @@ -318,17 +308,15 @@ public function executeMigrationsGroup(): void TableInterface::class, ]); - $commandExecute = $this->commandTester + $commandExecute = $this->commandTesterNewMigration ->setInputs([ + $connectionName, MigrationFactory::VIEW, ]) - ->execute([ - 'migration' => self::MIGRATION_NAME, - '--connection' => $connectionName, - ]); + ->execute(['migration' => self::MIGRATION_NAME]); $this->assertSame(Command::SUCCESS, $commandExecute); - $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTester->getDisplay()); + $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTesterNewMigration->getDisplay()); $this->assertFileExists(self::URL_PATH_MYSQL_VIEW . self::FILE_NAME); /** @@ -343,19 +331,18 @@ public function executeMigrationsGroup(): void ViewInterface::class, ]); - $commandExecute = $this->commandTester + $commandExecute = $this->commandTesterNewMigration ->setInputs([ + $connectionName, MigrationFactory::STORED_PROCEDURE, ]) - ->execute([ - 'migration' => self::MIGRATION_NAME, - '--connection' => $connectionName, - ]); + ->execute(['migration' => self::MIGRATION_NAME]); $this->assertSame(Command::SUCCESS, $commandExecute); - $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTester->getDisplay()); + $this->assertStringContainsString(self::OUTPUT_MESSAGE, $this->commandTesterNewMigration->getDisplay()); $this->assertFileExists(self::URL_PATH_MYSQL_STORED_PROCEDURE . self::FILE_NAME); + /** @phpstan-ignore-next-line */ $objClass = new (self::CLASS_NAMESPACE_STORE_PROCEDURE . self::CLASS_NAME)(); $this->assertIsObject($objClass); @@ -382,7 +369,7 @@ public function processingWithStaticConnectionsConnectionDoesNotExists(string $c { $this->expectException(RuntimeException::class); $this->expectExceptionCode(Http::INTERNAL_SERVER_ERROR); - $this->expectExceptionMessage("The connection '{$connectionName}' does not exist."); + $this->expectExceptionMessageIs("The connection '{$connectionName}' does not exist."); $this->migrations->processingWithStaticConnections($connectionName, fn (): bool => true); } diff --git a/tests/Helpers/Commands/Seeds/SeedsTest.php b/tests/Helpers/Commands/Seeds/SeedsTest.php index c2b69221..33db9a34 100644 --- a/tests/Helpers/Commands/Seeds/SeedsTest.php +++ b/tests/Helpers/Commands/Seeds/SeedsTest.php @@ -98,10 +98,12 @@ public function executeSeedsGroup(): void $seedsPath = Migrations::SEEDS_PATH . "{$dbNamePascal}/{$dbType}/"; - $this->assertSame(Command::SUCCESS, $this->commandTester->execute([ - 'seed' => self::CLASS_NAME, - '--connection' => $connectionName, - ])); + $this->assertSame( + Command::SUCCESS, + $this->commandTester + ->setInputs([getDefaultConnection()]) + ->execute(['seed' => self::CLASS_NAME]) + ); $this->assertStringContainsString(self::MESSAGE_OUTPUT, $this->commandTester->getDisplay()); $this->assertFileExists($seedsPath . self::FILE_NAME); diff --git a/tests/Helpers/Commands/Selection/MenuCommandTest.php b/tests/Helpers/Commands/Selection/MenuCommandTest.php index 477d9876..68f86e2c 100644 --- a/tests/Helpers/Commands/Selection/MenuCommandTest.php +++ b/tests/Helpers/Commands/Selection/MenuCommandTest.php @@ -7,6 +7,7 @@ use DI\DependencyException; use DI\NotFoundException; use Exception; +use Lion\Bundle\Helpers\Commands\Migrations\MigrationFactory; use Lion\Bundle\Helpers\Commands\Selection\MenuCommand; use Lion\Database\Connection; use Lion\Database\Drivers\MySQL as DB; @@ -118,7 +119,7 @@ public function setStore(): void public function selectedProjectNotAvailable(): void { $this->expectException(Exception::class); - $this->expectExceptionMessage('there are no projects available'); + $this->expectExceptionMessageIs('there are no projects available'); $this->expectExceptionCode(Http::INTERNAL_SERVER_ERROR); $this->createDirectory(self::RESOURCES_PATH); @@ -317,7 +318,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { - $template = $this->selectedTemplate($input, $output, self::VITE_TEMPLATES, 'React', 2); + $template = $this->selectedTemplate($input, $output, self::VITE_TEMPLATES); $output->write("({$template})"); @@ -397,7 +398,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { - $connection = $this->selectConnection($input, $output); + $connection = $this->selectConnection(); $output->write("({$connection})"); @@ -414,12 +415,33 @@ protected function execute(InputInterface $input, OutputInterface $output): int $commandTester = new CommandTester($application->find('test:menu:command')); - $this->assertSame(Command::SUCCESS, $commandTester->setInputs(["0"])->execute([])); - $this->assertStringContainsString("(local)", $commandTester->getDisplay()); - $this->assertSame($_ENV['SELECTED_CONNECTION'], 'local'); - $this->assertSame(Command::SUCCESS, $commandTester->setInputs(["1"])->execute([])); - $this->assertStringContainsString("(lion_database_test)", $commandTester->getDisplay()); - $this->assertSame($_ENV['SELECTED_CONNECTION'], 'lion_database_test'); + $connectionName = getDefaultConnection(); + + $this->assertSame( + Command::SUCCESS, + $commandTester + ->setInputs([$connectionName]) + ->execute([]) + ); + + $this->assertStringContainsString('local[mysql]', $commandTester->getDisplay()); + $this->assertSame($connectionName, $_ENV['SELECTED_CONNECTION']); + + unset($_ENV['SELECTED_CONNECTION']); + + $this->assertArrayNotHasKey('SELECTED_CONNECTION', $_ENV); + + $connectionName = env('DB_NAME_TEST'); + + $this->assertSame( + Command::SUCCESS, + $commandTester + ->setInputs([$connectionName]) + ->execute([]) + ); + + $this->assertStringContainsString('lion_database_test[mysql]', $commandTester->getDisplay()); + $this->assertSame($connectionName, $_ENV['SELECTED_CONNECTION']); unset($_ENV['SELECTED_CONNECTION']); @@ -442,7 +464,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { - $connection = $this->selectConnection($input, $output); + $connection = $this->selectConnection(); $output->write("({$connection})"); @@ -478,7 +500,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->assertSame(Command::SUCCESS, $commandTester->setInputs([''])->execute([])); $this->assertStringContainsString('(local)', $commandTester->getDisplay()); - $this->assertSame($_ENV['SELECTED_CONNECTION'], 'local'); + $this->assertSame('local', $_ENV['SELECTED_CONNECTION']); unset($_ENV['SELECTED_CONNECTION']); @@ -526,9 +548,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int $commandTester = new CommandTester($application->find('test:menu:command')); - $this->assertSame(Command::SUCCESS, $commandTester->setInputs(['0'])->execute([])); - $this->assertStringContainsString('(local)', $commandTester->getDisplay()); - $this->assertSame($_ENV['SELECTED_CONNECTION'], 'local'); + $connectionName = getDefaultConnection(); + + $this->assertSame( + Command::SUCCESS, + $commandTester + ->setInputs([$connectionName]) + ->execute([]) + ); + + $this->assertStringContainsString($connectionName, $commandTester->getDisplay()); + $this->assertSame($connectionName, $_ENV['SELECTED_CONNECTION']); unset($_ENV['SELECTED_CONNECTION']); @@ -568,12 +598,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int $commandTester = new CommandTester($application->find('test:menu:command')); - $this->assertSame(Command::SUCCESS, $commandTester->setInputs(["0"])->execute([])); - $this->assertStringContainsString("(local)", $commandTester->getDisplay()); - $this->assertSame($_ENV['SELECTED_CONNECTION'], 'local'); - $this->assertSame(Command::SUCCESS, $commandTester->setInputs(["0"])->execute([])); - $this->assertStringContainsString("(local)", $commandTester->getDisplay()); - $this->assertSame($_ENV['SELECTED_CONNECTION'], 'local'); + $connectionName = getDefaultConnection(); + + $this->assertSame( + Command::SUCCESS, + $commandTester + ->setInputs([$connectionName]) + ->execute([]) + ); + + $this->assertStringContainsString("$connectionName", $commandTester->getDisplay()); + $this->assertSame($connectionName, $_ENV['SELECTED_CONNECTION']); + + $this->assertSame( + Command::SUCCESS, + $commandTester + ->setInputs([$connectionName]) + ->execute([]) + ); + + $this->assertStringContainsString($connectionName, $commandTester->getDisplay()); + $this->assertSame($connectionName, $_ENV['SELECTED_CONNECTION']); unset($_ENV['SELECTED_CONNECTION']); @@ -588,17 +633,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int public function selectMigrationType(): void { $command = new class () extends MenuCommand { - private const string SCHEMA = 'Schema'; - private const string TABLE = 'Table'; - private const string VIEW = 'View'; - private const string STORE_PROCEDURE = 'Store-Procedure'; - private const array OPTIONS = [ - self::SCHEMA, - self::TABLE, - self::VIEW, - self::STORE_PROCEDURE, - ]; - protected function configure(): void { $this->setName('test:menu:command'); @@ -606,7 +640,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { - $connection = $this->selectMigrationType($input, $output, self::OPTIONS); + $connection = $this->selectMigrationType(); $output->write("({$connection})"); @@ -623,14 +657,41 @@ protected function execute(InputInterface $input, OutputInterface $output): int $commandTester = new CommandTester($application->find('test:menu:command')); - $this->assertSame(Command::SUCCESS, $commandTester->setInputs(['0'])->execute([])); - $this->assertStringContainsString('(Schema)', $commandTester->getDisplay()); - $this->assertSame(Command::SUCCESS, $commandTester->setInputs(['1'])->execute([])); - $this->assertStringContainsString('(Table)', $commandTester->getDisplay()); - $this->assertSame(Command::SUCCESS, $commandTester->setInputs(['2'])->execute([])); - $this->assertStringContainsString('(View)', $commandTester->getDisplay()); - $this->assertSame(Command::SUCCESS, $commandTester->setInputs(['3'])->execute([])); - $this->assertStringContainsString('(Store-Procedure)', $commandTester->getDisplay()); + $this->assertSame( + Command::SUCCESS, + $commandTester + ->setInputs([MigrationFactory::SCHEMA]) + ->execute([]) + ); + + $this->assertStringContainsString(MigrationFactory::SCHEMA, $commandTester->getDisplay()); + + $this->assertSame( + Command::SUCCESS, + $commandTester + ->setInputs([MigrationFactory::TABLE]) + ->execute([]) + ); + + $this->assertStringContainsString(MigrationFactory::TABLE, $commandTester->getDisplay()); + + $this->assertSame( + Command::SUCCESS, + $commandTester + ->setInputs([MigrationFactory::VIEW]) + ->execute([]) + ); + + $this->assertStringContainsString(MigrationFactory::VIEW, $commandTester->getDisplay()); + + $this->assertSame( + Command::SUCCESS, + $commandTester + ->setInputs([MigrationFactory::STORED_PROCEDURE]) + ->execute([]) + ); + + $this->assertStringContainsString(MigrationFactory::STORED_PROCEDURE, $commandTester->getDisplay()); } /** diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 778bb22e..51c5a5e4 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -18,7 +18,6 @@ require_once(__DIR__ . '/../vendor/autoload.php'); use Dotenv\Dotenv; -use Lion\Database\Driver; use Lion\Files\Store; /** @@ -41,40 +40,4 @@ * ----------------------------------------------------------------------------- */ -$privateConnections = [ - env('DB_DEFAULT') => [ - 'type' => env('DB_TYPE'), - 'host' => env('DB_HOST'), - 'port' => env('DB_PORT'), - 'dbname' => env('DB_NAME'), - 'user' => env('DB_USER'), - 'password' => env('DB_PASSWORD'), - ], - env('DB_NAME_TEST') => [ - 'type' => env('DB_TYPE_TEST'), - 'host' => env('DB_HOST_TEST'), - 'port' => env('DB_PORT_TEST'), - 'dbname' => env('DB_NAME_TEST'), - 'user' => env('DB_USER_TEST'), - 'password' => env('DB_PASSWORD_TEST'), - ], - env('DB_NAME_TEST_POSTGRESQL') => [ - 'type' => env('DB_TYPE_TEST_POSTGRESQL'), - 'host' => env('DB_HOST_TEST_POSTGRESQL'), - 'port' => env('DB_PORT_TEST_POSTGRESQL'), - 'dbname' => env('DB_NAME'), - 'user' => env('DB_USER_TEST_POSTGRESQL'), - 'password' => env('DB_PASSWORD_TEST_POSTGRESQL'), - ], - 'lion_database_sqlite' => [ - 'type' => env('DB_TYPE_TEST_SQLITE'), - 'dbname' => __DIR__ . '/' . env('DB_NAME_TEST_SQLITE'), - ], -]; - -Driver::run([ - 'default' => env('DB_DEFAULT'), - 'connections' => $privateConnections, -]); - -define('NUMBER_OF_ACTIVE_CONNECTIONS', count($privateConnections)); +require_once(__DIR__ . '/../public/connections.php');