diff --git a/src/AbstractSeed.php b/src/AbstractSeed.php index fd993807..cfcc9f08 100644 --- a/src/AbstractSeed.php +++ b/src/AbstractSeed.php @@ -23,6 +23,8 @@ * Class AbstractSeed * Extends Phinx base AbstractSeed class in order to extend the features the seed class * offers. + * + * @deprecated 4.5.0 You should use Migrations\BaseSeed for new seeds. */ abstract class AbstractSeed extends BaseAbstractSeed { diff --git a/src/BaseSeed.php b/src/BaseSeed.php new file mode 100644 index 00000000..9ccb8eca --- /dev/null +++ b/src/BaseSeed.php @@ -0,0 +1,235 @@ +adapter = $adapter; + + return $this; + } + + /** + * {@inheritDoc} + */ + public function getAdapter(): AdapterInterface + { + if (!$this->adapter) { + throw new RuntimeException('Adapter not set.'); + } + + return $this->adapter; + } + + /** + * {@inheritDoc} + */ + public function setIo(ConsoleIo $io) + { + $this->io = $io; + + return $this; + } + + /** + * {@inheritDoc} + */ + public function getIo(): ?ConsoleIo + { + return $this->io; + } + + /** + * {@inheritDoc} + */ + public function getConfig(): ?ConfigInterface + { + return $this->config; + } + + /** + * {@inheritDoc} + */ + public function setConfig(ConfigInterface $config) + { + $this->config = $config; + + return $this; + } + + /** + * {@inheritDoc} + */ + public function getName(): string + { + return static::class; + } + + /** + * {@inheritDoc} + */ + public function execute(string $sql, array $params = []): int + { + return $this->getAdapter()->execute($sql, $params); + } + + /** + * {@inheritDoc} + */ + public function query(string $sql, array $params = []): mixed + { + return $this->getAdapter()->query($sql, $params); + } + + /** + * {@inheritDoc} + */ + public function fetchRow(string $sql): array|false + { + return $this->getAdapter()->fetchRow($sql); + } + + /** + * {@inheritDoc} + */ + public function fetchAll(string $sql): array + { + return $this->getAdapter()->fetchAll($sql); + } + + /** + * {@inheritDoc} + */ + public function insert(string $tableName, array $data): void + { + // convert to table object + $table = new Table($tableName, [], $this->getAdapter()); + $table->insert($data)->save(); + } + + /** + * {@inheritDoc} + */ + public function hasTable(string $tableName): bool + { + return $this->getAdapter()->hasTable($tableName); + } + + /** + * {@inheritDoc} + */ + public function table(string $tableName, array $options = []): Table + { + return new Table($tableName, $options, $this->getAdapter()); + } + + /** + * {@inheritDoc} + */ + public function shouldExecute(): bool + { + return true; + } + + /** + * {@inheritDoc} + */ + public function call(string $seeder, array $options = []): void + { + $io = $this->getIo(); + assert($io !== null, 'Requires ConsoleIo'); + $io->out(''); + $io->out( + ' ====' . + ' ' . $seeder . ':' . + ' seeding' + ); + + $start = microtime(true); + $this->runCall($seeder, $options); + $end = microtime(true); + + $io->out( + ' ====' . + ' ' . $seeder . ':' . + ' seeded' . + ' ' . sprintf('%.4fs', $end - $start) . '' + ); + $io->out(''); + } + + /** + * Calls another seeder from this seeder. + * It will load the Seed class you are calling and run it. + * + * @param string $seeder Name of the seeder to call from the current seed + * @param array $options The CLI options passed to ManagerFactory. + * @return void + */ + protected function runCall(string $seeder, array $options = []): void + { + [$pluginName, $seeder] = pluginSplit($seeder); + $adapter = $this->getAdapter(); + $connection = $adapter->getConnection()->configName(); + + $factory = new ManagerFactory([ + 'plugin' => $options['plugin'] ?? $pluginName ?? null, + 'source' => $options['source'] ?? null, + 'connection' => $options['connection'] ?? $connection, + ]); + $io = $this->getIo(); + assert($io !== null, 'Missing ConsoleIo instance'); + $manager = $factory->createManager($io); + $manager->seed($seeder); + } +} diff --git a/src/SeedInterface.php b/src/SeedInterface.php index cd2e3243..50ee1285 100644 --- a/src/SeedInterface.php +++ b/src/SeedInterface.php @@ -161,7 +161,7 @@ public function hasTable(string $tableName): bool; * @param array $options Options * @return \Migrations\Db\Table */ - public function table(string $tableName, array $options): Table; + public function table(string $tableName, array $options = []): Table; /** * Checks to see if the seed should be executed. @@ -180,7 +180,8 @@ public function shouldExecute(): bool; * for instance to respect foreign key constraints. * * @param string $seeder Name of the seeder to call from the current seed + * @param array $options The CLI options for the seeder. * @return void */ - public function call(string $seeder): void; + public function call(string $seeder, array $options = []): void; } diff --git a/src/Shim/SeedAdapter.php b/src/Shim/SeedAdapter.php index c33840e8..b1cc65f0 100644 --- a/src/Shim/SeedAdapter.php +++ b/src/Shim/SeedAdapter.php @@ -229,7 +229,7 @@ public function hasTable(string $tableName): bool /** * {@inheritDoc} */ - public function table(string $tableName, array $options): Table + public function table(string $tableName, array $options = []): Table { throw new RuntimeException('Not implemented'); } @@ -245,7 +245,7 @@ public function shouldExecute(): bool /** * {@inheritDoc} */ - public function call(string $seeder): void + public function call(string $seeder, array $options = []): void { throw new RuntimeException('Not implemented'); } diff --git a/tests/TestCase/Command/SeedCommandTest.php b/tests/TestCase/Command/SeedCommandTest.php index 8a9bedcc..8bac092e 100644 --- a/tests/TestCase/Command/SeedCommandTest.php +++ b/tests/TestCase/Command/SeedCommandTest.php @@ -122,6 +122,25 @@ public function testSeederOne(): void $this->assertEquals(1, $query->fetchColumn(0)); } + public function testSeederBaseSeed(): void + { + $this->createTables(); + $this->exec('migrations seed -c test --source BaseSeeds --seed MigrationSeedNumbers'); + $this->assertExitSuccess(); + $this->assertOutputContains('MigrationSeedNumbers: seeding'); + $this->assertOutputContains('AnotherNumbersSeed: seeding'); + $this->assertOutputContains('radix=10'); + $this->assertOutputContains('fetchRow=121'); + $this->assertOutputContains('hasTable=1'); + $this->assertOutputContains('fetchAll=121'); + $this->assertOutputContains('All Done'); + + $connection = ConnectionManager::get('test'); + $query = $connection->execute('SELECT COUNT(*) FROM numbers'); + // Two seeders run == 2 rows + $this->assertEquals(2, $query->fetchColumn(0)); + } + public function testSeederImplictAll(): void { $this->createTables(); diff --git a/tests/test_app/config/BaseSeeds/MigrationSeedNumbers.php b/tests/test_app/config/BaseSeeds/MigrationSeedNumbers.php new file mode 100644 index 00000000..e778b424 --- /dev/null +++ b/tests/test_app/config/BaseSeeds/MigrationSeedNumbers.php @@ -0,0 +1,45 @@ + '5', + 'radix' => '10', + ], + ]; + + // Call various methods on the seeder for runtime checks + // and generate output to assert behavior with in an integration test. + $this->table('numbers'); + $this->insert('numbers', $data); + + $this->call('AnotherNumbersSeed', ['source' => 'AltSeeds']); + + $io = $this->getIo(); + $query = $this->query('SELECT radix FROM numbers'); + $io->out('radix=' . $query->fetchColumn(0)); + + $row = $this->fetchRow('SELECT 121 as row_val'); + $io->out('fetchRow=' . $row['row_val']); + $io->out('hasTable=' . $this->hasTable('numbers')); + + $rows = $this->fetchAll('SELECT 121 as row_val'); + $io->out('fetchAll=' . $rows[0]['row_val']); + } +}