From 29398e2d420c10b807f785d9dbeb4e99578a84f2 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Mon, 23 Oct 2023 18:05:07 +0200 Subject: [PATCH 01/33] little fixes. Fixed some deprecations for CakePHP 4.5 --- CHANGELOG.md | 3 +++ src/Driver/Sqlite.php | 4 ++-- src/TestSuite/CommandTestCase.php | 14 +++++++++++--- tests/bootstrap.php | 17 ++++++++++------- version | 2 +- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70dc39d3..8a2599b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # 2.x branch ## 2.12 branch +### 2.12.2 +* little fixes. Fixed some deprecations for CakePHP 4.5. + ### 2.12.1 * fixed a little bug in the `bootstrap.php` file; * the `Exceptionist` class provided by me-tools is no longer used (in anticipation of an upcoming deprecation). diff --git a/src/Driver/Sqlite.php b/src/Driver/Sqlite.php index ed0fc56a..6c2b23fa 100644 --- a/src/Driver/Sqlite.php +++ b/src/Driver/Sqlite.php @@ -38,8 +38,8 @@ public function beforeImport(): bool } //Needs disconnect and re-connect because the database schema has changed - $connection->disconnect(); - $connection->connect(); + $connection->getDriver()->disconnect(); + $connection->getDriver()->connect(); return true; } diff --git a/src/TestSuite/CommandTestCase.php b/src/TestSuite/CommandTestCase.php index 6f19d7e4..d8f41cc8 100644 --- a/src/TestSuite/CommandTestCase.php +++ b/src/TestSuite/CommandTestCase.php @@ -16,7 +16,6 @@ namespace DatabaseBackup\TestSuite; use DatabaseBackup\Utility\BackupManager; -use MeTools\TestSuite\CommandTestCase as BaseCommandTestCase; /** * Abstract class for test commands @@ -24,8 +23,17 @@ abstract class CommandTestCase extends BaseCommandTestCase { /** - * Called after every test method - * @return void + * @inheritDoc + */ + public function setUp(): void + { + parent::setUp(); + + $this->useCommandRunner(); + } + + /** + * @inheritDoc */ public function tearDown(): void { diff --git a/tests/bootstrap.php b/tests/bootstrap.php index a6eb5995..8e1ff3ec 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -28,6 +28,10 @@ mb_internal_encoding('UTF-8'); ini_set('intl.default_locale', 'en_US'); +if (!defined('DS')) { + define('DS', DIRECTORY_SEPARATOR); +} + define('ROOT', dirname(__DIR__) . DS); define('CAKE_CORE_INCLUDE_PATH', ROOT . 'vendor' . DS . 'cakephp' . DS . 'cakephp' . DS); define('CORE_PATH', ROOT . 'vendor' . DS . 'cakephp' . DS . 'cakephp' . DS); @@ -118,10 +122,7 @@ class_alias('Tools\TestSuite\ReflectionTrait', 'Tools\ReflectionTrait'); } if (!trait_exists('MeTools\TestSuite\ConsoleIntegrationTestTrait')) { - class_alias('Cake\TestSuite\ConsoleIntegrationTestTrait', 'MeTools\TestSuite\ConsoleIntegrationTestTrait'); -} -if (!class_exists('MeTools\TestSuite\CommandTestCase')) { - class_alias('DatabaseBackup\TestSuite\BaseCommandTestCase', 'MeTools\TestSuite\CommandTestCase'); + class_alias('Cake\Console\TestSuite\ConsoleIntegrationTestTrait', 'MeTools\TestSuite\ConsoleIntegrationTestTrait'); } if (!function_exists('createBackup')) { @@ -129,7 +130,8 @@ class_alias('DatabaseBackup\TestSuite\BaseCommandTestCase', 'MeTools\TestSuite\C * Global function to create a backup file * @param string $filename Filename * @return string - * @throws \Tools\Exception\NotWritableException|\ErrorException + * @throws \LogicException + * @throws \ReflectionException */ function createBackup(string $filename = 'backup.sql'): string { @@ -141,7 +143,8 @@ function createBackup(string $filename = 'backup.sql'): string /** * Global function to create some backup files * @return array - * @throws \Tools\Exception\NotWritableException|\ErrorException + * @throws \LogicException + * @throws \ReflectionException */ function createSomeBackups(): array { @@ -153,6 +156,6 @@ function createSomeBackups(): array $files[] = $file; } - return array_reverse($files); + return array_reverse($files ?? []); } } diff --git a/version b/version index 3cf561c0..371a952d 100644 --- a/version +++ b/version @@ -1 +1 @@ -2.12.1 +2.12.2 From b153bb9731ab19126af87c614b2f17b79d03cc62 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Mon, 23 Oct 2023 18:13:33 +0200 Subject: [PATCH 02/33] removed (old and useless) `BaseCommandTestCase` class --- CHANGELOG.md | 3 ++- src/TestSuite/BaseCommandTestCase.php | 27 --------------------------- src/TestSuite/CommandTestCase.php | 5 ++++- tests/bootstrap.php | 3 --- 4 files changed, 6 insertions(+), 32 deletions(-) delete mode 100644 src/TestSuite/BaseCommandTestCase.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a2599b1..f64516b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ # 2.x branch ## 2.12 branch ### 2.12.2 -* little fixes. Fixed some deprecations for CakePHP 4.5. +* little fixes. Fixed some deprecations for CakePHP 4.5 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97)); +* removed (old and useless) `BaseCommandTestCase` class. ### 2.12.1 * fixed a little bug in the `bootstrap.php` file; diff --git a/src/TestSuite/BaseCommandTestCase.php b/src/TestSuite/BaseCommandTestCase.php deleted file mode 100644 index b0c79c45..00000000 --- a/src/TestSuite/BaseCommandTestCase.php +++ /dev/null @@ -1,27 +0,0 @@ - Date: Mon, 23 Oct 2023 18:17:19 +0200 Subject: [PATCH 03/33] fixed description --- tests/bootstrap.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 59e7cbfc..d8e7c235 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -116,7 +116,7 @@ echo 'Running tests for `' . BackupManager::getDriverName() . '` driver ' . PHP_EOL; /** - * @todo to be removed in a later release. These allow it to work with older versions of me-tools and cakephp + * @todo to be removed in a later release. These allow it to work with older versions of me-tools */ if (!trait_exists('Tools\ReflectionTrait')) { class_alias('Tools\TestSuite\ReflectionTrait', 'Tools\ReflectionTrait'); From b0b1b929121ee575490205b1c02d345c1b600b48 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Mon, 23 Oct 2023 22:25:18 +0200 Subject: [PATCH 04/33] the `TestCase` class does not need me-tools --- composer.json | 2 +- src/TestSuite/TestCase.php | 2 +- tests/TestCase/Utility/BackupManagerTest.php | 2 +- tests/bootstrap.php | 7 ------- 4 files changed, 3 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index 297932d0..e418bd7a 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ } ], "require": { - "php": ">=7.4", + "php": ">=7.4.0", "ext-fileinfo": "*", "cakephp/cakephp": "^4.0", "mirko-pagliai/php-tools": "~1.7.4" diff --git a/src/TestSuite/TestCase.php b/src/TestSuite/TestCase.php index 4030b55b..e10bd452 100644 --- a/src/TestSuite/TestCase.php +++ b/src/TestSuite/TestCase.php @@ -15,9 +15,9 @@ */ namespace DatabaseBackup\TestSuite; +use Cake\TestSuite\TestCase as BaseTestCase; use DatabaseBackup\BackupTrait; use DatabaseBackup\Utility\BackupManager; -use MeTools\TestSuite\TestCase as BaseTestCase; /** * TestCase class diff --git a/tests/TestCase/Utility/BackupManagerTest.php b/tests/TestCase/Utility/BackupManagerTest.php index 5de49237..ae5729c0 100644 --- a/tests/TestCase/Utility/BackupManagerTest.php +++ b/tests/TestCase/Utility/BackupManagerTest.php @@ -113,7 +113,7 @@ public function testIndex(): void //Checks for properties of each backup object foreach ($files as $file) { $this->assertInstanceOf(Entity::class, $file); - $this->assertIsPositive($file->get('size')); + $this->assertGreaterThan(0, $file->get('size')); $this->assertInstanceOf(FrozenTime::class, $file->get('datetime')); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d8e7c235..3c2b7d62 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -115,13 +115,6 @@ echo 'Running tests for `' . BackupManager::getDriverName() . '` driver ' . PHP_EOL; -/** - * @todo to be removed in a later release. These allow it to work with older versions of me-tools - */ -if (!trait_exists('Tools\ReflectionTrait')) { - class_alias('Tools\TestSuite\ReflectionTrait', 'Tools\ReflectionTrait'); -} - if (!function_exists('createBackup')) { /** * Global function to create a backup file From 3f4344a63a8befaba640a5318199f2a824eae32f Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Mon, 23 Oct 2023 22:27:07 +0200 Subject: [PATCH 05/33] it no longer needs the `me-tools` package. This removes several (useless) dependencies --- CHANGELOG.md | 1 + composer.json | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f64516b1..5d534a52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 2.x branch ## 2.12 branch ### 2.12.2 +* it no longer needs the `me-tools` package. This removes several (useless) dependencies; * little fixes. Fixed some deprecations for CakePHP 4.5 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97)); * removed (old and useless) `BaseCommandTestCase` class. diff --git a/composer.json b/composer.json index e418bd7a..4bce5769 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,6 @@ "require-dev": { "cakephp/cakephp-codesniffer": "^4.4", "cakephp/migrations": "^3.2", - "mirko-pagliai/me-tools": "^2.20.9", "phpunit/phpunit": "^9.1|^9.5", "phpstan/phpstan": "^1.7", "psalm/phar": "^4.24" From f54b3b61be8a15d6284eb722b3faa31f73f777af Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Mon, 23 Oct 2023 22:52:19 +0200 Subject: [PATCH 06/33] some, possible changes that prepare it for CakePHP 5 and PHPUnit 10 --- CHANGELOG.md | 1 + config/bootstrap.php | 7 +++++++ src/Command/SendCommand.php | 7 +++---- src/TestSuite/CommandTestCase.php | 7 ++++++- src/Utility/BackupManager.php | 3 ++- tests/TestCase/BackupTraitTest.php | 4 +--- tests/TestCase/Utility/BackupManagerTest.php | 4 +--- 7 files changed, 21 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d534a52..769e0d3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 2.x branch ## 2.12 branch ### 2.12.2 +* some, possible changes that prepare it for CakePHP 5 and PHPUnit 10; * it no longer needs the `me-tools` package. This removes several (useless) dependencies; * little fixes. Fixed some deprecations for CakePHP 4.5 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97)); * removed (old and useless) `BaseCommandTestCase` class. diff --git a/config/bootstrap.php b/config/bootstrap.php index cb3d0d56..220964f8 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -17,6 +17,13 @@ use Cake\Core\Configure; use Symfony\Component\Process\ExecutableFinder; +/** + * @todo remove the `file_exists()` on CakePHP >= 5 + */ +if (file_exists(CAKE . 'functions.php')) { + require CAKE . 'functions.php'; +} + /** * Executables. Name of driver as keys, Then, as value, an array that contains * first the executable to export and then the executable to import backups. diff --git a/src/Command/SendCommand.php b/src/Command/SendCommand.php index f774ddfe..5f4e32d4 100644 --- a/src/Command/SendCommand.php +++ b/src/Command/SendCommand.php @@ -29,9 +29,7 @@ class SendCommand extends Command { /** - * Hook method for defining this command's option parser - * @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined - * @return \Cake\Console\ConsoleOptionParser + * @inheritDoc */ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser { @@ -53,7 +51,8 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption * @param \Cake\Console\Arguments $args The command arguments * @param \Cake\Console\ConsoleIo $io The console io * @return void - * @throws \Cake\Console\Exception\StopException|\ReflectionException + * @throws \Cake\Console\Exception\StopException + * @throws \ReflectionException */ public function execute(Arguments $args, ConsoleIo $io): void { diff --git a/src/TestSuite/CommandTestCase.php b/src/TestSuite/CommandTestCase.php index 95b56297..22deeec6 100644 --- a/src/TestSuite/CommandTestCase.php +++ b/src/TestSuite/CommandTestCase.php @@ -32,7 +32,12 @@ public function setUp(): void { parent::setUp(); - $this->useCommandRunner(); + /** + * @todo remove on CakePHP >= 5 + */ + if (method_exists($this, 'useCommandRunner')) { + $this->useCommandRunner(); + } } /** diff --git a/src/Utility/BackupManager.php b/src/Utility/BackupManager.php index b3e02547..5c9a6405 100644 --- a/src/Utility/BackupManager.php +++ b/src/Utility/BackupManager.php @@ -15,6 +15,7 @@ */ namespace DatabaseBackup\Utility; +use Cake\Collection\Collection; use Cake\Collection\CollectionInterface; use Cake\Core\Configure; use Cake\I18n\FrozenTime; @@ -71,7 +72,7 @@ public static function index(): CollectionInterface { $finder = (new Finder())->files()->name('/\.sql(\.(gz|bz2))?$/')->in(Configure::readOrFail('DatabaseBackup.target')); - return collection($finder)->map(function (SplFileInfo $file) { + return (new Collection($finder))->map(function (SplFileInfo $file) { $filename = $file->getFilename(); return new Entity(compact('filename') + [ diff --git a/tests/TestCase/BackupTraitTest.php b/tests/TestCase/BackupTraitTest.php index eb7e9174..f1192c4d 100644 --- a/tests/TestCase/BackupTraitTest.php +++ b/tests/TestCase/BackupTraitTest.php @@ -39,8 +39,7 @@ class BackupTraitTest extends TestCase public $fixtures = ['core.Articles', 'core.Comments']; /** - * Called before every test method - * @return void + * @inheritDoc */ public function setUp(): void { @@ -96,7 +95,6 @@ public function testGetConnection(): void $this->assertSame('fake', $connection->config()['name']); $this->expectException(MissingDatasourceConfigException::class); - $this->expectExceptionMessage('The datasource configuration "noExisting" was not found'); $this->getConnection('noExisting'); } diff --git a/tests/TestCase/Utility/BackupManagerTest.php b/tests/TestCase/Utility/BackupManagerTest.php index ae5729c0..09b9e5b3 100644 --- a/tests/TestCase/Utility/BackupManagerTest.php +++ b/tests/TestCase/Utility/BackupManagerTest.php @@ -43,8 +43,7 @@ class BackupManagerTest extends TestCase protected BackupManager $BackupManager; /** - * Called before every test method - * @return void + * @inheritDoc */ public function setUp(): void { @@ -166,7 +165,6 @@ public function testSend(): void //With an invalid sender $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Invalid email set for "from". You passed "invalidSender".'); unlink($file); Configure::write('DatabaseBackup.mailSender', 'invalidSender'); $this->BackupManager->send(createBackup(), 'recipient@example.com'); From bb170d6b86473cf744d6060a23907e1b777530ad Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Mon, 23 Oct 2023 22:57:18 +0200 Subject: [PATCH 07/33] updated --- phpstan-baseline.neon | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 232acd18..409b1e28 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -15,3 +15,8 @@ parameters: count: 1 path: tests/TestCase/Utility/BackupExportAndImportTest.php + - + message: "#^Left side of && is always false\\.$#" + count: 1 + path: tests/bootstrap.php + From 0b3dc720395ca9a64b9d9791164d0dfe437d2f9f Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Mon, 23 Oct 2023 22:57:36 +0200 Subject: [PATCH 08/33] removed useless file --- php-doc-maker.xml | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 php-doc-maker.xml diff --git a/php-doc-maker.xml b/php-doc-maker.xml deleted file mode 100644 index 45508ef7..00000000 --- a/php-doc-maker.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - cakephp-database-backup - ../cakephp-database-backup-gh-pages - \ No newline at end of file From 9f274ce159c1457ca9e457af94b7c9da6edaa272 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Mon, 23 Oct 2023 23:04:47 +0200 Subject: [PATCH 09/33] come back. Re-added alias for `ConsoleIntegrationTestTrait` --- tests/bootstrap.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3c2b7d62..2f6e6301 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -115,6 +115,13 @@ echo 'Running tests for `' . BackupManager::getDriverName() . '` driver ' . PHP_EOL; +/** + * @todo remove on CakePHP >= 5 + */ +if (!trait_exists('Cake\Console\TestSuite\ConsoleIntegrationTestTrait')) { + class_alias('Cake\TestSuite\ConsoleIntegrationTestTrait', 'Cake\Console\TestSuite\ConsoleIntegrationTestTrait'); +} + if (!function_exists('createBackup')) { /** * Global function to create a backup file From 4faa9a4bdb92abaa1209b5a10ceca3a34b725870 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Mon, 23 Oct 2023 23:07:19 +0200 Subject: [PATCH 10/33] updated --- .github/workflows/ci.yml | 2 +- tests/bootstrap.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0e851e59..26149832 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -105,7 +105,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.0' + php-version: '8.2' coverage: none tools: cs2pr diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 2f6e6301..4c8839d2 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -153,6 +153,6 @@ function createSomeBackups(): array $files[] = $file; } - return array_reverse($files ?? []); + return array_reverse($files); } } From 9d61c35aa7bcef4636ea5d6b099754f32ab65eaa Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Mon, 23 Oct 2023 23:10:18 +0200 Subject: [PATCH 11/33] fixefd --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 26149832..8d84941a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -105,7 +105,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.2' + php-version: '8.1' coverage: none tools: cs2pr @@ -126,7 +126,7 @@ jobs: if: success() || failure() env: db_dsn: 'sqlite:///:memory:' - run: vendor/bin/psalm.phar --output-format=github --php-version=8.0 + run: vendor/bin/psalm.phar --output-format=github --php-version=8.1 - name: Run phpstan if: success() || failure() From 8d8af1e236f32899dc22cd06c63543816bc4b784 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Mon, 23 Oct 2023 23:12:31 +0200 Subject: [PATCH 12/33] updated --- phpstan-baseline.neon | 6 ------ 1 file changed, 6 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 409b1e28..2b4f7930 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -14,9 +14,3 @@ parameters: message: "#^Expression \"\\$this\\-\\>BackupExport\\-\\>noExistingProperty\" on a separate line does not do anything\\.$#" count: 1 path: tests/TestCase/Utility/BackupExportAndImportTest.php - - - - message: "#^Left side of && is always false\\.$#" - count: 1 - path: tests/bootstrap.php - From 5b0c05e5be5f5917499bde1e3e625f5f8b5b1140 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 24 Oct 2023 15:47:35 +0200 Subject: [PATCH 13/33] improved and fixed a bug for `ExportCommand`, in handling some exceptions --- CHANGELOG.md | 1 + src/Command/ExportCommand.php | 21 +++++++++++++------ tests/TestCase/Command/ExportCommandTest.php | 22 ++++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 769e0d3e..348a30d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 2.12 branch ### 2.12.2 * some, possible changes that prepare it for CakePHP 5 and PHPUnit 10; +* improved and fixed a bug for `ExportCommand`, in handling some exceptions; * it no longer needs the `me-tools` package. This removes several (useless) dependencies; * little fixes. Fixed some deprecations for CakePHP 4.5 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97)); * removed (old and useless) `BaseCommandTestCase` class. diff --git a/src/Command/ExportCommand.php b/src/Command/ExportCommand.php index aa82b0a4..8130bedc 100644 --- a/src/Command/ExportCommand.php +++ b/src/Command/ExportCommand.php @@ -18,6 +18,7 @@ use Cake\Console\Arguments; use Cake\Console\ConsoleIo; use Cake\Console\ConsoleOptionParser; +use Cake\Console\Exception\StopException; use Cake\Core\Configure; use DatabaseBackup\Console\Command; use DatabaseBackup\Utility\BackupExport; @@ -66,6 +67,15 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption ]); } + /** + * Internal method to get a `BackupExport` instance + * @return \DatabaseBackup\Utility\BackupExport + */ + protected function getBackupExport(): BackupExport + { + return new BackupExport(); + } + /** * Exports a database backup. * @@ -81,7 +91,7 @@ public function execute(Arguments $args, ConsoleIo $io): void parent::execute($args, $io); try { - $BackupExport = new BackupExport(); + $BackupExport = $this->getBackupExport(); //Sets the output filename or the compression type. Regarding the `rotate` option, the //`BackupShell::rotate()` method will be called at the end, instead of `BackupExport::rotate()` @@ -98,8 +108,7 @@ public function execute(Arguments $args, ConsoleIo $io): void /** @var string $file */ $file = $BackupExport->export(); if (!$file) { - $io->error(__d('database_backup', 'The `{0}` event stopped the operation', 'Backup.beforeExport')); - $this->abort(); + throw new StopException(__d('database_backup', 'The `Backup.beforeExport` event stopped the operation')); } $io->success(__d('database_backup', 'Backup `{0}` has been exported', rtr($file))); @@ -111,9 +120,9 @@ public function execute(Arguments $args, ConsoleIo $io): void if ($args->getOption('rotate')) { $this->executeCommand(RotateCommand::class, array_merge([(string)$args->getOption('rotate')], $extraOptions), $io); } - } catch (Exception $e) { - $io->error($e->getMessage()); - $this->abort(); + } + catch (Exception $e) { + $io->abort($e->getMessage()); } } } diff --git a/tests/TestCase/Command/ExportCommandTest.php b/tests/TestCase/Command/ExportCommandTest.php index 95c4ff1a..2db0f2d9 100644 --- a/tests/TestCase/Command/ExportCommandTest.php +++ b/tests/TestCase/Command/ExportCommandTest.php @@ -15,7 +15,13 @@ */ namespace DatabaseBackup\Test\TestCase\Command; +use Cake\Console\ConsoleIo; +use Cake\Console\Exception\StopException; +use Cake\Console\TestSuite\StubConsoleInput; +use Cake\Console\TestSuite\StubConsoleOutput; +use DatabaseBackup\Command\ExportCommand; use DatabaseBackup\TestSuite\CommandTestCase; +use DatabaseBackup\Utility\BackupExport; /** * ExportCommandTest class @@ -45,6 +51,22 @@ public function testExecute(): void $this->assertExitError(); } + /** + * Test for `execute()` method on stopped event + * @test + * @uses \DatabaseBackup\Command\ExportCommand::execute() + */ + public function testExecuteOnStoppedEvent() + { + $this->expectException(StopException::class); + $this->expectExceptionMessage('The `Backup.beforeExport` event stopped the operation'); + $BackupExport = $this->createPartialMock(BackupExport::class, ['export']); + $BackupExport->method('export')->willReturn(false); + $ExportCommand = $this->createPartialMock(ExportCommand::class, ['getBackupExport']); + $ExportCommand->method('getBackupExport')->willReturn($BackupExport); + $ExportCommand->run(['-v'], new ConsoleIo(new StubConsoleOutput(), new StubConsoleOutput())); + } + /** * Test for `execute()` method, with `compression` option * @test From 6a1aed62c7aa43991bfeda5b3232a3f8012d7a81 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 24 Oct 2023 15:55:04 +0200 Subject: [PATCH 14/33] fixed --- tests/bootstrap.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 4c8839d2..57fd4697 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -121,6 +121,9 @@ if (!trait_exists('Cake\Console\TestSuite\ConsoleIntegrationTestTrait')) { class_alias('Cake\TestSuite\ConsoleIntegrationTestTrait', 'Cake\Console\TestSuite\ConsoleIntegrationTestTrait'); } +if (!class_exists('Cake\Console\TestSuite\StubConsoleOutput')) { + class_alias('Cake\TestSuite\Stub\ConsoleOutput', 'Cake\Console\TestSuite\StubConsoleOutput'); +} if (!function_exists('createBackup')) { /** From bd184eb8d5a6e767b0037c5586d8a9565118e2a9 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 24 Oct 2023 15:57:06 +0200 Subject: [PATCH 15/33] added return type --- tests/TestCase/Command/ExportCommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TestCase/Command/ExportCommandTest.php b/tests/TestCase/Command/ExportCommandTest.php index 2db0f2d9..988b64ad 100644 --- a/tests/TestCase/Command/ExportCommandTest.php +++ b/tests/TestCase/Command/ExportCommandTest.php @@ -56,7 +56,7 @@ public function testExecute(): void * @test * @uses \DatabaseBackup\Command\ExportCommand::execute() */ - public function testExecuteOnStoppedEvent() + public function testExecuteOnStoppedEvent(): void { $this->expectException(StopException::class); $this->expectExceptionMessage('The `Backup.beforeExport` event stopped the operation'); From 11e66e44bf79b478b502ce206784bcfb312c45d0 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 24 Oct 2023 18:38:02 +0200 Subject: [PATCH 16/33] improved and fixed a bug for `ExportCommand` and `ImportCommand`, in handling some exceptions --- CHANGELOG.md | 2 +- src/Command/ExportCommand.php | 6 ++---- src/Command/ImportCommand.php | 19 +++++++++++------ tests/TestCase/Command/ExportCommandTest.php | 9 +++----- tests/TestCase/Command/ImportCommandTest.php | 22 ++++++++++++++++++-- 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 348a30d5..46e6afc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 2.12 branch ### 2.12.2 * some, possible changes that prepare it for CakePHP 5 and PHPUnit 10; -* improved and fixed a bug for `ExportCommand`, in handling some exceptions; +* improved and fixed a bug for `ExportCommand` and `ImportCommand`, in handling some exceptions; * it no longer needs the `me-tools` package. This removes several (useless) dependencies; * little fixes. Fixed some deprecations for CakePHP 4.5 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97)); * removed (old and useless) `BaseCommandTestCase` class. diff --git a/src/Command/ExportCommand.php b/src/Command/ExportCommand.php index 8130bedc..6e6c749e 100644 --- a/src/Command/ExportCommand.php +++ b/src/Command/ExportCommand.php @@ -105,10 +105,9 @@ public function execute(Arguments $args, ConsoleIo $io): void $BackupExport->timeout((int)$args->getOption('timeout')); } - /** @var string $file */ $file = $BackupExport->export(); if (!$file) { - throw new StopException(__d('database_backup', 'The `Backup.beforeExport` event stopped the operation')); + throw new StopException(__d('database_backup', 'The `{0}` event stopped the operation', 'Backup.beforeExport')); } $io->success(__d('database_backup', 'Backup `{0}` has been exported', rtr($file))); @@ -120,8 +119,7 @@ public function execute(Arguments $args, ConsoleIo $io): void if ($args->getOption('rotate')) { $this->executeCommand(RotateCommand::class, array_merge([(string)$args->getOption('rotate')], $extraOptions), $io); } - } - catch (Exception $e) { + } catch (Exception $e) { $io->abort($e->getMessage()); } } diff --git a/src/Command/ImportCommand.php b/src/Command/ImportCommand.php index 87002645..4d01e8e0 100644 --- a/src/Command/ImportCommand.php +++ b/src/Command/ImportCommand.php @@ -18,6 +18,7 @@ use Cake\Console\Arguments; use Cake\Console\ConsoleIo; use Cake\Console\ConsoleOptionParser; +use Cake\Console\Exception\StopException; use Cake\Core\Configure; use DatabaseBackup\Console\Command; use DatabaseBackup\Utility\BackupImport; @@ -47,6 +48,15 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption ]); } + /** + * Internal method to get a `BackupImport` instance + * @return \DatabaseBackup\Utility\BackupImport + */ + protected function getBackupImport(): BackupImport + { + return new BackupImport(); + } + /** * Imports a database backup * @param \Cake\Console\Arguments $args The command arguments @@ -59,7 +69,7 @@ public function execute(Arguments $args, ConsoleIo $io): void parent::execute($args, $io); try { - $BackupImport = new BackupImport(); + $BackupImport = $this->getBackupImport(); $BackupImport->filename((string)$args->getArgument('filename')); @@ -68,16 +78,13 @@ public function execute(Arguments $args, ConsoleIo $io): void $BackupImport->timeout((int)$args->getOption('timeout')); } - /** @var string $file */ $file = $BackupImport->import(); if (!$file) { - $io->error(__d('database_backup', 'The `{0}` event stopped the operation', 'Backup.beforeImport')); - $this->abort(); + throw new StopException(__d('database_backup', 'The `{0}` event stopped the operation', 'Backup.beforeImport')); } $io->success(__d('database_backup', 'Backup `{0}` has been imported', rtr($file))); } catch (Exception $e) { - $io->error($e->getMessage()); - $this->abort(); + $io->abort($e->getMessage()); } } } diff --git a/tests/TestCase/Command/ExportCommandTest.php b/tests/TestCase/Command/ExportCommandTest.php index 988b64ad..ff549521 100644 --- a/tests/TestCase/Command/ExportCommandTest.php +++ b/tests/TestCase/Command/ExportCommandTest.php @@ -17,7 +17,6 @@ use Cake\Console\ConsoleIo; use Cake\Console\Exception\StopException; -use Cake\Console\TestSuite\StubConsoleInput; use Cake\Console\TestSuite\StubConsoleOutput; use DatabaseBackup\Command\ExportCommand; use DatabaseBackup\TestSuite\CommandTestCase; @@ -60,11 +59,9 @@ public function testExecuteOnStoppedEvent(): void { $this->expectException(StopException::class); $this->expectExceptionMessage('The `Backup.beforeExport` event stopped the operation'); - $BackupExport = $this->createPartialMock(BackupExport::class, ['export']); - $BackupExport->method('export')->willReturn(false); - $ExportCommand = $this->createPartialMock(ExportCommand::class, ['getBackupExport']); - $ExportCommand->method('getBackupExport')->willReturn($BackupExport); - $ExportCommand->run(['-v'], new ConsoleIo(new StubConsoleOutput(), new StubConsoleOutput())); + $Command = $this->createPartialMock(ExportCommand::class, ['getBackupExport']); + $Command->method('getBackupExport')->willReturn($this->createConfiguredMock(BackupExport::class, ['export' => false])); + $Command->run([], new ConsoleIo(new StubConsoleOutput(), new StubConsoleOutput())); } /** diff --git a/tests/TestCase/Command/ImportCommandTest.php b/tests/TestCase/Command/ImportCommandTest.php index 717e4949..3d0ea254 100644 --- a/tests/TestCase/Command/ImportCommandTest.php +++ b/tests/TestCase/Command/ImportCommandTest.php @@ -15,7 +15,12 @@ */ namespace DatabaseBackup\Test\TestCase\Command; +use Cake\Console\ConsoleIo; +use Cake\Console\Exception\StopException; +use Cake\Console\TestSuite\StubConsoleOutput; +use DatabaseBackup\Command\ImportCommand; use DatabaseBackup\TestSuite\CommandTestCase; +use DatabaseBackup\Utility\BackupImport; /** * ImportCommandTest class @@ -46,6 +51,20 @@ public function testExecute(): void $this->assertExitError(); } + /** + * Test for `execute()` method on stopped event + * @test + * @uses \DatabaseBackup\Command\ImportCommand::execute() + */ + public function testExecuteOnStoppedEvent(): void + { + $this->expectException(StopException::class); + $this->expectExceptionMessage('The `Backup.beforeImport` event stopped the operation'); + $Command = $this->createPartialMock(ImportCommand::class, ['getBackupImport']); + $Command->method('getBackupImport')->willReturn($this->createConfiguredMock(BackupImport::class, ['import' => false])); + $Command->run(['--filename' => createBackup()], new ConsoleIo(new StubConsoleOutput(), new StubConsoleOutput())); + } + /** * Test for `execute()` method, with `timeout` option * @test @@ -53,8 +72,7 @@ public function testExecute(): void */ public function testExecuteTimeoutOption(): void { - $backup = createBackup(); - $this->exec($this->command . ' --timeout 10 ' . $backup); + $this->exec($this->command . ' --timeout 10 ' . createBackup()); $this->assertExitSuccess(); $this->assertOutputContains('Timeout for shell commands: 10 seconds'); $this->assertErrorEmpty(); From 78fba56d6058617a6472cc3f7483912c95a71be1 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 24 Oct 2023 18:50:41 +0200 Subject: [PATCH 17/33] added tests --- src/Driver/AbstractDriver.php | 1 + tests/TestCase/Driver/AbstractDriverTest.php | 38 ++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 tests/TestCase/Driver/AbstractDriverTest.php diff --git a/src/Driver/AbstractDriver.php b/src/Driver/AbstractDriver.php index f2524ef1..5fa06aff 100644 --- a/src/Driver/AbstractDriver.php +++ b/src/Driver/AbstractDriver.php @@ -69,6 +69,7 @@ final public function implementedEvents(): array */ private function getExecutable(string $type): string { + /** @codeCoverageIgnore */ if (!in_array($type, ['export', 'import'])) { throw new LogicException(__d('database_backup', '`$type` parameter should be `export` or `import`')); } diff --git a/tests/TestCase/Driver/AbstractDriverTest.php b/tests/TestCase/Driver/AbstractDriverTest.php new file mode 100644 index 00000000..77f9bb82 --- /dev/null +++ b/tests/TestCase/Driver/AbstractDriverTest.php @@ -0,0 +1,38 @@ +getMockForAbstractClass(AbstractDriver::class); + $this->assertNotEmpty($Driver->getBinary('mysql')); + + $this->expectExceptionMessage('Binary for `noExistingBinary` could not be found. You have to set its path manually'); + $Driver->getBinary('noExistingBinary'); + } +} From 51a6e4cef6085aba42b935afadf5cfce7c43a666 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 24 Oct 2023 19:03:42 +0200 Subject: [PATCH 18/33] some testing methods that have been missing for a long time have been added; the `BackupTrait::getDriverName()` method can no longer be static --- CHANGELOG.md | 2 + src/BackupTrait.php | 4 +- src/Driver/AbstractDriver.php | 2 +- .../Utility/AbstractBackupUtilityTest.php | 49 +++++++++++++++++++ tests/bootstrap.php | 2 +- 5 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 tests/TestCase/Utility/AbstractBackupUtilityTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 46e6afc2..ce724dda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ * improved and fixed a bug for `ExportCommand` and `ImportCommand`, in handling some exceptions; * it no longer needs the `me-tools` package. This removes several (useless) dependencies; * little fixes. Fixed some deprecations for CakePHP 4.5 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97)); +* some testing methods that have been missing for a long time have been added; +* the `BackupTrait::getDriverName()` method can no longer be static; * removed (old and useless) `BaseCommandTestCase` class. ### 2.12.1 diff --git a/src/BackupTrait.php b/src/BackupTrait.php index fec35ad1..8508662b 100644 --- a/src/BackupTrait.php +++ b/src/BackupTrait.php @@ -64,9 +64,9 @@ public static function getConnection(?string $name = null): ConnectionInterface * @throws \ReflectionException * @since 2.9.2 */ - public static function getDriverName(): string + public function getDriverName(): string { - return get_class_short_name(self::getConnection()->getDriver()); + return get_class_short_name($this->getConnection()->getDriver()); } /** diff --git a/src/Driver/AbstractDriver.php b/src/Driver/AbstractDriver.php index 5fa06aff..0c691e53 100644 --- a/src/Driver/AbstractDriver.php +++ b/src/Driver/AbstractDriver.php @@ -73,7 +73,7 @@ private function getExecutable(string $type): string if (!in_array($type, ['export', 'import'])) { throw new LogicException(__d('database_backup', '`$type` parameter should be `export` or `import`')); } - $driverName = strtolower(self::getDriverName()); + $driverName = strtolower($this->getDriverName()); $replacements = [ '{{BINARY}}' => escapeshellarg($this->getBinary(DATABASE_BACKUP_EXECUTABLES[$driverName][$type])), '{{AUTH_FILE}}' => method_exists($this, 'getAuthFilePath') && $this->getAuthFilePath() ? escapeshellarg($this->getAuthFilePath()) : '', diff --git a/tests/TestCase/Utility/AbstractBackupUtilityTest.php b/tests/TestCase/Utility/AbstractBackupUtilityTest.php new file mode 100644 index 00000000..8f97ec80 --- /dev/null +++ b/tests/TestCase/Utility/AbstractBackupUtilityTest.php @@ -0,0 +1,49 @@ +getMockForAbstractClass(AbstractBackupUtility::class); + $this->assertInstanceOf(AbstractDriver::class, $Utility->getDriver()); + + $this->expectExceptionMessage('The `noExistingDriver` driver does not exist'); + $Utility = $this->getMockForAbstractClass( + AbstractBackupUtility::class, + [], + '', + true, + true, + true, + ['getDriverName'] + ); + $Utility->method('getDriverName')->willReturn('noExistingDriver'); + $Utility->getDriver(); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 57fd4697..00c90d64 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -113,7 +113,7 @@ $loader = new SchemaLoader(); $loader->loadInternalFile(CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'schema.php'); -echo 'Running tests for `' . BackupManager::getDriverName() . '` driver ' . PHP_EOL; +echo 'Running tests for `' . (new BackupManager())->getDriverName() . '` driver ' . PHP_EOL; /** * @todo remove on CakePHP >= 5 From 383b5a6be61660e12a83a51448ab3c17f0983afb Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 24 Oct 2023 19:04:15 +0200 Subject: [PATCH 19/33] fixed --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce724dda..f91038da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,9 @@ # 2.x branch ## 2.12 branch ### 2.12.2 -* some, possible changes that prepare it for CakePHP 5 and PHPUnit 10; * improved and fixed a bug for `ExportCommand` and `ImportCommand`, in handling some exceptions; * it no longer needs the `me-tools` package. This removes several (useless) dependencies; +* some, possible changes that prepare it for CakePHP 5 and PHPUnit 10 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97)); * little fixes. Fixed some deprecations for CakePHP 4.5 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97)); * some testing methods that have been missing for a long time have been added; * the `BackupTrait::getDriverName()` method can no longer be static; From 51d13f25c92b3883243cabecbff16cdfd487a370 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 24 Oct 2023 19:11:32 +0200 Subject: [PATCH 20/33] fixed --- src/Driver/AbstractDriver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Driver/AbstractDriver.php b/src/Driver/AbstractDriver.php index 0c691e53..b7a71eeb 100644 --- a/src/Driver/AbstractDriver.php +++ b/src/Driver/AbstractDriver.php @@ -69,8 +69,8 @@ final public function implementedEvents(): array */ private function getExecutable(string $type): string { - /** @codeCoverageIgnore */ if (!in_array($type, ['export', 'import'])) { + /** @codeCoverageIgnore */ throw new LogicException(__d('database_backup', '`$type` parameter should be `export` or `import`')); } $driverName = strtolower($this->getDriverName()); From b421c5ba904fd199ad1a5ad5aec2c78707195872 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 24 Oct 2023 22:35:21 +0200 Subject: [PATCH 21/33] fixed tag --- src/Driver/AbstractDriver.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Driver/AbstractDriver.php b/src/Driver/AbstractDriver.php index b7a71eeb..7b3e057c 100644 --- a/src/Driver/AbstractDriver.php +++ b/src/Driver/AbstractDriver.php @@ -69,10 +69,11 @@ final public function implementedEvents(): array */ private function getExecutable(string $type): string { + // @codeCoverageIgnoreStart if (!in_array($type, ['export', 'import'])) { - /** @codeCoverageIgnore */ throw new LogicException(__d('database_backup', '`$type` parameter should be `export` or `import`')); } + // @codeCoverageIgnoreEnd $driverName = strtolower($this->getDriverName()); $replacements = [ '{{BINARY}}' => escapeshellarg($this->getBinary(DATABASE_BACKUP_EXECUTABLES[$driverName][$type])), From 580c8848b78a7b8dd8fbe321d2de0c24ff582b8f Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 24 Oct 2023 22:57:32 +0200 Subject: [PATCH 22/33] added tests for PHP 8.2 --- .github/workflows/ci.yml | 5 ++++- CHANGELOG.md | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8d84941a..bae20fe4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,7 @@ jobs: strategy: fail-fast: false matrix: - php-version: ['7.4', '8.0', '8.1'] + php-version: ['7.4', '8.0', '8.1', '8.2'] db-type: [sqlite, mysql, pgsql] prefer-lowest: [''] exclude: @@ -30,6 +30,9 @@ jobs: - php-version: '8.1' db-type: 'mysql' prefer-lowest: 'prefer-lowest' + - php-version: '8.2' + db-type: 'mysql' + prefer-lowest: 'prefer-lowest' steps: - name: Setup Database diff --git a/CHANGELOG.md b/CHANGELOG.md index f91038da..317855c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ * little fixes. Fixed some deprecations for CakePHP 4.5 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97)); * some testing methods that have been missing for a long time have been added; * the `BackupTrait::getDriverName()` method can no longer be static; -* removed (old and useless) `BaseCommandTestCase` class. +* removed (old and useless) `BaseCommandTestCase` class; +* added tests for PHP 8.2. ### 2.12.1 * fixed a little bug in the `bootstrap.php` file; From 97dd0e7ed7bce7bbac8c69b20ef0003322569d3f Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Tue, 24 Oct 2023 23:46:36 +0200 Subject: [PATCH 23/33] fixed --- src/Driver/Sqlite.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Driver/Sqlite.php b/src/Driver/Sqlite.php index 6c2b23fa..f905e859 100644 --- a/src/Driver/Sqlite.php +++ b/src/Driver/Sqlite.php @@ -34,7 +34,9 @@ public function beforeImport(): bool //Drops each table foreach ($schemaCollection->listTables() as $table) { - array_map([$connection, 'execute'], $schemaCollection->describe($table)->dropSql($connection)); + /** @var \Cake\Database\Schema\TableSchema $TableSchema */ + $TableSchema = $schemaCollection->describe($table); + array_map([$connection, 'execute'], $TableSchema->dropSql($connection)); } //Needs disconnect and re-connect because the database schema has changed From 5f9d3793cbdd0587355f7fd55f1470f620660792 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Wed, 25 Oct 2023 00:27:40 +0200 Subject: [PATCH 24/33] fixed --- src/Driver/AbstractDriver.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Driver/AbstractDriver.php b/src/Driver/AbstractDriver.php index 7b3e057c..786b4c68 100644 --- a/src/Driver/AbstractDriver.php +++ b/src/Driver/AbstractDriver.php @@ -83,6 +83,7 @@ private function getExecutable(string $type): string '{{DB_HOST}}' => $this->getConfig('host'), '{{DB_NAME}}' => $this->getConfig('database'), ]; + /** @var string $exec */ $exec = Configure::readOrFail('DatabaseBackup.' . $driverName . '.' . $type); return str_replace(array_keys($replacements), $replacements, $exec); From 99a37e81d48d7d2518c1bb99618b6552ef11f694 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Wed, 25 Oct 2023 09:41:44 +0200 Subject: [PATCH 25/33] updated --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index a194ac9c..085081d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ .directory .idea -.phpunit.result.cache +.phpunit.*cache *_old *.old.* composer.lock From de1b29d452c78213a25bc328ee59f0019be8f651 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Wed, 25 Oct 2023 09:43:42 +0200 Subject: [PATCH 26/33] improved `BackuManager::index()` method --- CHANGELOG.md | 1 + src/Utility/BackupManager.php | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 317855c7..8eef3a52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * it no longer needs the `me-tools` package. This removes several (useless) dependencies; * some, possible changes that prepare it for CakePHP 5 and PHPUnit 10 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97)); * little fixes. Fixed some deprecations for CakePHP 4.5 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97)); +* improved `BackuManager::index()` method; * some testing methods that have been missing for a long time have been added; * the `BackupTrait::getDriverName()` method can no longer be static; * removed (old and useless) `BaseCommandTestCase` class; diff --git a/src/Utility/BackupManager.php b/src/Utility/BackupManager.php index 5c9a6405..3a901c42 100644 --- a/src/Utility/BackupManager.php +++ b/src/Utility/BackupManager.php @@ -70,16 +70,19 @@ public static function deleteAll(): array */ public static function index(): CollectionInterface { - $finder = (new Finder())->files()->name('/\.sql(\.(gz|bz2))?$/')->in(Configure::readOrFail('DatabaseBackup.target')); + $Finder = new Finder(); + $Finder->files() + ->in(Configure::readOrFail('DatabaseBackup.target')) + ->name('/\.sql(\.(gz|bz2))?$/') ; - return (new Collection($finder))->map(function (SplFileInfo $file) { - $filename = $file->getFilename(); + return (new Collection($Finder))->map(function (SplFileInfo $File) { + $filename = $File->getFilename(); return new Entity(compact('filename') + [ 'extension' => self::getExtension($filename), 'compression' => self::getCompression($filename), - 'size' => $file->getSize(), - 'datetime' => FrozenTime::createFromTimestamp($file->getMTime()), + 'size' => $File->getSize(), + 'datetime' => FrozenTime::createFromTimestamp($File->getMTime()), ]); })->sortBy('datetime'); } From 89c7c475f5ec85a3acf00180da2e75de9a2f1a90 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Wed, 25 Oct 2023 09:57:44 +0200 Subject: [PATCH 27/33] fixed --- tests/bootstrap.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 00c90d64..ce1ada73 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -151,8 +151,9 @@ function createSomeBackups(): array $timestamp = time(); foreach (array_keys(DATABASE_BACKUP_EXTENSIONS) as $extension) { + $timestamp--; $file = createBackup('backup_test_' . $timestamp . '.' . $extension); - touch($file, $timestamp--); + touch($file, $timestamp); $files[] = $file; } From 29d068a1303066fc53181b19ad357aa1f8bbbebc Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Wed, 25 Oct 2023 11:27:19 +0200 Subject: [PATCH 28/33] improved `BackuManager::index()` method, also regarding the correct files sorting. This also solves a small bug for the `rotate()` method (which precisely affects `index()`) --- CHANGELOG.md | 3 ++- src/Utility/BackupManager.php | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8eef3a52..7407ab6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,8 @@ * it no longer needs the `me-tools` package. This removes several (useless) dependencies; * some, possible changes that prepare it for CakePHP 5 and PHPUnit 10 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97)); * little fixes. Fixed some deprecations for CakePHP 4.5 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97)); -* improved `BackuManager::index()` method; +* improved `BackuManager::index()` method, also regarding the correct files sorting. This also solves a small bug for + the `rotate()` method (which precisely affects `index()`); * some testing methods that have been missing for a long time have been added; * the `BackupTrait::getDriverName()` method can no longer be static; * removed (old and useless) `BaseCommandTestCase` class; diff --git a/src/Utility/BackupManager.php b/src/Utility/BackupManager.php index 3a901c42..a10e60fc 100644 --- a/src/Utility/BackupManager.php +++ b/src/Utility/BackupManager.php @@ -73,9 +73,11 @@ public static function index(): CollectionInterface $Finder = new Finder(); $Finder->files() ->in(Configure::readOrFail('DatabaseBackup.target')) - ->name('/\.sql(\.(gz|bz2))?$/') ; + ->name('/\.sql(\.(gz|bz2))?$/') + ->sortByModifiedTime() + ->reverseSorting(); - return (new Collection($Finder))->map(function (SplFileInfo $File) { + return (new Collection($Finder))->map(function (SplFileInfo $File): Entity { $filename = $File->getFilename(); return new Entity(compact('filename') + [ @@ -84,7 +86,7 @@ public static function index(): CollectionInterface 'size' => $File->getSize(), 'datetime' => FrozenTime::createFromTimestamp($File->getMTime()), ]); - })->sortBy('datetime'); + })->compile(false); } /** From 29244ab08e733637e2a45268c1a7596cbbfcc9c3 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Wed, 25 Oct 2023 11:38:22 +0200 Subject: [PATCH 29/33] updated phpstan and psalm --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 4bce5769..fe04f0d3 100644 --- a/composer.json +++ b/composer.json @@ -20,8 +20,8 @@ "cakephp/cakephp-codesniffer": "^4.4", "cakephp/migrations": "^3.2", "phpunit/phpunit": "^9.1|^9.5", - "phpstan/phpstan": "^1.7", - "psalm/phar": "^4.24" + "phpstan/phpstan": "^1.10.38", + "vimeo/psalm": "^5.15.0" }, "autoload": { "psr-4": { From 399329958bbbe7fb3cfac45b5b5efb2b1bc3b7ca Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Wed, 25 Oct 2023 11:40:57 +0200 Subject: [PATCH 30/33] test --- .github/workflows/ci.yml | 4 ++-- psalm.xml | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bae20fe4..b70bd2c6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -129,13 +129,13 @@ jobs: if: success() || failure() env: db_dsn: 'sqlite:///:memory:' - run: vendor/bin/psalm.phar --output-format=github --php-version=8.1 + run: vendor/bin/psalm --output-format=github --php-version=8.1 - name: Run phpstan if: success() || failure() env: db_dsn: 'sqlite:///:memory:' - run: vendor/bin/phpstan.phar analyse --error-format=github + run: vendor/bin/phpstan analyse --error-format=github testsuite-windows: diff --git a/psalm.xml b/psalm.xml index 8f0d13b5..4cce0170 100644 --- a/psalm.xml +++ b/psalm.xml @@ -7,7 +7,8 @@ xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" autoloader="tests/bootstrap.php" errorBaseline="psalm-baseline.xml" -> + findUnusedBaselineEntry="true" + findUnusedCode="false"> From 52b7b34b66a663f2e8a82b77ee83e3353cc2be07 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Wed, 25 Oct 2023 11:48:04 +0200 Subject: [PATCH 31/33] updated --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b70bd2c6..bd26e939 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -56,7 +56,7 @@ jobs: - name: Cache Composer packages id: composer-cache - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: vendor key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} @@ -164,7 +164,7 @@ jobs: - name: Cache Composer packages id: composer-cache - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: vendor key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} From 9fd0118f18bc04afc5d325f6f78c108221476554 Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Wed, 25 Oct 2023 11:49:15 +0200 Subject: [PATCH 32/33] updated --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bd26e939..e62987c6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -96,7 +96,7 @@ jobs: - name: Submit code coverage if: matrix.php-version == '8.0' - uses: codecov/codecov-action@v2 + uses: codecov/codecov-action@v3 cs-stan: name: Coding Standard & Static Analysis From 88070eab00f48872de4197d6ec8f383b9db3d3fa Mon Sep 17 00:00:00 2001 From: mirko-pagliai Date: Wed, 25 Oct 2023 12:04:22 +0200 Subject: [PATCH 33/33] improved `BackuManager::index()` method, also regarding the correct files sorting. This also solves a small bug for the `rotate()` method (which precisely affects `index()`) --- CHANGELOG.md | 3 ++- psalm-baseline.xml | 5 ----- src/Command/IndexCommand.php | 11 +++++------ src/Command/RotateCommand.php | 2 +- src/Utility/BackupManager.php | 20 ++++++++------------ tests/TestCase/Utility/BackupManagerTest.php | 13 ++++++++----- 6 files changed, 24 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7407ab6b..8c22d8ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ * some, possible changes that prepare it for CakePHP 5 and PHPUnit 10 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97)); * little fixes. Fixed some deprecations for CakePHP 4.5 ([issue #97](https://github.com/mirko-pagliai/cakephp-database-backup/issues/97)); * improved `BackuManager::index()` method, also regarding the correct files sorting. This also solves a small bug for - the `rotate()` method (which precisely affects `index()`); + the `rotate()` method (which precisely affects `index()`). The `index()` method now returns a collection of arrays ( + and no longer a collection of `Entity`); * some testing methods that have been missing for a long time have been added; * the `BackupTrait::getDriverName()` method can no longer be static; * removed (old and useless) `BaseCommandTestCase` class; diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 749aa3fe..4b5974b6 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -16,9 +16,4 @@ empty($this->Driver) - - - \Cake\Collection\CollectionInterface<\Cake\ORM\Entity> - - diff --git a/src/Command/IndexCommand.php b/src/Command/IndexCommand.php index 7c522098..6439041c 100644 --- a/src/Command/IndexCommand.php +++ b/src/Command/IndexCommand.php @@ -19,7 +19,6 @@ use Cake\Console\ConsoleIo; use Cake\Console\ConsoleOptionParser; use Cake\I18n\Number; -use Cake\ORM\Entity; use DatabaseBackup\Console\Command; use DatabaseBackup\Utility\BackupManager; @@ -63,11 +62,11 @@ public function execute(Arguments $args, ConsoleIo $io): void __d('database_backup', 'Size'), __d('database_backup', 'Datetime'), ]; - $cells = $backups->map(fn(Entity $backup): array => $backup - ->set('compression', $backup->get('compression') ?: '') - ->set('datetime', $backup->get('datetime')->nice()) - ->set('size', Number::toReadableSize($backup->get('size'))) - ->toArray()); + $cells = $backups->map(fn(array $backup): array => [ + 'compression' => $backup['compression'] ?: '', + 'datetime' => $backup['datetime']->nice(), + 'size' => Number::toReadableSize($backup['size']), + ] + $backup); $io->helper('table')->output(array_merge([$headers], $cells->toList())); } } diff --git a/src/Command/RotateCommand.php b/src/Command/RotateCommand.php index 1dbf1288..8947a759 100644 --- a/src/Command/RotateCommand.php +++ b/src/Command/RotateCommand.php @@ -67,7 +67,7 @@ public function execute(Arguments $args, ConsoleIo $io): void } foreach ($files as $file) { - $io->verbose(__d('database_backup', 'Backup `{0}` has been deleted', $file->get('filename'))); + $io->verbose(__d('database_backup', 'Backup `{0}` has been deleted', $file['filename'])); } $io->success(__d('database_backup', 'Deleted backup files: {0}', count($files))); diff --git a/src/Utility/BackupManager.php b/src/Utility/BackupManager.php index a10e60fc..1161d576 100644 --- a/src/Utility/BackupManager.php +++ b/src/Utility/BackupManager.php @@ -20,7 +20,6 @@ use Cake\Core\Configure; use Cake\I18n\FrozenTime; use Cake\Mailer\Mailer; -use Cake\ORM\Entity; use DatabaseBackup\BackupTrait; use LogicException; use Symfony\Component\Finder\Finder; @@ -65,7 +64,7 @@ public static function deleteAll(): array /** * Returns a list of database backups - * @return \Cake\Collection\CollectionInterface<\Cake\ORM\Entity> Backups + * @return \Cake\Collection\CollectionInterface Array of backups * @see https://github.com/mirko-pagliai/cakephp-database-backup/wiki/How-to-use-the-BackupManager-utility#index */ public static function index(): CollectionInterface @@ -77,16 +76,13 @@ public static function index(): CollectionInterface ->sortByModifiedTime() ->reverseSorting(); - return (new Collection($Finder))->map(function (SplFileInfo $File): Entity { - $filename = $File->getFilename(); - - return new Entity(compact('filename') + [ - 'extension' => self::getExtension($filename), - 'compression' => self::getCompression($filename), - 'size' => $File->getSize(), - 'datetime' => FrozenTime::createFromTimestamp($File->getMTime()), - ]); - })->compile(false); + return (new Collection($Finder))->map(fn(SplFileInfo $File): array => [ + 'filename' => $File->getFilename(), + 'extension' => self::getExtension($File->getFilename()), + 'compression' => self::getCompression($File->getFilename()), + 'size' => $File->getSize(), + 'datetime' => FrozenTime::createFromTimestamp($File->getMTime()), + ])->compile(false); } /** diff --git a/tests/TestCase/Utility/BackupManagerTest.php b/tests/TestCase/Utility/BackupManagerTest.php index 09b9e5b3..f359e728 100644 --- a/tests/TestCase/Utility/BackupManagerTest.php +++ b/tests/TestCase/Utility/BackupManagerTest.php @@ -17,7 +17,6 @@ use Cake\Core\Configure; use Cake\I18n\FrozenTime; -use Cake\ORM\Entity; use Cake\TestSuite\EmailTrait; use DatabaseBackup\TestSuite\TestCase; use DatabaseBackup\Utility\BackupExport; @@ -111,9 +110,9 @@ public function testIndex(): void //Checks for properties of each backup object foreach ($files as $file) { - $this->assertInstanceOf(Entity::class, $file); - $this->assertGreaterThan(0, $file->get('size')); - $this->assertInstanceOf(FrozenTime::class, $file->get('datetime')); + $this->assertIsArray($file); + $this->assertGreaterThan(0, $file['size']); + $this->assertInstanceOf(FrozenTime::class, $file['datetime']); } } @@ -139,7 +138,11 @@ public function testRotate(): void $this->assertSame(['bzip2', 'gzip'], $filesAfterRotate->extract('compression')->toList()); //Gets the difference - $diff = array_udiff($initialFiles->toList(), $filesAfterRotate->toList(), fn(Entity $first, Entity $second): int => strcmp($first->get('filename'), $second->get('filename'))); + $diff = array_udiff( + $initialFiles->toList(), + $filesAfterRotate->toList(), + fn(array $first, array $second): int => strcmp($first['filename'], $second['filename']) + ); //Again, only 1 backup was deleted. The difference is the same $this->assertCount(1, $diff);