diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 3e399791..68066412 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -150,10 +150,4 @@ self::VERBOSITY_* - - - $split[0] - $splitted[0] - - diff --git a/src/Command/BakeMigrationSnapshotCommand.php b/src/Command/BakeMigrationSnapshotCommand.php index 4b0427e7..ea711069 100644 --- a/src/Command/BakeMigrationSnapshotCommand.php +++ b/src/Command/BakeMigrationSnapshotCommand.php @@ -24,7 +24,7 @@ use Cake\Datasource\ConnectionManager; use Cake\Event\Event; use Cake\Event\EventManager; -use Migrations\TableFinderTrait; +use Migrations\Util\TableFinder; use Migrations\Util\UtilTrait; /** @@ -33,7 +33,6 @@ class BakeMigrationSnapshotCommand extends BakeSimpleMigrationCommand { use SnapshotTrait; - use TableFinderTrait; use UtilTrait; /** @@ -95,7 +94,8 @@ public function templateData(Arguments $arguments): array 'require-table' => $arguments->getOption('require-table'), 'plugin' => $this->plugin, ]; - $tables = $this->getTablesToBake($collection, $options); + $finder = new TableFinder($this->connection); + $tables = $finder->getTablesToBake($collection, $options); sort($tables, SORT_NATURAL); diff --git a/src/Command/DumpCommand.php b/src/Command/DumpCommand.php index 8309f7f3..ba672f89 100644 --- a/src/Command/DumpCommand.php +++ b/src/Command/DumpCommand.php @@ -21,7 +21,7 @@ use Cake\Datasource\ConnectionManager; use Migrations\Config\ConfigInterface; use Migrations\Migration\ManagerFactory; -use Migrations\TableFinderTrait; +use Migrations\Util\TableFinder; /** * Dump command class. @@ -30,8 +30,6 @@ */ class DumpCommand extends Command { - use TableFinderTrait; - protected string $connection; /** @@ -113,7 +111,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int 'connection' => $args->getOption('connection'), ]); $config = $factory->createConfig(); - $path = $config->getMigrationPaths()[0]; + $path = $config->getMigrationPath(); $connectionName = (string)$config->getConnection(); $connection = ConnectionManager::get($connectionName); assert($connection instanceof Connection); @@ -125,7 +123,8 @@ public function execute(Arguments $args, ConsoleIo $io): ?int ]; // The connection property is used by the trait methods. $this->connection = $connectionName; - $tables = $this->getTablesToBake($collection, $options); + $finder = new TableFinder($connectionName); + $tables = $finder->getTablesToBake($collection, $options); $dump = []; if ($tables) { diff --git a/src/Command/MarkMigratedCommand.php b/src/Command/MarkMigratedCommand.php index 4567b315..6f969d8d 100644 --- a/src/Command/MarkMigratedCommand.php +++ b/src/Command/MarkMigratedCommand.php @@ -116,8 +116,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int ]); $manager = $factory->createManager($io); $config = $manager->getConfig(); - $migrationPaths = $config->getMigrationPaths(); - $path = array_pop($migrationPaths); + $path = $config->getMigrationPath(); if ($this->invalidOnlyOrExclude($args)) { $io->err( diff --git a/src/Command/MigrateCommand.php b/src/Command/MigrateCommand.php index 5de18e3e..acd906fc 100644 --- a/src/Command/MigrateCommand.php +++ b/src/Command/MigrateCommand.php @@ -138,7 +138,7 @@ protected function executeMigrations(Arguments $args, ConsoleIo $io): ?int } $io->out('using connection ' . (string)$args->getOption('connection')); $io->out('using connection ' . (string)$args->getOption('connection')); - $io->out('using paths ' . implode(', ', $config->getMigrationPaths())); + $io->out('using paths ' . $config->getMigrationPath()); $io->out('ordering by ' . $versionOrder . ' time'); if ($fake) { diff --git a/src/Command/Phinx/Dump.php b/src/Command/Phinx/Dump.php index b796df1d..78208837 100644 --- a/src/Command/Phinx/Dump.php +++ b/src/Command/Phinx/Dump.php @@ -16,7 +16,7 @@ use Cake\Database\Connection; use Cake\Datasource\ConnectionManager; use Migrations\ConfigurationTrait; -use Migrations\TableFinderTrait; +use Migrations\Util\TableFinder; use Phinx\Console\Command\AbstractCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -31,7 +31,6 @@ class Dump extends AbstractCommand { use CommandTrait; use ConfigurationTrait; - use TableFinderTrait; /** * Output object. @@ -96,7 +95,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'require-table' => false, 'plugin' => $this->getPlugin($input), ]; - $tables = $this->getTablesToBake($collection, $options); + $finder = new TableFinder($connectionName); + $tables = $finder->getTablesToBake($collection, $options); $dump = []; if ($tables) { diff --git a/src/Command/RollbackCommand.php b/src/Command/RollbackCommand.php index a0018bab..0711030c 100644 --- a/src/Command/RollbackCommand.php +++ b/src/Command/RollbackCommand.php @@ -141,7 +141,7 @@ protected function executeMigrations(Arguments $args, ConsoleIo $io): ?int $versionOrder = $config->getVersionOrder(); $io->out('using connection ' . (string)$args->getOption('connection')); - $io->out('using paths ' . implode(', ', $config->getMigrationPaths())); + $io->out('using paths ' . $config->getMigrationPath()); $io->out('ordering by ' . $versionOrder . ' time'); if ($dryRun) { diff --git a/src/Command/SeedCommand.php b/src/Command/SeedCommand.php index c14e3a03..b9dbacac 100644 --- a/src/Command/SeedCommand.php +++ b/src/Command/SeedCommand.php @@ -115,7 +115,7 @@ protected function executeSeeds(Arguments $args, ConsoleIo $io): ?int $versionOrder = $config->getVersionOrder(); $io->out('using connection ' . (string)$args->getOption('connection')); - $io->out('using paths ' . implode(', ', $config->getMigrationPaths())); + $io->out('using paths ' . $config->getMigrationPath()); $io->out('ordering by ' . $versionOrder . ' time'); $start = microtime(true); diff --git a/src/Config/Config.php b/src/Config/Config.php index 80f50202..2d6d9be9 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -10,7 +10,6 @@ use Closure; use InvalidArgumentException; -use Psr\Container\ContainerInterface; use ReturnTypeWillChange; use UnexpectedValueException; @@ -50,22 +49,26 @@ public function __construct(array $configArray) */ public function getEnvironment(): ?array { - // TODO evolve this into connection only. - return $this->values['environment'] ?? null; + if (empty($this->values['environment'])) { + return null; + } + $config = (array)$this->values['environment']; + $config['version_order'] = $this->getVersionOrder(); + + return $config; } /** * @inheritDoc * @throws \UnexpectedValueException */ - public function getMigrationPaths(): array + public function getMigrationPath(): string { if (!isset($this->values['paths']['migrations'])) { throw new UnexpectedValueException('Migrations path missing from config file'); } - - if (is_string($this->values['paths']['migrations'])) { - $this->values['paths']['migrations'] = [$this->values['paths']['migrations']]; + if (is_array($this->values['paths']['migrations']) && isset($this->values['paths']['migrations'][0])) { + return $this->values['paths']['migrations'][0]; } return $this->values['paths']['migrations']; @@ -75,14 +78,13 @@ public function getMigrationPaths(): array * @inheritDoc * @throws \UnexpectedValueException */ - public function getSeedPaths(): array + public function getSeedPath(): string { if (!isset($this->values['paths']['seeds'])) { throw new UnexpectedValueException('Seeds path missing from config file'); } - - if (is_string($this->values['paths']['seeds'])) { - $this->values['paths']['seeds'] = [$this->values['paths']['seeds']]; + if (is_array($this->values['paths']['seeds']) && isset($this->values['paths']['seeds'][0])) { + return $this->values['paths']['seeds'][0]; } return $this->values['paths']['seeds']; @@ -93,6 +95,7 @@ public function getSeedPaths(): array */ public function getMigrationBaseClassName(bool $dropNamespace = true): string { + /** @var string $className */ $className = !isset($this->values['migration_base_class']) ? 'Phinx\Migration\AbstractMigration' : $this->values['migration_base_class']; return $dropNamespace ? (substr((string)strrchr($className, '\\'), 1) ?: $className) : $className; @@ -103,6 +106,7 @@ public function getMigrationBaseClassName(bool $dropNamespace = true): string */ public function getSeedBaseClassName(bool $dropNamespace = true): string { + /** @var string $className */ $className = !isset($this->values['seed_base_class']) ? 'Phinx\Seed\AbstractSeed' : $this->values['seed_base_class']; return $dropNamespace ? substr((string)strrchr($className, '\\'), 1) : $className; @@ -152,18 +156,6 @@ public function getTemplateStyle(): string return $this->values['templates']['style'] === self::TEMPLATE_STYLE_UP_DOWN ? self::TEMPLATE_STYLE_UP_DOWN : self::TEMPLATE_STYLE_CHANGE; } - /** - * @inheritDoc - */ - public function getContainer(): ?ContainerInterface - { - if (!isset($this->values['container'])) { - return null; - } - - return $this->values['container']; - } - /** * @inheritdoc */ diff --git a/src/Config/ConfigInterface.php b/src/Config/ConfigInterface.php index b1b2db12..790f4c15 100644 --- a/src/Config/ConfigInterface.php +++ b/src/Config/ConfigInterface.php @@ -9,7 +9,6 @@ namespace Migrations\Config; use ArrayAccess; -use Psr\Container\ContainerInterface; /** * Phinx configuration interface. @@ -32,18 +31,18 @@ interface ConfigInterface extends ArrayAccess public function getEnvironment(): ?array; /** - * Gets the paths to search for migration files. + * Gets the path to search for migration files. * - * @return string[] + * @return string */ - public function getMigrationPaths(): array; + public function getMigrationPath(): string; /** - * Gets the paths to search for seed files. + * Gets the path to search for seed files. * - * @return string[] + * @return string */ - public function getSeedPaths(): array; + public function getSeedPath(): string; /** * Get the connection namee @@ -73,13 +72,6 @@ public function getTemplateClass(): string|false; */ public function getTemplateStyle(): string; - /** - * Get the user-provided container for instantiating seeds - * - * @return \Psr\Container\ContainerInterface|null - */ - public function getContainer(): ?ContainerInterface; - /** * Get the version order. * diff --git a/src/Db/Adapter/PdoAdapter.php b/src/Db/Adapter/PdoAdapter.php index 0cf25416..eab82677 100644 --- a/src/Db/Adapter/PdoAdapter.php +++ b/src/Db/Adapter/PdoAdapter.php @@ -134,8 +134,6 @@ public function setOptions(array $options): AdapterInterface */ public function setConnection(Connection $connection): AdapterInterface { - // TODO how do PDO connection flags get set? Phinx used to - // turn on exception error mode, and I don't think Cake does that by default. $this->connection = $connection; // Create the schema table if it doesn't already exist diff --git a/src/Db/Literal.php b/src/Db/Literal.php index 45e9bb4f..b318daa1 100644 --- a/src/Db/Literal.php +++ b/src/Db/Literal.php @@ -8,7 +8,10 @@ namespace Migrations\Db; -// TODO replace/merge with cakephp/database +/** + * Represent a value that should be used as a literal value when being + * interpolated into SQL commands. + */ class Literal { /** diff --git a/src/Migration/BuiltinBackend.php b/src/Migration/BuiltinBackend.php index ff6d798c..2b806d0e 100644 --- a/src/Migration/BuiltinBackend.php +++ b/src/Migration/BuiltinBackend.php @@ -197,7 +197,7 @@ public function markMigrated(int|string|null $version = null, array $options = [ $manager = $this->getManager($options); $config = $manager->getConfig(); - $path = $config->getMigrationPaths()[0]; + $path = $config->getMigrationPath(); $versions = $manager->getVersionsToMark($args); $manager->markVersionsAsMigrated($path, $versions); diff --git a/src/Migration/Manager.php b/src/Migration/Manager.php index 475620cb..52a495d3 100644 --- a/src/Migration/Manager.php +++ b/src/Migration/Manager.php @@ -708,7 +708,7 @@ public function getEnvironment(): Environment $config = $this->getConfig(); // create an environment instance and cache it $envOptions = $config->getEnvironment(); - $envOptions['version_order'] = $config->getVersionOrder(); + assert(is_array($envOptions)); $environment = new Environment('default', $envOptions); $environment->setIo($this->getIo()); @@ -888,7 +888,7 @@ function ($phpFile) { */ protected function getMigrationFiles(): array { - return Util::getFiles($this->getConfig()->getMigrationPaths()); + return Util::getFiles($this->getConfig()->getMigrationPath()); } /** @@ -1040,7 +1040,7 @@ public function getSeeds(): array */ protected function getSeedFiles(): array { - return Util::getFiles($this->getConfig()->getSeedPaths()); + return Util::getFiles($this->getConfig()->getSeedPath()); } /** diff --git a/src/Migration/ManagerFactory.php b/src/Migration/ManagerFactory.php index d7bca990..09ea5dfe 100644 --- a/src/Migration/ManagerFactory.php +++ b/src/Migration/ManagerFactory.php @@ -110,7 +110,6 @@ public function createConfig(): ConfigInterface $configData = [ 'paths' => [ - // TODO make paths a simple list. 'migrations' => $dir, 'seeds' => $dir, ], @@ -125,7 +124,6 @@ public function createConfig(): ConfigInterface 'unsigned_primary_keys' => Configure::read('Migrations.unsigned_primary_keys'), 'column_null_default' => Configure::read('Migrations.column_null_default'), ], - // TODO do we want to support the DI container in migrations? ]; return new Config($configData); diff --git a/src/Migration/PhinxBackend.php b/src/Migration/PhinxBackend.php index 2c41fd55..8b81a511 100644 --- a/src/Migration/PhinxBackend.php +++ b/src/Migration/PhinxBackend.php @@ -146,9 +146,6 @@ public function getCommand(): string */ public function status(array $options = []): array { - // TODO This class could become an interface that chooses between a phinx and builtin - // implementation. Having two implementations would be easier to cleanup - // than having all the logic in one class with branching $input = $this->getInput('Status', [], $options); $params = ['default', $input->getOption('format')]; diff --git a/src/TableFinderTrait.php b/src/Util/TableFinder.php similarity index 88% rename from src/TableFinderTrait.php rename to src/Util/TableFinder.php index 7320ebcc..c9cbe392 100644 --- a/src/TableFinderTrait.php +++ b/src/Util/TableFinder.php @@ -11,7 +11,7 @@ * @link https://cakephp.org CakePHP(tm) Project * @license https://www.opensource.org/licenses/mit-license.php MIT License */ -namespace Migrations; +namespace Migrations\Util; use Cake\Core\App; use Cake\Core\Plugin as CorePlugin; @@ -20,8 +20,10 @@ use Cake\ORM\TableRegistry; use ReflectionClass; -// TODO(mark) Make this into a standalone class instead of a trait. -trait TableFinderTrait +/** + * @internal + */ +class TableFinder { /** * Tables to skip @@ -37,6 +39,15 @@ trait TableFinderTrait */ public string $skipTablesRegex = '_phinxlog'; + /** + * Constructor + * + * @param string $connection The connection name to use. + */ + public function __construct(protected string $connection) + { + } + /** * Gets a list of table to baked based on the Collection instance passed and the options passed to * the shell call. @@ -46,7 +57,7 @@ trait TableFinderTrait * @param array $options Array of options passed to a shell call. * @return array */ - protected function getTablesToBake(CollectionInterface $collection, array $options = []): array + public function getTablesToBake(CollectionInterface $collection, array $options = []): array { $options += [ 'require-table' => false, @@ -71,7 +82,7 @@ protected function getTablesToBake(CollectionInterface $collection, array $optio $config = (array)ConnectionManager::getConfig($this->connection); $key = isset($config['schema']) ? 'schema' : 'database'; - if ($config[$key] === $split[1]) { + if (isset($split[0]) && $config[$key] === $split[1]) { $table = $split[0]; } } @@ -99,7 +110,7 @@ protected function getTablesToBake(CollectionInterface $collection, array $optio * @param string|null $pluginName Plugin name if exists. * @return string[] */ - protected function getTableNames(?string $pluginName = null): array + public function getTableNames(?string $pluginName = null): array { if ($pluginName !== null && !CorePlugin::getCollection()->has($pluginName)) { return []; @@ -124,7 +135,7 @@ protected function getTableNames(?string $pluginName = null): array * @param string|null $pluginName Plugin name if exists. * @return array */ - protected function findTables(?string $pluginName = null): array + public function findTables(?string $pluginName = null): array { $path = 'Model' . DS . 'Table' . DS; if ($pluginName) { @@ -148,7 +159,7 @@ protected function findTables(?string $pluginName = null): array * @param string|null $pluginName Plugin name if exists. * @return string[] */ - protected function fetchTableName(string $className, ?string $pluginName = null): array + public function fetchTableName(string $className, ?string $pluginName = null): array { $tables = []; $className = str_replace('Table.php', '', $className); @@ -186,7 +197,7 @@ protected function fetchTableName(string $className, ?string $pluginName = null) $config = ConnectionManager::getConfig($this->connection); if ($config) { $key = isset($config['schema']) ? 'schema' : 'database'; - if ($config[$key] === $splitted[1]) { + if (isset($splitted[0]) && $config[$key] === $splitted[1]) { $tableName = $splitted[0]; } } diff --git a/tests/TestCase/Command/BakeMigrationSnapshotCommandTest.php b/tests/TestCase/Command/BakeMigrationSnapshotCommandTest.php index 60d7da8d..8d715f26 100644 --- a/tests/TestCase/Command/BakeMigrationSnapshotCommandTest.php +++ b/tests/TestCase/Command/BakeMigrationSnapshotCommandTest.php @@ -82,32 +82,6 @@ public function tearDown(): void } } - /** - * Test that the BakeMigrationSnapshotCommand::getTableNames properly returns the table list - * when we want tables from a plugin - * - * @return void - */ - public function testGetTableNames() - { - /** @var \Migrations\Test\TestCase\Command\TestClassWithSnapshotTrait|\PHPUnit\Framework\MockObject\MockObject $class */ - $class = $this->getMockBuilder(TestClassWithSnapshotTrait::class) - ->onlyMethods(['findTables', 'fetchTableName']) - ->getMock(); - - $class->expects($this->any()) - ->method('findTables') - ->with('TestBlog') - ->will($this->returnValue(['ArticlesTable.php', 'TagsTable.php'])); - - $class->method('fetchTableName') - ->will($this->onConsecutiveCalls(['articles_tags', 'articles'], ['articles_tags', 'tags'])); - - $results = $class->getTableNames('TestBlog'); - $expected = ['articles_tags', 'articles', 'tags']; - $this->assertEquals(array_values($expected), array_values($results)); - } - /** * Test baking a snapshot * @@ -235,40 +209,6 @@ protected function runSnapshotTest(string $scenario, string $arguments = ''): vo $this->assertCorrectSnapshot($bakeName, file_get_contents($this->generatedFiles[0])); } - /** - * Test that using MigrationSnapshotTask::fetchTableName in a Table object class - * where the table name is composed with the database name (e.g. mydb.mytable) - * will return : - * - only the table name if the current connection `database` parameter is the first part - * of the table name - * - the full string (e.g. mydb.mytable) if the current connection `database` parameter - * is not the first part of the table name - * - * @return void - */ - public function testFetchTableNames() - { - $class = new TestClassWithSnapshotTrait(); - $class->connection = 'alternative'; - $expected = ['alternative.special_tags']; - $this->assertEquals($expected, $class->fetchTableName('SpecialTagsTable.php', 'TestBlog')); - - ConnectionManager::setConfig('alternative', [ - 'database' => 'alternative', - ]); - $class->connection = 'alternative'; - $expected = ['special_tags']; - $this->assertEquals($expected, $class->fetchTableName('SpecialTagsTable.php', 'TestBlog')); - - ConnectionManager::drop('alternative'); - ConnectionManager::setConfig('alternative', [ - 'schema' => 'alternative', - ]); - $class->connection = 'alternative'; - $expected = ['special_tags']; - $this->assertEquals($expected, $class->fetchTableName('SpecialTagsTable.php', 'TestBlog')); - } - /** * Get the baked filename based on the current db environment * diff --git a/tests/TestCase/Command/TestClassWithSnapshotTrait.php b/tests/TestCase/Command/TestClassWithSnapshotTrait.php deleted file mode 100644 index f20977fb..00000000 --- a/tests/TestCase/Command/TestClassWithSnapshotTrait.php +++ /dev/null @@ -1,52 +0,0 @@ -publicFetchTableName($className, $pluginName); - } - - /** - * @param string|null $pluginName - * @return string[] - */ - public function getTableNames($pluginName = null) - { - return $this->publicGetTableNames($pluginName); - } -} diff --git a/tests/TestCase/Config/AbstractConfigTestCase.php b/tests/TestCase/Config/AbstractConfigTestCase.php index e0a0c0cf..91091c20 100644 --- a/tests/TestCase/Config/AbstractConfigTestCase.php +++ b/tests/TestCase/Config/AbstractConfigTestCase.php @@ -48,8 +48,8 @@ public function getConfigArray() ], ], 'paths' => [ - 'migrations' => $this->getMigrationPaths(), - 'seeds' => $this->getSeedPaths(), + 'migrations' => $this->getMigrationPath(), + 'seeds' => $this->getSeedPath(), ], 'templates' => [ 'file' => '%%PHINX_CONFIG_PATH%%/tpl/testtemplate.txt', @@ -74,8 +74,8 @@ public function getMigrationsConfigArray(): array return [ 'paths' => [ - 'migrations' => $this->getMigrationPaths(), - 'seeds' => $this->getSeedPaths(), + 'migrations' => $this->getMigrationPath(), + 'seeds' => $this->getSeedPath(), ], 'environment' => $adapter, ]; @@ -84,28 +84,28 @@ public function getMigrationsConfigArray(): array /** * Generate dummy migration paths * - * @return string[] + * @return string */ - protected function getMigrationPaths() + protected function getMigrationPath(): string { if ($this->migrationPath === null) { $this->migrationPath = uniqid('phinx', true); } - return [$this->migrationPath]; + return $this->migrationPath; } /** * Generate dummy seed paths * - * @return string[] + * @return string */ - protected function getSeedPaths() + protected function getSeedPath(): string { if ($this->seedPath === null) { $this->seedPath = uniqid('phinx', true); } - return [$this->seedPath]; + return $this->seedPath; } } diff --git a/tests/TestCase/Config/ConfigMigrationPathsTest.php b/tests/TestCase/Config/ConfigMigrationPathsTest.php index eeac98a5..8a1165ba 100644 --- a/tests/TestCase/Config/ConfigMigrationPathsTest.php +++ b/tests/TestCase/Config/ConfigMigrationPathsTest.php @@ -16,7 +16,7 @@ public function testGetMigrationPathsThrowsExceptionForNoPath() $this->expectException(UnexpectedValueException::class); - $config->getMigrationPaths(); + $config->getMigrationPath(); } /** @@ -25,21 +25,6 @@ public function testGetMigrationPathsThrowsExceptionForNoPath() public function testGetMigrationPaths() { $config = new Config($this->getConfigArray()); - $this->assertEquals($this->getMigrationPaths(), $config->getMigrationPaths()); - } - - public function testGetMigrationPathConvertsStringToArray() - { - $values = [ - 'paths' => [ - 'migrations' => '/test', - ], - ]; - - $config = new Config($values); - $paths = $config->getMigrationPaths(); - - $this->assertIsArray($paths); - $this->assertCount(1, $paths); + $this->assertEquals($this->getMigrationPath(), $config->getMigrationPath()); } } diff --git a/tests/TestCase/Config/ConfigSeedPathsTest.php b/tests/TestCase/Config/ConfigSeedPathsTest.php index f8bfb147..8697979b 100644 --- a/tests/TestCase/Config/ConfigSeedPathsTest.php +++ b/tests/TestCase/Config/ConfigSeedPathsTest.php @@ -16,7 +16,7 @@ public function testGetSeedPathsThrowsExceptionForNoPath() $this->expectException(UnexpectedValueException::class); - $config->getSeedPaths(); + $config->getSeedPath(); } /** @@ -25,7 +25,7 @@ public function testGetSeedPathsThrowsExceptionForNoPath() public function testGetSeedPaths() { $config = new Config($this->getConfigArray()); - $this->assertEquals($this->getSeedPaths(), $config->getSeedPaths()); + $this->assertEquals($this->getSeedPath(), $config->getSeedPath()); } public function testGetSeedPathConvertsStringToArray() @@ -37,9 +37,7 @@ public function testGetSeedPathConvertsStringToArray() ]; $config = new Config($values); - $paths = $config->getSeedPaths(); - - $this->assertIsArray($paths); - $this->assertCount(1, $paths); + $path = $config->getSeedPath(); + $this->assertEquals('/test', $path); } } diff --git a/tests/TestCase/Config/ConfigSeedTemplatePathsTest.php b/tests/TestCase/Config/ConfigSeedTemplatePathsTest.php index d259d05e..580c7c61 100644 --- a/tests/TestCase/Config/ConfigSeedTemplatePathsTest.php +++ b/tests/TestCase/Config/ConfigSeedTemplatePathsTest.php @@ -56,6 +56,6 @@ public function testNoCustomSeedTemplate() $actualValue = $config->getSeedTemplateFile(); $this->assertNull($actualValue); - $config->getSeedPaths(); + $config->getSeedPath(); } } diff --git a/tests/TestCase/Config/ConfigTest.php b/tests/TestCase/Config/ConfigTest.php index df93d20a..ada4bd41 100644 --- a/tests/TestCase/Config/ConfigTest.php +++ b/tests/TestCase/Config/ConfigTest.php @@ -112,10 +112,10 @@ public function testGetTemplateValuesFalseOnEmpty() public function testGetSeedPath() { $config = new Config(['paths' => ['seeds' => 'db/seeds']]); - $this->assertEquals(['db/seeds'], $config->getSeedPaths()); + $this->assertEquals('db/seeds', $config->getSeedPath()); $config = new Config(['paths' => ['seeds' => ['db/seeds1', 'db/seeds2']]]); - $this->assertEquals(['db/seeds1', 'db/seeds2'], $config->getSeedPaths()); + $this->assertEquals('db/seeds1', $config->getSeedPath()); } /** @@ -128,7 +128,7 @@ public function testGetSeedPathThrowsException() $this->expectException(UnexpectedValueException::class); $this->expectExceptionMessage('Seeds path missing from config file'); - $config->getSeedPaths(); + $config->getSeedPath(); } /** diff --git a/tests/TestCase/Util/TableFinderTest.php b/tests/TestCase/Util/TableFinderTest.php new file mode 100644 index 00000000..8dbb0de0 --- /dev/null +++ b/tests/TestCase/Util/TableFinderTest.php @@ -0,0 +1,65 @@ +loadPlugins(['TestBlog']); + $finder = new TableFinder('test'); + + $result = $finder->getTableNames('TestBlog'); + $this->assertContains('articles', $result); + $this->assertContains('categories', $result); + $this->assertContains('dogs', $result); + $this->assertContains('parts', $result); + } + + /** + * Test that using fetchTableName in a Table object class + * where the table name is composed with the database name (e.g. mydb.mytable) + * will return: + * + * - only the table name if the current connection `database` parameter is the first part + * of the table name + * - the full string (e.g. mydb.mytable) if the current connection `database` parameter + * is not the first part of the table name + */ + public function testFetchTableNames(): void + { + $finder = new TableFinder('test'); + $expected = ['alternative.special_tags']; + $this->assertEquals($expected, $finder->fetchTableName('SpecialTagsTable.php', 'TestBlog')); + + ConnectionManager::setConfig('alternative', [ + 'database' => 'alternative', + ]); + $finder = new TableFinder('alternative'); + $expected = ['special_tags']; + $this->assertEquals($expected, $finder->fetchTableName('SpecialTagsTable.php', 'TestBlog')); + + ConnectionManager::drop('alternative'); + ConnectionManager::setConfig('alternative', [ + 'schema' => 'alternative', + ]); + $finder = new TableFinder('alternative'); + $expected = ['special_tags']; + $this->assertEquals($expected, $finder->fetchTableName('SpecialTagsTable.php', 'TestBlog')); + } +}