From bc1311ee47fd1ccd930dde259f698be6237cffec Mon Sep 17 00:00:00 2001 From: Dominik Zogg Date: Wed, 5 Jun 2019 19:26:56 +0200 Subject: [PATCH 1/3] feat/JMP-592/enhancePolicies --- README.md | 2 +- composer.json | 2 +- .../DateTimeFieldDenormalizer.php | 6 +- src/Denormalizer/Denormalizer.php | 23 +++ src/Denormalizer/DenormalizerContext.php | 51 ++++- .../DenormalizerContextBuilder.php | 24 ++- .../DenormalizerContextBuilderInterface.php | 17 +- .../DenormalizerContextInterface.php | 30 ++- src/Mapping/DenormalizationFieldMapping.php | 29 ++- .../DenormalizationFieldMappingBuilder.php | 31 ++- ...malizationFieldMappingBuilderInterface.php | 13 ++ .../DenormalizationFieldMappingInterface.php | 11 ++ src/Policy/AndPolicy.php | 40 ++++ src/Policy/CallbackPolicy.php | 34 ++++ src/Policy/GroupPolicy.php | 51 +++++ src/Policy/NullPolicy.php | 21 +++ src/Policy/OrPolicy.php | 40 ++++ src/Policy/PolicyInterface.php | 18 ++ .../DenormalizerContextBuilderTest.php | 3 + .../Denormalizer/DenormalizerContextTest.php | 22 ++- tests/Denormalizer/DenormalizerTest.php | 177 +++++++++++++++++- ...DenormalizationFieldMappingBuilderTest.php | 34 ++++ .../DenormalizationFieldMappingTest.php | 14 ++ tests/Policy/AndPolicyTest.php | 68 +++++++ tests/Policy/CallbackPolicyTest.php | 53 ++++++ tests/Policy/GroupPolicyTest.php | 114 +++++++++++ tests/Policy/NullPolicyTest.php | 31 +++ tests/Policy/OrPolicyTest.php | 68 +++++++ 28 files changed, 1003 insertions(+), 24 deletions(-) create mode 100644 src/Policy/AndPolicy.php create mode 100644 src/Policy/CallbackPolicy.php create mode 100644 src/Policy/GroupPolicy.php create mode 100644 src/Policy/NullPolicy.php create mode 100644 src/Policy/OrPolicy.php create mode 100644 src/Policy/PolicyInterface.php create mode 100644 tests/Policy/AndPolicyTest.php create mode 100644 tests/Policy/CallbackPolicyTest.php create mode 100644 tests/Policy/GroupPolicyTest.php create mode 100644 tests/Policy/NullPolicyTest.php create mode 100644 tests/Policy/OrPolicyTest.php diff --git a/README.md b/README.md index 6e03bb1..903d488 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ A simple deserialization. Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-deserialization][1]. ```sh -composer require chubbyphp/chubbyphp-deserialization "~2.10" +composer require chubbyphp/chubbyphp-deserialization "~2.11" ``` ## Usage diff --git a/composer.json b/composer.json index 212a18c..d06344d 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ }, "extra": { "branch-alias": { - "dev-master": "2.10-dev" + "dev-master": "2.11-dev" } } } diff --git a/src/Denormalizer/DateTimeFieldDenormalizer.php b/src/Denormalizer/DateTimeFieldDenormalizer.php index dd10857..1825980 100644 --- a/src/Denormalizer/DateTimeFieldDenormalizer.php +++ b/src/Denormalizer/DateTimeFieldDenormalizer.php @@ -33,8 +33,8 @@ final class DateTimeFieldDenormalizer implements FieldDenormalizerInterface /** * @param AccessorInterface|FieldDenormalizerInterface $accessor - * @param bool $emptyToNull - * @param \DateTimeZone|null $dateTimeZone + * @param bool $emptyToNull + * @param \DateTimeZone|null $dateTimeZone */ public function __construct($accessor, bool $emptyToNull = false, \DateTimeZone $dateTimeZone = null) { @@ -104,7 +104,7 @@ public function denormalizeField( try { $dateTime = new \DateTime($trimmedValue); - if(null !== $this->dateTimeZone) { + if (null !== $this->dateTimeZone) { $dateTime->setTimezone($this->dateTimeZone); } diff --git a/src/Denormalizer/Denormalizer.php b/src/Denormalizer/Denormalizer.php index 93c931f..9d9f83b 100644 --- a/src/Denormalizer/Denormalizer.php +++ b/src/Denormalizer/Denormalizer.php @@ -156,6 +156,10 @@ private function denormalizeField( array $data, $object ) { + if (!$this->isCompliant($context, $denormalizationFieldMapping, $object)) { + return; + } + if (!$this->isWithinGroup($context, $denormalizationFieldMapping)) { return; } @@ -183,6 +187,25 @@ private function handleNotAllowedAdditionalFields(string $path, array $names) throw $exception; } + /** + * @param DenormalizerContextInterface $context + * @param DenormalizationFieldMappingInterface $mapping + * @param object $object + * + * @return bool + */ + private function isCompliant( + DenormalizerContextInterface $context, + DenormalizationFieldMappingInterface $mapping, + $object + ): bool { + if (!is_callable([$mapping, 'getPolicy'])) { + return true; + } + + return $mapping->getPolicy()->isCompliant($context, $object); + } + /** * @param DenormalizerContextInterface $context * @param DenormalizationFieldMappingInterface $fieldMapping diff --git a/src/Denormalizer/DenormalizerContext.php b/src/Denormalizer/DenormalizerContext.php index 4da5fa7..10724f4 100644 --- a/src/Denormalizer/DenormalizerContext.php +++ b/src/Denormalizer/DenormalizerContext.php @@ -14,6 +14,8 @@ final class DenormalizerContext implements DenormalizerContextInterface private $allowedAdditionalFields; /** + * @deprecated + * * @var string[] */ private $groups = []; @@ -28,17 +30,24 @@ final class DenormalizerContext implements DenormalizerContextInterface */ private $resetMissingFields; + /** + * @var array + */ + private $attributes; + /** * @param array|null $allowedAdditionalFields * @param string[] $groups * @param ServerRequestInterface|null $request * @param bool $resetMissingFields + * @param array $attributes */ public function __construct( array $allowedAdditionalFields = null, array $groups = [], ServerRequestInterface $request = null, - bool $resetMissingFields = false + bool $resetMissingFields = false, + array $attributes = [] ) { $this->allowedAdditionalFields = $allowedAdditionalFields; $this->groups = $groups; @@ -52,6 +61,7 @@ public function __construct( } $this->resetMissingFields = $resetMissingFields; + $this->attributes = $attributes; } /** @@ -63,6 +73,8 @@ public function getAllowedAdditionalFields() } /** + * @deprecated + * * @return string[] */ public function getGroups(): array @@ -87,4 +99,41 @@ public function isResetMissingFields(): bool { return $this->resetMissingFields; } + + /** + * @return array + */ + public function getAttributes(): array + { + return $this->attributes; + } + + /** + * @param string $name + * @param mixed $default + * + * @return mixed + */ + public function getAttribute(string $name, $default = null) + { + if (isset($this->attributes[$name])) { + return $this->attributes[$name]; + } + + return $default; + } + + /** + * @param string $name + * @param mixed $value + * + * @return DenormalizerContextInterface + */ + public function withAttribute(string $name, $value): DenormalizerContextInterface + { + $context = clone $this; + $context->attributes[$name] = $value; + + return $context; + } } diff --git a/src/Denormalizer/DenormalizerContextBuilder.php b/src/Denormalizer/DenormalizerContextBuilder.php index fea3a6f..0a745d2 100644 --- a/src/Denormalizer/DenormalizerContextBuilder.php +++ b/src/Denormalizer/DenormalizerContextBuilder.php @@ -14,6 +14,8 @@ final class DenormalizerContextBuilder implements DenormalizerContextBuilderInte private $allowedAdditionalFields; /** + * @deprecated + * * @var string[] */ private $groups = []; @@ -28,6 +30,11 @@ final class DenormalizerContextBuilder implements DenormalizerContextBuilderInte */ private $resetMissingFields = false; + /** + * @var array + */ + private $attributes = []; + private function __construct() { } @@ -54,6 +61,8 @@ public function setAllowedAdditionalFields( } /** + * @deprecated + * * @param string[] $groups * * @return DenormalizerContextBuilderInterface @@ -96,6 +105,18 @@ public function setResetMissingFields(bool $resetMissingFields): DenormalizerCon return $this; } + /** + * @param array $attributes + * + * @return DenormalizerContextBuilderInterface + */ + public function setAttributes(array $attributes): DenormalizerContextBuilderInterface + { + $this->attributes = $attributes; + + return $this; + } + /** * @return DenormalizerContextInterface */ @@ -105,7 +126,8 @@ public function getContext(): DenormalizerContextInterface $this->allowedAdditionalFields, $this->groups, $this->request, - $this->resetMissingFields + $this->resetMissingFields, + $this->attributes ); } } diff --git a/src/Denormalizer/DenormalizerContextBuilderInterface.php b/src/Denormalizer/DenormalizerContextBuilderInterface.php index e38105a..c8f4ffa 100644 --- a/src/Denormalizer/DenormalizerContextBuilderInterface.php +++ b/src/Denormalizer/DenormalizerContextBuilderInterface.php @@ -6,6 +6,9 @@ use Psr\Http\Message\ServerRequestInterface; +/** + * @method setAttributes(array $attributes): self + */ interface DenormalizerContextBuilderInterface { /** @@ -21,6 +24,8 @@ public static function create(): self; public function setAllowedAdditionalFields(array $allowedAdditionalFields = null): self; /** + * @deprecated + * * @param string[] $groups * * @return self @@ -34,12 +39,12 @@ public function setGroups(array $groups): self; */ public function setRequest(ServerRequestInterface $request = null): self; - // /** - // * @param bool $resetMissingFields - // * - // * @return self - // */ - // public function setResetMissingFields(bool $resetMissingFields): self; + /** + * @param array $attributes + * + * @return self + */ + //public function setAttributes(array $attributes): self; /** * @return DenormalizerContextInterface diff --git a/src/Denormalizer/DenormalizerContextInterface.php b/src/Denormalizer/DenormalizerContextInterface.php index 6870b1f..1904abe 100644 --- a/src/Denormalizer/DenormalizerContextInterface.php +++ b/src/Denormalizer/DenormalizerContextInterface.php @@ -6,6 +6,11 @@ use Psr\Http\Message\ServerRequestInterface; +/** + * @method getAttributes(): array + * @method getAttribute(string $name, $default = null) + * @method withAttribute(string $name, $value): self + */ interface DenormalizerContextInterface { /** @@ -14,6 +19,8 @@ interface DenormalizerContextInterface public function getAllowedAdditionalFields(); /** + * @deprecated + * * @return string[] */ public function getGroups(): array; @@ -23,8 +30,23 @@ public function getGroups(): array; */ public function getRequest(); - // /** - // * @return bool - // */ - // public function isResetMissingFields(): bool; + /* + * @return array + */ + //public function getAttributes(): array; + + /* + * @param string $name + * @param mixed $default + * + * @return mixed + */ + //public function getAttribute(string $name, $default = null); + + /* + * @param string $name + * @param mixed $value + * @return self + */ + //public function withAttribute(string $name, $value): self; } diff --git a/src/Mapping/DenormalizationFieldMapping.php b/src/Mapping/DenormalizationFieldMapping.php index ae92212..66704d2 100644 --- a/src/Mapping/DenormalizationFieldMapping.php +++ b/src/Mapping/DenormalizationFieldMapping.php @@ -5,6 +5,8 @@ namespace Chubbyphp\Deserialization\Mapping; use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface; +use Chubbyphp\Deserialization\Policy\NullPolicy; +use Chubbyphp\Deserialization\Policy\PolicyInterface; final class DenormalizationFieldMapping implements DenormalizationFieldMappingInterface { @@ -14,6 +16,8 @@ final class DenormalizationFieldMapping implements DenormalizationFieldMappingIn private $name; /** + * @deprecated + * * @var array */ private $groups; @@ -23,16 +27,27 @@ final class DenormalizationFieldMapping implements DenormalizationFieldMappingIn */ private $fieldDenormalizer; + /** + * @var PolicyInterface + */ + private $policy; + /** * @param string $name * @param array $groups * @param FieldDenormalizerInterface $fieldDenormalizer + * @param PolicyInterface|null $policy */ - public function __construct($name, array $groups = [], FieldDenormalizerInterface $fieldDenormalizer) - { + public function __construct( + $name, + array $groups = [], + FieldDenormalizerInterface $fieldDenormalizer, + PolicyInterface $policy = null + ) { $this->name = $name; $this->groups = $groups; $this->fieldDenormalizer = $fieldDenormalizer; + $this->policy = $policy ?? new NullPolicy(); } /** @@ -44,6 +59,8 @@ public function getName(): string } /** + * @deprecated + * * @return array */ public function getGroups(): array @@ -58,4 +75,12 @@ public function getFieldDenormalizer(): FieldDenormalizerInterface { return $this->fieldDenormalizer; } + + /** + * @return PolicyInterface + */ + public function getPolicy(): PolicyInterface + { + return $this->policy; + } } diff --git a/src/Mapping/DenormalizationFieldMappingBuilder.php b/src/Mapping/DenormalizationFieldMappingBuilder.php index f0fd226..8e691b4 100644 --- a/src/Mapping/DenormalizationFieldMappingBuilder.php +++ b/src/Mapping/DenormalizationFieldMappingBuilder.php @@ -14,6 +14,8 @@ use Chubbyphp\Deserialization\Denormalizer\Relation\EmbedOneFieldDenormalizer; use Chubbyphp\Deserialization\Denormalizer\Relation\ReferenceManyFieldDenormalizer; use Chubbyphp\Deserialization\Denormalizer\Relation\ReferenceOneFieldDenormalizer; +use Chubbyphp\Deserialization\Policy\NullPolicy; +use Chubbyphp\Deserialization\Policy\PolicyInterface; final class DenormalizationFieldMappingBuilder implements DenormalizationFieldMappingBuilderInterface { @@ -23,6 +25,8 @@ final class DenormalizationFieldMappingBuilder implements DenormalizationFieldMa private $name; /** + * @deprecated + * * @var array */ private $groups = []; @@ -32,6 +36,11 @@ final class DenormalizationFieldMappingBuilder implements DenormalizationFieldMa */ private $fieldDenormalizer; + /** + * @var PolicyInterface|null + */ + private $policy; + private function __construct(string $name) { $this->name = $name; @@ -83,9 +92,10 @@ public static function createConvertType( } /** - * @param string $name - * @param bool $emptyToNull + * @param string $name + * @param bool $emptyToNull * @param \DateTimeZone $dateTimeZone + * * @return DenormalizationFieldMappingBuilderInterface */ public static function createDateTime( @@ -166,6 +176,8 @@ public static function createReferenceOne( } /** + * @deprecated + * * @param array $groups * * @return DenormalizationFieldMappingBuilderInterface @@ -190,6 +202,18 @@ public function setFieldDenormalizer( return $this; } + /** + * @param PolicyInterface $policy + * + * @return DenormalizationFieldMappingBuilderInterface + */ + public function setPolicy(PolicyInterface $policy): DenormalizationFieldMappingBuilderInterface + { + $this->policy = $policy; + + return $this; + } + /** * @return DenormalizationFieldMappingInterface */ @@ -198,7 +222,8 @@ public function getMapping(): DenormalizationFieldMappingInterface return new DenormalizationFieldMapping( $this->name, $this->groups, - $this->fieldDenormalizer + $this->fieldDenormalizer, + $this->policy ?? new NullPolicy() ); } } diff --git a/src/Mapping/DenormalizationFieldMappingBuilderInterface.php b/src/Mapping/DenormalizationFieldMappingBuilderInterface.php index 83bf5db..4aa2b8f 100644 --- a/src/Mapping/DenormalizationFieldMappingBuilderInterface.php +++ b/src/Mapping/DenormalizationFieldMappingBuilderInterface.php @@ -5,7 +5,11 @@ namespace Chubbyphp\Deserialization\Mapping; use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface; +use Chubbyphp\Deserialization\Policy\PolicyInterface; +/** + * @method setPolicy(PolicyInterface $policy): self + */ interface DenormalizationFieldMappingBuilderInterface { /** @@ -16,6 +20,8 @@ interface DenormalizationFieldMappingBuilderInterface public static function create(string $name): self; /** + * @deprecated + * * @param array $groups * * @return self @@ -29,6 +35,13 @@ public function setGroups(array $groups): self; */ public function setFieldDenormalizer(FieldDenormalizerInterface $fieldDenormalizer): self; + /** + * @param PolicyInterface $policy + * + * @return self + */ + //public function setPolicy(PolicyInterface $policy): self; + /** * @return DenormalizationFieldMappingInterface */ diff --git a/src/Mapping/DenormalizationFieldMappingInterface.php b/src/Mapping/DenormalizationFieldMappingInterface.php index 5042dc8..1cc50f6 100644 --- a/src/Mapping/DenormalizationFieldMappingInterface.php +++ b/src/Mapping/DenormalizationFieldMappingInterface.php @@ -5,7 +5,11 @@ namespace Chubbyphp\Deserialization\Mapping; use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface; +use Chubbyphp\Deserialization\Policy\PolicyInterface; +/** + * @method getPolicy(): PolicyInterface + */ interface DenormalizationFieldMappingInterface { /** @@ -14,6 +18,8 @@ interface DenormalizationFieldMappingInterface public function getName(): string; /** + * @deprecated + * * @return array */ public function getGroups(): array; @@ -22,4 +28,9 @@ public function getGroups(): array; * @return FieldDenormalizerInterface */ public function getFieldDenormalizer(): FieldDenormalizerInterface; + + /* + * @return PolicyInterface + */ + //public function getPolicy(): PolicyInterface; } diff --git a/src/Policy/AndPolicy.php b/src/Policy/AndPolicy.php new file mode 100644 index 0000000..d39fdf7 --- /dev/null +++ b/src/Policy/AndPolicy.php @@ -0,0 +1,40 @@ +policies = $policies; + } + + /** + * @param DenormalizerContextInterface $context + * @param object|mixed $object + * + * @return bool + */ + public function isCompliant(DenormalizerContextInterface $context, $object): bool + { + foreach ($this->policies as $policy) { + if (false === $policy->isCompliant($context, $object)) { + return false; + } + } + + return true; + } +} diff --git a/src/Policy/CallbackPolicy.php b/src/Policy/CallbackPolicy.php new file mode 100644 index 0000000..b986045 --- /dev/null +++ b/src/Policy/CallbackPolicy.php @@ -0,0 +1,34 @@ +callback = $callback; + } + + /** + * @param DenormalizerContextInterface $context + * @param object|mixed $object + * + * @return bool + */ + public function isCompliant(DenormalizerContextInterface $context, $object): bool + { + return ($this->callback)($context, $object); + } +} diff --git a/src/Policy/GroupPolicy.php b/src/Policy/GroupPolicy.php new file mode 100644 index 0000000..ae2519c --- /dev/null +++ b/src/Policy/GroupPolicy.php @@ -0,0 +1,51 @@ +groups = $groups; + } + + /** + * @param DenormalizerContextInterface $context + * @param object|mixed $object + * + * @return bool + */ + public function isCompliant(DenormalizerContextInterface $context, $object): bool + { + if ([] === $this->groups) { + return true; + } + + $contextGroups = $context->getAttribute(self::ATTRIBUTE_GROUPS, []); + + foreach ($this->groups as $group) { + if (in_array($group, $contextGroups, true)) { + return true; + } + } + + return false; + } +} diff --git a/src/Policy/NullPolicy.php b/src/Policy/NullPolicy.php new file mode 100644 index 0000000..190bc7c --- /dev/null +++ b/src/Policy/NullPolicy.php @@ -0,0 +1,21 @@ +policies = $policies; + } + + /** + * @param DenormalizerContextInterface $context + * @param object|mixed $object + * + * @return bool + */ + public function isCompliant(DenormalizerContextInterface $context, $object): bool + { + foreach ($this->policies as $policy) { + if (true === $policy->isCompliant($context, $object)) { + return true; + } + } + + return false; + } +} diff --git a/src/Policy/PolicyInterface.php b/src/Policy/PolicyInterface.php new file mode 100644 index 0000000..a8a4db1 --- /dev/null +++ b/src/Policy/PolicyInterface.php @@ -0,0 +1,18 @@ +getGroups()); self::assertNull($context->getRequest()); self::assertFalse($context->isResetMissingFields()); + self::assertSame([], $context->getAttributes()); } public function testCreateWithSetResetMissingField() @@ -56,6 +57,7 @@ public function testCreateWithOverridenSettings() ->setAllowedAdditionalFields(['allowed_field']) ->setGroups(['group1']) ->setRequest($request) + ->setAttributes(['attribute' => 'value']) ->getContext(); self::assertInstanceOf(DenormalizerContextInterface::class, $context); @@ -64,6 +66,7 @@ public function testCreateWithOverridenSettings() self::assertSame(['group1'], $context->getGroups()); self::assertSame($request, $context->getRequest()); self::assertFalse($context->isResetMissingFields()); + self::assertSame(['attribute' => 'value'], $context->getAttributes()); } public function testCreateSetNullRequest() diff --git a/tests/Denormalizer/DenormalizerContextTest.php b/tests/Denormalizer/DenormalizerContextTest.php index 428094a..a4b7d0e 100644 --- a/tests/Denormalizer/DenormalizerContextTest.php +++ b/tests/Denormalizer/DenormalizerContextTest.php @@ -25,6 +25,9 @@ public function testCreate() self::assertSame([], $context->getGroups()); self::assertNull($context->getRequest()); self::assertFalse($context->isResetMissingFields()); + self::assertSame([], $context->getAttributes()); + self::assertNull($context->getAttribute('nonExistingAttribute')); + self::assertSame('default', $context->getAttribute('nonExistingAttribute', 'default')); } public function testCreateWithOverridenSettings() @@ -32,12 +35,14 @@ public function testCreateWithOverridenSettings() /** @var ServerRequestInterface|MockObject $request */ $request = $this->getMockByCalls(ServerRequestInterface::class); - $context = new DenormalizerContext(['allowed_field'], ['group1'], $request, false); + $context = new DenormalizerContext(['allowed_field'], ['group1'], $request, false, ['attribute' => 'value']); self::assertSame(['allowed_field'], $context->getAllowedAdditionalFields()); self::assertSame(['group1'], $context->getGroups()); self::assertSame($request, $context->getRequest()); self::assertFalse($context->isResetMissingFields()); + self::assertSame(['attribute' => 'value'], $context->getAttributes()); + self::assertSame('value', $context->getAttribute('attribute')); } public function testWithResetMissingFieldsExpectDeprecation() @@ -55,4 +60,19 @@ public function testWithResetMissingFieldsExpectDeprecation() self::assertSame(E_USER_DEPRECATED, $error['type']); self::assertSame('resetMissingFields is broken by design, please do this your self by model or repository', $error['message']); } + + public function testWithAttribute() + { + /** @var ServerRequestInterface|MockObject $request */ + $request = $this->getMockByCalls(ServerRequestInterface::class); + + $context = new DenormalizerContext(['allowed_field'], ['group1'], $request, false, ['attribute' => 'value']); + + $newContext = $context->withAttribute('otherAttribute', 'value2'); + + self::assertNotSame($context, $newContext); + + self::assertSame(['attribute' => 'value', 'otherAttribute' => 'value2'], $newContext->getAttributes()); + self::assertSame(['attribute' => 'value'], $context->getAttributes()); + } } diff --git a/tests/Denormalizer/DenormalizerTest.php b/tests/Denormalizer/DenormalizerTest.php index 7f8d65c..87a5331 100644 --- a/tests/Denormalizer/DenormalizerTest.php +++ b/tests/Denormalizer/DenormalizerTest.php @@ -14,6 +14,7 @@ use Chubbyphp\Deserialization\DeserializerRuntimeException; use Chubbyphp\Deserialization\Mapping\DenormalizationFieldMappingInterface; use Chubbyphp\Deserialization\Mapping\DenormalizationObjectMappingInterface; +use Chubbyphp\Deserialization\Policy\PolicyInterface; use Chubbyphp\Mock\Argument\ArgumentInstanceOf; use Chubbyphp\Mock\Call; use Chubbyphp\Mock\MockByCallsTrait; @@ -141,7 +142,7 @@ public function testDenormalizeWithNewAndType() ); } - public function testDenormalizeWithNewAndTypeAndResetMissinFields() + public function testDenormalizeWithNewAndTypeAndResetMissingFields() { $object = new \stdClass(); @@ -561,4 +562,178 @@ public function testDenormalizeWithGroupsButNoGroupOnField() self::assertSame($object, $denormalizer->denormalize(\stdClass::class, ['name' => 'name'], $context)); } + + public function testDenormalizeWithCompliantPolicy() + { + $object = new \stdClass(); + + $factory = function () use ($object) { + return $object; + }; + + /** @var DenormalizerContextInterface|MockObject $context */ + $context = $this->getMockByCalls(DenormalizerContextInterface::class, [ + Call::create('getGroups')->with()->willReturn([]), + Call::create('getAllowedAdditionalFields')->with()->willReturn(null), + ]); + + /** @var FieldDenormalizerInterface|MockObject $nameFieldDenormalizer */ + $nameFieldDenormalizer = $this->getMockByCalls(FieldDenormalizerInterface::class, [ + Call::create('denormalizeField') + ->with('name', $object, 'name', $context, new ArgumentInstanceOf(DenormalizerInterface::class)), + ]); + + /** @var PolicyInterface|MockObject $policy */ + $policy = $this->getMockByCalls(PolicyInterface::class, [ + Call::create('isCompliant')->with($context, $object)->willReturn(true), + ]); + + $denormalizationNameFieldMapping = $this->getDenormalizationFieldMappingWithPolicy( + 'name', + $nameFieldDenormalizer, + $policy + ); + + /** @var DenormalizationFieldMappingInterface|MockObject $denormalizationValueFieldMapping */ + $denormalizationValueFieldMapping = $this->getMockByCalls(DenormalizationFieldMappingInterface::class, [ + Call::create('getName')->with()->willReturn('value'), + ]); + + /** @var DenormalizationObjectMappingInterface|MockObject $objectMapping */ + $objectMapping = $this->getMockByCalls(DenormalizationObjectMappingInterface::class, [ + Call::create('getDenormalizationFactory')->with('', null)->willReturn($factory), + Call::create('getDenormalizationFieldMappings')->with('', null)->willReturn([ + $denormalizationNameFieldMapping, + $denormalizationValueFieldMapping, + ]), + ]); + + /** @var DenormalizerObjectMappingRegistryInterface|MockObject $registry */ + $registry = $this->getMockByCalls(DenormalizerObjectMappingRegistryInterface::class, [ + Call::create('getObjectMapping')->with(\stdClass::class)->willReturn($objectMapping), + ]); + + /** @var LoggerInterface|MockObject $logger */ + $logger = $this->getMockByCalls(LoggerInterface::class, [ + Call::create('info')->with('deserialize: path {path}', ['path' => 'name']), + ]); + + $denormalizer = new Denormalizer($registry, $logger); + + self::assertSame($object, $denormalizer->denormalize(\stdClass::class, ['name' => 'name'], $context)); + } + + public function testDenormalizeWithNotCompliantPolicy() + { + $object = new \stdClass(); + + $factory = function () use ($object) { + return $object; + }; + + /** @var DenormalizerContextInterface|MockObject $context */ + $context = $this->getMockByCalls(DenormalizerContextInterface::class, [ + Call::create('getAllowedAdditionalFields')->with()->willReturn(null), + ]); + + /** @var FieldDenormalizerInterface|MockObject $nameFieldDenormalizer */ + $nameFieldDenormalizer = $this->getMockByCalls(FieldDenormalizerInterface::class); + + /** @var PolicyInterface|MockObject $policy */ + $policy = $this->getMockByCalls(PolicyInterface::class, [ + Call::create('isCompliant')->with($context, $object)->willReturn(false), + ]); + + $denormalizationNameFieldMapping = $this->getDenormalizationFieldMappingWithPolicy( + 'name', + $nameFieldDenormalizer, + $policy + ); + + /** @var DenormalizationFieldMappingInterface|MockObject $denormalizationValueFieldMapping */ + $denormalizationValueFieldMapping = $this->getMockByCalls(DenormalizationFieldMappingInterface::class, [ + Call::create('getName')->with()->willReturn('value'), + ]); + + /** @var DenormalizationObjectMappingInterface|MockObject $objectMapping */ + $objectMapping = $this->getMockByCalls(DenormalizationObjectMappingInterface::class, [ + Call::create('getDenormalizationFactory')->with('', null)->willReturn($factory), + Call::create('getDenormalizationFieldMappings')->with('', null)->willReturn([ + $denormalizationNameFieldMapping, + $denormalizationValueFieldMapping, + ]), + ]); + + /** @var DenormalizerObjectMappingRegistryInterface|MockObject $registry */ + $registry = $this->getMockByCalls(DenormalizerObjectMappingRegistryInterface::class, [ + Call::create('getObjectMapping')->with(\stdClass::class)->willReturn($objectMapping), + ]); + + /** @var LoggerInterface|MockObject $logger */ + $logger = $this->getMockByCalls(LoggerInterface::class); + + $denormalizer = new Denormalizer($registry, $logger); + + self::assertSame($object, $denormalizer->denormalize(\stdClass::class, ['name' => 'name'], $context)); + } + + /** + * @param string $name + * @param FieldDenormalizerInterface $fieldDenormalizer + * @param PolicyInterface $policy + * @param array $groups + * + * @return DenormalizationFieldMappingInterface + * + * @todo remove as soon getPolicy() is part of the mapping interface + */ + private function getDenormalizationFieldMappingWithPolicy( + string $name, + FieldDenormalizerInterface $fieldDenormalizer, + PolicyInterface $policy, + array $groups = [] + ): DenormalizationFieldMappingInterface { + return new class($name, $fieldDenormalizer, $policy, $groups) implements DenormalizationFieldMappingInterface { + private $name; + private $fieldDenormalizer; + private $policy; + private $groups; + + public function __construct($name, $fieldDenormalizer, $policy, $groups) + { + $this->name = $name; + $this->fieldDenormalizer = $fieldDenormalizer; + $this->policy = $policy; + $this->groups = $groups; + } + + public function getName(): string + { + return $this->name; + } + + /** + * @return string[] + * + * @deprecated + */ + public function getGroups(): array + { + return $this->groups; + } + + /** + * @return FieldDenormalizerInterface + */ + public function getFieldDenormalizer(): FieldDenormalizerInterface + { + return $this->fieldDenormalizer; + } + + public function getPolicy(): PolicyInterface + { + return $this->policy; + } + }; + } } diff --git a/tests/Mapping/DenormalizationFieldMappingBuilderTest.php b/tests/Mapping/DenormalizationFieldMappingBuilderTest.php index f7ca182..a3b3a8a 100644 --- a/tests/Mapping/DenormalizationFieldMappingBuilderTest.php +++ b/tests/Mapping/DenormalizationFieldMappingBuilderTest.php @@ -15,6 +15,8 @@ use Chubbyphp\Deserialization\Denormalizer\Relation\ReferenceOneFieldDenormalizer; use Chubbyphp\Deserialization\Mapping\DenormalizationFieldMappingBuilder; use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Deserialization\Policy\NullPolicy; +use Chubbyphp\Deserialization\Policy\PolicyInterface; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -40,6 +42,8 @@ public function testGetDefaultMapping() $reflectionObject->setAccessible(true); self::assertFalse($reflectionObject->getValue($fieldDenormalizer)); + + self::assertInstanceOf(NullPolicy::class, $fieldMapping->getPolicy()); } public function testGetDefaultMappingWithEmptyToNull() @@ -57,6 +61,8 @@ public function testGetDefaultMappingWithEmptyToNull() $reflectionObject->setAccessible(true); self::assertTrue($reflectionObject->getValue($fieldDenormalizer)); + + self::assertInstanceOf(NullPolicy::class, $fieldMapping->getPolicy()); } public function testGetDefaultMappingForCallback() @@ -66,6 +72,8 @@ public function testGetDefaultMappingForCallback() self::assertSame('name', $fieldMapping->getName()); self::assertSame([], $fieldMapping->getGroups()); self::assertInstanceOf(CallbackFieldDenormalizer::class, $fieldMapping->getFieldDenormalizer()); + + self::assertInstanceOf(NullPolicy::class, $fieldMapping->getPolicy()); } public function testGetDefaultMappingForConvertType() @@ -86,6 +94,8 @@ public function testGetDefaultMappingForConvertType() $reflectionObject->setAccessible(true); self::assertFalse($reflectionObject->getValue($fieldDenormalizer)); + + self::assertInstanceOf(NullPolicy::class, $fieldMapping->getPolicy()); } public function testGetDefaultMappingForConvertTypeWithEmptyToNull() @@ -107,6 +117,8 @@ public function testGetDefaultMappingForConvertTypeWithEmptyToNull() $reflectionObject->setAccessible(true); self::assertTrue($reflectionObject->getValue($fieldDenormalizer)); + + self::assertInstanceOf(NullPolicy::class, $fieldMapping->getPolicy()); } public function testGetDefaultMappingForDateTime() @@ -124,6 +136,8 @@ public function testGetDefaultMappingForDateTime() $reflectionObject->setAccessible(true); self::assertFalse($reflectionObject->getValue($fieldDenormalizer)); + + self::assertInstanceOf(NullPolicy::class, $fieldMapping->getPolicy()); } public function testGetDefaultMappingForDateTimeWithEmptyToNull() @@ -141,6 +155,8 @@ public function testGetDefaultMappingForDateTimeWithEmptyToNull() $reflectionObject->setAccessible(true); self::assertTrue($reflectionObject->getValue($fieldDenormalizer)); + + self::assertInstanceOf(NullPolicy::class, $fieldMapping->getPolicy()); } public function testGetDefaultMappingForDateTimeWithTimezone() @@ -150,6 +166,8 @@ public function testGetDefaultMappingForDateTimeWithTimezone() self::assertSame('name', $fieldMapping->getName()); self::assertSame([], $fieldMapping->getGroups()); self::assertInstanceOf(DateTimeFieldDenormalizer::class, $fieldMapping->getFieldDenormalizer()); + + self::assertInstanceOf(NullPolicy::class, $fieldMapping->getPolicy()); } public function testGetDefaultMappingForEmbedMany() @@ -159,6 +177,8 @@ public function testGetDefaultMappingForEmbedMany() self::assertSame('name', $fieldMapping->getName()); self::assertSame([], $fieldMapping->getGroups()); self::assertInstanceOf(EmbedManyFieldDenormalizer::class, $fieldMapping->getFieldDenormalizer()); + + self::assertInstanceOf(NullPolicy::class, $fieldMapping->getPolicy()); } public function testGetDefaultMappingForEmbedOne() @@ -168,6 +188,8 @@ public function testGetDefaultMappingForEmbedOne() self::assertSame('name', $fieldMapping->getName()); self::assertSame([], $fieldMapping->getGroups()); self::assertInstanceOf(EmbedOneFieldDenormalizer::class, $fieldMapping->getFieldDenormalizer()); + + self::assertInstanceOf(NullPolicy::class, $fieldMapping->getPolicy()); } public function testGetDefaultMappingForReferenceMany() @@ -177,6 +199,8 @@ public function testGetDefaultMappingForReferenceMany() self::assertSame('name', $fieldMapping->getName()); self::assertSame([], $fieldMapping->getGroups()); self::assertInstanceOf(ReferenceManyFieldDenormalizer::class, $fieldMapping->getFieldDenormalizer()); + + self::assertInstanceOf(NullPolicy::class, $fieldMapping->getPolicy()); } public function testGetDefaultMappingForReferenceOne() @@ -194,6 +218,8 @@ public function testGetDefaultMappingForReferenceOne() $reflectionObject->setAccessible(true); self::assertFalse($reflectionObject->getValue($fieldDenormalizer)); + + self::assertInstanceOf(NullPolicy::class, $fieldMapping->getPolicy()); } public function testGetDefaultMappingForReferenceOneWithEmptyToNull() @@ -215,6 +241,8 @@ function () {}, $reflectionObject->setAccessible(true); self::assertTrue($reflectionObject->getValue($fieldDenormalizer)); + + self::assertInstanceOf(NullPolicy::class, $fieldMapping->getPolicy()); } public function testGetMapping() @@ -222,13 +250,19 @@ public function testGetMapping() /** @var FieldDenormalizerInterface|MockObject $fieldDenormalizer */ $fieldDenormalizer = $this->getMockByCalls(FieldDenormalizerInterface::class); + /** @var PolicyInterface|MockObject $policy */ + $policy = $this->getMockByCalls(PolicyInterface::class); + $fieldMapping = DenormalizationFieldMappingBuilder::create('name') ->setGroups(['group1']) ->setFieldDenormalizer($fieldDenormalizer) + ->setPolicy($policy) ->getMapping(); self::assertSame('name', $fieldMapping->getName()); self::assertSame(['group1'], $fieldMapping->getGroups()); self::assertSame($fieldDenormalizer, $fieldMapping->getFieldDenormalizer()); + + self::assertSame($policy, $fieldMapping->getPolicy()); } } diff --git a/tests/Mapping/DenormalizationFieldMappingTest.php b/tests/Mapping/DenormalizationFieldMappingTest.php index c4b356c..27d532a 100644 --- a/tests/Mapping/DenormalizationFieldMappingTest.php +++ b/tests/Mapping/DenormalizationFieldMappingTest.php @@ -6,6 +6,7 @@ use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface; use Chubbyphp\Deserialization\Mapping\DenormalizationFieldMapping; +use Chubbyphp\Deserialization\Policy\PolicyInterface; use Chubbyphp\Mock\MockByCallsTrait; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -46,4 +47,17 @@ public function testGetFieldDenormalizer() self::assertSame($fieldDenormalizer, $fieldMapping->getFieldDenormalizer()); } + + public function testGetPolicy() + { + /** @var FieldDenormalizerInterface|MockObject $fieldDenormalizer */ + $fieldDenormalizer = $this->getMockByCalls(FieldDenormalizerInterface::class); + + /** @var PolicyInterface|MockObject $policy */ + $policy = $this->getMockByCalls(PolicyInterface::class); + + $fieldMapping = new DenormalizationFieldMapping('name', ['group1'], $fieldDenormalizer, $policy); + + self::assertSame($policy, $fieldMapping->getPolicy()); + } } diff --git a/tests/Policy/AndPolicyTest.php b/tests/Policy/AndPolicyTest.php new file mode 100644 index 0000000..a7dc331 --- /dev/null +++ b/tests/Policy/AndPolicyTest.php @@ -0,0 +1,68 @@ +getMockByCalls(DenormalizerContextInterface::class, []); + + /** @var PolicyInterface|MockObject $compliantPolicy1 */ + $compliantPolicy1 = $this->getMockByCalls(PolicyInterface::class, [ + Call::create('isCompliant')->with($context, $object)->willReturn(true), + ]); + + /** @var PolicyInterface|MockObject $compliantPolicy2 */ + $compliantPolicy2 = $this->getMockByCalls(PolicyInterface::class, [ + Call::create('isCompliant')->with($context, $object)->willReturn(true), + ]); + + $policy = new AndPolicy([$compliantPolicy1, $compliantPolicy2]); + + self::assertTrue($policy->isCompliant($context, $object)); + } + + public function testIsCompliantReturnsFalseWithNonCompliantPolicy() + { + $object = new \stdClass(); + + /** @var DenormalizerContextInterface|MockObject $context */ + $context = $this->getMockByCalls(DenormalizerContextInterface::class, []); + + /** @var PolicyInterface|MockObject $compliantPolicy1 */ + $compliantPolicy1 = $this->getMockByCalls(PolicyInterface::class, [ + Call::create('isCompliant')->with($context, $object)->willReturn(true), + ]); + + /** @var PolicyInterface|MockObject $nonCompliantPolicy */ + $nonCompliantPolicy = $this->getMockByCalls(PolicyInterface::class, [ + Call::create('isCompliant')->with($context, $object)->willReturn(false), + ]); + + /** @var PolicyInterface|MockObject $notExpectedToBeCalledPolicy */ + $notExpectedToBeCalledPolicy = $this->getMockByCalls(PolicyInterface::class); + + $policy = new AndPolicy([$compliantPolicy1, $nonCompliantPolicy, $notExpectedToBeCalledPolicy]); + + self::assertFalse($policy->isCompliant($context, $object)); + } +} diff --git a/tests/Policy/CallbackPolicyTest.php b/tests/Policy/CallbackPolicyTest.php new file mode 100644 index 0000000..4c32d4b --- /dev/null +++ b/tests/Policy/CallbackPolicyTest.php @@ -0,0 +1,53 @@ +getMockByCalls(DenormalizerContextInterface::class, []); + + $policy = new CallbackPolicy(function ($contextParameter, $objectParameter) use ($context, $object) { + self::assertSame($context, $contextParameter); + self::assertSame($object, $objectParameter); + + return true; + }); + + self::assertTrue($policy->isCompliant($context, $object)); + } + + public function testIsCompliantReturnsFalseIfCallbackReturnsFalse() + { + $object = new \stdClass(); + + /** @var DenormalizerContextInterface|MockObject $context */ + $context = $this->getMockByCalls(DenormalizerContextInterface::class, []); + + $policy = new CallbackPolicy(function ($contextParameter, $objectParameter) use ($context, $object) { + self::assertSame($context, $contextParameter); + self::assertSame($object, $objectParameter); + + return false; + }); + + self::assertFalse($policy->isCompliant($context, $object)); + } +} diff --git a/tests/Policy/GroupPolicyTest.php b/tests/Policy/GroupPolicyTest.php new file mode 100644 index 0000000..dcc53de --- /dev/null +++ b/tests/Policy/GroupPolicyTest.php @@ -0,0 +1,114 @@ +getMockByCalls(DenormalizerContextInterface::class); + + $policy = new GroupPolicy([]); + + self::assertTrue($policy->isCompliant($context, $object)); + } + + public function testIsCompliantReturnsTrueIfOneGroupMatches() + { + $object = new \stdClass(); + + /** @var DenormalizerContextInterface|MockObject $context */ + $context = $this->getDenormalizerContextWithGroupAttribute(['group2']); + + $policy = new GroupPolicy(['group1', 'group2']); + + self::assertTrue($policy->isCompliant($context, $object)); + } + + public function testIsCompliantReturnsFalseIfNoGroupsAreSetInContext() + { + $object = new \stdClass(); + + /** @var DenormalizerContextInterface|MockObject $context */ + $context = $this->getDenormalizerContextWithGroupAttribute([]); + + $policy = new GroupPolicy(['group1', 'group2']); + + self::assertFalse($policy->isCompliant($context, $object)); + } + + public function testIsCompliantReturnsFalseIfNoGroupsMatch() + { + $object = new \stdClass(); + + /** @var DenormalizerContextInterface|MockObject $context */ + $context = $this->getDenormalizerContextWithGroupAttribute(['unknownGroup']); + + $policy = new GroupPolicy(['group1', 'group2']); + + self::assertFalse($policy->isCompliant($context, $object)); + } + + /** + * @param array $groups + * + * @return DenormalizerContextInterface + */ + private function getDenormalizerContextWithGroupAttribute(array $groups): DenormalizerContextInterface + { + return new class($groups) implements DenormalizerContextInterface { + private $groups; + + public function __construct($groups) + { + $this->groups = $groups; + } + + public function getAllowedAdditionalFields() + { + return null; + } + + public function getGroups(): array + { + return []; + } + + public function getRequest() + { + return null; + } + + public function getAttributes(): array + { + return []; + } + + public function getAttribute(string $name, $default = null) + { + return $this->groups; + } + + public function withAttribute(string $name, $value): DenormalizerContextInterface + { + return $this; + } + }; + } +} diff --git a/tests/Policy/NullPolicyTest.php b/tests/Policy/NullPolicyTest.php new file mode 100644 index 0000000..a540a41 --- /dev/null +++ b/tests/Policy/NullPolicyTest.php @@ -0,0 +1,31 @@ +getMockByCalls(DenormalizerContextInterface::class); + + $policy = new NullPolicy(); + + self::assertTrue($policy->isCompliant($context, $object)); + } +} diff --git a/tests/Policy/OrPolicyTest.php b/tests/Policy/OrPolicyTest.php new file mode 100644 index 0000000..92e614c --- /dev/null +++ b/tests/Policy/OrPolicyTest.php @@ -0,0 +1,68 @@ +getMockByCalls(DenormalizerContextInterface::class, []); + + /** @var PolicyInterface|MockObject $nonCompliantPolicy */ + $nonCompliantPolicy = $this->getMockByCalls(PolicyInterface::class, [ + Call::create('isCompliant')->with($context, $object)->willReturn(false), + ]); + + /** @var PolicyInterface|MockObject $compliantPolicy */ + $compliantPolicy = $this->getMockByCalls(PolicyInterface::class, [ + Call::create('isCompliant')->with($context, $object)->willReturn(true), + ]); + + /** @var PolicyInterface|MockObject $notToBeCalledPolicy */ + $notToBeCalledPolicy = $this->getMockByCalls(PolicyInterface::class, []); + + $policy = new OrPolicy([$nonCompliantPolicy, $compliantPolicy, $notToBeCalledPolicy]); + + self::assertTrue($policy->isCompliant($context, $object)); + } + + public function testIsCompliantReturnsFalseIfAllPoliciesReturnFalse() + { + $object = new \stdClass(); + + /** @var DenormalizerContextInterface|MockObject $context */ + $context = $this->getMockByCalls(DenormalizerContextInterface::class, []); + + /** @var PolicyInterface|MockObject $nonCompliantPolicy1 */ + $nonCompliantPolicy1 = $this->getMockByCalls(PolicyInterface::class, [ + Call::create('isCompliant')->with($context, $object)->willReturn(false), + ]); + + /** @var PolicyInterface|MockObject $nonCompliantPolicy2 */ + $nonCompliantPolicy2 = $this->getMockByCalls(PolicyInterface::class, [ + Call::create('isCompliant')->with($context, $object)->willReturn(false), + ]); + + $policy = new OrPolicy([$nonCompliantPolicy1, $nonCompliantPolicy2]); + + self::assertFalse($policy->isCompliant($context, $object)); + } +} From 1b3373b3ed6d1d2e3eebccb5c5ed3e1bb648c877 Mon Sep 17 00:00:00 2001 From: Dominik Zogg Date: Wed, 10 Jul 2019 20:50:45 +0200 Subject: [PATCH 2/3] add the seiralizer changes --- src/Denormalizer/Denormalizer.php | 10 ++++++++ .../DenormalizationFieldMappingBuilder.php | 23 ++++++++++++++++--- ...malizationFieldMappingBuilderInterface.php | 8 +++++++ src/Policy/AndPolicy.php | 2 +- src/Policy/GroupPolicy.php | 9 ++++++-- src/Policy/OrPolicy.php | 2 +- ...DenormalizationFieldMappingBuilderTest.php | 22 ++++++++++++++++++ tests/Policy/GroupPolicyTest.php | 18 ++++++++++++--- 8 files changed, 84 insertions(+), 10 deletions(-) diff --git a/src/Denormalizer/Denormalizer.php b/src/Denormalizer/Denormalizer.php index ea15b61..ab1555e 100644 --- a/src/Denormalizer/Denormalizer.php +++ b/src/Denormalizer/Denormalizer.php @@ -9,6 +9,7 @@ use Chubbyphp\Deserialization\DeserializerRuntimeException; use Chubbyphp\Deserialization\Mapping\DenormalizationFieldMappingInterface; use Chubbyphp\Deserialization\Mapping\DenormalizationObjectMappingInterface; +use Chubbyphp\Deserialization\Policy\GroupPolicy; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; @@ -221,6 +222,15 @@ private function isWithinGroup( return true; } + @trigger_error( + sprintf( + 'Use "%s" instead of "%s::setGroups"', + GroupPolicy::class, + DenormalizerContextInterface::class + ), + E_USER_DEPRECATED + ); + foreach ($fieldMapping->getGroups() as $group) { if (in_array($group, $groups, true)) { return true; diff --git a/src/Mapping/DenormalizationFieldMappingBuilder.php b/src/Mapping/DenormalizationFieldMappingBuilder.php index 8e691b4..ed954ae 100644 --- a/src/Mapping/DenormalizationFieldMappingBuilder.php +++ b/src/Mapping/DenormalizationFieldMappingBuilder.php @@ -41,6 +41,9 @@ final class DenormalizationFieldMappingBuilder implements DenormalizationFieldMa */ private $policy; + /** + * @param string $name + */ private function __construct(string $name) { $this->name = $name; @@ -48,13 +51,22 @@ private function __construct(string $name) /** * @param string $name + * @param bool $emptyToNull + * @param FieldDenormalizerInterface|null * * @return DenormalizationFieldMappingBuilderInterface */ - public static function create(string $name, bool $emptyToNull = false): DenormalizationFieldMappingBuilderInterface - { + public static function create( + string $name, + bool $emptyToNull = false, + FieldDenormalizerInterface $fieldDenormalizer = null + ): DenormalizationFieldMappingBuilderInterface { + if (null === $fieldDenormalizer) { + $fieldDenormalizer = new FieldDenormalizer(new PropertyAccessor($name), $emptyToNull); + } + $self = new self($name); - $self->fieldDenormalizer = new FieldDenormalizer(new PropertyAccessor($name), $emptyToNull); + $self->fieldDenormalizer = $fieldDenormalizer; return $self; } @@ -197,6 +209,11 @@ public function setGroups(array $groups): DenormalizationFieldMappingBuilderInte public function setFieldDenormalizer( FieldDenormalizerInterface $fieldDenormalizer ): DenormalizationFieldMappingBuilderInterface { + @trigger_error( + 'Utilize third parameter of create method instead', + E_USER_DEPRECATED + ); + $this->fieldDenormalizer = $fieldDenormalizer; return $this; diff --git a/src/Mapping/DenormalizationFieldMappingBuilderInterface.php b/src/Mapping/DenormalizationFieldMappingBuilderInterface.php index 4aa2b8f..31a331d 100644 --- a/src/Mapping/DenormalizationFieldMappingBuilderInterface.php +++ b/src/Mapping/DenormalizationFieldMappingBuilderInterface.php @@ -19,6 +19,14 @@ interface DenormalizationFieldMappingBuilderInterface */ public static function create(string $name): self; + /** + * @param string $name + * @param FieldNormalizerInterface|null $fieldNormalizer + * + * @return NormalizationFieldMappingBuilderInterface + */ + //public static function create(string $name, FieldNormalizerInterface $fieldNormalizer = null): self; + /** * @deprecated * diff --git a/src/Policy/AndPolicy.php b/src/Policy/AndPolicy.php index d39fdf7..9ef9f80 100644 --- a/src/Policy/AndPolicy.php +++ b/src/Policy/AndPolicy.php @@ -30,7 +30,7 @@ public function __construct(array $policies) public function isCompliant(DenormalizerContextInterface $context, $object): bool { foreach ($this->policies as $policy) { - if (false === $policy->isCompliant($context, $object)) { + if (!$policy->isCompliant($context, $object)) { return false; } } diff --git a/src/Policy/GroupPolicy.php b/src/Policy/GroupPolicy.php index ae2519c..9d2d3d7 100644 --- a/src/Policy/GroupPolicy.php +++ b/src/Policy/GroupPolicy.php @@ -13,6 +13,11 @@ final class GroupPolicy implements PolicyInterface */ const ATTRIBUTE_GROUPS = 'groups'; + /** + * @var string + */ + const GROUP_DEFAULT = 'default'; + /** * @var string[] */ @@ -21,7 +26,7 @@ final class GroupPolicy implements PolicyInterface /** * @param array $groups */ - public function __construct(array $groups) + public function __construct(array $groups = [self::GROUP_DEFAULT]) { $this->groups = $groups; } @@ -38,7 +43,7 @@ public function isCompliant(DenormalizerContextInterface $context, $object): boo return true; } - $contextGroups = $context->getAttribute(self::ATTRIBUTE_GROUPS, []); + $contextGroups = $context->getAttribute(self::ATTRIBUTE_GROUPS, [self::GROUP_DEFAULT]); foreach ($this->groups as $group) { if (in_array($group, $contextGroups, true)) { diff --git a/src/Policy/OrPolicy.php b/src/Policy/OrPolicy.php index 79713d2..37126ce 100644 --- a/src/Policy/OrPolicy.php +++ b/src/Policy/OrPolicy.php @@ -30,7 +30,7 @@ public function __construct(array $policies) public function isCompliant(DenormalizerContextInterface $context, $object): bool { foreach ($this->policies as $policy) { - if (true === $policy->isCompliant($context, $object)) { + if ($policy->isCompliant($context, $object)) { return true; } } diff --git a/tests/Mapping/DenormalizationFieldMappingBuilderTest.php b/tests/Mapping/DenormalizationFieldMappingBuilderTest.php index a3b3a8a..a3e2a51 100644 --- a/tests/Mapping/DenormalizationFieldMappingBuilderTest.php +++ b/tests/Mapping/DenormalizationFieldMappingBuilderTest.php @@ -27,6 +27,21 @@ class DenormalizationFieldMappingBuilderTest extends TestCase { use MockByCallsTrait; + public function testGetMappingWithDenormalizer() + { + /** @var FieldDenormalizerInterface|MockObject $fieldDenormalizer */ + $fieldDenormalizer = $this->getMockByCalls(FieldDenormalizerInterface::class); + + $fieldMapping = DenormalizationFieldMappingBuilder::create('name', false, $fieldDenormalizer)->getMapping(); + + self::assertSame('name', $fieldMapping->getName()); + self::assertSame([], $fieldMapping->getGroups()); + + self::assertSame($fieldDenormalizer, $fieldMapping->getFieldDenormalizer()); + + self::assertInstanceOf(NullPolicy::class, $fieldMapping->getPolicy()); + } + public function testGetDefaultMapping() { $fieldMapping = DenormalizationFieldMappingBuilder::create('name')->getMapping(); @@ -253,12 +268,19 @@ public function testGetMapping() /** @var PolicyInterface|MockObject $policy */ $policy = $this->getMockByCalls(PolicyInterface::class); + error_clear_last(); + $fieldMapping = DenormalizationFieldMappingBuilder::create('name') ->setGroups(['group1']) ->setFieldDenormalizer($fieldDenormalizer) ->setPolicy($policy) ->getMapping(); + $error = error_get_last(); + + self::assertSame(E_USER_DEPRECATED, $error['type']); + self::assertSame('Utilize third parameter of create method instead', $error['message']); + self::assertSame('name', $fieldMapping->getName()); self::assertSame(['group1'], $fieldMapping->getGroups()); self::assertSame($fieldDenormalizer, $fieldMapping->getFieldDenormalizer()); diff --git a/tests/Policy/GroupPolicyTest.php b/tests/Policy/GroupPolicyTest.php index dcc53de..d130172 100644 --- a/tests/Policy/GroupPolicyTest.php +++ b/tests/Policy/GroupPolicyTest.php @@ -29,6 +29,18 @@ public function testIsCompliantReturnsTrueIfNoGroupsAreSet() self::assertTrue($policy->isCompliant($context, $object)); } + public function testIsCompliantReturnsTrueWithDefaultValues() + { + $object = new \stdClass(); + + /** @var DenormalizerContextInterface|MockObject $context */ + $context = $this->getDenormalizerContextWithGroupAttribute(null); + + $policy = new GroupPolicy(); + + self::assertTrue($policy->isCompliant($context, $object)); + } + public function testIsCompliantReturnsTrueIfOneGroupMatches() { $object = new \stdClass(); @@ -66,11 +78,11 @@ public function testIsCompliantReturnsFalseIfNoGroupsMatch() } /** - * @param array $groups + * @param array|null $groups * * @return DenormalizerContextInterface */ - private function getDenormalizerContextWithGroupAttribute(array $groups): DenormalizerContextInterface + private function getDenormalizerContextWithGroupAttribute(array $groups = null): DenormalizerContextInterface { return new class($groups) implements DenormalizerContextInterface { private $groups; @@ -102,7 +114,7 @@ public function getAttributes(): array public function getAttribute(string $name, $default = null) { - return $this->groups; + return $this->groups ?? $default; } public function withAttribute(string $name, $value): DenormalizerContextInterface From 41abf730eb41873eea8a38b9a1cfacd4ac2dfc56 Mon Sep 17 00:00:00 2001 From: Dominik Zogg Date: Wed, 17 Jul 2019 22:24:15 +0200 Subject: [PATCH 3/3] cs fix --- .php_cs | 35 +++++++++++++++++++ src/Accessor/AccessorInterface.php | 4 +-- src/Accessor/MethodAccessor.php | 12 +++---- src/Accessor/PropertyAccessor.php | 4 +-- src/Decoder/Decoder.php | 20 +++++------ src/Decoder/DecoderInterface.php | 4 +-- src/Decoder/JsonTypeDecoder.php | 4 +-- src/Decoder/JsonxTypeDecoder.php | 4 +-- src/Decoder/TypeDecoderInterface.php | 4 +-- src/Decoder/UrlEncodedTypeDecoder.php | 4 +-- src/Decoder/XmlTypeDecoder.php | 4 +-- src/Decoder/YamlTypeDecoder.php | 4 +-- .../ConvertTypeFieldDenormalizer.php | 19 +++++----- src/Denormalizer/Denormalizer.php | 8 ++--- src/Denormalizer/DenormalizerInterface.php | 4 +-- .../DenormalizerObjectMappingRegistry.php | 20 +++++------ ...rmalizerObjectMappingRegistryInterface.php | 4 +-- .../DeserializationCompilerPass.php | 24 ++++++++----- src/Deserializer.php | 8 ++--- .../DenormalizationObjectMappingInterface.php | 8 ++--- tests/Accessor/MethodAccessorTest.php | 2 ++ tests/Accessor/PropertyAccessorTest.php | 2 ++ tests/Decoder/DecoderTest.php | 2 ++ tests/Decoder/JsonTypeDecoderTest.php | 6 ++-- tests/Decoder/JsonxTypeDecoderTest.php | 6 ++-- tests/Decoder/UrlEncodedTypeDecoderTest.php | 2 ++ tests/Decoder/XmlTypeDecoderTest.php | 6 ++-- tests/Decoder/YamlTypeDecoderTest.php | 6 ++-- .../CallbackFieldDenormalizerTest.php | 2 ++ .../ConvertTypeFieldDenormalizerTest.php | 2 ++ .../DateFieldDenormalizerTest.php | 2 ++ .../DateTimeFieldDenormalizerTest.php | 2 ++ .../DenormalizerContextBuilderTest.php | 8 +++-- .../Denormalizer/DenormalizerContextTest.php | 2 ++ .../DenormalizerObjectMappingRegistryTest.php | 2 ++ tests/Denormalizer/DenormalizerTest.php | 2 ++ tests/Denormalizer/FieldDenormalizerTest.php | 2 ++ .../EmbedManyFieldDenormalizerTest.php | 2 ++ .../EmbedOneFieldDenormalizerTest.php | 2 ++ .../ReferenceManyFieldDenormalizerTest.php | 2 ++ .../ReferenceOneFieldDenormalizerTest.php | 2 ++ .../DeserializationCompilerPassTest.php | 5 ++- tests/DeserializerIntegrationTest.php | 2 ++ tests/DeserializerLogicExceptionTest.php | 2 ++ tests/DeserializerRuntimeExceptionTest.php | 2 ++ tests/DeserializerTest.php | 2 ++ ...llableDenormalizationObjectMappingTest.php | 2 ++ ...DenormalizationFieldMappingBuilderTest.php | 7 ++-- .../DenormalizationFieldMappingTest.php | 2 ++ .../LazyDenormalizationObjectMappingTest.php | 2 ++ tests/Policy/AndPolicyTest.php | 6 ++-- tests/Policy/CallbackPolicyTest.php | 4 ++- tests/Policy/GroupPolicyTest.php | 4 ++- tests/Policy/NullPolicyTest.php | 4 ++- tests/Policy/OrPolicyTest.php | 6 ++-- .../Provider/DeserializationProviderTest.php | 4 +++ .../Mapping/BaseManyModelMapping.php | 8 ++--- tests/Resources/Mapping/ManyModelMapping.php | 8 ++--- tests/Resources/Mapping/ModelMapping.php | 8 ++--- tests/Resources/Mapping/OneModelMapping.php | 8 ++--- 60 files changed, 234 insertions(+), 114 deletions(-) create mode 100644 .php_cs diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..9325b7e --- /dev/null +++ b/.php_cs @@ -0,0 +1,35 @@ +files() + ->name('*.php') + ->in(__DIR__ . '/src') + ->in(__DIR__ . '/tests') +; + +return PhpCsFixer\Config::create() + ->setIndent(" ") + ->setLineEnding("\n") + ->setRules([ + '@DoctrineAnnotation' => true, + '@PhpCsFixer' => true, + '@Symfony' => true, + 'array_syntax' => ['syntax' => 'short'], + 'declare_strict_types' => true, + 'dir_constant' => true, + 'is_null' => true, + 'linebreak_after_opening_tag' => true, + 'list_syntax' => true, + 'method_chaining_indentation' => false, + 'no_php4_constructor' => true, + 'ordered_interfaces' => true, + 'php_unit_dedicate_assert' => true, + 'php_unit_expectation' => true, + 'php_unit_mock' => true, + 'php_unit_namespaced' => true, + 'php_unit_no_expectation_annotation' => true, + 'ternary_to_null_coalescing' => true, + ]) + ->setRiskyAllowed(true) + ->setFinder($finder) +; diff --git a/src/Accessor/AccessorInterface.php b/src/Accessor/AccessorInterface.php index 65482b2..cb3a9bd 100644 --- a/src/Accessor/AccessorInterface.php +++ b/src/Accessor/AccessorInterface.php @@ -19,9 +19,9 @@ public function setValue($object, $value); /** * @param object $object * - * @return mixed - * * @throws DeserializerLogicException + * + * @return mixed */ public function getValue($object); } diff --git a/src/Accessor/MethodAccessor.php b/src/Accessor/MethodAccessor.php index 7eb7da0..fcffd26 100644 --- a/src/Accessor/MethodAccessor.php +++ b/src/Accessor/MethodAccessor.php @@ -34,15 +34,15 @@ public function setValue($object, $value) throw DeserializerLogicException::createMissingMethod(get_class($object), [$set]); } - return $object->$set($value); + return $object->{$set}($value); } /** * @param object $object * - * @return mixed - * * @throws DeserializerLogicException + * + * @return mixed */ public function getValue($object) { @@ -51,15 +51,15 @@ public function getValue($object) $is = 'is'.ucfirst($this->property); if (method_exists($object, $get)) { - return $object->$get(); + return $object->{$get}(); } if (method_exists($object, $has)) { - return $object->$has(); + return $object->{$has}(); } if (method_exists($object, $is)) { - return $object->$is(); + return $object->{$is}(); } throw DeserializerLogicException::createMissingMethod(get_class($object), [$get, $has, $is]); diff --git a/src/Accessor/PropertyAccessor.php b/src/Accessor/PropertyAccessor.php index c5a0e34..e3b308b 100644 --- a/src/Accessor/PropertyAccessor.php +++ b/src/Accessor/PropertyAccessor.php @@ -36,7 +36,7 @@ public function setValue($object, $value) $setter = \Closure::bind( function ($property, $value) { - $this->$property = $value; + $this->{$property} = $value; }, $object, $class @@ -60,7 +60,7 @@ public function getValue($object) $getter = \Closure::bind( function ($property) { - return $this->$property; + return $this->{$property}; }, $object, $class diff --git a/src/Decoder/Decoder.php b/src/Decoder/Decoder.php index aa7aae4..210e3c8 100644 --- a/src/Decoder/Decoder.php +++ b/src/Decoder/Decoder.php @@ -25,14 +25,6 @@ public function __construct(array $decoderTypes) } } - /** - * @param TypeDecoderInterface $decoderType - */ - private function addTypeDecoder(TypeDecoderInterface $decoderType) - { - $this->decoderTypes[$decoderType->getContentType()] = $decoderType; - } - /** * @return array */ @@ -45,10 +37,10 @@ public function getContentTypes(): array * @param string $data * @param string $contentType * - * @return array - * * @throws DeserializerLogicException * @throws DeserializerRuntimeException + * + * @return array */ public function decode(string $data, string $contentType): array { @@ -58,4 +50,12 @@ public function decode(string $data, string $contentType): array throw DeserializerLogicException::createMissingContentType($contentType); } + + /** + * @param TypeDecoderInterface $decoderType + */ + private function addTypeDecoder(TypeDecoderInterface $decoderType) + { + $this->decoderTypes[$decoderType->getContentType()] = $decoderType; + } } diff --git a/src/Decoder/DecoderInterface.php b/src/Decoder/DecoderInterface.php index 6f35111..b1da85f 100644 --- a/src/Decoder/DecoderInterface.php +++ b/src/Decoder/DecoderInterface.php @@ -18,10 +18,10 @@ public function getContentTypes(): array; * @param string $data * @param string $contentType * - * @return array - * * @throws DeserializerLogicException * @throws DeserializerRuntimeException + * + * @return array */ public function decode(string $data, string $contentType): array; } diff --git a/src/Decoder/JsonTypeDecoder.php b/src/Decoder/JsonTypeDecoder.php index d97995d..96a6f1c 100644 --- a/src/Decoder/JsonTypeDecoder.php +++ b/src/Decoder/JsonTypeDecoder.php @@ -19,9 +19,9 @@ public function getContentType(): string /** * @param string $data * - * @return array - * * @throws DeserializerRuntimeException + * + * @return array */ public function decode(string $data): array { diff --git a/src/Decoder/JsonxTypeDecoder.php b/src/Decoder/JsonxTypeDecoder.php index c1bf3eb..5d15939 100644 --- a/src/Decoder/JsonxTypeDecoder.php +++ b/src/Decoder/JsonxTypeDecoder.php @@ -29,9 +29,9 @@ public function getContentType(): string /** * @param string $data * - * @return array - * * @throws DeserializerRuntimeException + * + * @return array */ public function decode(string $data): array { diff --git a/src/Decoder/TypeDecoderInterface.php b/src/Decoder/TypeDecoderInterface.php index 82c4ce7..ee7ca5d 100644 --- a/src/Decoder/TypeDecoderInterface.php +++ b/src/Decoder/TypeDecoderInterface.php @@ -16,9 +16,9 @@ public function getContentType(): string; /** * @param string $data * - * @return array - * * @throws DeserializerRuntimeException + * + * @return array */ public function decode(string $data): array; } diff --git a/src/Decoder/UrlEncodedTypeDecoder.php b/src/Decoder/UrlEncodedTypeDecoder.php index cdc1d1e..22e112c 100644 --- a/src/Decoder/UrlEncodedTypeDecoder.php +++ b/src/Decoder/UrlEncodedTypeDecoder.php @@ -19,9 +19,9 @@ public function getContentType(): string /** * @param string $data * - * @return array - * * @throws DeserializerRuntimeException + * + * @return array */ public function decode(string $data): array { diff --git a/src/Decoder/XmlTypeDecoder.php b/src/Decoder/XmlTypeDecoder.php index a72f73d..a5e8dd3 100644 --- a/src/Decoder/XmlTypeDecoder.php +++ b/src/Decoder/XmlTypeDecoder.php @@ -19,9 +19,9 @@ public function getContentType(): string /** * @param string $data * - * @return array - * * @throws DeserializerRuntimeException + * + * @return array */ public function decode(string $data): array { diff --git a/src/Decoder/YamlTypeDecoder.php b/src/Decoder/YamlTypeDecoder.php index 19d83e0..a972f1e 100644 --- a/src/Decoder/YamlTypeDecoder.php +++ b/src/Decoder/YamlTypeDecoder.php @@ -21,9 +21,9 @@ public function getContentType(): string /** * @param string $data * - * @return array - * * @throws DeserializerRuntimeException + * + * @return array */ public function decode(string $data): array { diff --git a/src/Denormalizer/ConvertTypeFieldDenormalizer.php b/src/Denormalizer/ConvertTypeFieldDenormalizer.php index eb49a52..7dbb398 100644 --- a/src/Denormalizer/ConvertTypeFieldDenormalizer.php +++ b/src/Denormalizer/ConvertTypeFieldDenormalizer.php @@ -10,16 +10,6 @@ final class ConvertTypeFieldDenormalizer implements FieldDenormalizerInterface { - /** - * @var AccessorInterface - */ - private $accessor; - - /** - * @var string - */ - private $type; - const TYPE_BOOL = 'boolean'; const TYPE_FLOAT = 'float'; const TYPE_INT = 'int'; @@ -31,6 +21,15 @@ final class ConvertTypeFieldDenormalizer implements FieldDenormalizerInterface self::TYPE_INT, self::TYPE_STRING, ]; + /** + * @var AccessorInterface + */ + private $accessor; + + /** + * @var string + */ + private $type; /** * @var bool diff --git a/src/Denormalizer/Denormalizer.php b/src/Denormalizer/Denormalizer.php index ab1555e..e5c80cd 100644 --- a/src/Denormalizer/Denormalizer.php +++ b/src/Denormalizer/Denormalizer.php @@ -43,10 +43,10 @@ public function __construct( * @param DenormalizerContextInterface|null $context * @param string $path * - * @return object - * * @throws DeserializerLogicException * @throws DeserializerRuntimeException + * + * @return object */ public function denormalize($object, array $data, DenormalizerContextInterface $context = null, string $path = '') { @@ -102,9 +102,9 @@ public function denormalize($object, array $data, DenormalizerContextInterface $ /** * @param string $class * - * @return DenormalizationObjectMappingInterface - * * @throws DeserializerLogicException + * + * @return DenormalizationObjectMappingInterface */ private function getObjectMapping(string $class): DenormalizationObjectMappingInterface { diff --git a/src/Denormalizer/DenormalizerInterface.php b/src/Denormalizer/DenormalizerInterface.php index 3bbb558..459599f 100644 --- a/src/Denormalizer/DenormalizerInterface.php +++ b/src/Denormalizer/DenormalizerInterface.php @@ -15,10 +15,10 @@ interface DenormalizerInterface * @param DenormalizerContextInterface|null $context * @param string $path * - * @return object - * * @throws DeserializerLogicException * @throws DeserializerRuntimeException + * + * @return object */ public function denormalize($object, array $data, DenormalizerContextInterface $context = null, string $path = ''); } diff --git a/src/Denormalizer/DenormalizerObjectMappingRegistry.php b/src/Denormalizer/DenormalizerObjectMappingRegistry.php index 39cf30d..734a8d6 100644 --- a/src/Denormalizer/DenormalizerObjectMappingRegistry.php +++ b/src/Denormalizer/DenormalizerObjectMappingRegistry.php @@ -25,20 +25,12 @@ public function __construct(array $objectMappings) } } - /** - * @param DenormalizationObjectMappingInterface $objectMapping - */ - private function addObjectMapping(DenormalizationObjectMappingInterface $objectMapping) - { - $this->objectMappings[$objectMapping->getClass()] = $objectMapping; - } - /** * @param string $class * - * @return DenormalizationObjectMappingInterface - * * @throws DeserializerLogicException + * + * @return DenormalizationObjectMappingInterface */ public function getObjectMapping(string $class): DenormalizationObjectMappingInterface { @@ -57,4 +49,12 @@ public function getObjectMapping(string $class): DenormalizationObjectMappingInt throw DeserializerLogicException::createMissingMapping($class); } + + /** + * @param DenormalizationObjectMappingInterface $objectMapping + */ + private function addObjectMapping(DenormalizationObjectMappingInterface $objectMapping) + { + $this->objectMappings[$objectMapping->getClass()] = $objectMapping; + } } diff --git a/src/Denormalizer/DenormalizerObjectMappingRegistryInterface.php b/src/Denormalizer/DenormalizerObjectMappingRegistryInterface.php index 6c053ca..e2415c4 100644 --- a/src/Denormalizer/DenormalizerObjectMappingRegistryInterface.php +++ b/src/Denormalizer/DenormalizerObjectMappingRegistryInterface.php @@ -12,9 +12,9 @@ interface DenormalizerObjectMappingRegistryInterface /** * @param string $class * - * @return DenormalizationObjectMappingInterface - * * @throws DeserializerLogicException + * + * @return DenormalizationObjectMappingInterface */ public function getObjectMapping(string $class): DenormalizationObjectMappingInterface; } diff --git a/src/DependencyInjection/DeserializationCompilerPass.php b/src/DependencyInjection/DeserializationCompilerPass.php index 457e6bb..54a464a 100644 --- a/src/DependencyInjection/DeserializationCompilerPass.php +++ b/src/DependencyInjection/DeserializationCompilerPass.php @@ -33,24 +33,29 @@ public function process(ContainerBuilder $container) $container ->register('chubbyphp.deserializer.decoder.type.json', JsonTypeDecoder::class) - ->addTag('chubbyphp.deserializer.decoder.type'); + ->addTag('chubbyphp.deserializer.decoder.type') + ; $container ->register('chubbyphp.deserializer.decoder.type.jsonx', JsonxTypeDecoder::class) - ->addTag('chubbyphp.deserializer.decoder.type'); + ->addTag('chubbyphp.deserializer.decoder.type') + ; $container ->register('chubbyphp.deserializer.decoder.type.urlencoded', UrlEncodedTypeDecoder::class) - ->addTag('chubbyphp.deserializer.decoder.type'); + ->addTag('chubbyphp.deserializer.decoder.type') + ; $container ->register('chubbyphp.deserializer.decoder.type.xml', XmlTypeDecoder::class) - ->addTag('chubbyphp.deserializer.decoder.type'); + ->addTag('chubbyphp.deserializer.decoder.type') + ; if (class_exists(Yaml::class)) { $container ->register('chubbyphp.deserializer.decoder.type.yml', YamlTypeDecoder::class) - ->addTag('chubbyphp.deserializer.decoder.type'); + ->addTag('chubbyphp.deserializer.decoder.type') + ; } $decoderTypeReferences = []; @@ -61,7 +66,8 @@ public function process(ContainerBuilder $container) $container ->register('chubbyphp.deserializer.decoder', Decoder::class) ->setPublic(true) - ->setArguments([$decoderTypeReferences]); + ->setArguments([$decoderTypeReferences]) + ; $container ->register('chubbyphp.deserializer.denormalizer', Denormalizer::class) @@ -69,7 +75,8 @@ public function process(ContainerBuilder $container) ->setArguments([ new Reference('chubbyphp.deserializer.denormalizer.objectmappingregistry'), new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE), - ]); + ]) + ; $denormalizerObjectMappingReferences = []; foreach ($container->findTaggedServiceIds('chubbyphp.deserializer.denormalizer.objectmapping') as $id => $tags) { @@ -82,6 +89,7 @@ public function process(ContainerBuilder $container) DenormalizerObjectMappingRegistry::class ) ->setPublic(true) - ->setArguments([$denormalizerObjectMappingReferences]); + ->setArguments([$denormalizerObjectMappingReferences]) + ; } } diff --git a/src/Deserializer.php b/src/Deserializer.php index a5f3b66..1eb54a7 100644 --- a/src/Deserializer.php +++ b/src/Deserializer.php @@ -61,10 +61,10 @@ public function getContentTypes(): array * @param string $data * @param string $contentType * - * @return array - * * @throws DeserializerLogicException * @throws DeserializerRuntimeException + * + * @return array */ public function decode(string $data, string $contentType): array { @@ -77,10 +77,10 @@ public function decode(string $data, string $contentType): array * @param DenormalizerContextInterface|null $context * @param string $path * - * @return object - * * @throws DeserializerLogicException * @throws DeserializerRuntimeException + * + * @return object */ public function denormalize($object, array $data, DenormalizerContextInterface $context = null, string $path = '') { diff --git a/src/Mapping/DenormalizationObjectMappingInterface.php b/src/Mapping/DenormalizationObjectMappingInterface.php index a960aa9..b1e3f52 100644 --- a/src/Mapping/DenormalizationObjectMappingInterface.php +++ b/src/Mapping/DenormalizationObjectMappingInterface.php @@ -17,9 +17,9 @@ public function getClass(): string; * @param string $path * @param string|null $type * - * @return callable - * * @throws DeserializerRuntimeException + * + * @return callable */ public function getDenormalizationFactory(string $path, string $type = null): callable; @@ -27,9 +27,9 @@ public function getDenormalizationFactory(string $path, string $type = null): ca * @param string $path * @param string|null $type * - * @return DenormalizationFieldMappingInterface[] - * * @throws DeserializerRuntimeException + * + * @return DenormalizationFieldMappingInterface[] */ public function getDenormalizationFieldMappings(string $path, string $type = null): array; } diff --git a/tests/Accessor/MethodAccessorTest.php b/tests/Accessor/MethodAccessorTest.php index 8414914..0083f0d 100644 --- a/tests/Accessor/MethodAccessorTest.php +++ b/tests/Accessor/MethodAccessorTest.php @@ -10,6 +10,8 @@ /** * @covers \Chubbyphp\Deserialization\Accessor\MethodAccessor + * + * @internal */ class MethodAccessorTest extends TestCase { diff --git a/tests/Accessor/PropertyAccessorTest.php b/tests/Accessor/PropertyAccessorTest.php index ac911a8..f55f5d1 100644 --- a/tests/Accessor/PropertyAccessorTest.php +++ b/tests/Accessor/PropertyAccessorTest.php @@ -12,6 +12,8 @@ /** * @covers \Chubbyphp\Deserialization\Accessor\PropertyAccessor + * + * @internal */ class PropertyAccessorTest extends TestCase { diff --git a/tests/Decoder/DecoderTest.php b/tests/Decoder/DecoderTest.php index 2e9aa56..eb671c3 100644 --- a/tests/Decoder/DecoderTest.php +++ b/tests/Decoder/DecoderTest.php @@ -14,6 +14,8 @@ /** * @covers \Chubbyphp\Deserialization\Decoder\Decoder + * + * @internal */ class DecoderTest extends TestCase { diff --git a/tests/Decoder/JsonTypeDecoderTest.php b/tests/Decoder/JsonTypeDecoderTest.php index 959a636..e8d41e2 100644 --- a/tests/Decoder/JsonTypeDecoderTest.php +++ b/tests/Decoder/JsonTypeDecoderTest.php @@ -9,6 +9,8 @@ /** * @covers \Chubbyphp\Deserialization\Decoder\JsonTypeDecoder + * + * @internal */ class JsonTypeDecoderTest extends AbstractTypeDecoderTest { @@ -26,7 +28,7 @@ public function testGetContentType() */ public function testDecode(array $expectedData) { - $json = << 1 @@ -166,7 +168,7 @@ public function testDecode(array $expectedData) public function testTypes() { - $jsonx = << id1 diff --git a/tests/Decoder/UrlEncodedTypeDecoderTest.php b/tests/Decoder/UrlEncodedTypeDecoderTest.php index 1469ba8..88359b4 100644 --- a/tests/Decoder/UrlEncodedTypeDecoderTest.php +++ b/tests/Decoder/UrlEncodedTypeDecoderTest.php @@ -9,6 +9,8 @@ /** * @covers \Chubbyphp\Deserialization\Decoder\UrlEncodedTypeDecoder + * + * @internal */ class UrlEncodedTypeDecoderTest extends AbstractTypeDecoderTest { diff --git a/tests/Decoder/XmlTypeDecoderTest.php b/tests/Decoder/XmlTypeDecoderTest.php index e5f0aa0..6dfc5e8 100644 --- a/tests/Decoder/XmlTypeDecoderTest.php +++ b/tests/Decoder/XmlTypeDecoderTest.php @@ -9,6 +9,8 @@ /** * @covers \Chubbyphp\Deserialization\Decoder\XmlTypeDecoder + * + * @internal */ class XmlTypeDecoderTest extends AbstractTypeDecoderTest { @@ -26,7 +28,7 @@ public function testGetContentType() */ public function testDecode(array $expectedData) { - $xml = << 1 @@ -163,7 +165,7 @@ public function testDecode(array $expectedData) public function testTypes() { - $xml = << id1 A fancy Name diff --git a/tests/Decoder/YamlTypeDecoderTest.php b/tests/Decoder/YamlTypeDecoderTest.php index b8c3bd2..0b5ad81 100644 --- a/tests/Decoder/YamlTypeDecoderTest.php +++ b/tests/Decoder/YamlTypeDecoderTest.php @@ -9,6 +9,8 @@ /** * @covers \Chubbyphp\Deserialization\Decoder\YamlTypeDecoder + * + * @internal */ class YamlTypeDecoderTest extends AbstractTypeDecoderTest { @@ -26,7 +28,7 @@ public function testGetContentType() */ public function testDecode(array $expectedData) { - $yaml = <<setGroups(['group1']) ->setRequest($request) ->setAttributes(['attribute' => 'value']) - ->getContext(); + ->getContext() + ; self::assertInstanceOf(DenormalizerContextInterface::class, $context); @@ -73,7 +76,8 @@ public function testCreateSetNullRequest() { $context = DenormalizerContextBuilder::create() ->setRequest() - ->getContext(); + ->getContext() + ; self::assertInstanceOf(DenormalizerContextInterface::class, $context); diff --git a/tests/Denormalizer/DenormalizerContextTest.php b/tests/Denormalizer/DenormalizerContextTest.php index a4b7d0e..5330a91 100644 --- a/tests/Denormalizer/DenormalizerContextTest.php +++ b/tests/Denormalizer/DenormalizerContextTest.php @@ -12,6 +12,8 @@ /** * @covers \Chubbyphp\Deserialization\Denormalizer\DenormalizerContext + * + * @internal */ class DenormalizerContextTest extends TestCase { diff --git a/tests/Denormalizer/DenormalizerObjectMappingRegistryTest.php b/tests/Denormalizer/DenormalizerObjectMappingRegistryTest.php index d6cbf13..dd00f2a 100644 --- a/tests/Denormalizer/DenormalizerObjectMappingRegistryTest.php +++ b/tests/Denormalizer/DenormalizerObjectMappingRegistryTest.php @@ -16,6 +16,8 @@ /** * @covers \Chubbyphp\Deserialization\Denormalizer\DenormalizerObjectMappingRegistry + * + * @internal */ class DenormalizerObjectMappingRegistryTest extends TestCase { diff --git a/tests/Denormalizer/DenormalizerTest.php b/tests/Denormalizer/DenormalizerTest.php index 8f3297c..6e47af9 100644 --- a/tests/Denormalizer/DenormalizerTest.php +++ b/tests/Denormalizer/DenormalizerTest.php @@ -24,6 +24,8 @@ /** * @covers \Chubbyphp\Deserialization\Denormalizer\Denormalizer + * + * @internal */ class DenormalizerTest extends TestCase { diff --git a/tests/Denormalizer/FieldDenormalizerTest.php b/tests/Denormalizer/FieldDenormalizerTest.php index 5f6ee91..ffd9ba3 100644 --- a/tests/Denormalizer/FieldDenormalizerTest.php +++ b/tests/Denormalizer/FieldDenormalizerTest.php @@ -14,6 +14,8 @@ /** * @covers \Chubbyphp\Deserialization\Denormalizer\FieldDenormalizer + * + * @internal */ class FieldDenormalizerTest extends TestCase { diff --git a/tests/Denormalizer/Relation/EmbedManyFieldDenormalizerTest.php b/tests/Denormalizer/Relation/EmbedManyFieldDenormalizerTest.php index d4cc2ec..465fdef 100644 --- a/tests/Denormalizer/Relation/EmbedManyFieldDenormalizerTest.php +++ b/tests/Denormalizer/Relation/EmbedManyFieldDenormalizerTest.php @@ -18,6 +18,8 @@ /** * @covers \Chubbyphp\Deserialization\Denormalizer\Relation\EmbedManyFieldDenormalizer + * + * @internal */ class EmbedManyFieldDenormalizerTest extends TestCase { diff --git a/tests/Denormalizer/Relation/EmbedOneFieldDenormalizerTest.php b/tests/Denormalizer/Relation/EmbedOneFieldDenormalizerTest.php index 3adc7a4..f33c31f 100644 --- a/tests/Denormalizer/Relation/EmbedOneFieldDenormalizerTest.php +++ b/tests/Denormalizer/Relation/EmbedOneFieldDenormalizerTest.php @@ -17,6 +17,8 @@ /** * @covers \Chubbyphp\Deserialization\Denormalizer\Relation\EmbedOneFieldDenormalizer + * + * @internal */ class EmbedOneFieldDenormalizerTest extends TestCase { diff --git a/tests/Denormalizer/Relation/ReferenceManyFieldDenormalizerTest.php b/tests/Denormalizer/Relation/ReferenceManyFieldDenormalizerTest.php index 5842f67..10befa6 100644 --- a/tests/Denormalizer/Relation/ReferenceManyFieldDenormalizerTest.php +++ b/tests/Denormalizer/Relation/ReferenceManyFieldDenormalizerTest.php @@ -16,6 +16,8 @@ /** * @covers \Chubbyphp\Deserialization\Denormalizer\Relation\ReferenceManyFieldDenormalizer + * + * @internal */ class ReferenceManyFieldDenormalizerTest extends TestCase { diff --git a/tests/Denormalizer/Relation/ReferenceOneFieldDenormalizerTest.php b/tests/Denormalizer/Relation/ReferenceOneFieldDenormalizerTest.php index aaa0171..b67c6fe 100644 --- a/tests/Denormalizer/Relation/ReferenceOneFieldDenormalizerTest.php +++ b/tests/Denormalizer/Relation/ReferenceOneFieldDenormalizerTest.php @@ -15,6 +15,8 @@ /** * @covers \Chubbyphp\Deserialization\Denormalizer\Relation\ReferenceOneFieldDenormalizer + * + * @internal */ class ReferenceOneFieldDenormalizerTest extends TestCase { diff --git a/tests/DependencyInjection/DeserializationCompilerPassTest.php b/tests/DependencyInjection/DeserializationCompilerPassTest.php index 08a74a7..a64a5dd 100644 --- a/tests/DependencyInjection/DeserializationCompilerPassTest.php +++ b/tests/DependencyInjection/DeserializationCompilerPassTest.php @@ -16,6 +16,8 @@ /** * @covers \Chubbyphp\Deserialization\DependencyInjection\DeserializationCompilerPass + * + * @internal */ class DeserializationCompilerPassTest extends TestCase { @@ -29,7 +31,8 @@ public function testProcess() $container ->register('stdclass', $stdClassMappingClass) - ->addTag('chubbyphp.deserializer.denormalizer.objectmapping'); + ->addTag('chubbyphp.deserializer.denormalizer.objectmapping') + ; $container->compile(); diff --git a/tests/DeserializerIntegrationTest.php b/tests/DeserializerIntegrationTest.php index 9985583..aa998e4 100644 --- a/tests/DeserializerIntegrationTest.php +++ b/tests/DeserializerIntegrationTest.php @@ -23,6 +23,8 @@ /** * @coversNothing + * + * @internal */ class DeserializerIntegrationTest extends TestCase { diff --git a/tests/DeserializerLogicExceptionTest.php b/tests/DeserializerLogicExceptionTest.php index 88c3e9e..37768ce 100644 --- a/tests/DeserializerLogicExceptionTest.php +++ b/tests/DeserializerLogicExceptionTest.php @@ -9,6 +9,8 @@ /** * @covers \Chubbyphp\Deserialization\DeserializerLogicException + * + * @internal */ class DeserializerLogicExceptionTest extends TestCase { diff --git a/tests/DeserializerRuntimeExceptionTest.php b/tests/DeserializerRuntimeExceptionTest.php index c8ba683..1dbc2cf 100644 --- a/tests/DeserializerRuntimeExceptionTest.php +++ b/tests/DeserializerRuntimeExceptionTest.php @@ -9,6 +9,8 @@ /** * @covers \Chubbyphp\Deserialization\DeserializerRuntimeException + * + * @internal */ class DeserializerRuntimeExceptionTest extends TestCase { diff --git a/tests/DeserializerTest.php b/tests/DeserializerTest.php index 5a90caa..c5ccd64 100644 --- a/tests/DeserializerTest.php +++ b/tests/DeserializerTest.php @@ -15,6 +15,8 @@ /** * @covers \Chubbyphp\Deserialization\Deserializer + * + * @internal */ class DeserializerTest extends TestCase { diff --git a/tests/Mapping/CallableDenormalizationObjectMappingTest.php b/tests/Mapping/CallableDenormalizationObjectMappingTest.php index 7e402f7..cdc9265 100644 --- a/tests/Mapping/CallableDenormalizationObjectMappingTest.php +++ b/tests/Mapping/CallableDenormalizationObjectMappingTest.php @@ -13,6 +13,8 @@ /** * @covers \Chubbyphp\Deserialization\Mapping\CallableDenormalizationObjectMapping + * + * @internal */ class CallableDenormalizationObjectMappingTest extends TestCase { diff --git a/tests/Mapping/DenormalizationFieldMappingBuilderTest.php b/tests/Mapping/DenormalizationFieldMappingBuilderTest.php index a3e2a51..b695f4c 100644 --- a/tests/Mapping/DenormalizationFieldMappingBuilderTest.php +++ b/tests/Mapping/DenormalizationFieldMappingBuilderTest.php @@ -14,14 +14,16 @@ use Chubbyphp\Deserialization\Denormalizer\Relation\ReferenceManyFieldDenormalizer; use Chubbyphp\Deserialization\Denormalizer\Relation\ReferenceOneFieldDenormalizer; use Chubbyphp\Deserialization\Mapping\DenormalizationFieldMappingBuilder; -use Chubbyphp\Mock\MockByCallsTrait; use Chubbyphp\Deserialization\Policy\NullPolicy; use Chubbyphp\Deserialization\Policy\PolicyInterface; +use Chubbyphp\Mock\MockByCallsTrait; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** * @covers \Chubbyphp\Deserialization\Mapping\DenormalizationFieldMappingBuilder + * + * @internal */ class DenormalizationFieldMappingBuilderTest extends TestCase { @@ -274,7 +276,8 @@ public function testGetMapping() ->setGroups(['group1']) ->setFieldDenormalizer($fieldDenormalizer) ->setPolicy($policy) - ->getMapping(); + ->getMapping() + ; $error = error_get_last(); diff --git a/tests/Mapping/DenormalizationFieldMappingTest.php b/tests/Mapping/DenormalizationFieldMappingTest.php index 27d532a..4625b9e 100644 --- a/tests/Mapping/DenormalizationFieldMappingTest.php +++ b/tests/Mapping/DenormalizationFieldMappingTest.php @@ -13,6 +13,8 @@ /** * @covers \Chubbyphp\Deserialization\Mapping\DenormalizationFieldMapping + * + * @internal */ class DenormalizationFieldMappingTest extends TestCase { diff --git a/tests/Mapping/LazyDenormalizationObjectMappingTest.php b/tests/Mapping/LazyDenormalizationObjectMappingTest.php index 9ccac34..8c91a81 100644 --- a/tests/Mapping/LazyDenormalizationObjectMappingTest.php +++ b/tests/Mapping/LazyDenormalizationObjectMappingTest.php @@ -15,6 +15,8 @@ /** * @covers \Chubbyphp\Deserialization\Mapping\LazyDenormalizationObjectMapping + * + * @internal */ class LazyDenormalizationObjectMappingTest extends TestCase { diff --git a/tests/Policy/AndPolicyTest.php b/tests/Policy/AndPolicyTest.php index a7dc331..ce406e4 100644 --- a/tests/Policy/AndPolicyTest.php +++ b/tests/Policy/AndPolicyTest.php @@ -4,16 +4,18 @@ namespace Chubbyphp\Tests\Deserialization\Policy; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; use Chubbyphp\Deserialization\Denormalizer\DenormalizerContextInterface; use Chubbyphp\Deserialization\Policy\AndPolicy; use Chubbyphp\Deserialization\Policy\PolicyInterface; +use Chubbyphp\Mock\Call; +use Chubbyphp\Mock\MockByCallsTrait; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** * @covers \Chubbyphp\Deserialization\Policy\AndPolicy + * + * @internal */ class AndPolicyTest extends TestCase { diff --git a/tests/Policy/CallbackPolicyTest.php b/tests/Policy/CallbackPolicyTest.php index 4c32d4b..0c682a1 100644 --- a/tests/Policy/CallbackPolicyTest.php +++ b/tests/Policy/CallbackPolicyTest.php @@ -4,14 +4,16 @@ namespace Chubbyphp\Tests\Deserialization\Policy; -use Chubbyphp\Mock\MockByCallsTrait; use Chubbyphp\Deserialization\Denormalizer\DenormalizerContextInterface; use Chubbyphp\Deserialization\Policy\CallbackPolicy; +use Chubbyphp\Mock\MockByCallsTrait; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** * @covers \Chubbyphp\Deserialization\Policy\CallbackPolicy + * + * @internal */ class CallbackPolicyTest extends TestCase { diff --git a/tests/Policy/GroupPolicyTest.php b/tests/Policy/GroupPolicyTest.php index d130172..77797cf 100644 --- a/tests/Policy/GroupPolicyTest.php +++ b/tests/Policy/GroupPolicyTest.php @@ -4,14 +4,16 @@ namespace Chubbyphp\Tests\Deserialization\Policy; -use Chubbyphp\Mock\MockByCallsTrait; use Chubbyphp\Deserialization\Denormalizer\DenormalizerContextInterface; use Chubbyphp\Deserialization\Policy\GroupPolicy; +use Chubbyphp\Mock\MockByCallsTrait; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** * @covers \Chubbyphp\Deserialization\Policy\GroupPolicy + * + * @internal */ class GroupPolicyTest extends TestCase { diff --git a/tests/Policy/NullPolicyTest.php b/tests/Policy/NullPolicyTest.php index a540a41..c35f8a4 100644 --- a/tests/Policy/NullPolicyTest.php +++ b/tests/Policy/NullPolicyTest.php @@ -4,14 +4,16 @@ namespace Chubbyphp\Tests\Deserialization\Policy; -use Chubbyphp\Mock\MockByCallsTrait; use Chubbyphp\Deserialization\Denormalizer\DenormalizerContextInterface; use Chubbyphp\Deserialization\Policy\NullPolicy; +use Chubbyphp\Mock\MockByCallsTrait; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** * @covers \Chubbyphp\Deserialization\Policy\NullPolicy + * + * @internal */ class NullPolicyTest extends TestCase { diff --git a/tests/Policy/OrPolicyTest.php b/tests/Policy/OrPolicyTest.php index 92e614c..3b09f80 100644 --- a/tests/Policy/OrPolicyTest.php +++ b/tests/Policy/OrPolicyTest.php @@ -4,16 +4,18 @@ namespace Chubbyphp\Tests\Deserialization\Policy; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; use Chubbyphp\Deserialization\Denormalizer\DenormalizerContextInterface; use Chubbyphp\Deserialization\Policy\OrPolicy; use Chubbyphp\Deserialization\Policy\PolicyInterface; +use Chubbyphp\Mock\Call; +use Chubbyphp\Mock\MockByCallsTrait; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** * @covers \Chubbyphp\Deserialization\Policy\OrPolicy + * + * @internal */ class OrPolicyTest extends TestCase { diff --git a/tests/Provider/DeserializationProviderTest.php b/tests/Provider/DeserializationProviderTest.php index 453f1be..ea1d441 100644 --- a/tests/Provider/DeserializationProviderTest.php +++ b/tests/Provider/DeserializationProviderTest.php @@ -1,5 +1,7 @@