From 04d780d43df1cfc54262e7fe0bb342a3826c4492 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Thu, 23 Nov 2023 11:11:10 +0100 Subject: [PATCH 01/16] feat: bedita 5 --- .github/workflows/php.yml | 6 +++--- .scrutinizer.yml | 4 ++-- composer.json | 15 ++++++++------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index fcd4f6e..994e809 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -16,12 +16,12 @@ jobs: cs: uses: bedita/github-workflows/.github/workflows/php-cs.yml@v1 with: - php_versions: '["7.4"]' + php_versions: '["7.4", "8.1", "8.2"]' stan: uses: bedita/github-workflows/.github/workflows/php-stan.yml@v1 with: - php_versions: '["7.4"]' + php_versions: '["7.4", "8.1", "8.2"]' unit: name: 'Run unit tests' @@ -30,7 +30,7 @@ jobs: strategy: matrix: - php-version: [7.4] + php-version: [7.4, 8.1, 8.2] steps: - name: 'Checkout current revision' diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 2329f37..205a45d 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -7,12 +7,12 @@ filter: - 'vendor/*' build: - image: default-bionic + image: default-jammy nodes: analysis: environment: php: - version: 7.4 + version: 8.2 pecl_extensions: - zip tests: diff --git a/composer.json b/composer.json index 447dc61..5d3bdc9 100644 --- a/composer.json +++ b/composer.json @@ -5,15 +5,15 @@ "license": "LGPL-3.0-or-later", "require": { "php": ">= 7.4", - "cakephp/cakephp": "^3.8", - "bedita/core": "^4.7", - "bedita/api": "^4.7" + "cakephp/cakephp": "^4.4", + "bedita/core": "^5.14", + "bedita/api": "^5.14" }, "require-dev": { - "cakephp/cakephp-codesniffer": "^3.0", + "cakephp/cakephp-codesniffer": "^4.5", "cakephp/plugin-installer": "^1.3", - "phpunit/phpunit": "^6.0", - "phpstan/phpstan": "^1.5" + "phpunit/phpunit": "^9.5", + "phpstan/phpstan": "^1.7" }, "autoload": { "psr-4": { @@ -35,7 +35,8 @@ }, "config": { "allow-plugins": { - "cakephp/plugin-installer": true + "cakephp/plugin-installer": true, + "dealerdirect/phpcodesniffer-composer-installer": true } } } From 05c2ab326d6916c786ff427fe128c91778c1f6a1 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Thu, 23 Nov 2023 14:16:31 +0100 Subject: [PATCH 02/16] fix: phpcs --- .../Component/PlaceholdersComponent.php | 15 +++++++++++++-- src/Event/BootstrapEventHandler.php | 4 +++- src/Event/JsonSchemaEventHandler.php | 2 ++ src/Model/Behavior/GetAssociationTrait.php | 2 ++ src/Model/Behavior/PlaceholdedBehavior.php | 3 ++- src/Model/Behavior/PlaceholdersBehavior.php | 10 +++++++--- src/Plugin.php | 2 ++ tests/Fixture/ObjectRelationsFixture.php | 1 + tests/Fixture/RelationTypesFixture.php | 1 + tests/Fixture/RelationsFixture.php | 1 + tests/TestApp/Application.php | 4 +++- tests/TestApp/Filesystem/Adapter/NullAdapter.php | 1 + .../Component/PlaceholdersComponentTest.php | 3 ++- .../TestCase/Event/BootstrapEventHandlerTest.php | 5 +---- .../TestCase/Event/JsonSchemaEventHandlerTest.php | 5 +---- .../Model/Behavior/PlaceholdedBehaviorTest.php | 6 ++---- .../Model/Behavior/PlaceholdersBehaviorTest.php | 7 ++----- tests/bootstrap.php | 2 ++ 18 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/Controller/Component/PlaceholdersComponent.php b/src/Controller/Component/PlaceholdersComponent.php index f300782..f03d4ff 100644 --- a/src/Controller/Component/PlaceholdersComponent.php +++ b/src/Controller/Component/PlaceholdersComponent.php @@ -1,4 +1,6 @@ getController()->getRequest(); - if ($request->getParam('action') !== 'relationships' || !in_array($request->getParam('relationship'), (array)$this->getConfig('relations'))) { + if ( + $request->getParam('action') !== 'relationships' || + !in_array($request->getParam('relationship'), (array)$this->getConfig('relations')) + ) { return; } if (!in_array(mb_strtoupper($request->getMethod()), ['GET', 'HEAD', 'OPTIONS'])) { - throw new ForbiddenException(__d('placeholders', 'Relationships of type {0} can only be managed saving an object', $request->getParam('relationship'))); + throw new ForbiddenException( + __d( + 'placeholders', + 'Relationships of type {0} can only be managed saving an object', + $request->getParam('relationship') + ) + ); } } } diff --git a/src/Event/BootstrapEventHandler.php b/src/Event/BootstrapEventHandler.php index 0251601..850d6dc 100644 --- a/src/Event/BootstrapEventHandler.php +++ b/src/Event/BootstrapEventHandler.php @@ -1,4 +1,6 @@ Date: Thu, 23 Nov 2023 16:41:12 +0100 Subject: [PATCH 03/16] fix: stan - part 1 --- phpstan.neon.dist | 1 + src/Controller/Component/PlaceholdersComponent.php | 2 +- src/Event/JsonSchemaEventHandler.php | 2 +- src/Model/Behavior/PlaceholdedBehavior.php | 5 +++-- src/Model/Behavior/PlaceholdersBehavior.php | 2 +- src/Plugin.php | 2 +- tests/TestApp/Application.php | 5 +++-- tests/TestApp/Filesystem/Adapter/NullAdapter.php | 4 ++-- .../Controller/Component/PlaceholdersComponentTest.php | 4 ++-- tests/TestCase/Event/BootstrapEventHandlerTest.php | 2 +- tests/TestCase/Event/JsonSchemaEventHandlerTest.php | 4 ++-- tests/TestCase/Model/Behavior/PlaceholdedBehaviorTest.php | 2 +- tests/TestCase/Model/Behavior/PlaceholdersBehaviorTest.php | 2 +- tests/bootstrap.php | 6 +++--- 14 files changed, 23 insertions(+), 20 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index c162141..c72c541 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -6,3 +6,4 @@ parameters: - tests level: 8 checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false diff --git a/src/Controller/Component/PlaceholdersComponent.php b/src/Controller/Component/PlaceholdersComponent.php index f03d4ff..3a1fa3a 100644 --- a/src/Controller/Component/PlaceholdersComponent.php +++ b/src/Controller/Component/PlaceholdersComponent.php @@ -26,7 +26,7 @@ class PlaceholdersComponent extends Component /** * Default configuration. * - * @var array + * @var array */ protected $_defaultConfig = [ 'relations' => ['placeholder', 'placeholded'], diff --git a/src/Event/JsonSchemaEventHandler.php b/src/Event/JsonSchemaEventHandler.php index fa8fee4..b310ad7 100644 --- a/src/Event/JsonSchemaEventHandler.php +++ b/src/Event/JsonSchemaEventHandler.php @@ -30,7 +30,7 @@ class JsonSchemaEventHandler implements EventListenerInterface /** * @inheritDoc */ - public function implementedEvents() + public function implementedEvents(): array { return [ 'ObjectType.getSchema' => 'onGetSchema', diff --git a/src/Model/Behavior/PlaceholdedBehavior.php b/src/Model/Behavior/PlaceholdedBehavior.php index fd2c9dd..3c3596f 100644 --- a/src/Model/Behavior/PlaceholdedBehavior.php +++ b/src/Model/Behavior/PlaceholdedBehavior.php @@ -19,6 +19,7 @@ use Cake\Datasource\EntityInterface; use Cake\Event\Event; use Cake\ORM\Behavior; +use Cake\Utility\Hash; /** * Placeholded behavior @@ -32,7 +33,7 @@ class PlaceholdedBehavior extends Behavior * * - `relations`: names of the BEdita relations to check. * - * @var array + * @var array */ protected $_defaultConfig = [ 'relations' => ['placeholded'], @@ -84,7 +85,7 @@ protected function ensureNotPlaceholded(EntityInterface $entity): void throw new LockedResourceException(__d( 'placeholders', 'Cannot delete object {0} because it is still {1} in {2,plural,=1{one object} other{# objects}}', - $entity->id, + (string)Hash::get($entity, 'id'), $relation, $refCount )); diff --git a/src/Model/Behavior/PlaceholdersBehavior.php b/src/Model/Behavior/PlaceholdersBehavior.php index bbbbadf..c35d8ea 100644 --- a/src/Model/Behavior/PlaceholdersBehavior.php +++ b/src/Model/Behavior/PlaceholdersBehavior.php @@ -49,7 +49,7 @@ class PlaceholdersBehavior extends Behavior * a list of associative arrays with `id` and `params` fields. * If `null`, uses {@see \BEdita\Core\Model\Behavior\PlaceholdersBehavior::extractPlaceholders()}. * - * @var array + * @var array */ protected $_defaultConfig = [ 'relation' => 'placeholder', diff --git a/src/Plugin.php b/src/Plugin.php index 2545b0e..9e91b44 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -31,7 +31,7 @@ class Plugin extends BasePlugin * * @codeCoverageIgnore */ - public function bootstrap(PluginApplicationInterface $app) + public function bootstrap(PluginApplicationInterface $app): void { parent::bootstrap($app); diff --git a/tests/TestApp/Application.php b/tests/TestApp/Application.php index a6764cf..7202392 100644 --- a/tests/TestApp/Application.php +++ b/tests/TestApp/Application.php @@ -16,6 +16,7 @@ namespace BEdita\Placeholders\Test\TestApp; use Cake\Http\BaseApplication; +use Cake\Http\MiddlewareQueue; use Cake\Routing\Middleware\RoutingMiddleware; /** @@ -29,7 +30,7 @@ class Application extends BaseApplication /** * @inheritDoc */ - public function bootstrap() + public function bootstrap(): void { $this->addPlugin('BEdita/Core'); $this->addPlugin('BEdita/API'); @@ -39,7 +40,7 @@ public function bootstrap() * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to set in your App Class * @return \Cake\Http\MiddlewareQueue */ - public function middleware($middlewareQueue) + public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue { return $middlewareQueue->add(new RoutingMiddleware($this)); } diff --git a/tests/TestApp/Filesystem/Adapter/NullAdapter.php b/tests/TestApp/Filesystem/Adapter/NullAdapter.php index bfd5bb9..1f75074 100644 --- a/tests/TestApp/Filesystem/Adapter/NullAdapter.php +++ b/tests/TestApp/Filesystem/Adapter/NullAdapter.php @@ -4,7 +4,7 @@ namespace BEdita\Placeholders\Test\TestApp\Filesystem\Adapter; use BEdita\Core\Filesystem\FilesystemAdapter; -use League\Flysystem\Adapter\NullAdapter as FlysystemNullAdapter; +use League\Flysystem\FilesystemAdapter as LeagueFilesystemAdapter; /** * Null adapter, for testing. @@ -16,6 +16,6 @@ class NullAdapter extends FilesystemAdapter */ protected function buildAdapter(array $config) { - return new FlysystemNullAdapter(); + return new LeagueFilesystemAdapter(); } } diff --git a/tests/TestCase/Controller/Component/PlaceholdersComponentTest.php b/tests/TestCase/Controller/Component/PlaceholdersComponentTest.php index e19b59d..7055085 100644 --- a/tests/TestCase/Controller/Component/PlaceholdersComponentTest.php +++ b/tests/TestCase/Controller/Component/PlaceholdersComponentTest.php @@ -39,7 +39,7 @@ class PlaceholdersComponentTest extends TestCase /** * @inheritDoc */ - public function setUp() + public function setUp(): void { parent::setUp(); @@ -50,7 +50,7 @@ public function setUp() /** * @inheritDoc */ - public function tearDown() + public function tearDown(): void { unset($this->controller); diff --git a/tests/TestCase/Event/BootstrapEventHandlerTest.php b/tests/TestCase/Event/BootstrapEventHandlerTest.php index 68b754f..6832b7a 100644 --- a/tests/TestCase/Event/BootstrapEventHandlerTest.php +++ b/tests/TestCase/Event/BootstrapEventHandlerTest.php @@ -37,7 +37,7 @@ class BootstrapEventHandlerTest extends TestCase /** * @inheritDoc */ - public function setUp() + public function setUp(): void { parent::setUp(); diff --git a/tests/TestCase/Event/JsonSchemaEventHandlerTest.php b/tests/TestCase/Event/JsonSchemaEventHandlerTest.php index f633f23..bd31466 100644 --- a/tests/TestCase/Event/JsonSchemaEventHandlerTest.php +++ b/tests/TestCase/Event/JsonSchemaEventHandlerTest.php @@ -44,7 +44,7 @@ class JsonSchemaEventHandlerTest extends TestCase /** * @inheritDoc */ - public function setUp() + public function setUp(): void { parent::setUp(); @@ -53,7 +53,7 @@ public function setUp() EventManager::instance()->on(new JsonSchemaEventHandler()); } - public function tearDown() + public function tearDown(): void { $this->getTableLocator()->clear(); diff --git a/tests/TestCase/Model/Behavior/PlaceholdedBehaviorTest.php b/tests/TestCase/Model/Behavior/PlaceholdedBehaviorTest.php index dc4b320..b39d618 100644 --- a/tests/TestCase/Model/Behavior/PlaceholdedBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/PlaceholdedBehaviorTest.php @@ -58,7 +58,7 @@ class PlaceholdedBehaviorTest extends TestCase /** * @inheritDoc */ - public function setUp() + public function setUp(): void { parent::setUp(); diff --git a/tests/TestCase/Model/Behavior/PlaceholdersBehaviorTest.php b/tests/TestCase/Model/Behavior/PlaceholdersBehaviorTest.php index 49c9a89..9f27ea2 100644 --- a/tests/TestCase/Model/Behavior/PlaceholdersBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/PlaceholdersBehaviorTest.php @@ -60,7 +60,7 @@ class PlaceholdersBehaviorTest extends TestCase /** * @inheritDoc */ - public function setUp() + public function setUp(): void { parent::setUp(); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 7cd46ef..27b7340 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -102,6 +102,6 @@ // clear all before running tests TableRegistry::getTableLocator()->clear(); -Cache::clear(false, '_cake_core_'); -Cache::clear(false, '_cake_model_'); -Cache::clear(false, '_bedita_object_types_'); +Cache::clear('_cake_core_'); +Cache::clear('_cake_model_'); +Cache::clear('_bedita_object_types_'); From 14a91f9c82bc1346211f22d38f5ea40d9e228464 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Thu, 23 Nov 2023 17:16:48 +0100 Subject: [PATCH 04/16] fix: stan part 2 --- tests/Fixture/ObjectRelationsFixture.php | 4 +- tests/Fixture/RelationTypesFixture.php | 4 +- tests/Fixture/RelationsFixture.php | 4 +- .../Filesystem/Adapter/NullAdapter.php | 84 ++++++++++++++++++- .../Event/JsonSchemaEventHandlerTest.php | 16 +++- .../Behavior/PlaceholdedBehaviorTest.php | 26 +++++- .../Behavior/PlaceholdersBehaviorTest.php | 26 +++++- 7 files changed, 144 insertions(+), 20 deletions(-) diff --git a/tests/Fixture/ObjectRelationsFixture.php b/tests/Fixture/ObjectRelationsFixture.php index 453577e..e8bded7 100644 --- a/tests/Fixture/ObjectRelationsFixture.php +++ b/tests/Fixture/ObjectRelationsFixture.php @@ -3,12 +3,12 @@ namespace BEdita\Placeholders\Test\Fixture; -use BEdita\Core\TestSuite\Fixture\TestFixture; +use BEdita\Core\Test\Fixture\ObjectRelationsFixture as BEditaObjectRelationsFixture; /** * Fixture for `object_relations` table. */ -class ObjectRelationsFixture extends TestFixture +class ObjectRelationsFixture extends BEditaObjectRelationsFixture { /** * Records diff --git a/tests/Fixture/RelationTypesFixture.php b/tests/Fixture/RelationTypesFixture.php index 6065676..f2cd867 100644 --- a/tests/Fixture/RelationTypesFixture.php +++ b/tests/Fixture/RelationTypesFixture.php @@ -3,12 +3,12 @@ namespace BEdita\Placeholders\Test\Fixture; -use BEdita\Core\TestSuite\Fixture\TestFixture; +use BEdita\Core\Test\Fixture\RelationTypesFixture as BEditaRelationTypesFixture; /** * Fixture for `relation_types` table. */ -class RelationTypesFixture extends TestFixture +class RelationTypesFixture extends BEditaRelationTypesFixture { /** * Records diff --git a/tests/Fixture/RelationsFixture.php b/tests/Fixture/RelationsFixture.php index a0336bb..7d9389e 100644 --- a/tests/Fixture/RelationsFixture.php +++ b/tests/Fixture/RelationsFixture.php @@ -3,12 +3,12 @@ namespace BEdita\Placeholders\Test\Fixture; -use BEdita\Core\TestSuite\Fixture\TestFixture; +use BEdita\Core\Test\Fixture\RelationsFixture as BEditaRelationsFixture; /** * Fixture for `relations` table. */ -class RelationsFixture extends TestFixture +class RelationsFixture extends BEditaRelationsFixture { /** * Records diff --git a/tests/TestApp/Filesystem/Adapter/NullAdapter.php b/tests/TestApp/Filesystem/Adapter/NullAdapter.php index 1f75074..c0b8c30 100644 --- a/tests/TestApp/Filesystem/Adapter/NullAdapter.php +++ b/tests/TestApp/Filesystem/Adapter/NullAdapter.php @@ -4,7 +4,9 @@ namespace BEdita\Placeholders\Test\TestApp\Filesystem\Adapter; use BEdita\Core\Filesystem\FilesystemAdapter; -use League\Flysystem\FilesystemAdapter as LeagueFilesystemAdapter; +use League\Flysystem\Config; +use League\Flysystem\FileAttributes; +use League\Flysystem\FilesystemAdapter as FlysystemFilesystemAdapter; /** * Null adapter, for testing. @@ -16,6 +18,84 @@ class NullAdapter extends FilesystemAdapter */ protected function buildAdapter(array $config) { - return new LeagueFilesystemAdapter(); + return new class () implements FlysystemFilesystemAdapter + { + public function fileExists(string $path): bool + { + return false; + } + + public function write(string $path, string $contents, Config $config): void + { + } + + public function writeStream(string $path, $contents, Config $config): void + { + } + + public function read(string $path): string + { + return ''; + } + + public function readStream(string $path) + { + /** @var resource $stream */ + $stream = \fopen('php://temp', 'w+b'); + \fwrite($stream, ''); + \rewind($stream); + + return $stream; + } + + public function delete(string $path): void + { + } + + public function deleteDirectory(string $path): void + { + } + + public function createDirectory(string $path, Config $config): void + { + } + + public function setVisibility(string $path, string $visibility): void + { + } + + public function visibility(string $path): FileAttributes + { + return new FileAttributes(''); + } + + public function mimeType(string $path): FileAttributes + { + return new FileAttributes(''); + } + + public function lastModified(string $path): FileAttributes + { + return new FileAttributes(''); + } + + public function fileSize(string $path): FileAttributes + { + return new FileAttributes(''); + } + + public function listContents(string $path, bool $deep): iterable + { + return []; + } + + public function move(string $source, string $destination, Config $config): void + { + } + + public function copy(string $source, string $destination, Config $config): void + { + } + }; } } diff --git a/tests/TestCase/Event/JsonSchemaEventHandlerTest.php b/tests/TestCase/Event/JsonSchemaEventHandlerTest.php index bd31466..df45ecf 100644 --- a/tests/TestCase/Event/JsonSchemaEventHandlerTest.php +++ b/tests/TestCase/Event/JsonSchemaEventHandlerTest.php @@ -5,19 +5,18 @@ use BEdita\Placeholders\Event\BootstrapEventHandler; use BEdita\Placeholders\Event\JsonSchemaEventHandler; -use Cake\Datasource\ModelAwareTrait; use Cake\Event\EventManager; +use Cake\ORM\Locator\LocatorAwareTrait; use Cake\TestSuite\TestCase; /** * {@see \BEdita\Placeholders\Event\JsonSchemaEventHandler} Test Case * * @coversDefaultClass \BEdita\Placeholders\Event\JsonSchemaEventHandler - * @property \BEdita\Core\Model\Table\ObjectTypesTable $ObjectTypes */ class JsonSchemaEventHandlerTest extends TestCase { - use ModelAwareTrait; + use LocatorAwareTrait; /** * @inheritDoc @@ -41,6 +40,13 @@ class JsonSchemaEventHandlerTest extends TestCase 'plugin.BEdita/Core.History', ]; + /** + * ObjectTypesTable instance + * + * @var \BEdita\Core\Model\Table\ObjectTypesTable + */ + protected $ObjectTypes = null; + /** * @inheritDoc */ @@ -48,7 +54,9 @@ public function setUp(): void { parent::setUp(); - $this->loadModel('ObjectTypes'); + /** @var \BEdita\Core\Model\Table\ObjectTypesTable $objectTypes */ + $objectTypes = $this->fetchTable('ObjectTypes'); + $this->ObjectTypes = $objectTypes; EventManager::instance()->on(new JsonSchemaEventHandler()); } diff --git a/tests/TestCase/Model/Behavior/PlaceholdedBehaviorTest.php b/tests/TestCase/Model/Behavior/PlaceholdedBehaviorTest.php index b39d618..7436d46 100644 --- a/tests/TestCase/Model/Behavior/PlaceholdedBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/PlaceholdedBehaviorTest.php @@ -17,8 +17,8 @@ use BEdita\Core\Exception\LockedResourceException; use BEdita\Placeholders\Event\BootstrapEventHandler; -use Cake\Datasource\ModelAwareTrait; use Cake\Event\EventManager; +use Cake\ORM\Locator\LocatorAwareTrait; use Cake\TestSuite\TestCase; use Cake\Utility\Hash; @@ -31,7 +31,7 @@ */ class PlaceholdedBehaviorTest extends TestCase { - use ModelAwareTrait; + use LocatorAwareTrait; /** * @inheritDoc @@ -55,6 +55,20 @@ class PlaceholdedBehaviorTest extends TestCase 'plugin.BEdita/Core.History', ]; + /** + * ObjectsTable instance + * + * @var \BEdita\Core\Model\Table\ObjectsTable + */ + protected $Documents = null; + + /** + * MediaTable instance + * + * @var \BEdita\Core\Model\Table\MediaTable + */ + protected $Media = null; + /** * @inheritDoc */ @@ -64,8 +78,12 @@ public function setUp(): void EventManager::instance()->on(new BootstrapEventHandler()); - $this->loadModel('Documents'); - $this->loadModel('Media'); + /** @var \BEdita\Core\Model\Table\ObjectsTable $documents */ + $documents = $this->fetchTable('Documents'); + $this->Documents = $documents; + /** @var \BEdita\Core\Model\Table\MediaTable $media */ + $media = $this->fetchTable('Media'); + $this->Media = $media; } /** diff --git a/tests/TestCase/Model/Behavior/PlaceholdersBehaviorTest.php b/tests/TestCase/Model/Behavior/PlaceholdersBehaviorTest.php index 9f27ea2..cf15606 100644 --- a/tests/TestCase/Model/Behavior/PlaceholdersBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/PlaceholdersBehaviorTest.php @@ -18,9 +18,9 @@ use BEdita\Core\Model\Action\AddRelatedObjectsAction; use BEdita\Placeholders\Event\BootstrapEventHandler; use BEdita\Placeholders\Model\Behavior\PlaceholdersBehavior; -use Cake\Datasource\ModelAwareTrait; use Cake\Event\EventManager; use Cake\ORM\Entity; +use Cake\ORM\Locator\LocatorAwareTrait; use Cake\TestSuite\TestCase; use Cake\Utility\Hash; @@ -33,7 +33,7 @@ */ class PlaceholdersBehaviorTest extends TestCase { - use ModelAwareTrait; + use LocatorAwareTrait; /** * @inheritDoc @@ -57,6 +57,20 @@ class PlaceholdersBehaviorTest extends TestCase 'plugin.BEdita/Core.History', ]; + /** + * ObjectsTable instance + * + * @var \BEdita\Core\Model\Table\ObjectsTable + */ + protected $Documents = null; + + /** + * MediaTable instance + * + * @var \BEdita\Core\Model\Table\MediaTable + */ + protected $Media = null; + /** * @inheritDoc */ @@ -66,8 +80,12 @@ public function setUp(): void EventManager::instance()->on(new BootstrapEventHandler()); - $this->loadModel('Documents'); - $this->loadModel('Media'); + /** @var \BEdita\Core\Model\Table\ObjectsTable $documents */ + $documents = $this->fetchTable('Documents'); + $this->Documents = $documents; + /** @var \BEdita\Core\Model\Table\MediaTable $media */ + $media = $this->fetchTable('Media'); + $this->Media = $media; } /** From 65f472708880847d8dfc51549c30d0ec9c09b79f Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Thu, 23 Nov 2023 17:23:31 +0100 Subject: [PATCH 05/16] fix: phpunit.xml.dist --- phpunit.xml.dist | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index bb6f918..a0ddac1 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,33 +3,26 @@ colors="true" processIsolation="false" stopOnFailure="false" - bootstrap="tests/bootstrap.php" - > + bootstrap="./tests/bootstrap.php" + forceCoversAnnotation="true" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> + - - - tests/TestCase/ + ./tests/TestCase - - - - - - - - - - - - - src/ - - - + + + + + + ./src/ + + From a69fe61ef2ce5a289378e896baff079240a054b5 Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Thu, 23 Nov 2023 17:29:56 +0100 Subject: [PATCH 06/16] fix: use IntegrationTestCase in tests --- tests/TestCase/Event/BootstrapEventHandlerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/TestCase/Event/BootstrapEventHandlerTest.php b/tests/TestCase/Event/BootstrapEventHandlerTest.php index 6832b7a..d9ec676 100644 --- a/tests/TestCase/Event/BootstrapEventHandlerTest.php +++ b/tests/TestCase/Event/BootstrapEventHandlerTest.php @@ -5,6 +5,7 @@ use BEdita\API\Controller\FoldersController; use BEdita\API\Controller\RolesController; +use BEdita\API\TestSuite\IntegrationTestCase; use BEdita\Placeholders\Controller\Component\PlaceholdersComponent; use BEdita\Placeholders\Event\BootstrapEventHandler; use BEdita\Placeholders\Model\Behavior\PlaceholdedBehavior; @@ -12,14 +13,13 @@ use Cake\Event\EventManager; use Cake\Http\ServerRequest; use Cake\ORM\Locator\LocatorAwareTrait; -use Cake\TestSuite\TestCase; /** * {@see \BEdita\Placeholders\Event\BootstrapEventHandler} Test Case * * @coversDefaultClass \BEdita\Placeholders\Event\BootstrapEventHandler */ -class BootstrapEventHandlerTest extends TestCase +class BootstrapEventHandlerTest extends IntegrationTestCase { use LocatorAwareTrait; From 2b967b16775f5e72e9387b8a02906446d34e63ea Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Thu, 23 Nov 2023 17:59:08 +0100 Subject: [PATCH 07/16] fix: use cake TestSuite --- tests/TestCase/Event/BootstrapEventHandlerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/TestCase/Event/BootstrapEventHandlerTest.php b/tests/TestCase/Event/BootstrapEventHandlerTest.php index d9ec676..6832b7a 100644 --- a/tests/TestCase/Event/BootstrapEventHandlerTest.php +++ b/tests/TestCase/Event/BootstrapEventHandlerTest.php @@ -5,7 +5,6 @@ use BEdita\API\Controller\FoldersController; use BEdita\API\Controller\RolesController; -use BEdita\API\TestSuite\IntegrationTestCase; use BEdita\Placeholders\Controller\Component\PlaceholdersComponent; use BEdita\Placeholders\Event\BootstrapEventHandler; use BEdita\Placeholders\Model\Behavior\PlaceholdedBehavior; @@ -13,13 +12,14 @@ use Cake\Event\EventManager; use Cake\Http\ServerRequest; use Cake\ORM\Locator\LocatorAwareTrait; +use Cake\TestSuite\TestCase; /** * {@see \BEdita\Placeholders\Event\BootstrapEventHandler} Test Case * * @coversDefaultClass \BEdita\Placeholders\Event\BootstrapEventHandler */ -class BootstrapEventHandlerTest extends IntegrationTestCase +class BootstrapEventHandlerTest extends TestCase { use LocatorAwareTrait; From 7c7f10b0aa5cdb1232e5128b2feed557f1ec1bdb Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Thu, 23 Nov 2023 17:59:20 +0100 Subject: [PATCH 08/16] fix: review tests bootstrap --- tests/bootstrap.php | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 27b7340..5d4c95c 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,15 +1,8 @@ ['className' => NullEngine::class], ]); +ConnectionManager::drop('test'); if (!getenv('db_dsn')) { putenv('db_dsn=sqlite:///:memory:'); } -ConnectionManager::setConfig('test', [ - 'url' => getenv('db_dsn'), - // 'log' => true, -]); +ConnectionManager::setConfig('test', ['url' => getenv('db_dsn')]); ConnectionManager::alias('test', 'default'); Router::reload(); -Security::setSalt('BEDITA'); + +if (!TableRegistry::getTableLocator() instanceof TableLocator) { + TableRegistry::setTableLocator(new TableLocator()); +} + +Security::setSalt('3ikcOGwIYlAP6msatcNj76a6iueuyasdNTn'); + +(new Migrator())->runMany([ + ['plugin' => 'BEdita/Core'], + ['connection' => 'test'], +]); FilesystemRegistry::setConfig([ 'default' => ['className' => NullAdapter::class], @@ -102,6 +103,4 @@ // clear all before running tests TableRegistry::getTableLocator()->clear(); -Cache::clear('_cake_core_'); -Cache::clear('_cake_model_'); -Cache::clear('_bedita_object_types_'); +Cache::clearAll(); From dae2d11f804aa23d58d0d31f120e7a5190510da9 Mon Sep 17 00:00:00 2001 From: batopa Date: Thu, 14 Dec 2023 19:03:01 +0100 Subject: [PATCH 09/16] test(chore): remove useless conf --- phpunit.xml.dist | 1 - 1 file changed, 1 deletion(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a0ddac1..77b5f75 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -10,7 +10,6 @@ - From 25613b1aa6b5b197ba377507b696de6ac4791156 Mon Sep 17 00:00:00 2001 From: batopa Date: Thu, 14 Dec 2023 19:03:25 +0100 Subject: [PATCH 10/16] chore: gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index bc7ac80..406118b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /phpunit.xml /vendor config/Migrations/schema-dump-default.lock +.phpunit.result.cache From b6334a7a32976029e4e29cf6c9f4f824a820fc50 Mon Sep 17 00:00:00 2001 From: batopa Date: Thu, 14 Dec 2023 19:06:00 +0100 Subject: [PATCH 11/16] refactor: deprecation --- src/Model/Behavior/GetAssociationTrait.php | 4 ++-- src/Model/Behavior/PlaceholdedBehavior.php | 2 +- src/Model/Behavior/PlaceholdersBehavior.php | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Model/Behavior/GetAssociationTrait.php b/src/Model/Behavior/GetAssociationTrait.php index c606302..255375e 100644 --- a/src/Model/Behavior/GetAssociationTrait.php +++ b/src/Model/Behavior/GetAssociationTrait.php @@ -28,7 +28,7 @@ trait GetAssociationTrait * * @return \Cake\ORM\Table */ - abstract public function getTable(); + abstract public function table(); /** * Get association for a relation. @@ -39,7 +39,7 @@ abstract public function getTable(); protected function getAssociation(string $relation): ?Association { $name = Inflector::camelize($relation); - $table = $this->getTable(); + $table = $this->table(); if (!$table->hasAssociation($name)) { return null; } diff --git a/src/Model/Behavior/PlaceholdedBehavior.php b/src/Model/Behavior/PlaceholdedBehavior.php index 3c3596f..1fcaa64 100644 --- a/src/Model/Behavior/PlaceholdedBehavior.php +++ b/src/Model/Behavior/PlaceholdedBehavior.php @@ -64,7 +64,7 @@ public function beforeSave(Event $event, EntityInterface $entity): void */ protected function ensureNotPlaceholded(EntityInterface $entity): void { - $Table = $this->getTable(); + $Table = $this->table(); $relations = $this->getConfig('relations', []); foreach ($relations as $relation) { diff --git a/src/Model/Behavior/PlaceholdersBehavior.php b/src/Model/Behavior/PlaceholdersBehavior.php index c35d8ea..451c9a3 100644 --- a/src/Model/Behavior/PlaceholdersBehavior.php +++ b/src/Model/Behavior/PlaceholdersBehavior.php @@ -165,6 +165,7 @@ protected function prepareEntities(Table $table, array $placeholders): array ->where(function (QueryExpression $exp) use ($table, $pk, $ids): QueryExpression { return $exp->in($table->aliasField($pk), $ids); }) + ->all() ->map(function (EntityInterface $entity) use ($pk, $placeholders): EntityInterface { $id = $entity->get($pk); foreach ($placeholders as $datum) { From 33059d790fe951c836d8cffde1a3536c65b2176b Mon Sep 17 00:00:00 2001 From: batopa Date: Thu, 14 Dec 2023 19:06:29 +0100 Subject: [PATCH 12/16] test: fix fixtures --- tests/Fixture/ObjectRelationsFixture.php | 4 ++-- tests/Fixture/RelationTypesFixture.php | 4 ++-- tests/Fixture/RelationsFixture.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Fixture/ObjectRelationsFixture.php b/tests/Fixture/ObjectRelationsFixture.php index e8bded7..d1cc2d5 100644 --- a/tests/Fixture/ObjectRelationsFixture.php +++ b/tests/Fixture/ObjectRelationsFixture.php @@ -3,12 +3,12 @@ namespace BEdita\Placeholders\Test\Fixture; -use BEdita\Core\Test\Fixture\ObjectRelationsFixture as BEditaObjectRelationsFixture; +use Cake\TestSuite\Fixture\TestFixture; /** * Fixture for `object_relations` table. */ -class ObjectRelationsFixture extends BEditaObjectRelationsFixture +class ObjectRelationsFixture extends TestFixture { /** * Records diff --git a/tests/Fixture/RelationTypesFixture.php b/tests/Fixture/RelationTypesFixture.php index f2cd867..b6a39c5 100644 --- a/tests/Fixture/RelationTypesFixture.php +++ b/tests/Fixture/RelationTypesFixture.php @@ -3,12 +3,12 @@ namespace BEdita\Placeholders\Test\Fixture; -use BEdita\Core\Test\Fixture\RelationTypesFixture as BEditaRelationTypesFixture; +use Cake\TestSuite\Fixture\TestFixture; /** * Fixture for `relation_types` table. */ -class RelationTypesFixture extends BEditaRelationTypesFixture +class RelationTypesFixture extends TestFixture { /** * Records diff --git a/tests/Fixture/RelationsFixture.php b/tests/Fixture/RelationsFixture.php index 7d9389e..2f053d4 100644 --- a/tests/Fixture/RelationsFixture.php +++ b/tests/Fixture/RelationsFixture.php @@ -3,12 +3,12 @@ namespace BEdita\Placeholders\Test\Fixture; -use BEdita\Core\Test\Fixture\RelationsFixture as BEditaRelationsFixture; +use Cake\TestSuite\Fixture\TestFixture; /** * Fixture for `relations` table. */ -class RelationsFixture extends BEditaRelationsFixture +class RelationsFixture extends TestFixture { /** * Records From 399fcfd88e5ef4c8f9389a0d8cdff1246790d613 Mon Sep 17 00:00:00 2001 From: batopa Date: Thu, 14 Dec 2023 19:07:22 +0100 Subject: [PATCH 13/16] test: fix cases --- tests/TestApp/Application.php | 1 + .../Event/BootstrapEventHandlerTest.php | 26 ++++++++++++++----- tests/bootstrap.php | 5 +--- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/tests/TestApp/Application.php b/tests/TestApp/Application.php index 7202392..243463c 100644 --- a/tests/TestApp/Application.php +++ b/tests/TestApp/Application.php @@ -34,6 +34,7 @@ public function bootstrap(): void { $this->addPlugin('BEdita/Core'); $this->addPlugin('BEdita/API'); + $this->addPlugin('BEdita/Placeholders'); } /** diff --git a/tests/TestCase/Event/BootstrapEventHandlerTest.php b/tests/TestCase/Event/BootstrapEventHandlerTest.php index 6832b7a..c514e8c 100644 --- a/tests/TestCase/Event/BootstrapEventHandlerTest.php +++ b/tests/TestCase/Event/BootstrapEventHandlerTest.php @@ -3,8 +3,12 @@ namespace BEdita\Placeholders\Test\TestCase\Event; +use Authentication\AuthenticationService; +use Authorization\AuthorizationService; +use Authorization\Policy\MapResolver; use BEdita\API\Controller\FoldersController; use BEdita\API\Controller\RolesController; +use BEdita\API\Policy\EndpointPolicy; use BEdita\Placeholders\Controller\Component\PlaceholdersComponent; use BEdita\Placeholders\Event\BootstrapEventHandler; use BEdita\Placeholders\Model\Behavior\PlaceholdedBehavior; @@ -52,10 +56,15 @@ public function setUp(): void */ public function testOnControllerInitialize() { - $controller = new FoldersController(new ServerRequest([ + $request = new ServerRequest([ 'environment' => ['HTTP_ACCEPT' => 'application/vnd.api+json'], - 'params' => ['_ext' => 'json', 'object_type' => 'folders'], - ])); + 'params' => ['_ext' => 'json', 'object_type' => 'folders', 'action' => 'index'], + ]); + $request = $request->withAttribute('authentication', new AuthenticationService()) + ->withAttribute('authorization', new AuthorizationService(new MapResolver([ + ServerRequest::class => EndpointPolicy::class, + ]))); + $controller = new FoldersController($request); $response = $controller->startupProcess(); static::assertNull($response); @@ -71,10 +80,15 @@ public function testOnControllerInitialize() */ public function testOnControllerInitializeResourcesController() { - $controller = new RolesController(new ServerRequest([ + $request = new ServerRequest([ 'environment' => ['HTTP_ACCEPT' => 'application/vnd.api+json'], - 'params' => ['_ext' => 'json'], - ])); + 'params' => ['_ext' => 'json', 'action' => 'index'], + ]); + $request = $request->withAttribute('authentication', new AuthenticationService()) + ->withAttribute('authorization', new AuthorizationService(new MapResolver([ + ServerRequest::class => EndpointPolicy::class, + ]))); + $controller = new RolesController($request); $response = $controller->startupProcess(); static::assertNull($response); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 5d4c95c..4d40b45 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -87,10 +87,7 @@ Security::setSalt('3ikcOGwIYlAP6msatcNj76a6iueuyasdNTn'); -(new Migrator())->runMany([ - ['plugin' => 'BEdita/Core'], - ['connection' => 'test'], -]); +(new Migrator())->run(['plugin' => 'BEdita/Core']); FilesystemRegistry::setConfig([ 'default' => ['className' => NullAdapter::class], From e458db63dcff1311bff6c2d248b64edc15df220d Mon Sep 17 00:00:00 2001 From: batopa Date: Fri, 15 Dec 2023 09:54:32 +0100 Subject: [PATCH 14/16] test: use InMemoryFilesystemAdapter for tests --- composer.json | 3 +- .../Filesystem/Adapter/NullAdapter.php | 87 ++----------------- 2 files changed, 7 insertions(+), 83 deletions(-) diff --git a/composer.json b/composer.json index 5d3bdc9..f76b27b 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "cakephp/cakephp-codesniffer": "^4.5", "cakephp/plugin-installer": "^1.3", "phpunit/phpunit": "^9.5", - "phpstan/phpstan": "^1.7" + "phpstan/phpstan": "^1.7", + "league/flysystem-memory": "^2.0.0" }, "autoload": { "psr-4": { diff --git a/tests/TestApp/Filesystem/Adapter/NullAdapter.php b/tests/TestApp/Filesystem/Adapter/NullAdapter.php index c0b8c30..2f9f636 100644 --- a/tests/TestApp/Filesystem/Adapter/NullAdapter.php +++ b/tests/TestApp/Filesystem/Adapter/NullAdapter.php @@ -4,12 +4,13 @@ namespace BEdita\Placeholders\Test\TestApp\Filesystem\Adapter; use BEdita\Core\Filesystem\FilesystemAdapter; -use League\Flysystem\Config; -use League\Flysystem\FileAttributes; -use League\Flysystem\FilesystemAdapter as FlysystemFilesystemAdapter; +use League\Flysystem\InMemory\InMemoryFilesystemAdapter; /** * Null adapter, for testing. + * Internally it uses \League\Flysystem\InMemory\InMemoryFilesystemAdapter + * + * @see https://flysystem.thephpleague.com/docs/adapter/in-memory/ */ class NullAdapter extends FilesystemAdapter { @@ -18,84 +19,6 @@ class NullAdapter extends FilesystemAdapter */ protected function buildAdapter(array $config) { - return new class () implements FlysystemFilesystemAdapter - { - public function fileExists(string $path): bool - { - return false; - } - - public function write(string $path, string $contents, Config $config): void - { - } - - public function writeStream(string $path, $contents, Config $config): void - { - } - - public function read(string $path): string - { - return ''; - } - - public function readStream(string $path) - { - /** @var resource $stream */ - $stream = \fopen('php://temp', 'w+b'); - \fwrite($stream, ''); - \rewind($stream); - - return $stream; - } - - public function delete(string $path): void - { - } - - public function deleteDirectory(string $path): void - { - } - - public function createDirectory(string $path, Config $config): void - { - } - - public function setVisibility(string $path, string $visibility): void - { - } - - public function visibility(string $path): FileAttributes - { - return new FileAttributes(''); - } - - public function mimeType(string $path): FileAttributes - { - return new FileAttributes(''); - } - - public function lastModified(string $path): FileAttributes - { - return new FileAttributes(''); - } - - public function fileSize(string $path): FileAttributes - { - return new FileAttributes(''); - } - - public function listContents(string $path, bool $deep): iterable - { - return []; - } - - public function move(string $source, string $destination, Config $config): void - { - } - - public function copy(string $source, string $destination, Config $config): void - { - } - }; + return new InMemoryFilesystemAdapter(); } } From 83998903dda9e83aabbcc538c4c0d68e846ce30b Mon Sep 17 00:00:00 2001 From: dante di domenico Date: Fri, 15 Dec 2023 10:06:45 +0100 Subject: [PATCH 15/16] ci: release add 1.x to dist_branches --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4f1eb0e..ca1ebc5 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -17,5 +17,5 @@ jobs: uses: bedita/github-workflows/.github/workflows/release.yml@v1 with: main_branch: 'main' - dist_branches: '["main"]' + dist_branches: '["main","1.x"]' version_bump: ${{ inputs.releaseType }} From 4a69a0a2631d71fb3a3436c3da4307f2abbbd012 Mon Sep 17 00:00:00 2001 From: batopa Date: Fri, 15 Dec 2023 10:28:22 +0100 Subject: [PATCH 16/16] chore: remove space in doc block [skip ci] --- tests/TestApp/Filesystem/Adapter/NullAdapter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TestApp/Filesystem/Adapter/NullAdapter.php b/tests/TestApp/Filesystem/Adapter/NullAdapter.php index 2f9f636..ad9e74a 100644 --- a/tests/TestApp/Filesystem/Adapter/NullAdapter.php +++ b/tests/TestApp/Filesystem/Adapter/NullAdapter.php @@ -8,7 +8,7 @@ /** * Null adapter, for testing. - * Internally it uses \League\Flysystem\InMemory\InMemoryFilesystemAdapter + * Internally it uses `\League\Flysystem\InMemory\InMemoryFilesystemAdapter` * * @see https://flysystem.thephpleague.com/docs/adapter/in-memory/ */