From e3747dc2a168dfe3ce22497c2e7071b4f060f1da Mon Sep 17 00:00:00 2001 From: Andrej Griniuk Date: Fri, 8 Dec 2023 16:26:54 +0700 Subject: [PATCH 1/2] upgrade to cakephp 5 --- .gitignore | 3 +- .travis.yml | 15 ++++-- README.md | 2 +- composer.json | 8 +-- phpunit.xml => phpunit.xml.dist | 18 +++---- src/Command/BakeTransformerCommand.php | 2 +- src/Plugin.php | 52 ------------------- src/View/FractalTransformerView.php | 33 +++++++----- tests/Fixture/ArticlesFixture.php | 2 +- tests/Fixture/AuthorsFixture.php | 2 +- .../View/FractalTransformerViewTest.php | 6 +-- tests/bootstrap.php | 4 +- 12 files changed, 52 insertions(+), 95 deletions(-) rename phpunit.xml => phpunit.xml.dist (74%) diff --git a/.gitignore b/.gitignore index dbc0b77..77c68a6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,8 @@ /composer.phar /phpunit.xml /.phpunit.result.cache +/.phpunit.cache /phpunit.phar /config/Migrations/schema-dump-default.lock /vendor/ /.idea/ -/build/ -/tmp/ diff --git a/.travis.yml b/.travis.yml index 71253ea..18f3f45 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,14 @@ language: php +dist: jammy +addons: + apt: + packages: + - "libonig5" + php: - - 7.4 - - 8.0 - 8.1 + - 8.2 sudo: false @@ -17,16 +22,16 @@ matrix: fast_finish: true include: - - php: 7.4 + - php: 8.2 env: PHPCS=1 DEFAULT=0 - - php: 7.4 + - php: 8.2 env: COVERAGE=1 DEFAULT=0 before_script: - composer self-update - composer install --prefer-source --no-interaction --dev - - if [[ $PHPCS == 1 ]]; then composer require --dev cakephp/cakephp-codesniffer:^4.0 ; fi + - if [[ $PHPCS == 1 ]]; then composer require --dev cakephp/cakephp-codesniffer:^5.0 ; fi script: - if [[ $COVERAGE == 1 ]]; then export XDEBUG_MODE=coverage && vendor/bin/phpunit --coverage-clover=coverage.xml ; fi diff --git a/README.md b/README.md index 5bf6295..34badb3 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ This plugin is a thin wrapper for `JsonView` that allows using [Fractal transfor ## Requirements -- CakePHP 4.x (use ~1.0 for CakePHP 3.x) +- CakePHP 5.x (use ~1.0 for CakePHP 3.x, ~2.0 for CakePHP 4.x) ## Installation diff --git a/composer.json b/composer.json index 37baa25..fbf0306 100644 --- a/composer.json +++ b/composer.json @@ -12,13 +12,13 @@ } ], "require": { - "php": ">=7.4", - "cakephp/cakephp": "~4.0", + "php": ">=8.1", + "cakephp/cakephp": "^5.0", "league/fractal": "^0.20.1" }, "require-dev": { - "phpunit/phpunit": "^9.3", - "cakephp/bake": "^2.8" + "phpunit/phpunit": "^10", + "cakephp/bake": "^3.0" }, "autoload": { "psr-4": { diff --git a/phpunit.xml b/phpunit.xml.dist similarity index 74% rename from phpunit.xml rename to phpunit.xml.dist index c43dff2..af0cc91 100644 --- a/phpunit.xml +++ b/phpunit.xml.dist @@ -1,25 +1,23 @@ - + + - tests/TestCase/ - - - - - - - + + src/ - + + + + diff --git a/src/Command/BakeTransformerCommand.php b/src/Command/BakeTransformerCommand.php index e470997..ddc28d5 100644 --- a/src/Command/BakeTransformerCommand.php +++ b/src/Command/BakeTransformerCommand.php @@ -15,7 +15,7 @@ class BakeTransformerCommand extends SimpleBakeCommand * * @var string */ - protected $pathFragment = 'Model/Transformer/'; + protected string $pathFragment = 'Model/Transformer/'; /** * Get the generated object's name. diff --git a/src/Plugin.php b/src/Plugin.php index 1bf6c36..af6dc5a 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -5,9 +5,6 @@ use Cake\Console\CommandCollection; use Cake\Core\BasePlugin; -use Cake\Core\PluginApplicationInterface; -use Cake\Http\MiddlewareQueue; -use Cake\Routing\RouteBuilder; use FractalTransformerView\Command\BakeTransformerCommand; /** @@ -15,55 +12,6 @@ */ class Plugin extends BasePlugin { - /** - * Load all the plugin configuration and bootstrap logic. - * - * The host application is provided as an argument. This allows you to load - * additional plugin dependencies, or attach events. - * - * @param \Cake\Core\PluginApplicationInterface $app The host application - * @return void - */ - public function bootstrap(PluginApplicationInterface $app): void - { - } - - /** - * Add routes for the plugin. - * - * If your plugin has many routes and you would like to isolate them into a separate file, - * you can create `$plugin/config/routes.php` and delete this method. - * - * @param \Cake\Routing\RouteBuilder $routes The route builder to update. - * @return void - */ - public function routes(RouteBuilder $routes): void - { - $routes->plugin( - 'FractalTransformerView', - ['path' => '/fractal-transformer-view'], - function (RouteBuilder $builder) { - // Add custom routes here - - $builder->fallbacks(); - } - ); - parent::routes($routes); - } - - /** - * Add middleware for the plugin. - * - * @param \Cake\Http\MiddlewareQueue $middleware The middleware queue to update. - * @return \Cake\Http\MiddlewareQueue - */ - public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue - { - // Add your middlewares here - - return $middlewareQueue; - } - /** * Add console commands for the plugin. * diff --git a/src/View/FractalTransformerView.php b/src/View/FractalTransformerView.php index bbfef66..29f7ca8 100644 --- a/src/View/FractalTransformerView.php +++ b/src/View/FractalTransformerView.php @@ -5,7 +5,10 @@ use Cake\Collection\CollectionInterface; use Cake\Datasource\EntityInterface; +use Cake\Datasource\Paging\PaginatedResultSet; +use Cake\Datasource\ResultSetDecorator; use Cake\ORM\Query; +use Cake\ORM\ResultSet; use Cake\Utility\Hash; use Cake\View\JsonView; use Exception; @@ -20,11 +23,6 @@ */ class FractalTransformerView extends JsonView { - /** - * @var \League\Fractal\Serializer\SerializerAbstract - */ - protected $_serializer; - /** * Default config options. * @@ -47,7 +45,7 @@ class FractalTransformerView extends JsonView * @var array * @pslam-var array{serialize:string|bool|null, jsonOptions: int|null, jsonp: bool|string|null} */ - protected $_defaultConfig = [ + protected array $_defaultConfig = [ 'serialize' => null, 'jsonOptions' => null, 'jsonp' => null, @@ -60,14 +58,21 @@ class FractalTransformerView extends JsonView /** * Get transform class name for given var by figuring out which entity it belongs to. Return FALSE otherwise * - * @param \Cake\ORM\Query|\Cake\Collection\CollectionInterface|\Cake\Datasource\EntityInterface $var variable + * @param \Cake\ORM\Query|\Cake\ORM\ResultSet|\Cake\Datasource\ResultSetDecorator|\Cake\Datasource\EntityInterface|\Cake\Collection\CollectionInterface|mixed $var variable * @return string|null */ - protected function getTransformerClass($var): ?string + protected function getTransformerClass(mixed $var): ?string { $entity = null; if ($var instanceof Query) { $entity = $var->getRepository()->newEmptyEntity(); + } elseif ($var instanceof PaginatedResultSet) { + /** @phpstan-ignore-next-line */ + $entity = $var->items()->first(); + } elseif ($var instanceof ResultSetDecorator) { + $entity = $var->first(); + } elseif ($var instanceof ResultSet) { + $entity = $var->first(); } elseif ($var instanceof CollectionInterface) { $entity = $var->first(); } elseif ($var instanceof EntityInterface) { @@ -76,7 +81,7 @@ protected function getTransformerClass($var): ?string $entity = reset($var); } - if (!$entity || !$entity instanceof EntityInterface) { + if (!$entity || !is_object($entity)) { return null; } @@ -98,7 +103,7 @@ protected function getTransformerClass($var): ?string * @return \League\Fractal\TransformerAbstract|null * @throws \Exception */ - protected function getTransformer($var, $varName = null): ?TransformerAbstract + protected function getTransformer(mixed $var, ?string $varName = null): ?TransformerAbstract { $_transform = $this->getConfig('transform'); $transformerClass = $varName @@ -136,10 +141,10 @@ protected function getTransformer($var, $varName = null): ?TransformerAbstract * @param \League\Fractal\Manager $manager Manager * @param mixed $var Variable * @param string|null $varName Variable name - * @return array + * @return mixed * @throws \Exception */ - protected function transform(Manager $manager, $var, $varName = null) + protected function transform(Manager $manager, mixed $var, ?string $varName = null): mixed { $transformer = $this->getTransformer($var, $varName); if (!$transformer) { @@ -148,7 +153,7 @@ protected function transform(Manager $manager, $var, $varName = null) $resourceKey = $this->getConfig('resourceKey'); - if (is_array($var) || $var instanceof Query || $var instanceof CollectionInterface) { + if (is_array($var) || $var instanceof Query || $var instanceof ResultSet || $var instanceof ResultSetDecorator || $var instanceof PaginatedResultSet) { $resource = new Collection($var, $transformer, $resourceKey); } elseif ($var instanceof EntityInterface) { $resource = new Item($var, $transformer, $resourceKey); @@ -166,7 +171,7 @@ protected function transform(Manager $manager, $var, $varName = null) * @return mixed The data to serialize. * @throws \Exception */ - protected function _dataToSerialize($serialize) + protected function _dataToSerialize(array|string $serialize): mixed { $data = parent::_dataToSerialize($serialize); diff --git a/tests/Fixture/ArticlesFixture.php b/tests/Fixture/ArticlesFixture.php index 567acdf..281023c 100644 --- a/tests/Fixture/ArticlesFixture.php +++ b/tests/Fixture/ArticlesFixture.php @@ -15,7 +15,7 @@ class ArticlesFixture extends TestFixture * * @var array */ - public $records = [ + public array $records = [ ['title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y'], ['title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y'], ['title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y'], diff --git a/tests/Fixture/AuthorsFixture.php b/tests/Fixture/AuthorsFixture.php index 739844b..d36962f 100644 --- a/tests/Fixture/AuthorsFixture.php +++ b/tests/Fixture/AuthorsFixture.php @@ -15,7 +15,7 @@ class AuthorsFixture extends TestFixture * * @var array */ - public $records = [ + public array $records = [ ['name' => 'mariano'], ['name' => 'nate'], ['name' => 'larry'], diff --git a/tests/TestCase/View/FractalTransformerViewTest.php b/tests/TestCase/View/FractalTransformerViewTest.php index 4f258ea..2247d06 100644 --- a/tests/TestCase/View/FractalTransformerViewTest.php +++ b/tests/TestCase/View/FractalTransformerViewTest.php @@ -8,6 +8,7 @@ use FractalTransformerView\Serializer\ArraySerializer; use FractalTransformerView\View\FractalTransformerView; use League\Fractal\Manager; +use ReflectionClass; use stdClass; /** @@ -15,7 +16,7 @@ */ class FractalTransformerViewTest extends TestCase { - public $fixtures = ['plugin.FractalTransformerView.Articles', 'plugin.FractalTransformerView.Authors']; + public array $fixtures = ['plugin.FractalTransformerView.Articles', 'plugin.FractalTransformerView.Authors']; /** * @var \Cake\ORM\Table @@ -370,9 +371,8 @@ public function testDataToSerializeSingle(): void */ public function protectedMethodCall(object $obj, string $name, array $args = []) { - $class = new \ReflectionClass($obj); + $class = new ReflectionClass($obj); $method = $class->getMethod($name); - $method->setAccessible(true); return $method->invokeArgs($obj, $args); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 1cf6fbb..8863c9c 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -12,6 +12,7 @@ use Cake\Core\Configure; use Cake\Core\Plugin; use Cake\TestSuite\Fixture\SchemaLoader; +use FractalTransformerView\Plugin as FractalTransformerViewPlugin; $findRoot = function ($root) { do { @@ -52,6 +53,7 @@ 'plugins' => [ROOT . 'Plugin' . DS], 'templates' => [ROOT . 'templates' . DS], ], + 'encoding' => 'UTF-8', ] ); @@ -59,7 +61,7 @@ putenv('db_dsn=sqlite:///:memory:'); } -Plugin::getCollection()->add(new \FractalTransformerView\Plugin()); +Plugin::getCollection()->add(new FractalTransformerViewPlugin()); $_SERVER['PHP_SELF'] = '/'; From f66a35345d6a328ee0b01e82f791e873f9e155f4 Mon Sep 17 00:00:00 2001 From: Andrej Griniuk Date: Mon, 11 Dec 2023 11:36:40 +0700 Subject: [PATCH 2/2] rename plugin class --- src/{Plugin.php => FractalTransformerViewPlugin.php} | 2 +- tests/bootstrap.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/{Plugin.php => FractalTransformerViewPlugin.php} (92%) diff --git a/src/Plugin.php b/src/FractalTransformerViewPlugin.php similarity index 92% rename from src/Plugin.php rename to src/FractalTransformerViewPlugin.php index af6dc5a..e8b9829 100644 --- a/src/Plugin.php +++ b/src/FractalTransformerViewPlugin.php @@ -10,7 +10,7 @@ /** * Plugin for FractalTransformerView */ -class Plugin extends BasePlugin +class FractalTransformerViewPlugin extends BasePlugin { /** * Add console commands for the plugin. diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 8863c9c..32f2cfd 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -12,7 +12,7 @@ use Cake\Core\Configure; use Cake\Core\Plugin; use Cake\TestSuite\Fixture\SchemaLoader; -use FractalTransformerView\Plugin as FractalTransformerViewPlugin; +use FractalTransformerView\FractalTransformerViewPlugin; $findRoot = function ($root) { do {