Skip to content

Commit

Permalink
Merge pull request #13 from andrej-griniuk/cake5
Browse files Browse the repository at this point in the history
Cake5
  • Loading branch information
andrej-griniuk authored Dec 11, 2023
2 parents e70270a + f66a353 commit cd8f6d5
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 124 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
15 changes: 10 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
language: php

dist: jammy
addons:
apt:
packages:
- "libonig5"

php:
- 7.4
- 8.0
- 8.1
- 8.2

sudo: false

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
18 changes: 8 additions & 10 deletions phpunit.xml → phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" processIsolation="false" stopOnFailure="false" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" processIsolation="false" stopOnFailure="false" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache">
<coverage/>
<php>
<ini name="memory_limit" value="-1"/>
<ini name="apc.enable_cli" value="1"/>
</php>

<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="FractalTransformerView">
<directory>tests/TestCase/</directory>
</testsuite>
</testsuites>

<!-- Load extension for fixtures -->
<extensions>
<extension class="Cake\TestSuite\Fixture\PHPUnitExtension"/>
</extensions>

<coverage>
<!-- Setup a listener for fixtures -->
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
</source>
<extensions>
<bootstrap class="Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
</extensions>
</phpunit>
2 changes: 1 addition & 1 deletion src/Command/BakeTransformerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions src/FractalTransformerViewPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

namespace FractalTransformerView;

use Cake\Console\CommandCollection;
use Cake\Core\BasePlugin;
use FractalTransformerView\Command\BakeTransformerCommand;

/**
* Plugin for FractalTransformerView
*/
class FractalTransformerViewPlugin extends BasePlugin
{
/**
* Add console commands for the plugin.
*
* @param \Cake\Console\CommandCollection $commands The command collection to update
* @return \Cake\Console\CommandCollection
*/
public function console(CommandCollection $commands): CommandCollection
{
if (class_exists('Bake\Command\SimpleBakeCommand')) {
$commands->add('bake transformer', BakeTransformerCommand::class);
}

return $commands;
}
}
81 changes: 0 additions & 81 deletions src/Plugin.php

This file was deleted.

33 changes: 19 additions & 14 deletions src/View/FractalTransformerView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,11 +23,6 @@
*/
class FractalTransformerView extends JsonView
{
/**
* @var \League\Fractal\Serializer\SerializerAbstract
*/
protected $_serializer;

/**
* Default config options.
*
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -76,7 +81,7 @@ protected function getTransformerClass($var): ?string
$entity = reset($var);
}

if (!$entity || !$entity instanceof EntityInterface) {
if (!$entity || !is_object($entity)) {
return null;
}

Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/ArticlesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/AuthorsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AuthorsFixture extends TestFixture
*
* @var array
*/
public $records = [
public array $records = [
['name' => 'mariano'],
['name' => 'nate'],
['name' => 'larry'],
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/View/FractalTransformerViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
use FractalTransformerView\Serializer\ArraySerializer;
use FractalTransformerView\View\FractalTransformerView;
use League\Fractal\Manager;
use ReflectionClass;
use stdClass;

/**
* FractalTransformerViewTest
*/
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
Expand Down Expand Up @@ -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);
}
Expand Down
Loading

0 comments on commit cd8f6d5

Please sign in to comment.