diff --git a/.editorconfig b/.editorconfig index 7061901..6907488 100644 --- a/.editorconfig +++ b/.editorconfig @@ -13,6 +13,12 @@ trim_trailing_whitespace = true [*.bat] end_of_line = crlf +[templates/bake/layout/*.yml] +insert_final_newline = false + [*.yml] indent_style = space indent_size = 2 + +[*.twig] +insert_final_newline = false diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 994e809..a9026e5 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -14,14 +14,14 @@ on: jobs: cs: - uses: bedita/github-workflows/.github/workflows/php-cs.yml@v1 + uses: bedita/github-workflows/.github/workflows/php-cs.yml@v2 with: - php_versions: '["7.4", "8.1", "8.2"]' + php_versions: '["8.3"]' stan: - uses: bedita/github-workflows/.github/workflows/php-stan.yml@v1 + uses: bedita/github-workflows/.github/workflows/php-stan.yml@v2 with: - php_versions: '["7.4", "8.1", "8.2"]' + php_versions: '["8.3"]' unit: name: 'Run unit tests' @@ -30,7 +30,7 @@ jobs: strategy: matrix: - php-version: [7.4, 8.1, 8.2] + php-version: [8.1, 8.2, 8.3] steps: - name: 'Checkout current revision' diff --git a/.gitignore b/.gitignore index d70a7ba..ef8ee10 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ phinx.yml /phpstan.neon /phpunit.xml .phpunit.result.cache +.phpunit.cache diff --git a/composer.json b/composer.json index 1dc8a1f..70f002b 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "bedita/dev-tools", "type": "cakephp-plugin", "description": "A plugin to give some useful tools to BEdita developers.", - "keywords": ["cakephp", "cake3", "plugin", "bedita", "debug_kit", "travis", "scrutinizer"], + "keywords": ["cakephp", "cake5", "plugin", "bedita", "debug_kit", "scrutinizer"], "license": "LGPL-3.0-or-later", "support": { "source": "https://github.com/bedita/dev-tools", @@ -21,17 +21,17 @@ } ], "require": { - "php": ">=7.4", + "php": ">=8.1", "ext-json": "*", - "cakephp/cakephp": "^4.3.0", - "cakephp/debug_kit": "^4.7" + "cakephp/cakephp": "^5.1.0", + "cakephp/debug_kit": "^5.0.0" }, "require-dev": { - "cakephp/bake": "^2.6", - "cakephp/migrations": "^3.4.0", - "cakephp/cakephp-codesniffer": "~4.5.1", + "cakephp/bake": "^3.0.0", + "cakephp/migrations": "^4.0.0", + "cakephp/cakephp-codesniffer": "^5.0", "phpstan/phpstan": "^1.5", - "phpunit/phpunit": "^9.0" + "phpunit/phpunit": "^10.1.0" }, "autoload": { "psr-4": { diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 922ec3c..e2feb5b 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -10,4 +10,5 @@ + /tests/comparisons/* diff --git a/phpstan.neon.dist b/phpstan.neon.dist index ba20f22..e31812b 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,9 +1,13 @@ parameters: - phpVersion: 70400 # PHP 7.4 bootstrapFiles: - tests/bootstrap.php paths: - src - tests level: 9 - checkMissingIterableValueType: false + treatPhpDocTypesAsCertain: false + ignoreErrors: + - identifier: missingType.iterableValue + - identifier: missingType.generics + excludePaths: + - tests/comparisons/* diff --git a/phpunit.xml.dist b/phpunit.xml.dist index f1c542b..4eff771 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,38 +1,31 @@ - - - - - ./src/ - - - - - - - - - - ./tests/TestCase - - - - - - - - - - - + + + + + + + + + ./tests/TestCase + + + + + + + + + + src/ + + diff --git a/src/Command/ChangeLogCommand.php b/src/Command/ChangeLogCommand.php index cfd0d08..fb76873 100644 --- a/src/Command/ChangeLogCommand.php +++ b/src/Command/ChangeLogCommand.php @@ -36,7 +36,7 @@ class ChangeLogCommand extends Command * * @var array */ - protected $_defaultConfig = [ + protected array $_defaultConfig = [ // Classification filter on labels 'filter' => [ 'integration' => [ @@ -97,7 +97,7 @@ public function initialize(): void * * @param \Cake\Console\Arguments $args The command arguments. * @param \Cake\Console\ConsoleIo $io The console io - * @return null|int The exit code or null for success + * @return int|null The exit code or null for success */ public function execute(Arguments $args, ConsoleIo $io): ?int { diff --git a/src/Shell/Task/ResourcesMigrationTask.php b/src/Command/ResourcesMigrationCommand.php similarity index 58% rename from src/Shell/Task/ResourcesMigrationTask.php rename to src/Command/ResourcesMigrationCommand.php index 95aa49a..5907aaf 100644 --- a/src/Shell/Task/ResourcesMigrationTask.php +++ b/src/Command/ResourcesMigrationCommand.php @@ -3,7 +3,7 @@ /** * BEdita, API-first content management framework - * Copyright 2017-2022 ChannelWeb Srl, Chialab Srl + * Copyright 2024 ChannelWeb Srl, Chialab Srl * * This file is part of BEdita: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published @@ -12,24 +12,25 @@ * * See LICENSE.LGPL or for more details. */ -namespace BEdita\DevTools\Shell\Task; +namespace BEdita\DevTools\Command; -use Bake\Utility\TemplateRenderer; -use Migrations\Shell\Task\SimpleMigrationTask; +use Cake\Console\Arguments; +use Cake\Console\ConsoleIo; +use Migrations\Command\BakeSimpleMigrationCommand; /** * {@inheritDoc} * - * Task class for generating resources migrations files. + * Command class for generating resources migrations files. */ -class ResourcesMigrationTask extends SimpleMigrationTask +class ResourcesMigrationCommand extends BakeSimpleMigrationCommand { /** * Main migration file name. * * @var string|null */ - protected $migrationFile = null; + protected ?string $migrationFile = null; /** * @inheritDoc @@ -62,19 +63,17 @@ public function template(): string /** * @inheritDoc */ - public function bake($name): string + public function bake(string $name, Arguments $args, ConsoleIo $io): void { // create .php file first, then .yml - parent::bake($name); + parent::bake($name, $args, $io); - $renderer = new TemplateRenderer($this->theme); - $renderer->set('name', $name); - $renderer->set($this->templateData()); - $contents = $renderer->generate('BEdita/DevTools.yaml'); + $contents = $this->createTemplateRenderer() + ->set('name', $name) + ->set($this->templateData($args)) + ->generate('BEdita/DevTools.yaml'); - $filename = $this->getPath() . str_replace('.php', '.yml', $this->fileName($name)); - $this->createFile($filename, $contents); - - return $contents; + $filename = $this->getPath($args) . str_replace('.php', '.yml', $this->fileName($name)); + $io->createFile($filename, $contents, $this->force); } } diff --git a/src/Panel/ConfigurationPanel.php b/src/Panel/ConfigurationPanel.php index f6544e2..34e22b2 100644 --- a/src/Panel/ConfigurationPanel.php +++ b/src/Panel/ConfigurationPanel.php @@ -27,7 +27,7 @@ class ConfigurationPanel extends DebugPanel * * @var string */ - public $plugin = 'BEdita/DevTools'; + public string $plugin = 'BEdita/DevTools'; /** * Collect configuration data when panel is initialized. diff --git a/templates/Common/html.php b/templates/Common/html.php index d048b6e..ce6214a 100644 --- a/templates/Common/html.php +++ b/templates/Common/html.php @@ -1,6 +1,6 @@ assign('title', __('BEdita 4 - API Response')); $this->Html->css('BEdita/DevTools.jquery.jsonview', ['block' => true]); diff --git a/templates/bake/resources.twig b/templates/bake/resources.twig index 72bd18c..ea80a75 100644 --- a/templates/bake/resources.twig +++ b/templates/bake/resources.twig @@ -3,4 +3,4 @@ use BEdita\Core\Migration\ResourcesMigration; class {{ name }} extends ResourcesMigration { -} +} \ No newline at end of file diff --git a/templates/bake/yaml.twig b/templates/bake/yaml.twig index d3f5a18..8600799 100644 --- a/templates/bake/yaml.twig +++ b/templates/bake/yaml.twig @@ -5,4 +5,4 @@ #update: -#remove: +#remove: \ No newline at end of file diff --git a/tests/TestCase/Command/ChangeLogCommandTest.php b/tests/TestCase/Command/ChangeLogCommandTest.php index 1a97b57..716c644 100644 --- a/tests/TestCase/Command/ChangeLogCommandTest.php +++ b/tests/TestCase/Command/ChangeLogCommandTest.php @@ -38,7 +38,6 @@ class ChangeLogCommandTest extends TestCase public function setUp(): void { parent::setUp(); - $this->useCommandRunner(); Router::reload(); } @@ -117,7 +116,7 @@ public function testExecute(): void ->getMock(); $mock->expects($this->once()) ->method('send') - ->will($this->returnValue([$response])); + ->willReturn([$response]); $current = (array)Configure::read('ChangeLog'); Configure::write('ChangeLog', [ diff --git a/tests/TestCase/Command/ResourcesMigrationCommandTest.php b/tests/TestCase/Command/ResourcesMigrationCommandTest.php new file mode 100644 index 0000000..564b35a --- /dev/null +++ b/tests/TestCase/Command/ResourcesMigrationCommandTest.php @@ -0,0 +1,133 @@ + for more details. + */ +namespace BEdita\DevTools\Test\TestCase\Shell\Task; + +use BEdita\DevTools\Command\ResourcesMigrationCommand; +use Cake\Console\TestSuite\ConsoleIntegrationTestTrait; +use Cake\Core\Plugin; +use Cake\TestSuite\StringCompareTrait; +use Cake\TestSuite\TestCase; + +/** + * Test resources migration task. + * + * @coversDefaultClass \BEdita\DevTools\Command\ResourcesMigrationCommand + */ +class ResourcesMigrationCommandTest extends TestCase +{ + use ConsoleIntegrationTestTrait; + use StringCompareTrait; + + /** + * Keep trace of created files to cleanup at the end of tests. + * + * @var string[] + */ + protected $createdFiles = []; + + /** + * @inheritDoc + */ + public function setUp(): void + { + parent::setUp(); + + $this->_compareBasePath = Plugin::path('BEdita/DevTools') . 'tests' . DS . 'comparisons' . DS . 'Migrations' . DS; + } + + /** + * @inheritDoc + */ + public function tearDown(): void + { + parent::tearDown(); + + foreach ($this->createdFiles as $file) { + unlink($file); + } + } + + /** + * Test `name`. + * + * @return void + * @covers ::name() + */ + public function testName(): void + { + $command = new ResourcesMigrationCommand(); + $expected = 'resources_migration'; + $actual = $command->name(); + static::assertEquals($actual, $expected); + } + + /** + * Test `fileName`. + * + * @return void + * @covers ::fileName() + */ + public function testFileName(): void + { + $command = new ResourcesMigrationCommand(); + $expected = $command->fileName('MyMigration'); + sleep(2); + $actual = $command->fileName('MyMigration'); + static::assertEquals($expected, $actual, 'Migration file name is not preserved'); + } + + /** + * Test `template`. + * + * @return void + * @covers ::template() + */ + public function testTemplate(): void + { + $command = new ResourcesMigrationCommand(); + $expected = 'BEdita/DevTools.resources'; + $actual = $command->template(); + static::assertEquals($actual, $expected); + } + + /** + * Test `bake`. + * + * @return void + * @covers ::bake() + */ + public function testBake(): void + { + $this->exec('bake resources_migration MyMigration'); + + $this->assertExitCode(ResourcesMigrationCommand::CODE_SUCCESS); + + $file = glob(CONFIG . ResourcesMigrationCommand::DEFAULT_MIGRATION_FOLDER . DS . '*_MyMigration.php'); + // @phpstan-ignore-next-line + $phpFile = current($file); + $file = glob(CONFIG . ResourcesMigrationCommand::DEFAULT_MIGRATION_FOLDER . DS . '*_MyMigration.yml'); + // @phpstan-ignore-next-line + $yamlFile = current($file); + + $phpResult = file_get_contents($phpFile); + $yamlResult = file_get_contents($yamlFile); + + $this->createdFiles[] = $phpFile; + $this->createdFiles[] = $yamlFile; + + $this->assertSameAsFile('testMyMigration.php', (string)$phpResult); + $this->assertSameAsFile('testMyMigration.yml', (string)$yamlResult); + } +} diff --git a/tests/TestCase/Panel/ConfigurationPanelTest.php b/tests/TestCase/Panel/ConfigurationPanelTest.php index e823815..c77ed4e 100644 --- a/tests/TestCase/Panel/ConfigurationPanelTest.php +++ b/tests/TestCase/Panel/ConfigurationPanelTest.php @@ -21,6 +21,8 @@ /** * Test configuration panel. + * + * @coversDefaultClass \BEdita\DevTools\Panel\ConfigurationPanel */ class ConfigurationPanelTest extends TestCase { @@ -60,6 +62,7 @@ public function tearDown(): void * Check data being serialized in panel. * * @return void + * @covers ::initialize() */ public function testData(): void { diff --git a/tests/TestCase/PluginTest.php b/tests/TestCase/PluginTest.php index 234b8ae..36a37a0 100644 --- a/tests/TestCase/PluginTest.php +++ b/tests/TestCase/PluginTest.php @@ -20,7 +20,6 @@ use Cake\Core\Configure; use Cake\Error\Middleware\ErrorHandlerMiddleware; use Cake\Http\BaseApplication; -use Cake\Http\Middleware\DoublePassDecoratorMiddleware; use Cake\Http\MiddlewareQueue; use Cake\Http\ServerRequest; use Cake\Routing\Middleware\AssetMiddleware; @@ -82,7 +81,7 @@ public function middleware($middleware): MiddlewareQueue * * @return array[] */ - public function middlewareProvider(): array + public static function middlewareProvider(): array { return [ 'false' => [ @@ -120,11 +119,7 @@ public function testMiddleware(array $expected, ?bool $acceptHtml): void static::assertSameSize($expected, $actual); foreach ($expected as $class) { - if ($actual->current() instanceof DoublePassDecoratorMiddleware) { - static::assertInstanceOf($class, $actual->current()->getCallable()); - } else { - static::assertInstanceOf($class, $actual->current()); - } + static::assertInstanceOf($class, $actual->current()); $actual->next(); } } diff --git a/tests/TestCase/Shell/Task/ResourcesMigrationTaskTest.php b/tests/TestCase/Shell/Task/ResourcesMigrationTaskTest.php deleted file mode 100644 index f8e8d3c..0000000 --- a/tests/TestCase/Shell/Task/ResourcesMigrationTaskTest.php +++ /dev/null @@ -1,119 +0,0 @@ - for more details. - */ - -namespace BEdita\DevTools\Test\TestCase\Shell\Task; - -use Bake\Utility\TemplateRenderer; -use BEdita\DevTools\Shell\Task\ResourcesMigrationTask; -use Cake\TestSuite\TestCase; - -/** - * Test resources migration task. - * - * @coversDefaultClass \BEdita\DevTools\Shell\Task\ResourcesMigrationTask - */ -class ResourcesMigrationTaskTest extends TestCase -{ - /** - * Keep trace of created files to cleanup at the end of tests. - * - * @var string[] - */ - protected $createdFiles = []; - - /** - * @inheritDoc - */ - public function tearDown(): void - { - parent::tearDown(); - - foreach ($this->createdFiles as $file) { - unlink($file); - } - } - - /** - * Test `name`. - * - * @return void - * @covers ::name() - */ - public function testName(): void - { - $task = new ResourcesMigrationTask(); - $expected = 'resources_migration'; - $actual = $task->name(); - static::assertEquals($actual, $expected); - } - - /** - * Test `fileName`. - * - * @return void - * @covers ::fileName() - */ - public function testFileName(): void - { - $task = new ResourcesMigrationTask(); - $expected = $task->fileName('MyMigration'); - sleep(2); - $actual = $task->fileName('MyMigration'); - static::assertEquals($expected, $actual, 'Migration file name is not preserved'); - } - - /** - * Test `template`. - * - * @return void - * @covers ::template() - */ - public function testTemplate(): void - { - $task = new ResourcesMigrationTask(); - $expected = 'BEdita/DevTools.resources'; - $actual = $task->template(); - static::assertEquals($actual, $expected); - } - - /** - * Test `bake`. - * - * @return void - * @covers ::bake() - */ - public function testBake(): void - { - $task = new ResourcesMigrationTask(); - $actual = $task->bake('MyMigration'); - - $renderer = new TemplateRenderer($task->theme); - $renderer->set('name', 'MyMigration'); - $renderer->set($task->templateData()); - $expected = $renderer->generate('BEdita/DevTools.yaml'); - - static::assertEquals($actual, $expected); - - // verify file php exists - $filename = $task->getPath() . $task->fileName('MyMigration'); - static::assertFileExists($filename); - $this->createdFiles[] = $filename; - - // verify file yml exists - $filename = $task->getPath() . str_replace('.php', '.yml', $task->fileName('MyMigration')); - static::assertFileExists($filename); - $this->createdFiles[] = $filename; - } -} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 00173ca..6b77ca5 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -34,15 +34,15 @@ chdir($root); -require_once 'vendor/cakephp/cakephp/src/basics.php'; -require_once 'vendor/autoload.php'; +require dirname(__DIR__) . '/vendor/autoload.php'; +require dirname(__DIR__) . '/vendor/cakephp/cakephp/src/functions.php'; define('ROOT', $root . DS . 'tests' . DS . 'test_app' . DS); define('APP', ROOT . 'TestApp' . DS); define('TMP', sys_get_temp_dir() . DS); define('LOGS', TMP . 'logs' . DS); define('CACHE', TMP . 'cache' . DS); -define('CONFIG', ROOT . DS . 'config' . DS); +define('CONFIG', ROOT . 'config' . DS); define('CAKE_CORE_INCLUDE_PATH', $root . DS . 'vendor' . DS . 'cakephp' . DS . 'cakephp'); define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS); diff --git a/tests/comparisons/Migrations/testMyMigration.php b/tests/comparisons/Migrations/testMyMigration.php new file mode 100644 index 0000000..05fe535 --- /dev/null +++ b/tests/comparisons/Migrations/testMyMigration.php @@ -0,0 +1,6 @@ +