From cb1551c4f313d73e4903edc31f3538159cabdc64 Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Tue, 13 Jun 2023 17:37:53 +0200 Subject: [PATCH] CakePHP 5 upgrade + fix CS (#29) * CakePHP 5 upgrade * fix CS --- .github/workflows/php.yml | 4 +- .github/workflows/phpstan.yml | 2 +- .gitignore | 2 +- composer.json | 17 ++++-- phpcs.xml | 8 +++ phpstan-baseline.neon | 31 ++++++++++ phpstan.neon | 3 + phpunit.xml.dist | 30 +++++----- src/ConfigReader.php | 42 +++++++------ src/Migrator.php | 59 ++++++++++--------- src/SchemaCleaner.php | 42 +++++++------ src/TestConnectionManager.php | 5 +- .../20200208100000_add_articles.php | 5 +- .../Migrations/20200210100000_create_view.php | 2 +- .../TestApp/src/Model/Table/ArticlesTable.php | 2 - tests/TestCase/ConfigReaderTest.php | 12 ++-- tests/TestCase/MigratorTest.php | 11 ++-- tests/TestCase/SchemaCleanerDumpTest.php | 40 ++++++++----- .../TestCase/SchemaCleanerListTablesTest.php | 9 ++- tests/TestCase/SchemaCleanerTest.php | 5 +- tests/TestCase/TestConnectionManagerTest.php | 1 - tests/bootstrap.php | 16 ++--- 22 files changed, 207 insertions(+), 141 deletions(-) create mode 100644 phpcs.xml create mode 100644 phpstan-baseline.neon diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 2e1c340..d3b6117 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -16,11 +16,11 @@ jobs: strategy: fail-fast: false matrix: - php-version: ['7.4'] + php-version: ['8.1', '8.2'] db-type: [sqlite, mysql, pgsql] composer-type: [lowest, stable, dev] exclude: - - php-version: '7.4' + - php-version: '8.1' db-type: sqlite composer-type: lowest diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 609b7de..da5cd1f 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -15,7 +15,7 @@ jobs: strategy: fail-fast: false matrix: - php-version: ['7.4'] + php-version: ['8.1'] name: PHP ${{ matrix.php-version }} diff --git a/.gitignore b/.gitignore index 9f67ad1..50492ff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ .idea /composer.lock /vendor/ -/.phpunit.result.cache +/.phpunit.cache **/test_migrator tests/.env /tmp diff --git a/composer.json b/composer.json index f9388d4..56d774c 100644 --- a/composer.json +++ b/composer.json @@ -9,14 +9,15 @@ } ], "minimum-stability": "dev", + "prefer-stable": true, "require": { - "cakephp/cakephp": "^4.0" + "cakephp/cakephp": "5.x-dev" }, "require-dev": { - "cakephp/cakephp-codesniffer": "^4.0", - "cakephp/migrations": "^3.1", + "cakephp/cakephp-codesniffer": "^5.1", + "cakephp/migrations": "4.x-dev as 4.0.0", "josegonzalez/dotenv": "dev-master", - "phpunit/phpunit": "^8.0" + "phpunit/phpunit": "^10.1" }, "autoload": { "psr-4": { @@ -36,9 +37,13 @@ "pgsql": "bash run_tests.sh Postgres", "sqlite": "bash run_tests.sh Sqlite", "phpstan": "./vendor/bin/phpstan analyse --memory-limit=-1", - "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:0.12.88 && mv composer.backup composer.json" + "phpstan-baseline": "./vendor/bin/phpstan --generate-baseline", + "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:1.10.18 && mv composer.backup composer.json" }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..1ae027b --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..50e403f --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,31 @@ +parameters: + ignoreErrors: + - + message: "#^Call to an undefined method Cake\\\\Datasource\\\\ConnectionInterface\\:\\:execute\\(\\)\\.$#" + count: 1 + path: src/Migrator.php + + - + message: "#^Call to an undefined method Cake\\\\Datasource\\\\ConnectionInterface\\:\\:disableConstraints\\(\\)\\.$#" + count: 1 + path: src/SchemaCleaner.php + + - + message: "#^Call to an undefined method Cake\\\\Datasource\\\\ConnectionInterface\\:\\:execute\\(\\)\\.$#" + count: 2 + path: src/SchemaCleaner.php + + - + message: "#^Call to an undefined method Cake\\\\Datasource\\\\ConnectionInterface\\:\\:getSchemaCollection\\(\\)\\.$#" + count: 2 + path: src/SchemaCleaner.php + + - + message: "#^Call to an undefined method Cake\\\\Datasource\\\\ConnectionInterface\\:\\:transactional\\(\\)\\.$#" + count: 1 + path: src/SchemaCleaner.php + + - + message: "#^Call to an undefined method object\\:\\:schemaDialect\\(\\)\\.$#" + count: 1 + path: src/SchemaCleaner.php diff --git a/phpstan.neon b/phpstan.neon index 73080b7..9bfe2a2 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,3 +1,6 @@ +includes: + - phpstan-baseline.neon + parameters: level: max checkMissingIterableValueType: false diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ef16892..d0ccc39 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,10 +1,12 @@ - + @@ -12,15 +14,15 @@ - - ./tests/TestCase/ - + + ./tests/TestCase/ + - - - ./src/ - - + + + ./src/ + + diff --git a/src/ConfigReader.php b/src/ConfigReader.php index b74cd0f..901d47b 100644 --- a/src/ConfigReader.php +++ b/src/ConfigReader.php @@ -13,7 +13,6 @@ */ namespace CakephpTestMigrator; - use Cake\Datasource\ConnectionManager; class ConfigReader @@ -21,7 +20,7 @@ class ConfigReader /** * @var array */ - private $config = []; + private array $config = []; /** * Read in the Datasources the 'migrations' key for each @@ -29,25 +28,28 @@ class ConfigReader * * @return $this */ - public function readMigrationsInDatasources(): self + public function readMigrationsInDatasources() { foreach ($this->getActiveConnections() as $connectionName) { $connection = ConnectionManager::getConfig($connectionName); $config = []; - if (isset($connection['migrations'])) { - if ($connection['migrations'] === true) { - $config = ['connection' => $connectionName ]; - $this->normalizeArray($config); - } elseif (is_array($connection['migrations'])) { - $config = $connection['migrations']; - $this->normalizeArray($config); - foreach ($config as $k => $v) { - $config[$k]['connection'] = $config[$k]['connection'] ?? $connectionName; + if (is_array($connection)) { + $migrations = $connection['migrations'] ?? false; + + if ($migrations) { + if ($migrations === true) { + $config = ['connection' => $connectionName ]; + $this->normalizeArray($config); + } elseif (is_array($migrations)) { + $config = $migrations; + $this->normalizeArray($config); + foreach ($config as $k => $v) { + $config[$k]['connection'] = $v['connection'] ?? $connectionName; + } } - + $this->config = array_merge($this->config, $config); } - $this->config = array_merge($this->config, $config); } } @@ -57,10 +59,10 @@ public function readMigrationsInDatasources(): self } /** - * @param string[]|array[] $config An array of migration configs + * @param array|array $config An array of migration configs * @return $this */ - public function readConfig(array $config = []): self + public function readConfig(array $config = []) { if (!empty($config)) { $this->normalizeArray($config); @@ -72,6 +74,11 @@ public function readConfig(array $config = []): self return $this; } + /** + * Make sure config is set properly + * + * @return void + */ public function processConfig(): void { foreach ($this->config as $k => $config) { @@ -101,7 +108,6 @@ public function getActiveConnections(): array /** * @param string $connectionName Connection name - * * @return bool */ public function skipConnection(string $connectionName): bool @@ -122,7 +128,7 @@ public function skipConnection(string $connectionName): bool /** * Make array an array of arrays * - * @param array $array + * @param array $array * @return void */ public function normalizeArray(array &$array): void diff --git a/src/Migrator.php b/src/Migrator.php index 7f1bd8a..2359f48 100644 --- a/src/Migrator.php +++ b/src/Migrator.php @@ -13,30 +13,31 @@ */ namespace CakephpTestMigrator; - use Cake\Console\ConsoleIo; use Cake\Datasource\ConnectionManager; use Migrations\Migrations; +use RuntimeException; class Migrator { /** - * @var ConfigReader + * @var \CakephpTestMigrator\ConfigReader */ - protected $configReader; + protected ConfigReader $configReader; /** - * @var ConsoleIo + * @var \Cake\Console\ConsoleIo */ - protected $io; + protected ConsoleIo $io; /** - * @var string[] + * @var array */ - protected $connectionsWithModifiedStatus = []; + protected array $connectionsWithModifiedStatus = []; /** * Migrator constructor. + * * @param bool $verbose * @param null $configReader */ @@ -59,11 +60,11 @@ final public function __construct(bool $verbose, ?ConfigReader $configReader = n * - verbose | bool | Set to true to display messages * - truncate | bool | Truncate tables after migrations are done. * - * @param array $config + * @param array $config * @param array{verbose?:bool,truncate?:bool}|bool $options Options for migrations - * @return Migrator + * @return self */ - public static function migrate(array $config = [], $options = []): Migrator + public static function migrate(array $config = [], array|bool $options = []): Migrator { if ($options === true || $options === false) { $options = ['verbose' => $options]; @@ -88,14 +89,14 @@ public static function migrate(array $config = [], $options = []): Migrator /** * Import the schema from a file, or an array of files. * - * @param string $connectionName Connection - * @param string|string[] $file File to dump - * @param bool $verbose Set to true to display messages + * @param string $connectionName Connection + * @param array|string $file File to dump + * @param bool $verbose Set to true to display messages * @return void * @throws \Exception if the truncation failed * @throws \RuntimeException if the file could not be processed */ - public static function dump(string $connectionName, $file, bool $verbose = false) + public static function dump(string $connectionName, string|array $file, bool $verbose = false): void { $files = (array)$file; @@ -105,12 +106,12 @@ public static function dump(string $connectionName, $file, bool $verbose = false foreach ($files as $file) { if (!file_exists($file)) { - throw new \RuntimeException('The file ' . $file . ' could not found.'); + throw new RuntimeException('The file ' . $file . ' could not found.'); } $sql = file_get_contents($file); if ($sql === false) { - throw new \RuntimeException('The file ' . $file . ' could not read.'); + throw new RuntimeException('The file ' . $file . ' could not read.'); } ConnectionManager::get($connectionName)->execute($sql); @@ -126,7 +127,7 @@ public static function dump(string $connectionName, $file, bool $verbose = false /** * Run migrations for all configured migrations. * - * @param string[] $config Migration configuration. + * @param array $config Migration configuration. * @return void */ protected function runMigrations(array $config): void @@ -136,18 +137,17 @@ protected function runMigrations(array $config): void $msg = 'Migrations for ' . $this->stringifyConfig($config); - if ($result === true) { $this->io->success($msg . ' successfully run.'); } else { - $this->io->error( $msg . ' failed.'); + $this->io->error($msg . ' failed.'); } } /** * Truncates connections on demand. * - * @param string[]|null $connections Connections names to truncate. Defaults to modified connections + * @param array|null $connections Connections names to truncate. Defaults to modified connections * @return void */ public function truncate(?array $connections = null): void @@ -166,7 +166,7 @@ public function truncate(?array $connections = null): void * * @return $this */ - protected function handleMigrationsStatus(): self + protected function handleMigrationsStatus() { $schemaCleaner = new SchemaCleaner($this->io); foreach ($this->getConfigs() as &$config) { @@ -174,15 +174,14 @@ protected function handleMigrationsStatus(): self $this->io->info("Reading migrations status for {$this->stringifyConfig($config)}..."); $migrations = new Migrations($config); if ($this->isStatusChanged($migrations)) { - if (!in_array($connectionName, $this->connectionsWithModifiedStatus)) - { + if (!in_array($connectionName, $this->connectionsWithModifiedStatus)) { $this->connectionsWithModifiedStatus[] = $connectionName; } } } if (empty($this->connectionsWithModifiedStatus)) { - $this->io->success("No migration changes detected."); + $this->io->success('No migration changes detected.'); return $this; } @@ -201,7 +200,7 @@ protected function handleMigrationsStatus(): self /** * Checks if any migrations are up but missing. * - * @param Migrations $migrations + * @param \Migrations\Migrations $migrations * @return bool */ protected function isStatusChanged(Migrations $migrations): bool @@ -209,10 +208,12 @@ protected function isStatusChanged(Migrations $migrations): bool foreach ($migrations->status() as $migration) { if ($migration['status'] === 'up' && ($migration['missing'] ?? false)) { $this->io->info('Missing migration(s) detected.'); + return true; } if ($migration['status'] === 'down') { $this->io->info('New migration(s) found.'); + return true; } } @@ -223,7 +224,7 @@ protected function isStatusChanged(Migrations $migrations): bool /** * Stringify the migration parameters. * - * @param string[] $config Config array + * @param array $config Config array * @return string */ protected function stringifyConfig(array $config): string @@ -231,7 +232,7 @@ protected function stringifyConfig(array $config): string $options = []; foreach (['connection', 'plugin', 'source', 'target'] as $option) { if (isset($config[$option])) { - $options[] = $option . ' "'.$config[$option].'"'; + $options[] = $option . ' "' . $config[$option] . '"'; } } @@ -247,7 +248,7 @@ public function getConfigs(): array } /** - * @return ConfigReader + * @return \CakephpTestMigrator\ConfigReader */ protected function getConfigReader(): ConfigReader { @@ -258,7 +259,7 @@ protected function getConfigReader(): ConfigReader * Returns an array of strings with all the connections * which migration status have changed and were migrated. * - * @return string[] + * @return array */ public function getConnectionsWithModifiedStatus(): array { diff --git a/src/SchemaCleaner.php b/src/SchemaCleaner.php index f8e0cad..24c23ed 100644 --- a/src/SchemaCleaner.php +++ b/src/SchemaCleaner.php @@ -16,18 +16,18 @@ use Cake\Console\ConsoleIo; use Cake\Database\Driver\Mysql; use Cake\Database\Driver\Postgres; -use Cake\Database\DriverInterface; use Cake\Database\Schema\TableSchema; use Cake\Datasource\ConnectionInterface; use Cake\Datasource\ConnectionManager; use Cake\Utility\Hash; +use PDOException; class SchemaCleaner { /** * @var \Cake\Console\ConsoleIo|null */ - protected $io; + protected ?ConsoleIo $io = null; /** * SchemaCleaner constructor. @@ -46,7 +46,7 @@ public function __construct(?ConsoleIo $io = null) * @return void * @throws \Exception if the dropping failed. */ - public function drop(string $connectionName) + public function drop(string $connectionName): void { $this->info("Dropping all tables for connection {$connectionName}."); @@ -55,7 +55,6 @@ public function drop(string $connectionName) foreach ($this->listTables($connection) as $table) { $table = $connection->getSchemaCollection()->describe($table); if ($table instanceof TableSchema) { - /** @var DriverInterface $driver */ $driver = $connection->getDriver(); $stmts = array_merge($stmts, $driver->schemaDialect()->dropTableSql($table)); } @@ -71,10 +70,13 @@ public function drop(string $connectionName) * @return void * @throws \Exception if the truncation failed. */ - public function truncate(string $connectionName) + public function truncate(string $connectionName): void { $this->info("Truncating all tables for connection {$connectionName}."); + /** + * @var \Cake\Database\Connection $connection +*/ $connection = ConnectionManager::get($connectionName); $stmts = []; $tables = $this->listTables($connection); @@ -82,7 +84,6 @@ public function truncate(string $connectionName) foreach ($tables as $table) { $table = $connection->getSchemaCollection()->describe($table); if ($table instanceof TableSchema) { - /** @var DriverInterface $driver */ $driver = $connection->getDriver(); $stmts = array_merge($stmts, $driver->schemaDialect()->truncateTableSql($table)); } @@ -94,7 +95,7 @@ public function truncate(string $connectionName) /** * List sall tables, without views. * - * @param ConnectionInterface $connection Connection. + * @param \Cake\Datasource\ConnectionInterface $connection Connection. * @return array */ public function listTables(ConnectionInterface $connection): array @@ -110,27 +111,31 @@ public function listTables(ConnectionInterface $connection): array $result = $connection->execute($query)->fetchAll(); if ($result === false) { - throw new \PDOException($query . ' failed'); + throw new PDOException($query . ' failed'); } return (array)Hash::extract($result, '{n}.0'); } /** - * @param \Cake\Datasource\ConnectionInterface $connection Connection. - * @param array $commands Sql commands to run + * @param \Cake\Datasource\ConnectionInterface $connection Connection. + * @param array $commands Sql commands to run * @return void * @throws \Exception */ protected function executeStatements(ConnectionInterface $connection, array $commands): void { - $connection->disableConstraints(function ($connection) use ($commands) { - $connection->transactional(function (ConnectionInterface $connection) use ($commands) { - foreach ($commands as $sql) { - $connection->execute($sql); - } - }); - }); + $connection->disableConstraints( + function (ConnectionInterface $connection) use ($commands): void { + $connection->transactional( + function (ConnectionInterface $connection) use ($commands): void { + foreach ($commands as $sql) { + $connection->execute($sql); + } + } + ); + } + ); } /** @@ -147,13 +152,14 @@ protected function info(string $msg): void /** * Unset the phinx migration tables from an array of tables. * - * @param string[] $tables + * @param array $tables * @return array */ public function unsetMigrationTables(array $tables): array { $endsWithPhinxlog = function (string $string) { $needle = 'phinxlog'; + return substr($string, -strlen($needle)) === $needle; }; diff --git a/src/TestConnectionManager.php b/src/TestConnectionManager.php index d072b50..6d2ed92 100644 --- a/src/TestConnectionManager.php +++ b/src/TestConnectionManager.php @@ -13,7 +13,6 @@ */ namespace CakephpTestMigrator; - use Cake\Datasource\ConnectionManager; final class TestConnectionManager @@ -21,12 +20,12 @@ final class TestConnectionManager /** * @var bool */ - private static $aliasConnectionIsLoaded = false; + private static bool $aliasConnectionIsLoaded = false; /** * @return void */ - public static function aliasConnections() + public static function aliasConnections(): void { if (!self::$aliasConnectionIsLoaded) { (new self())->_aliasConnections(); diff --git a/tests/TestApp/config/Migrations/20200208100000_add_articles.php b/tests/TestApp/config/Migrations/20200208100000_add_articles.php index 58d00a3..b91ed0d 100644 --- a/tests/TestApp/config/Migrations/20200208100000_add_articles.php +++ b/tests/TestApp/config/Migrations/20200208100000_add_articles.php @@ -12,7 +12,6 @@ * @license http://www.opensource.org/licenses/mit-license.php MIT License */ -use Cake\ORM\TableRegistry; use Migrations\AbstractMigration; class AddArticles extends AbstractMigration @@ -22,7 +21,9 @@ public function up(): void $this->table('articles') ->addPrimaryKey(['id']) ->addColumn( - 'title', 'string', [ + 'title', + 'string', + [ 'limit' => 128, 'null' => false, ] diff --git a/tests/TestApp/config/Migrations/20200210100000_create_view.php b/tests/TestApp/config/Migrations/20200210100000_create_view.php index a2d3ea6..21ff4cf 100644 --- a/tests/TestApp/config/Migrations/20200210100000_create_view.php +++ b/tests/TestApp/config/Migrations/20200210100000_create_view.php @@ -21,7 +21,7 @@ public function up(): void { try { $this->execute( - "CREATE VIEW " . SchemaCleanerListTablesTest::VIEW_NAME . " AS SELECT '" . SchemaCleanerListTablesTest::VIEW_CONTENT . "'" + 'CREATE VIEW ' . SchemaCleanerListTablesTest::VIEW_NAME . " AS SELECT '" . SchemaCleanerListTablesTest::VIEW_CONTENT . "'" ); } catch (Throwable $e) { // Do nothing, the view might already exist. diff --git a/tests/TestApp/src/Model/Table/ArticlesTable.php b/tests/TestApp/src/Model/Table/ArticlesTable.php index 6aade37..0622902 100644 --- a/tests/TestApp/src/Model/Table/ArticlesTable.php +++ b/tests/TestApp/src/Model/Table/ArticlesTable.php @@ -13,10 +13,8 @@ */ namespace MigratorTestApp\Model\Table; -use Cake\ORM\Query; use Cake\ORM\Table; class ArticlesTable extends Table { - } diff --git a/tests/TestCase/ConfigReaderTest.php b/tests/TestCase/ConfigReaderTest.php index 2f74abc..ca61a27 100644 --- a/tests/TestCase/ConfigReaderTest.php +++ b/tests/TestCase/ConfigReaderTest.php @@ -13,7 +13,6 @@ */ namespace CakephpTestMigrator\Test\TestCase; - use Cake\TestSuite\TestCase; use CakephpTestMigrator\ConfigReader; @@ -38,12 +37,12 @@ public function testSetConfigFromInjection(): void { $config = [ ['connection' => 'Foo', 'plugin' => 'Bar',], - ['plugin' => 'Bar',] + ['plugin' => 'Bar',], ]; $expect = [ ['connection' => 'Foo', 'plugin' => 'Bar',], - ['plugin' => 'Bar', 'connection' => 'test',] + ['plugin' => 'Bar', 'connection' => 'test',], ]; $this->ConfigReader->readConfig($config); @@ -54,7 +53,7 @@ public function testSetConfigFromInjection(): void public function testSetConfigFromEmptyInjection(): void { $expect = [ - ['connection' => 'test'] + ['connection' => 'test'], ]; $this->ConfigReader->readConfig(); @@ -66,7 +65,7 @@ public function testSetConfigWithConfigureAndInjection(): void { $config1 = [ 'connection' => 'Foo1_testSetConfigWithConfigureAndInjection', - 'plugin' => 'Bar1_testSetConfigWithConfigureAndInjection' + 'plugin' => 'Bar1_testSetConfigWithConfigureAndInjection', ]; $this->ConfigReader->readConfig($config1); @@ -88,7 +87,6 @@ public function testReadMigrationsInDatasource(): void $this->assertSame($expected, $act); } - public function testReadMigrationsInDatasourceAndInjection(): void { $this->ConfigReader->readMigrationsInDatasources(); @@ -101,7 +99,7 @@ public function testReadMigrationsInDatasourceAndInjection(): void $this->assertSame($expected, $act); } - public function arrays(): array + public static function arrays(): array { return [ [['a' => 'b'], [['a' => 'b']]], diff --git a/tests/TestCase/MigratorTest.php b/tests/TestCase/MigratorTest.php index 83d0e48..654531e 100644 --- a/tests/TestCase/MigratorTest.php +++ b/tests/TestCase/MigratorTest.php @@ -13,7 +13,6 @@ */ namespace CakephpTestMigrator\Test\TestCase; - use Cake\Datasource\ConnectionManager; use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; @@ -26,7 +25,7 @@ class MigratorTest extends TestCase private function fetchMigrationsInDB(string $dbTable): array { $result = ConnectionManager::get('test') - ->newQuery() + ->selectQuery() ->select('migration_name') ->from($dbTable) ->execute() @@ -100,11 +99,11 @@ public function testDropTablesForMissingMigrations(): void $connection = ConnectionManager::get('test'); $connection->insert('phinxlog', ['version' => 1, 'migration_name' => 'foo',]); - $count = $connection->newQuery()->select('version')->from('phinxlog')->execute()->count(); + $count = $connection->selectQuery()->select('version')->from('phinxlog')->execute()->rowCount(); $this->assertSame(4, $count); Migrator::migrate(); - $count = $connection->newQuery()->select('version')->from('phinxlog')->execute()->count(); + $count = $connection->selectQuery()->select('version')->from('phinxlog')->execute()->rowCount(); $this->assertSame(3, $count); } @@ -116,12 +115,12 @@ public function testMigrateWithoutTruncate(): void $migrator = Migrator::migrate([], ['truncate' => false]); - $count = $connection->newQuery()->select('title')->from('articles')->execute()->count(); + $count = $connection->selectQuery()->select('title')->from('articles')->execute()->rowCount(); $this->assertSame(1, $count); $migrator->truncate(); - $count = $connection->newQuery()->select('title')->from('articles')->execute()->count(); + $count = $connection->selectQuery()->select('title')->from('articles')->execute()->rowCount(); $this->assertSame(0, $count); } } diff --git a/tests/TestCase/SchemaCleanerDumpTest.php b/tests/TestCase/SchemaCleanerDumpTest.php index 62bf971..e7d7def 100644 --- a/tests/TestCase/SchemaCleanerDumpTest.php +++ b/tests/TestCase/SchemaCleanerDumpTest.php @@ -9,9 +9,9 @@ * For full copyright and license information, please see the LICENSE.txt * Redistributions of files must retain the above copyright notice * - * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) - * @since 4.3.0 - * @license https://opensource.org/licenses/mit-license.php MIT License + * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) + * @since 4.3.0 + * @license https://opensource.org/licenses/mit-license.php MIT License */ namespace CakephpTestMigrator\Test\TestCase; @@ -19,6 +19,7 @@ use Cake\Datasource\ConnectionManager; use Cake\TestSuite\TestCase; use CakephpTestMigrator\SchemaCleaner; +use PDOException; class SchemaCleanerDumpTest extends TestCase { @@ -29,14 +30,14 @@ public function testForeignKeyConstruction() $this->createSchemas(); $this->assertSame( 1, - $connection->newQuery()->select('id')->from('test_table')->execute()->count() + $connection->selectQuery()->select('id')->from('test_table')->execute()->rowCount() ); $this->assertSame( 1, - $connection->newQuery()->select('id')->from('test_table2')->execute()->count() + $connection->selectQuery()->select('id')->from('test_table2')->execute()->rowCount() ); - $this->expectException(\PDOException::class); + $this->expectException(PDOException::class); $connection->delete('test_table'); } @@ -69,11 +70,11 @@ public function testTruncateSchema() $this->assertSame( 0, - $connection->newQuery()->select('id')->from('test_table')->execute()->count() + $connection->selectQuery()->select('id')->from('test_table')->execute()->rowCount() ); $this->assertSame( 0, - $connection->newQuery()->select('id')->from('test_table2')->execute()->count() + $connection->selectQuery()->select('id')->from('test_table2')->execute()->rowCount() ); } @@ -98,10 +99,13 @@ private function createSchemas() $schema ->addColumn('id', 'integer') ->addColumn('name', 'string') - ->addConstraint('primary', [ + ->addConstraint( + 'primary', + [ 'type' => TableSchema::CONSTRAINT_PRIMARY, 'columns' => ['id'], - ]); + ] + ); $queries = $schema->createSql($connection); foreach ($queries as $sql) { @@ -113,15 +117,21 @@ private function createSchemas() ->addColumn('id', 'integer') ->addColumn('name', 'string') ->addColumn('fk_id', 'integer') - ->addConstraint('primary', [ + ->addConstraint( + 'primary', + [ 'type' => TableSchema::CONSTRAINT_PRIMARY, 'columns' => ['id'], - ]) - ->addConstraint('foreign_key', [ + ] + ) + ->addConstraint( + 'foreign_key', + [ 'columns' => ['fk_id'], 'type' => TableSchema::CONSTRAINT_FOREIGN, 'references' => ['test_table', 'id', ], - ]); + ] + ); $queries = $schema->createSql($connection); @@ -131,7 +141,7 @@ private function createSchemas() $connection->insert('test_table', ['name' => 'foo']); - $id = $connection->newQuery()->select('id')->from('test_table')->limit(1)->execute()->fetch()[0]; + $id = $connection->selectQuery()->select('id')->from('test_table')->limit(1)->execute()->fetch()[0]; $connection->insert('test_table2', ['name' => 'foo', 'fk_id' => $id]); $connection->execute($connection->getDriver()->enableForeignKeySQL()); diff --git a/tests/TestCase/SchemaCleanerListTablesTest.php b/tests/TestCase/SchemaCleanerListTablesTest.php index c573c98..760d747 100644 --- a/tests/TestCase/SchemaCleanerListTablesTest.php +++ b/tests/TestCase/SchemaCleanerListTablesTest.php @@ -6,14 +6,13 @@ * For full copyright and license information, please see the LICENSE.txt * Redistributions of files must retain the above copyright notice. * - * @copyright Copyright (c) 2020 Juan Pablo Ramirez and Nicolas Masson - * @link https://webrider.de/ - * @since 1.0.0 - * @license http://www.opensource.org/licenses/mit-license.php MIT License + * @copyright Copyright (c) 2020 Juan Pablo Ramirez and Nicolas Masson + * @link https://webrider.de/ + * @since 1.0.0 + * @license http://www.opensource.org/licenses/mit-license.php MIT License */ namespace CakephpTestMigrator\Test\TestCase; - use Cake\Datasource\ConnectionManager; use Cake\TestSuite\TestCase; use CakephpTestMigrator\SchemaCleaner; diff --git a/tests/TestCase/SchemaCleanerTest.php b/tests/TestCase/SchemaCleanerTest.php index f836e33..77de747 100644 --- a/tests/TestCase/SchemaCleanerTest.php +++ b/tests/TestCase/SchemaCleanerTest.php @@ -13,12 +13,11 @@ */ namespace CakephpTestMigrator\Test\TestCase; - use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; use CakephpTestMigrator\SchemaCleaner; +use Exception; use Migrations\Migrations; -use MigratorTestApp\Model\Table\ArticlesTable; class SchemaCleanerTest extends TestCase { @@ -50,7 +49,7 @@ public function testDropSchema(): void // Drop the schema (new SchemaCleaner())->drop('test'); - $this->expectException(\Exception::class); + $this->expectException(Exception::class); $this->Articles->find()->all(); } diff --git a/tests/TestCase/TestConnectionManagerTest.php b/tests/TestCase/TestConnectionManagerTest.php index 78ab279..39045e5 100644 --- a/tests/TestCase/TestConnectionManagerTest.php +++ b/tests/TestCase/TestConnectionManagerTest.php @@ -13,7 +13,6 @@ */ namespace CakephpTestMigrator\Test\TestCase; - use Cake\Datasource\ConnectionManager; use Cake\TestSuite\TestCase; use CakephpTestMigrator\TestConnectionManager; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 27ff799..a56c13d 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,9 +1,11 @@ 'TestApp', 'paths' => [ 'plugins' => [TESTS . 'Plugins' . DS], @@ -57,13 +60,12 @@ 'className' => 'File', 'path' => CACHE, 'url' => env('CACHE_DEFAULT_URL', null), - 'duration'=> '+2 minutes', + 'duration' => '+2 minutes', ]; Cache::setConfig('_cake_model_', $cacheConfig); Cache::setConfig('_cake_core_', $cacheConfig); - $dbConnection = [ 'className' => 'Cake\Database\Connection', 'driver' => 'Cake\Database\Driver\\' . $driver,