Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/en/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -846,11 +846,13 @@ beginning::
// in tests/bootstrap.php
use Migrations\TestSuite\Migrator;

$migrator = new Migrator();

// Simple setup for with no plugins
Migrator::migrate();
$migrator->run();

// Run migrations for multiple plugins
Migrator::migrate([
$migrator->run([
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to update the app skeleton, and testing docs again.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

['plugin' => 'Contacts'],
// Run the Documents migrations on the test_docs connection.
['plugin' => 'Documents', 'connection' => 'test_docs'],
Expand Down
57 changes: 42 additions & 15 deletions src/TestSuite/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,65 @@
namespace Migrations\TestSuite;

use Cake\Console\ConsoleIo;
use Cake\Core\InstanceConfigTrait;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\Fixture\SchemaCleaner;
use Cake\TestSuite\Fixture\SchemaLoader;
use Migrations\Migrations;

class Migrator extends SchemaLoader
class Migrator
{
use InstanceConfigTrait;

/**
* @var \Cake\Console\ConsoleIo
*/
protected $io;

/**
* @var \Cake\TestSuite\Fixture\SchemaCleaner
*/
protected $schemaCleaner;

/**
* @var array<string, mixed>
*/
protected $_defaultConfig = [
'outputLevel' => ConsoleIo::QUIET,
];

/**
* Constructor.
*
* @param array<string, mixed> $config Config settings
*/
public function __construct(array $config = [])
{
$this->setConfig($config);

$this->io = new ConsoleIo();
$this->io->level($this->getConfig('outputLevel'));

$this->schemaCleaner = new SchemaCleaner($this->io);
}

/**
* General command to run before your tests run
* E.g. in tests/bootstrap.php
*
* @param array $config Configuration data
* @param bool $verbose Set to true to display verbose output
* @return self
* @return void
*/
public static function migrate(array $config = [], $verbose = false): Migrator
public function run(array $config = []): void
{
$migrator = new self([
'outputLevel' => $verbose ? ConsoleIo::VERBOSE : ConsoleIo::QUIET,
]);
// Don't recreate schema if we are in a phpunit separate process test.
if (isset($GLOBALS['__PHPUNIT_BOOTSTRAP'])) {
return $migrator;
return;
}

$configReader = new ConfigReader();
$configReader->readMigrationsInDatasources();
$configReader->readConfig($config);
$migrator->handleMigrationsStatus($configReader->getConfig());

return $migrator;
$this->handleMigrationsStatus($configReader->getConfig());
}

/**
Expand Down Expand Up @@ -94,9 +122,8 @@ protected function handleMigrationsStatus(array $configs)
return $this;
}

$schemaCleaner = new SchemaCleaner($this->io);
foreach ($connectionsToDrop as $connectionName) {
$schemaCleaner->dropTables($connectionName);
$this->schemaCleaner->dropTables($connectionName);
}

foreach ($configs as $migration) {
Expand All @@ -108,7 +135,7 @@ protected function handleMigrationsStatus(array $configs)
$schema = ConnectionManager::get($connectionName)->getSchemaCollection();
$allTables = $schema->listTables();
$tablesToTruncate = $this->unsetMigrationTables($allTables);
$schemaCleaner->truncateTables($connectionName, $tablesToTruncate);
$this->schemaCleaner->truncateTables($connectionName, $tablesToTruncate);
}

return $this;
Expand Down
7 changes: 4 additions & 3 deletions tests/TestCase/TestSuite/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private function fetchMigrationsInDB(string $dbTable): array

public function testMigrate(): void
{
Migrator::migrate();
(new Migrator())->run();

$appMigrations = $this->fetchMigrationsInDB('phinxlog');
$fooPluginMigrations = $this->fetchMigrationsInDB('foo_plugin_phinxlog');
Expand All @@ -65,15 +65,16 @@ public function testMigrate(): void

public function testDropTablesForMissingMigrations(): void
{
Migrator::migrate();
$migrator = new Migrator();
$migrator->run();

$connection = ConnectionManager::get('test');
$connection->insert('phinxlog', ['version' => 1, 'migration_name' => 'foo',]);

$count = $connection->newQuery()->select('version')->from('phinxlog')->execute()->count();
$this->assertSame(2, $count);

Migrator::migrate();
$migrator->run();
$count = $connection->newQuery()->select('version')->from('phinxlog')->execute()->count();
$this->assertSame(1, $count);
}
Expand Down