diff --git a/README.md b/README.md index e8b8f46..55f881f 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ A simple serialization. ## Requirements * php: ^8.2 - * chubbyphp/chubbyphp-decode-encode: ^1.1 + * chubbyphp/chubbyphp-decode-encode: ^1.2 * doctrine/inflector: ^1.4.4|^2.0.10 * psr/http-message: ^1.1|^2.0 * psr/link: ^1.1.1|^2.0.1 diff --git a/composer.json b/composer.json index 1a567c6..2fa6d23 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "ext-dom": "*", "ext-json": "*", "ext-mbstring": "*", - "chubbyphp/chubbyphp-decode-encode": "^1.1", + "chubbyphp/chubbyphp-decode-encode": "^1.2", "doctrine/inflector": "^1.4.4|^2.0.10", "psr/http-message": "^1.1|^2.0", "psr/link": "^1.1.1|^2.0.1", @@ -23,15 +23,15 @@ "require-dev": { "chubbyphp/chubbyphp-container": "^2.2", "chubbyphp/chubbyphp-dev-helper": "dev-master", - "chubbyphp/chubbyphp-laminas-config-factory": "^1.3", - "chubbyphp/chubbyphp-mock": "^1.8.0", + "chubbyphp/chubbyphp-laminas-config-factory": "^1.4", + "chubbyphp/chubbyphp-mock": "^2.0@dev", "doctrine/collections": "^2.2.2", "doctrine/persistence": "^4.0", - "infection/infection": "^0.29.8", + "infection/infection": "^0.29.12", "php-coveralls/php-coveralls": "^2.7.0", "phpstan/extension-installer": "^1.4.3", - "phpstan/phpstan": "^2.0.3", - "phpunit/phpunit": "^11.5.0", + "phpstan/phpstan": "^2.1.6", + "phpunit/phpunit": "^11.5.9", "pimple/pimple": "^3.5", "psr/container": "^2.0.2", "symfony/config": "^5.4.46|^6.4.14|^7.2", diff --git a/tests/Unit/Mapping/CallableNormalizationObjectMappingTest.php b/tests/Unit/Mapping/CallableNormalizationObjectMappingTest.php index afa4152..d202926 100644 --- a/tests/Unit/Mapping/CallableNormalizationObjectMappingTest.php +++ b/tests/Unit/Mapping/CallableNormalizationObjectMappingTest.php @@ -4,8 +4,8 @@ namespace Chubbyphp\Tests\Serialization\Unit\Mapping; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Mapping\CallableNormalizationObjectMapping; use Chubbyphp\Serialization\Mapping\NormalizationFieldMappingInterface; use Chubbyphp\Serialization\Mapping\NormalizationLinkMappingInterface; @@ -19,8 +19,6 @@ */ final class CallableNormalizationObjectMappingTest extends TestCase { - use MockByCallsTrait; - public function testGetClass(): void { $mapping = new CallableNormalizationObjectMapping(\stdClass::class, static function (): void {}); @@ -30,42 +28,62 @@ public function testGetClass(): void public function testGetNormalizationType(): void { - $mapping = new CallableNormalizationObjectMapping(\stdClass::class, fn () => $this->getMockByCalls(NormalizationObjectMappingInterface::class, [ - Call::create('getNormalizationType')->with()->willReturn('type'), - ])); + $builder = new MockObjectBuilder(); + + /** @var NormalizationObjectMappingInterface $normalizationObjectMapping */ + $normalizationObjectMapping = $builder->create(NormalizationObjectMappingInterface::class, [ + new WithReturn('getNormalizationType', [], 'type'), + ]); + + $mapping = new CallableNormalizationObjectMapping(\stdClass::class, static fn () => $normalizationObjectMapping); self::assertSame('type', $mapping->getNormalizationType()); } public function testGetNormalizationFieldMappings(): void { - $fieldMapping = $this->getMockByCalls(NormalizationFieldMappingInterface::class); + $builder = new MockObjectBuilder(); + + /** @var NormalizationFieldMappingInterface $fieldMapping */ + $fieldMapping = $builder->create(NormalizationFieldMappingInterface::class, []); + + $normalizationObjectMapping = $builder->create(NormalizationObjectMappingInterface::class, [ + new WithReturn('getNormalizationFieldMappings', ['path'], [$fieldMapping]), + ]); - $mapping = new CallableNormalizationObjectMapping(\stdClass::class, fn () => $this->getMockByCalls(NormalizationObjectMappingInterface::class, [ - Call::create('getNormalizationFieldMappings')->with('path')->willReturn([$fieldMapping]), - ])); + $mapping = new CallableNormalizationObjectMapping(\stdClass::class, static fn () => $normalizationObjectMapping); self::assertSame($fieldMapping, $mapping->getNormalizationFieldMappings('path')[0]); } public function testGetNormalizationEmbeddedFieldMappings(): void { - $fieldMapping = $this->getMockByCalls(NormalizationFieldMappingInterface::class); + $builder = new MockObjectBuilder(); - $mapping = new CallableNormalizationObjectMapping(\stdClass::class, fn () => $this->getMockByCalls(NormalizationObjectMappingInterface::class, [ - Call::create('getNormalizationEmbeddedFieldMappings')->with('path')->willReturn([$fieldMapping]), - ])); + /** @var NormalizationFieldMappingInterface $fieldMapping */ + $fieldMapping = $builder->create(NormalizationFieldMappingInterface::class, []); + + $normalizationObjectMapping = $builder->create(NormalizationObjectMappingInterface::class, [ + new WithReturn('getNormalizationEmbeddedFieldMappings', ['path'], [$fieldMapping]), + ]); + + $mapping = new CallableNormalizationObjectMapping(\stdClass::class, static fn () => $normalizationObjectMapping); self::assertSame($fieldMapping, $mapping->getNormalizationEmbeddedFieldMappings('path')[0]); } public function testGetNormalizationLinkMappings(): void { - $linkMapping = $this->getMockByCalls(NormalizationLinkMappingInterface::class); + $builder = new MockObjectBuilder(); + + /** @var NormalizationLinkMappingInterface $linkMapping */ + $linkMapping = $builder->create(NormalizationLinkMappingInterface::class, []); + + $normalizationObjectMapping = $builder->create(NormalizationObjectMappingInterface::class, [ + new WithReturn('getNormalizationLinkMappings', ['path'], [$linkMapping]), + ]); - $mapping = new CallableNormalizationObjectMapping(\stdClass::class, fn () => $this->getMockByCalls(NormalizationObjectMappingInterface::class, [ - Call::create('getNormalizationLinkMappings')->with('path')->willReturn([$linkMapping]), - ])); + $mapping = new CallableNormalizationObjectMapping(\stdClass::class, static fn () => $normalizationObjectMapping); self::assertSame($linkMapping, $mapping->getNormalizationLinkMappings('path')[0]); } diff --git a/tests/Unit/Mapping/LazyNormalizationObjectMappingTest.php b/tests/Unit/Mapping/LazyNormalizationObjectMappingTest.php index c44649c..1138a01 100644 --- a/tests/Unit/Mapping/LazyNormalizationObjectMappingTest.php +++ b/tests/Unit/Mapping/LazyNormalizationObjectMappingTest.php @@ -4,8 +4,8 @@ namespace Chubbyphp\Tests\Serialization\Unit\Mapping; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Mapping\LazyNormalizationObjectMapping; use Chubbyphp\Serialization\Mapping\NormalizationFieldMappingInterface; use Chubbyphp\Serialization\Mapping\NormalizationLinkMappingInterface; @@ -21,36 +21,36 @@ */ final class LazyNormalizationObjectMappingTest extends TestCase { - use MockByCallsTrait; - public function testInvoke(): void { - $normalizationFieldMappings = [$this->getMockByCalls(NormalizationFieldMappingInterface::class)]; + $builder = new MockObjectBuilder(); + + $normalizationFieldMappings = [ + $builder->create(NormalizationFieldMappingInterface::class, []), + ]; - $normalizationEmbeddedFieldMappings = [$this->getMockByCalls(NormalizationFieldMappingInterface::class)]; + $normalizationEmbeddedFieldMappings = [ + $builder->create(NormalizationFieldMappingInterface::class, []), + ]; - $normalizationLinkMappings = [$this->getMockByCalls(NormalizationLinkMappingInterface::class)]; + $normalizationLinkMappings = [ + $builder->create(NormalizationLinkMappingInterface::class, []), + ]; /** @var MockObject|NormalizationObjectMappingInterface $normalizationObjectMapping */ - $normalizationObjectMapping = $this->getMockByCalls(NormalizationObjectMappingInterface::class, [ - Call::create('getNormalizationType')->with()->willReturn('type'), - Call::create('getNormalizationFieldMappings') - ->with('path') - ->willReturn($normalizationFieldMappings), - Call::create('getNormalizationEmbeddedFieldMappings') - ->with('path') - ->willReturn($normalizationEmbeddedFieldMappings), - Call::create('getNormalizationLinkMappings') - ->with('path') - ->willReturn($normalizationLinkMappings), + $normalizationObjectMapping = $builder->create(NormalizationObjectMappingInterface::class, [ + new WithReturn('getNormalizationType', [], 'type'), + new WithReturn('getNormalizationFieldMappings', ['path'], $normalizationFieldMappings), + new WithReturn('getNormalizationEmbeddedFieldMappings', ['path'], $normalizationEmbeddedFieldMappings), + new WithReturn('getNormalizationLinkMappings', ['path'], $normalizationLinkMappings), ]); /** @var ContainerInterface|MockObject $container */ - $container = $this->getMockByCalls(ContainerInterface::class, [ - Call::create('get')->with('service')->willReturn($normalizationObjectMapping), - Call::create('get')->with('service')->willReturn($normalizationObjectMapping), - Call::create('get')->with('service')->willReturn($normalizationObjectMapping), - Call::create('get')->with('service')->willReturn($normalizationObjectMapping), + $container = $builder->create(ContainerInterface::class, [ + new WithReturn('get', ['service'], $normalizationObjectMapping), + new WithReturn('get', ['service'], $normalizationObjectMapping), + new WithReturn('get', ['service'], $normalizationObjectMapping), + new WithReturn('get', ['service'], $normalizationObjectMapping), ]); $objectMapping = new LazyNormalizationObjectMapping($container, 'service', \stdClass::class); diff --git a/tests/Unit/Mapping/NormalizationFieldMappingBuilderTest.php b/tests/Unit/Mapping/NormalizationFieldMappingBuilderTest.php index a90944c..6cdaf26 100644 --- a/tests/Unit/Mapping/NormalizationFieldMappingBuilderTest.php +++ b/tests/Unit/Mapping/NormalizationFieldMappingBuilderTest.php @@ -4,7 +4,7 @@ namespace Chubbyphp\Tests\Serialization\Unit\Mapping; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Mapping\NormalizationFieldMappingBuilder; use Chubbyphp\Serialization\Normalizer\CallbackFieldNormalizer; use Chubbyphp\Serialization\Normalizer\DateTimeFieldNormalizer; @@ -26,8 +26,6 @@ */ final class NormalizationFieldMappingBuilderTest extends TestCase { - use MockByCallsTrait; - public function testGetDefaultMapping(): void { $fieldMapping = NormalizationFieldMappingBuilder::create('name')->getMapping(); @@ -39,8 +37,10 @@ public function testGetDefaultMapping(): void public function testGetMappingWithNormalizer(): void { + $builder = new MockObjectBuilder(); + /** @var FieldNormalizerInterface|MockObject $fieldNormalizer */ - $fieldNormalizer = $this->getMockByCalls(FieldNormalizerInterface::class); + $fieldNormalizer = $builder->create(FieldNormalizerInterface::class, []); $fieldMapping = NormalizationFieldMappingBuilder::create('name', $fieldNormalizer)->getMapping(); @@ -122,11 +122,13 @@ public function testGetDefaultMappingForReferenceOne(): void public function testGetMapping(): void { + $builder = new MockObjectBuilder(); + /** @var FieldNormalizerInterface|MockObject $fieldNormalizer */ - $fieldNormalizer = $this->getMockByCalls(FieldNormalizerInterface::class); + $fieldNormalizer = $builder->create(FieldNormalizerInterface::class, []); /** @var MockObject|PolicyInterface $policy */ - $policy = $this->getMockByCalls(PolicyInterface::class); + $policy = $builder->create(PolicyInterface::class, []); $fieldMapping = NormalizationFieldMappingBuilder::create('name', $fieldNormalizer) ->setPolicy($policy) diff --git a/tests/Unit/Mapping/NormalizationFieldMappingTest.php b/tests/Unit/Mapping/NormalizationFieldMappingTest.php index 4ec1e9a..826e8fc 100644 --- a/tests/Unit/Mapping/NormalizationFieldMappingTest.php +++ b/tests/Unit/Mapping/NormalizationFieldMappingTest.php @@ -4,7 +4,7 @@ namespace Chubbyphp\Tests\Serialization\Unit\Mapping; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Mapping\NormalizationFieldMapping; use Chubbyphp\Serialization\Normalizer\FieldNormalizerInterface; use Chubbyphp\Serialization\Policy\PolicyInterface; @@ -18,12 +18,12 @@ */ final class NormalizationFieldMappingTest extends TestCase { - use MockByCallsTrait; - public function testGetName(): void { + $builder = new MockObjectBuilder(); + /** @var FieldNormalizerInterface|MockObject $fieldNormalizer */ - $fieldNormalizer = $this->getMockByCalls(FieldNormalizerInterface::class); + $fieldNormalizer = $builder->create(FieldNormalizerInterface::class, []); $fieldMapping = new NormalizationFieldMapping('name', $fieldNormalizer); @@ -32,8 +32,10 @@ public function testGetName(): void public function testGetFieldNormalizer(): void { + $builder = new MockObjectBuilder(); + /** @var FieldNormalizerInterface|MockObject $fieldNormalizer */ - $fieldNormalizer = $this->getMockByCalls(FieldNormalizerInterface::class); + $fieldNormalizer = $builder->create(FieldNormalizerInterface::class, []); $fieldMapping = new NormalizationFieldMapping('name', $fieldNormalizer); @@ -42,11 +44,13 @@ public function testGetFieldNormalizer(): void public function testGetPolicy(): void { + $builder = new MockObjectBuilder(); + /** @var FieldNormalizerInterface|MockObject $fieldNormalizer */ - $fieldNormalizer = $this->getMockByCalls(FieldNormalizerInterface::class); + $fieldNormalizer = $builder->create(FieldNormalizerInterface::class, []); /** @var MockObject|PolicyInterface $policy */ - $policy = $this->getMockByCalls(PolicyInterface::class); + $policy = $builder->create(PolicyInterface::class, []); $fieldMapping = new NormalizationFieldMapping('name', $fieldNormalizer, $policy); diff --git a/tests/Unit/Mapping/NormalizationLinkMappingBuilderTest.php b/tests/Unit/Mapping/NormalizationLinkMappingBuilderTest.php index b25be41..4223fa8 100644 --- a/tests/Unit/Mapping/NormalizationLinkMappingBuilderTest.php +++ b/tests/Unit/Mapping/NormalizationLinkMappingBuilderTest.php @@ -4,7 +4,7 @@ namespace Chubbyphp\Tests\Serialization\Unit\Mapping; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Mapping\NormalizationLinkMappingBuilder; use Chubbyphp\Serialization\Normalizer\CallbackLinkNormalizer; use Chubbyphp\Serialization\Normalizer\LinkNormalizer; @@ -22,12 +22,12 @@ */ final class NormalizationLinkMappingBuilderTest extends TestCase { - use MockByCallsTrait; - public function testGetDefaultMapping(): void { + $builder = new MockObjectBuilder(); + /** @var LinkNormalizerInterface|MockObject $linkNormalizer */ - $linkNormalizer = $this->getMockByCalls(LinkNormalizerInterface::class); + $linkNormalizer = $builder->create(LinkNormalizerInterface::class, []); $linkMapping = NormalizationLinkMappingBuilder::create('name', $linkNormalizer)->getMapping(); @@ -47,8 +47,10 @@ public function testGetDefaultMappingForCallback(): void public function testGetDefaultMappingForLink(): void { + $builder = new MockObjectBuilder(); + /** @var LinkInterface|MockObject $link */ - $link = $this->getMockByCalls(LinkInterface::class); + $link = $builder->create(LinkInterface::class, []); $linkMapping = NormalizationLinkMappingBuilder::createLink('name', $link)->getMapping(); @@ -59,11 +61,13 @@ public function testGetDefaultMappingForLink(): void public function testGetMapping(): void { + $builder = new MockObjectBuilder(); + /** @var LinkNormalizerInterface|MockObject $linkNormalizer */ - $linkNormalizer = $this->getMockByCalls(LinkNormalizerInterface::class); + $linkNormalizer = $builder->create(LinkNormalizerInterface::class, []); /** @var MockObject|PolicyInterface $policy */ - $policy = $this->getMockByCalls(PolicyInterface::class); + $policy = $builder->create(PolicyInterface::class, []); $linkMapping = NormalizationLinkMappingBuilder::create('name', $linkNormalizer) ->setPolicy($policy) diff --git a/tests/Unit/Mapping/NormalizationLinkMappingTest.php b/tests/Unit/Mapping/NormalizationLinkMappingTest.php index fcfad4a..b53fa48 100644 --- a/tests/Unit/Mapping/NormalizationLinkMappingTest.php +++ b/tests/Unit/Mapping/NormalizationLinkMappingTest.php @@ -4,7 +4,7 @@ namespace Chubbyphp\Tests\Serialization\Unit\Mapping; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Mapping\NormalizationLinkMapping; use Chubbyphp\Serialization\Normalizer\LinkNormalizerInterface; use Chubbyphp\Serialization\Policy\PolicyInterface; @@ -18,12 +18,12 @@ */ final class NormalizationLinkMappingTest extends TestCase { - use MockByCallsTrait; - public function testGetName(): void { + $builder = new MockObjectBuilder(); + /** @var LinkNormalizerInterface|MockObject $linkNormalizer */ - $linkNormalizer = $this->getMockByCalls(LinkNormalizerInterface::class); + $linkNormalizer = $builder->create(LinkNormalizerInterface::class, []); $linkMapping = new NormalizationLinkMapping('name', $linkNormalizer); @@ -32,8 +32,10 @@ public function testGetName(): void public function testGetLinkNormalizer(): void { + $builder = new MockObjectBuilder(); + /** @var LinkNormalizerInterface|MockObject $linkNormalizer */ - $linkNormalizer = $this->getMockByCalls(LinkNormalizerInterface::class); + $linkNormalizer = $builder->create(LinkNormalizerInterface::class, []); $linkMapping = new NormalizationLinkMapping('name', $linkNormalizer); @@ -42,11 +44,13 @@ public function testGetLinkNormalizer(): void public function testGetPolicy(): void { + $builder = new MockObjectBuilder(); + /** @var LinkNormalizerInterface|MockObject $linkNormalizer */ - $linkNormalizer = $this->getMockByCalls(LinkNormalizerInterface::class); + $linkNormalizer = $builder->create(LinkNormalizerInterface::class, []); /** @var MockObject|PolicyInterface $policy */ - $policy = $this->getMockByCalls(PolicyInterface::class); + $policy = $builder->create(PolicyInterface::class, []); $linkMapping = new NormalizationLinkMapping('name', $linkNormalizer, $policy); diff --git a/tests/Unit/Normalizer/CallbackFieldNormalizerTest.php b/tests/Unit/Normalizer/CallbackFieldNormalizerTest.php index 703d698..640c57c 100644 --- a/tests/Unit/Normalizer/CallbackFieldNormalizerTest.php +++ b/tests/Unit/Normalizer/CallbackFieldNormalizerTest.php @@ -4,7 +4,7 @@ namespace Chubbyphp\Tests\Serialization\Unit\Normalizer; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Normalizer\CallbackFieldNormalizer; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; use Chubbyphp\Serialization\Normalizer\NormalizerInterface; @@ -18,12 +18,12 @@ */ final class CallbackFieldNormalizerTest extends TestCase { - use MockByCallsTrait; - public function testNormalizeField(): void { + $builder = new MockObjectBuilder(); + /** @var MockObject|NormalizerContextInterface $normalizerContext */ - $normalizerContext = $this->getMockByCalls(NormalizerContextInterface::class); + $normalizerContext = $builder->create(NormalizerContextInterface::class, []); $object = new \stdClass(); diff --git a/tests/Unit/Normalizer/CallbackLinkNormalizerTest.php b/tests/Unit/Normalizer/CallbackLinkNormalizerTest.php index 2304ca5..29f252e 100644 --- a/tests/Unit/Normalizer/CallbackLinkNormalizerTest.php +++ b/tests/Unit/Normalizer/CallbackLinkNormalizerTest.php @@ -4,8 +4,8 @@ namespace Chubbyphp\Tests\Serialization\Unit\Normalizer; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Normalizer\CallbackLinkNormalizer; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; use Chubbyphp\Serialization\SerializerLogicException; @@ -20,22 +20,22 @@ */ final class CallbackLinkNormalizerTest extends TestCase { - use MockByCallsTrait; - public function testNormalizeLink(): void { $object = new \stdClass(); + $builder = new MockObjectBuilder(); + /** @var LinkInterface|MockObject $link */ - $link = $this->getMockByCalls(LinkInterface::class, [ - Call::create('getHref')->with()->willReturn('/api/model/id1'), - Call::create('isTemplated')->with()->willReturn(false), - Call::create('getRels')->with()->willReturn(['model']), - Call::create('getAttributes')->with()->willReturn(['method' => 'GET']), + $link = $builder->create(LinkInterface::class, [ + new WithReturn('getHref', [], '/api/model/id1'), + new WithReturn('isTemplated', [], false), + new WithReturn('getRels', [], ['model']), + new WithReturn('getAttributes', [], ['method' => 'GET']), ]); /** @var MockObject|NormalizerContextInterface $normalizerContext */ - $normalizerContext = $this->getMockByCalls(NormalizerContextInterface::class); + $normalizerContext = $builder->create(NormalizerContextInterface::class, []); $linkNormalizer = new CallbackLinkNormalizer( static fn (string $path, $object, NormalizerContextInterface $context) => $link @@ -45,12 +45,8 @@ public function testNormalizeLink(): void [ 'href' => '/api/model/id1', 'templated' => false, - 'rel' => [ - 'model', - ], - 'attributes' => [ - 'method' => 'GET', - ], + 'rel' => ['model'], + 'attributes' => ['method' => 'GET'], ], $linkNormalizer->normalizeLink('name', $object, $normalizerContext) ); @@ -65,8 +61,10 @@ public function testNormalizeLinkWithWrongDataType(): void $object = new \stdClass(); + $builder = new MockObjectBuilder(); + /** @var MockObject|NormalizerContextInterface $normalizerContext */ - $normalizerContext = $this->getMockByCalls(NormalizerContextInterface::class); + $normalizerContext = $builder->create(NormalizerContextInterface::class, []); $linkNormalizer = new CallbackLinkNormalizer( static fn (string $path, $object, NormalizerContextInterface $context) => 'test' diff --git a/tests/Unit/Normalizer/DateTimeFieldNormalizerTest.php b/tests/Unit/Normalizer/DateTimeFieldNormalizerTest.php index f238d2f..b1cff4d 100644 --- a/tests/Unit/Normalizer/DateTimeFieldNormalizerTest.php +++ b/tests/Unit/Normalizer/DateTimeFieldNormalizerTest.php @@ -4,8 +4,8 @@ namespace Chubbyphp\Tests\Serialization\Unit\Normalizer; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Accessor\AccessorInterface; use Chubbyphp\Serialization\Normalizer\DateTimeFieldNormalizer; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; @@ -19,30 +19,26 @@ */ final class DateTimeFieldNormalizerTest extends TestCase { - use MockByCallsTrait; - public function testNormalizeField(): void { $object = $this->getObject(); $object->setDate(new \DateTimeImmutable('2017-01-01 22:00:00+01:00')); + $builder = new MockObjectBuilder(); + /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($object)->willReturn('2017-01-01 22:00:00+01:00'), + $accessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$object], '2017-01-01 22:00:00+01:00'), ]); $dateTimeFieldNormalizer = new DateTimeFieldNormalizer($accessor); self::assertSame( '2017-01-01T22:00:00+01:00', - $dateTimeFieldNormalizer->normalizeField( - 'date', - $object, - $context - ) + $dateTimeFieldNormalizer->normalizeField('date', $object, $context) ); } @@ -51,23 +47,21 @@ public function testNormalizeWithValidDateString(): void $object = $this->getObject(); $object->setDate('2017-01-01 22:00:00+01:00'); + $builder = new MockObjectBuilder(); + /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($object)->willReturn('2017-01-01 22:00:00+01:00'), + $accessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$object], '2017-01-01 22:00:00+01:00'), ]); $dateTimeFieldNormalizer = new DateTimeFieldNormalizer($accessor); self::assertSame( '2017-01-01T22:00:00+01:00', - $dateTimeFieldNormalizer->normalizeField( - 'date', - $object, - $context - ) + $dateTimeFieldNormalizer->normalizeField('date', $object, $context) ); } @@ -76,23 +70,21 @@ public function testNormalizeWithInvalidDateString(): void $object = $this->getObject(); $object->setDate('2017-01-01 25:00:00'); + $builder = new MockObjectBuilder(); + /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($object)->willReturn('2017-01-01 25:00:00'), + $accessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$object], '2017-01-01 25:00:00'), ]); $dateTimeFieldNormalizer = new DateTimeFieldNormalizer($accessor); self::assertSame( '2017-01-01 25:00:00', - $dateTimeFieldNormalizer->normalizeField( - 'date', - $object, - $context - ) + $dateTimeFieldNormalizer->normalizeField('date', $object, $context) ); } @@ -100,12 +92,14 @@ public function testNormalizeWithNull(): void { $object = $this->getObject(); + $builder = new MockObjectBuilder(); + /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($object)->willReturn(null), + $accessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$object], null), ]); $dateTimeFieldNormalizer = new DateTimeFieldNormalizer($accessor); diff --git a/tests/Unit/Normalizer/FieldNormalizerTest.php b/tests/Unit/Normalizer/FieldNormalizerTest.php index 063907e..6957433 100644 --- a/tests/Unit/Normalizer/FieldNormalizerTest.php +++ b/tests/Unit/Normalizer/FieldNormalizerTest.php @@ -4,8 +4,8 @@ namespace Chubbyphp\Tests\Serialization\Unit\Normalizer; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Accessor\AccessorInterface; use Chubbyphp\Serialization\Normalizer\FieldNormalizer; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; @@ -19,18 +19,18 @@ */ final class FieldNormalizerTest extends TestCase { - use MockByCallsTrait; - public function testNormalizeField(): void { $object = new \stdClass(); + $builder = new MockObjectBuilder(); + /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($object)->willReturn('name'), + $accessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$object], 'name'), ]); $fieldNormalizer = new FieldNormalizer($accessor); diff --git a/tests/Unit/Normalizer/LinkNormalizerTest.php b/tests/Unit/Normalizer/LinkNormalizerTest.php index f247de9..b84feb9 100644 --- a/tests/Unit/Normalizer/LinkNormalizerTest.php +++ b/tests/Unit/Normalizer/LinkNormalizerTest.php @@ -4,8 +4,8 @@ namespace Chubbyphp\Tests\Serialization\Unit\Normalizer; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Normalizer\LinkNormalizer; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; use PHPUnit\Framework\MockObject\MockObject; @@ -19,22 +19,22 @@ */ final class LinkNormalizerTest extends TestCase { - use MockByCallsTrait; - public function testNormalizeLink(): void { $object = new \stdClass(); + $builder = new MockObjectBuilder(); + /** @var LinkInterface|MockObject $link */ - $link = $this->getMockByCalls(LinkInterface::class, [ - Call::create('getHref')->with()->willReturn('/api/model/id1'), - Call::create('isTemplated')->with()->willReturn(false), - Call::create('getRels')->with()->willReturn(['model']), - Call::create('getAttributes')->with()->willReturn(['method' => 'GET']), + $link = $builder->create(LinkInterface::class, [ + new WithReturn('getHref', [], '/api/model/id1'), + new WithReturn('isTemplated', [], false), + new WithReturn('getRels', [], ['model']), + new WithReturn('getAttributes', [], ['method' => 'GET']), ]); /** @var MockObject|NormalizerContextInterface $normalizerContext */ - $normalizerContext = $this->getMockByCalls(NormalizerContextInterface::class); + $normalizerContext = $builder->create(NormalizerContextInterface::class, []); $linkNormalizer = new LinkNormalizer($link); diff --git a/tests/Unit/Normalizer/NormalizerContextBuilderTest.php b/tests/Unit/Normalizer/NormalizerContextBuilderTest.php index 897eb68..0841165 100644 --- a/tests/Unit/Normalizer/NormalizerContextBuilderTest.php +++ b/tests/Unit/Normalizer/NormalizerContextBuilderTest.php @@ -4,7 +4,7 @@ namespace Chubbyphp\Tests\Serialization\Unit\Normalizer; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Normalizer\NormalizerContextBuilder; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; use PHPUnit\Framework\MockObject\MockObject; @@ -18,8 +18,6 @@ */ final class NormalizerContextBuilderTest extends TestCase { - use MockByCallsTrait; - public function testCreate(): void { $context = NormalizerContextBuilder::create()->getContext(); @@ -32,8 +30,10 @@ public function testCreate(): void public function testCreateWithOverridenSettings(): void { + $builder = new MockObjectBuilder(); + /** @var MockObject|ServerRequestInterface $request */ - $request = $this->getMockByCalls(ServerRequestInterface::class); + $request = $builder->create(ServerRequestInterface::class, []); $context = NormalizerContextBuilder::create() ->setRequest($request) diff --git a/tests/Unit/Normalizer/NormalizerContextTest.php b/tests/Unit/Normalizer/NormalizerContextTest.php index 0dc9f8a..fc77597 100644 --- a/tests/Unit/Normalizer/NormalizerContextTest.php +++ b/tests/Unit/Normalizer/NormalizerContextTest.php @@ -4,7 +4,7 @@ namespace Chubbyphp\Tests\Serialization\Unit\Normalizer; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Normalizer\NormalizerContext; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -17,8 +17,6 @@ */ final class NormalizerContextTest extends TestCase { - use MockByCallsTrait; - public function testCreate(): void { $context = new NormalizerContext(); @@ -29,10 +27,12 @@ public function testCreate(): void self::assertSame('default', $context->getAttribute('nonExistingAttribute', 'default')); } - public function testCreateWithOverridenSettings(): void + public function testCreateWithOverriddenSettings(): void { + $builder = new MockObjectBuilder(); + /** @var MockObject|ServerRequestInterface $request */ - $request = $this->getMockByCalls(ServerRequestInterface::class); + $request = $builder->create(ServerRequestInterface::class, []); $context = new NormalizerContext( $request, @@ -46,8 +46,10 @@ public function testCreateWithOverridenSettings(): void public function testWithAttributes(): void { + $builder = new MockObjectBuilder(); + /** @var MockObject|ServerRequestInterface $request */ - $request = $this->getMockByCalls(ServerRequestInterface::class); + $request = $builder->create(ServerRequestInterface::class, []); $context = new NormalizerContext($request, ['attribute' => 'value']); @@ -61,8 +63,10 @@ public function testWithAttributes(): void public function testWithAttribute(): void { + $builder = new MockObjectBuilder(); + /** @var MockObject|ServerRequestInterface $request */ - $request = $this->getMockByCalls(ServerRequestInterface::class); + $request = $builder->create(ServerRequestInterface::class, []); $context = new NormalizerContext($request, ['attribute' => 'value'], ['allowed_field']); diff --git a/tests/Unit/Normalizer/NormalizerObjectMappingRegistryTest.php b/tests/Unit/Normalizer/NormalizerObjectMappingRegistryTest.php index af2bedd..e74c5ed 100644 --- a/tests/Unit/Normalizer/NormalizerObjectMappingRegistryTest.php +++ b/tests/Unit/Normalizer/NormalizerObjectMappingRegistryTest.php @@ -4,8 +4,8 @@ namespace Chubbyphp\Tests\Serialization\Unit\Normalizer; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Mapping\NormalizationObjectMappingInterface; use Chubbyphp\Serialization\Normalizer\NormalizerObjectMappingRegistry; use Chubbyphp\Serialization\SerializerLogicException; @@ -21,8 +21,6 @@ */ final class NormalizerObjectMappingRegistryTest extends TestCase { - use MockByCallsTrait; - public function testGetObjectMapping(): void { $object = $this->getObject(); @@ -50,9 +48,11 @@ public function testGetObjectMappingFromDoctrineProxy(): void { $object = $this->getProxyObject(); + $builder = new MockObjectBuilder(); + /** @var MockObject|NormalizationObjectMappingInterface $objectMapping */ - $objectMapping = $this->getMockByCalls(NormalizationObjectMappingInterface::class, [ - Call::create('getClass')->with()->willReturn(AbstractManyModel::class), + $objectMapping = $builder->create(NormalizationObjectMappingInterface::class, [ + new WithReturn('getClass', [], AbstractManyModel::class), ]); $registry = new NormalizerObjectMappingRegistry([$objectMapping]); @@ -66,9 +66,11 @@ private function getNormalizationObjectMapping(): NormalizationObjectMappingInte { $object = $this->getObject(); + $builder = new MockObjectBuilder(); + // @var NormalizationObjectMappingInterface|MockObject $objectMapping - return $this->getMockByCalls(NormalizationObjectMappingInterface::class, [ - Call::create('getClass')->with()->willReturn($object::class), + return $builder->create(NormalizationObjectMappingInterface::class, [ + new WithReturn('getClass', [], $object::class), ]); } @@ -94,17 +96,17 @@ public function setName(string $name): self private function getProxyObject(): object { return new class extends AbstractManyModel implements Proxy { - /** - * Initializes this proxy if its not yet initialized. - * - * Acts as a no-op if already initialized. - */ - public function __load(): void {} - - /** - * Returns whether this proxy is initialized or not. - */ - public function __isInitialized(): bool {} + private bool $initialized = false; + + public function __load(): void + { + $this->initialized = true; + } + + public function __isInitialized(): bool + { + return $this->initialized; + } }; } } diff --git a/tests/Unit/Normalizer/NormalizerTest.php b/tests/Unit/Normalizer/NormalizerTest.php index f106be2..9b100af 100644 --- a/tests/Unit/Normalizer/NormalizerTest.php +++ b/tests/Unit/Normalizer/NormalizerTest.php @@ -4,9 +4,10 @@ namespace Chubbyphp\Tests\Serialization\Unit\Normalizer; -use Chubbyphp\Mock\Argument\ArgumentInstanceOf; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithCallback; +use Chubbyphp\Mock\MockMethod\WithException; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Mapping\NormalizationFieldMappingInterface; use Chubbyphp\Serialization\Mapping\NormalizationLinkMappingInterface; use Chubbyphp\Serialization\Mapping\NormalizationObjectMappingInterface; @@ -27,103 +28,115 @@ */ final class NormalizerTest extends TestCase { - use MockByCallsTrait; - public function testNormalize(): void { + $builder = new MockObjectBuilder(); + $object = $this->getObject(); $object->setName('php'); $class = $object::class; /** @var MockObject|PolicyInterface $namePolicy */ - $namePolicy = $this->getMockByCalls(PolicyInterface::class, [ - Call::create('isCompliant') - ->with('name', $object, new ArgumentInstanceOf(NormalizerContextInterface::class)) - ->willReturn(true), + $namePolicy = $builder->create(PolicyInterface::class, [ + new WithCallback('isCompliant', static function ($name, $obj, $context) use ($object) { + self::assertSame('name', $name); + self::assertSame($object, $obj); + self::assertInstanceOf(NormalizerContextInterface::class, $context); + + return true; + }), ]); /** @var FieldNormalizerInterface|MockObject $nameFieldNormalizer */ - $nameFieldNormalizer = $this->getMockByCalls(FieldNormalizerInterface::class, [ - Call::create('normalizeField') - ->with( - 'name', - $object, - new ArgumentInstanceOf(NormalizerContextInterface::class), - new ArgumentInstanceOf(Normalizer::class) - ) - ->willReturn('php'), + $nameFieldNormalizer = $builder->create(FieldNormalizerInterface::class, [ + new WithCallback('normalizeField', static function ($field, $obj, $context, $normalizer) use ($object) { + self::assertSame('name', $field); + self::assertSame($object, $obj); + self::assertInstanceOf(NormalizerContextInterface::class, $context); + self::assertInstanceOf(Normalizer::class, $normalizer); + + return 'php'; + }), ]); /** @var MockObject|NormalizationFieldMappingInterface $nameFieldMapping */ - $nameFieldMapping = $this->getMockByCalls(NormalizationFieldMappingInterface::class, [ - Call::create('getName')->with()->willReturn('name'), - Call::create('getPolicy')->with()->willReturn($namePolicy), - Call::create('getFieldNormalizer')->with()->willReturn($nameFieldNormalizer), + $nameFieldMapping = $builder->create(NormalizationFieldMappingInterface::class, [ + new WithReturn('getName', [], 'name'), + new WithReturn('getPolicy', [], $namePolicy), + new WithReturn('getFieldNormalizer', [], $nameFieldNormalizer), ]); /** @var MockObject|PolicyInterface $embeddedNamePolicy */ - $embeddedNamePolicy = $this->getMockByCalls(PolicyInterface::class, [ - Call::create('isCompliant') - ->with('name', $object, new ArgumentInstanceOf(NormalizerContextInterface::class)) - ->willReturn(true), + $embeddedNamePolicy = $builder->create(PolicyInterface::class, [ + new WithCallback('isCompliant', static function ($name, $obj, $context) use ($object) { + self::assertSame('name', $name); + self::assertSame($object, $obj); + self::assertInstanceOf(NormalizerContextInterface::class, $context); + + return true; + }), ]); /** @var FieldNormalizerInterface|MockObject $embeddedNameFieldNormalizer */ - $embeddedNameFieldNormalizer = $this->getMockByCalls(FieldNormalizerInterface::class, [ - Call::create('normalizeField') - ->with( - 'name', - $object, - new ArgumentInstanceOf(NormalizerContextInterface::class), - new ArgumentInstanceOf(Normalizer::class) - ) - ->willReturn('php'), + $embeddedNameFieldNormalizer = $builder->create(FieldNormalizerInterface::class, [ + new WithCallback('normalizeField', static function ($field, $obj, $context, $normalizer) use ($object) { + self::assertSame('name', $field); + self::assertSame($object, $obj); + self::assertInstanceOf(NormalizerContextInterface::class, $context); + self::assertInstanceOf(Normalizer::class, $normalizer); + + return 'php'; + }), ]); /** @var MockObject|NormalizationFieldMappingInterface $embeddedNameFieldMapping */ - $embeddedNameFieldMapping = $this->getMockByCalls(NormalizationFieldMappingInterface::class, [ - Call::create('getName')->with()->willReturn('name'), - Call::create('getPolicy')->with()->willReturn($embeddedNamePolicy), - Call::create('getFieldNormalizer')->with()->willReturn($embeddedNameFieldNormalizer), + $embeddedNameFieldMapping = $builder->create(NormalizationFieldMappingInterface::class, [ + new WithReturn('getName', [], 'name'), + new WithReturn('getPolicy', [], $embeddedNamePolicy), + new WithReturn('getFieldNormalizer', [], $embeddedNameFieldNormalizer), ]); /** @var MockObject|PolicyInterface $nameLinkPolicy */ - $nameLinkPolicy = $this->getMockByCalls(PolicyInterface::class, [ - Call::create('isCompliant') - ->with('name', $object, new ArgumentInstanceOf(NormalizerContextInterface::class)) - ->willReturn(true), + $nameLinkPolicy = $builder->create(PolicyInterface::class, [ + new WithCallback('isCompliant', static function ($name, $obj, $context) use ($object) { + self::assertSame('name', $name); + self::assertSame($object, $obj); + self::assertInstanceOf(NormalizerContextInterface::class, $context); + + return true; + }), ]); /** @var LinkNormalizerInterface|MockObject $nameLinkNormalizer */ - $nameLinkNormalizer = $this->getMockByCalls(LinkNormalizerInterface::class, [ - Call::create('normalizeLink') - ->with( - '', - $object, - new ArgumentInstanceOf(NormalizerContextInterface::class) - ) - ->willReturn(['href' => '/api/model/id1']), + $nameLinkNormalizer = $builder->create(LinkNormalizerInterface::class, [ + new WithCallback('normalizeLink', static function ($linkName, $obj, $context) use ($object) { + self::assertSame('', $linkName); + self::assertSame($object, $obj); + self::assertInstanceOf(NormalizerContextInterface::class, $context); + + return ['href' => '/api/model/id1']; + }), ]); /** @var MockObject|NormalizationLinkMappingInterface $nameLinkMapping */ - $nameLinkMapping = $this->getMockByCalls(NormalizationLinkMappingInterface::class, [ - Call::create('getName')->with()->willReturn('name'), - Call::create('getPolicy')->with()->willReturn($nameLinkPolicy), - Call::create('getLinkNormalizer')->with()->willReturn($nameLinkNormalizer), + $nameLinkMapping = $builder->create(NormalizationLinkMappingInterface::class, [ + new WithReturn('getName', [], 'name'), + new WithReturn('getPolicy', [], $nameLinkPolicy), + new WithReturn('getLinkNormalizer', [], $nameLinkNormalizer), ]); /** @var MockObject|NormalizationObjectMappingInterface $objectMapping */ - $objectMapping = $this->getMockByCalls(NormalizationObjectMappingInterface::class, [ - Call::create('getNormalizationFieldMappings')->with('')->willReturn([$nameFieldMapping]), - Call::create('getNormalizationEmbeddedFieldMappings')->with('')->willReturn([$embeddedNameFieldMapping]), - Call::create('getNormalizationLinkMappings')->with('')->willReturn([$nameLinkMapping]), - Call::create('getNormalizationType')->with()->willReturn('object'), + $objectMapping = $builder->create(NormalizationObjectMappingInterface::class, [ + new WithReturn('getNormalizationFieldMappings', [''], [$nameFieldMapping]), + new WithReturn('getNormalizationEmbeddedFieldMappings', [''], [$embeddedNameFieldMapping]), + new WithReturn('getNormalizationLinkMappings', [''], [$nameLinkMapping]), + new WithReturn('getNormalizationType', [], 'object'), ]); /** @var MockObject|NormalizerObjectMappingRegistryInterface $objectMappingRegistry */ - $objectMappingRegistry = $this->getMockByCalls(NormalizerObjectMappingRegistryInterface::class, [ - Call::create('getObjectMapping')->with($class)->willReturn($objectMapping), + $objectMappingRegistry = $builder->create(NormalizerObjectMappingRegistryInterface::class, [ + new WithReturn('getObjectMapping', [$class], $objectMapping), ]); $normalizer = new Normalizer($objectMappingRegistry); @@ -147,61 +160,75 @@ public function testNormalize(): void public function testNormalizeWithNonCompliantPolicy(): void { + $builder = new MockObjectBuilder(); + $object = $this->getObject(); $object->setName('php'); $class = $object::class; /** @var MockObject|PolicyInterface $namePolicy */ - $namePolicy = $this->getMockByCalls(PolicyInterface::class, [ - Call::create('isCompliant') - ->with('name', $object, new ArgumentInstanceOf(NormalizerContextInterface::class)) - ->willReturn(false), + $namePolicy = $builder->create(PolicyInterface::class, [ + new WithCallback('isCompliant', static function ($name, $obj, $context) use ($object) { + self::assertSame('name', $name); + self::assertSame($object, $obj); + self::assertInstanceOf(NormalizerContextInterface::class, $context); + + return false; + }), ]); /** @var MockObject|NormalizationFieldMappingInterface $nameFieldMapping */ - $nameFieldMapping = $this->getMockByCalls(NormalizationFieldMappingInterface::class, [ - Call::create('getName')->with()->willReturn('name'), - Call::create('getPolicy')->with()->willReturn($namePolicy), + $nameFieldMapping = $builder->create(NormalizationFieldMappingInterface::class, [ + new WithReturn('getName', [], 'name'), + new WithReturn('getPolicy', [], $namePolicy), ]); /** @var MockObject|PolicyInterface $embeddedNamePolicy */ - $embeddedNamePolicy = $this->getMockByCalls(PolicyInterface::class, [ - Call::create('isCompliant') - ->with('name', $object, new ArgumentInstanceOf(NormalizerContextInterface::class)) - ->willReturn(false), + $embeddedNamePolicy = $builder->create(PolicyInterface::class, [ + new WithCallback('isCompliant', static function ($name, $obj, $context) use ($object) { + self::assertSame('name', $name); + self::assertSame($object, $obj); + self::assertInstanceOf(NormalizerContextInterface::class, $context); + + return false; + }), ]); /** @var MockObject|NormalizationFieldMappingInterface $embeddedNameFieldMapping */ - $embeddedNameFieldMapping = $this->getMockByCalls(NormalizationFieldMappingInterface::class, [ - Call::create('getName')->with()->willReturn('name'), - Call::create('getPolicy')->with()->willReturn($embeddedNamePolicy), + $embeddedNameFieldMapping = $builder->create(NormalizationFieldMappingInterface::class, [ + new WithReturn('getName', [], 'name'), + new WithReturn('getPolicy', [], $embeddedNamePolicy), ]); /** @var MockObject|PolicyInterface $nameLinkPolicy */ - $nameLinkPolicy = $this->getMockByCalls(PolicyInterface::class, [ - Call::create('isCompliant') - ->with('name', $object, new ArgumentInstanceOf(NormalizerContextInterface::class)) - ->willReturn(false), + $nameLinkPolicy = $builder->create(PolicyInterface::class, [ + new WithCallback('isCompliant', static function ($name, $obj, $context) use ($object) { + self::assertSame('name', $name); + self::assertSame($object, $obj); + self::assertInstanceOf(NormalizerContextInterface::class, $context); + + return false; + }), ]); /** @var MockObject|NormalizationLinkMappingInterface $nameLinkMapping */ - $nameLinkMapping = $this->getMockByCalls(NormalizationLinkMappingInterface::class, [ - Call::create('getName')->with()->willReturn('name'), - Call::create('getPolicy')->with()->willReturn($nameLinkPolicy), + $nameLinkMapping = $builder->create(NormalizationLinkMappingInterface::class, [ + new WithReturn('getName', [], 'name'), + new WithReturn('getPolicy', [], $nameLinkPolicy), ]); /** @var MockObject|NormalizationObjectMappingInterface $objectMapping */ - $objectMapping = $this->getMockByCalls(NormalizationObjectMappingInterface::class, [ - Call::create('getNormalizationFieldMappings')->with('')->willReturn([$nameFieldMapping]), - Call::create('getNormalizationEmbeddedFieldMappings')->with('')->willReturn([$embeddedNameFieldMapping]), - Call::create('getNormalizationLinkMappings')->with('')->willReturn([$nameLinkMapping]), - Call::create('getNormalizationType')->with()->willReturn('object'), + $objectMapping = $builder->create(NormalizationObjectMappingInterface::class, [ + new WithReturn('getNormalizationFieldMappings', [''], [$nameFieldMapping]), + new WithReturn('getNormalizationEmbeddedFieldMappings', [''], [$embeddedNameFieldMapping]), + new WithReturn('getNormalizationLinkMappings', [''], [$nameLinkMapping]), + new WithReturn('getNormalizationType', [], 'object'), ]); /** @var MockObject|NormalizerObjectMappingRegistryInterface $objectMappingRegistry */ - $objectMappingRegistry = $this->getMockByCalls(NormalizerObjectMappingRegistryInterface::class, [ - Call::create('getObjectMapping')->with($class)->willReturn($objectMapping), + $objectMappingRegistry = $builder->create(NormalizerObjectMappingRegistryInterface::class, [ + new WithReturn('getObjectMapping', [$class], $objectMapping), ]); $normalizer = new Normalizer($objectMappingRegistry); @@ -221,9 +248,11 @@ public function testNormalizeMissingMapping(): void $exception = SerializerLogicException::createMissingMapping(\stdClass::class); + $builder = new MockObjectBuilder(); + /** @var MockObject|NormalizerObjectMappingRegistryInterface $objectMappingRegistry */ - $objectMappingRegistry = $this->getMockByCalls(NormalizerObjectMappingRegistryInterface::class, [ - Call::create('getObjectMapping')->with(\stdClass::class)->willThrowException($exception), + $objectMappingRegistry = $builder->create(NormalizerObjectMappingRegistryInterface::class, [ + new WithException('getObjectMapping', [\stdClass::class], $exception), ]); $normalizer = new Normalizer($objectMappingRegistry); diff --git a/tests/Unit/Normalizer/Relation/EmbedManyFieldNormalizerTest.php b/tests/Unit/Normalizer/Relation/EmbedManyFieldNormalizerTest.php index 4a0b49e..b6316ee 100644 --- a/tests/Unit/Normalizer/Relation/EmbedManyFieldNormalizerTest.php +++ b/tests/Unit/Normalizer/Relation/EmbedManyFieldNormalizerTest.php @@ -4,8 +4,8 @@ namespace Chubbyphp\Tests\Serialization\Unit\Normalizer\Relation; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Accessor\AccessorInterface; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; use Chubbyphp\Serialization\Normalizer\NormalizerInterface; @@ -21,18 +21,18 @@ */ final class EmbedManyFieldNormalizerTest extends TestCase { - use MockByCallsTrait; - public function testNormalizeMissingNormalizer(): void { $this->expectException(SerializerLogicException::class); $this->expectExceptionMessage('There is no normalizer at path: "children"'); + $builder = new MockObjectBuilder(); + /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class); + $accessor = $builder->create(AccessorInterface::class, []); /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); $fieldNormalizer = new EmbedManyFieldNormalizer($accessor); @@ -50,34 +50,27 @@ public function testNormalize(): void $parent = $this->getParent(); $parent->setChildren([$child1, $child2]); + $builder = new MockObjectBuilder(); + /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($parent)->willReturn($parent->getChildren()), + $accessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$parent], $parent->getChildren()), ]); /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); /** @var MockObject|NormalizerInterface $normalizer */ - $normalizer = $this->getMockByCalls(NormalizerInterface::class, [ - Call::create('normalize') - ->with($child1, $context, 'children[0]') - ->willReturn(['name' => $child1->getName()]), - Call::create('normalize') - ->with($child2, $context, 'children[1]') - ->willReturn(['name' => $child2->getName()]), + $normalizer = $builder->create(NormalizerInterface::class, [ + new WithReturn('normalize', [$child1, $context, 'children[0]'], ['name' => $child1->getName()]), + new WithReturn('normalize', [$child2, $context, 'children[1]'], ['name' => $child2->getName()]), ]); $fieldNormalizer = new EmbedManyFieldNormalizer($accessor); self::assertSame( [['name' => 'name1'], ['name' => 'name2']], - $fieldNormalizer->normalizeField( - 'children', - $parent, - $context, - $normalizer - ) + $fieldNormalizer->normalizeField('children', $parent, $context, $normalizer) ); } @@ -86,27 +79,24 @@ public function testNormalizeEmpty(): void $parent = $this->getParent(); $parent->setChildren([]); + $builder = new MockObjectBuilder(); + /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($parent)->willReturn($parent->getChildren()), + $accessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$parent], $parent->getChildren()), ]); /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); /** @var MockObject|NormalizerInterface $normalizer */ - $normalizer = $this->getMockByCalls(NormalizerInterface::class); + $normalizer = $builder->create(NormalizerInterface::class, []); $fieldNormalizer = new EmbedManyFieldNormalizer($accessor); self::assertSame( [], - $fieldNormalizer->normalizeField( - 'children', - $parent, - $context, - $normalizer - ) + $fieldNormalizer->normalizeField('children', $parent, $context, $normalizer) ); } @@ -114,26 +104,23 @@ public function testNormalizeNull(): void { $parent = $this->getParent(); + $builder = new MockObjectBuilder(); + /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($parent)->willReturn($parent->getChildren()), + $accessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$parent], $parent->getChildren()), ]); /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); /** @var MockObject|NormalizerInterface $normalizer */ - $normalizer = $this->getMockByCalls(NormalizerInterface::class); + $normalizer = $builder->create(NormalizerInterface::class, []); $fieldNormalizer = new EmbedManyFieldNormalizer($accessor); self::assertNull( - $fieldNormalizer->normalizeField( - 'children', - $parent, - $context, - $normalizer - ) + $fieldNormalizer->normalizeField('children', $parent, $context, $normalizer) ); } diff --git a/tests/Unit/Normalizer/Relation/EmbedOneFieldNormalizerTest.php b/tests/Unit/Normalizer/Relation/EmbedOneFieldNormalizerTest.php index 6b9f279..2389d92 100644 --- a/tests/Unit/Normalizer/Relation/EmbedOneFieldNormalizerTest.php +++ b/tests/Unit/Normalizer/Relation/EmbedOneFieldNormalizerTest.php @@ -2,10 +2,10 @@ declare(strict_types=1); -namespace Chubbyphp\Tests\Serialization\Unit\Normalizer; +namespace Chubbyphp\Tests\Serialization\Unit\Normalizer\Relation; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Accessor\AccessorInterface; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; use Chubbyphp\Serialization\Normalizer\NormalizerInterface; @@ -21,8 +21,6 @@ */ final class EmbedOneFieldNormalizerTest extends TestCase { - use MockByCallsTrait; - public function testNormalizeFieldWithMissingNormalizer(): void { $this->expectException(SerializerLogicException::class); @@ -30,11 +28,13 @@ public function testNormalizeFieldWithMissingNormalizer(): void $object = $this->getObject(); + $builder = new MockObjectBuilder(); + /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class); + $accessor = $builder->create(AccessorInterface::class, []); /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); $fieldNormalizer = new EmbedOneFieldNormalizer($accessor); @@ -45,25 +45,22 @@ public function testNormalizeFieldWithNull(): void { $object = $this->getObject(); + $builder = new MockObjectBuilder(); + /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($object)->willReturn(null), + $accessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$object], null), ]); /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); /** @var MockObject|NormalizerInterface $normalizer */ - $normalizer = $this->getMockByCalls(NormalizerInterface::class); + $normalizer = $builder->create(NormalizerInterface::class, []); $fieldNormalizer = new EmbedOneFieldNormalizer($accessor); - $data = $fieldNormalizer->normalizeField( - 'relation', - $object, - $context, - $normalizer - ); + $data = $fieldNormalizer->normalizeField('relation', $object, $context, $normalizer); self::assertNull($data); } @@ -76,29 +73,24 @@ public function testNormalizeFieldWithObject(): void $object = $this->getObject(); $object->setRelation($relation); + $builder = new MockObjectBuilder(); + /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($object)->willReturn($relation), + $accessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$object], $relation), ]); /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); /** @var MockObject|NormalizerInterface $normalizer */ - $normalizer = $this->getMockByCalls(NormalizerInterface::class, [ - Call::create('normalize') - ->with($relation, $context, 'relation') - ->willReturn(['name' => $relation->getName()]), + $normalizer = $builder->create(NormalizerInterface::class, [ + new WithReturn('normalize', [$relation, $context, 'relation'], ['name' => $relation->getName()]), ]); $fieldNormalizer = new EmbedOneFieldNormalizer($accessor); - $data = $fieldNormalizer->normalizeField( - 'relation', - $object, - $context, - $normalizer - ); + $data = $fieldNormalizer->normalizeField('relation', $object, $context, $normalizer); self::assertSame(['name' => 'php'], $data); } diff --git a/tests/Unit/Normalizer/Relation/ReferenceManyFieldNormalizerTest.php b/tests/Unit/Normalizer/Relation/ReferenceManyFieldNormalizerTest.php index 06a35c2..0a37d8f 100644 --- a/tests/Unit/Normalizer/Relation/ReferenceManyFieldNormalizerTest.php +++ b/tests/Unit/Normalizer/Relation/ReferenceManyFieldNormalizerTest.php @@ -4,8 +4,8 @@ namespace Chubbyphp\Tests\Serialization\Unit\Normalizer\Relation; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Accessor\AccessorInterface; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; use Chubbyphp\Serialization\Normalizer\Relation\ReferenceManyFieldNormalizer; @@ -19,8 +19,6 @@ */ final class ReferenceManyFieldNormalizerTest extends TestCase { - use MockByCallsTrait; - public function testNormalize(): void { $child1 = $this->getChild('id1'); @@ -29,29 +27,27 @@ public function testNormalize(): void $parent = $this->getParent(); $parent->setChildren([$child1, $child2]); + $builder = new MockObjectBuilder(); + /** @var AccessorInterface|MockObject $identifierAccessor */ - $identifierAccessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($child1)->willReturn($child1->getId()), - Call::create('getValue')->with($child2)->willReturn($child2->getId()), + $identifierAccessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$child1], $child1->getId()), + new WithReturn('getValue', [$child2], $child2->getId()), ]); /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($parent)->willReturn($parent->getChildren()), + $accessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$parent], $parent->getChildren()), ]); /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); $fieldNormalizer = new ReferenceManyFieldNormalizer($identifierAccessor, $accessor); self::assertSame( ['id1', 'id2'], - $fieldNormalizer->normalizeField( - 'children', - $parent, - $context - ) + $fieldNormalizer->normalizeField('children', $parent, $context) ); } @@ -60,26 +56,24 @@ public function testNormalizeEmpty(): void $parent = $this->getParent(); $parent->setChildren([]); + $builder = new MockObjectBuilder(); + /** @var AccessorInterface|MockObject $identifierAccessor */ - $identifierAccessor = $this->getMockByCalls(AccessorInterface::class); + $identifierAccessor = $builder->create(AccessorInterface::class, []); /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($parent)->willReturn($parent->getChildren()), + $accessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$parent], $parent->getChildren()), ]); /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); $fieldNormalizer = new ReferenceManyFieldNormalizer($identifierAccessor, $accessor); self::assertSame( [], - $fieldNormalizer->normalizeField( - 'children', - $parent, - $context - ) + $fieldNormalizer->normalizeField('children', $parent, $context) ); } @@ -87,25 +81,23 @@ public function testNormalizeNull(): void { $parent = $this->getParent(); + $builder = new MockObjectBuilder(); + /** @var AccessorInterface|MockObject $identifierAccessor */ - $identifierAccessor = $this->getMockByCalls(AccessorInterface::class); + $identifierAccessor = $builder->create(AccessorInterface::class, []); /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($parent)->willReturn($parent->getChildren()), + $accessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$parent], $parent->getChildren()), ]); /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); $fieldNormalizer = new ReferenceManyFieldNormalizer($identifierAccessor, $accessor); self::assertNull( - $fieldNormalizer->normalizeField( - 'children', - $parent, - $context - ) + $fieldNormalizer->normalizeField('children', $parent, $context) ); } diff --git a/tests/Unit/Normalizer/Relation/ReferenceOneFieldNormalizerTest.php b/tests/Unit/Normalizer/Relation/ReferenceOneFieldNormalizerTest.php index c27df73..8922888 100644 --- a/tests/Unit/Normalizer/Relation/ReferenceOneFieldNormalizerTest.php +++ b/tests/Unit/Normalizer/Relation/ReferenceOneFieldNormalizerTest.php @@ -4,8 +4,8 @@ namespace Chubbyphp\Tests\Serialization\Unit\Normalizer; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Accessor\AccessorInterface; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; use Chubbyphp\Serialization\Normalizer\Relation\ReferenceOneFieldNormalizer; @@ -19,30 +19,26 @@ */ final class ReferenceOneFieldNormalizerTest extends TestCase { - use MockByCallsTrait; - public function testNormalizeFieldWithNull(): void { $object = new \stdClass(); + $builder = new MockObjectBuilder(); + /** @var AccessorInterface|MockObject $identifierAccessor */ - $identifierAccessor = $this->getMockByCalls(AccessorInterface::class); + $identifierAccessor = $builder->create(AccessorInterface::class, []); /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($object)->willReturn(null), + $accessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$object], null), ]); /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); $fieldNormalizer = new ReferenceOneFieldNormalizer($identifierAccessor, $accessor); - $data = $fieldNormalizer->normalizeField( - 'relation', - $object, - $context - ); + $data = $fieldNormalizer->normalizeField('relation', $object, $context); self::assertNull($data); } @@ -53,26 +49,24 @@ public function testNormalizeFieldWithObject(): void $object = new \stdClass(); + $builder = new MockObjectBuilder(); + /** @var AccessorInterface|MockObject $identifierAccessor */ - $identifierAccessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($relation)->willReturn('id1'), + $identifierAccessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$relation], 'id1'), ]); /** @var AccessorInterface|MockObject $accessor */ - $accessor = $this->getMockByCalls(AccessorInterface::class, [ - Call::create('getValue')->with($object)->willReturn($relation), + $accessor = $builder->create(AccessorInterface::class, [ + new WithReturn('getValue', [$object], $relation), ]); /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $context = $builder->create(NormalizerContextInterface::class, []); $fieldNormalizer = new ReferenceOneFieldNormalizer($identifierAccessor, $accessor); - $data = $fieldNormalizer->normalizeField( - 'relation', - $object, - $context - ); + $data = $fieldNormalizer->normalizeField('relation', $object, $context); self::assertSame('id1', $data); } diff --git a/tests/Unit/Policy/AndPolicyTest.php b/tests/Unit/Policy/AndPolicyTest.php index 62b06ae..82ef53c 100644 --- a/tests/Unit/Policy/AndPolicyTest.php +++ b/tests/Unit/Policy/AndPolicyTest.php @@ -4,12 +4,11 @@ namespace Chubbyphp\Tests\Serialization\Unit\Policy; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; use Chubbyphp\Serialization\Policy\AndPolicy; use Chubbyphp\Serialization\Policy\PolicyInterface; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** @@ -19,25 +18,25 @@ */ final class AndPolicyTest extends TestCase { - use MockByCallsTrait; - public function testIsCompliantIncludingPathReturnsTrueWithMultipleCompliantPolicies(): void { $object = new \stdClass(); $path = ''; - /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $builder = new MockObjectBuilder(); + + /** @var NormalizerContextInterface $context */ + $context = $builder->create(NormalizerContextInterface::class, []); - /** @var MockObject|PolicyInterface $compliantPolicy1 */ - $compliantPolicy1 = $this->getMockByCalls(PolicyInterface::class, [ - Call::create('isCompliant')->with($path, $object, $context)->willReturn(true), + /** @var PolicyInterface $compliantPolicy1 */ + $compliantPolicy1 = $builder->create(PolicyInterface::class, [ + new WithReturn('isCompliant', [$path, $object, $context], true), ]); - /** @var MockObject|PolicyInterface $compliantPolicy2 */ - $compliantPolicy2 = $this->getMockByCalls(PolicyInterface::class, [ - Call::create('isCompliant')->with($path, $object, $context)->willReturn(true), + /** @var PolicyInterface $compliantPolicy2 */ + $compliantPolicy2 = $builder->create(PolicyInterface::class, [ + new WithReturn('isCompliant', [$path, $object, $context], true), ]); $policy = new AndPolicy([$compliantPolicy1, $compliantPolicy2]); @@ -51,21 +50,23 @@ public function testIsCompliantIncludingPathReturnsFalseWithNonCompliantIncludin $path = ''; - /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $builder = new MockObjectBuilder(); + + /** @var NormalizerContextInterface $context */ + $context = $builder->create(NormalizerContextInterface::class, []); - /** @var MockObject|PolicyInterface $compliantPolicy */ - $compliantPolicy = $this->getMockByCalls(PolicyInterface::class, [ - Call::create('isCompliant')->with($path, $object, $context)->willReturn(true), + /** @var PolicyInterface $compliantPolicy */ + $compliantPolicy = $builder->create(PolicyInterface::class, [ + new WithReturn('isCompliant', [$path, $object, $context], true), ]); - /** @var MockObject|PolicyInterface $compliantPolicy2 */ - $nonCompliantPolicy = $this->getMockByCalls(PolicyInterface::class, [ - Call::create('isCompliant')->with($path, $object, $context)->willReturn(false), + /** @var PolicyInterface $nonCompliantPolicy */ + $nonCompliantPolicy = $builder->create(PolicyInterface::class, [ + new WithReturn('isCompliant', [$path, $object, $context], false), ]); - /** @var MockObject|PolicyInterface $notExpectedToBeCalledPolicy */ - $notExpectedToBeCalledPolicy = $this->getMockByCalls(PolicyInterface::class); + /** @var PolicyInterface $notExpectedToBeCalledPolicy */ + $notExpectedToBeCalledPolicy = $builder->create(PolicyInterface::class, []); $policy = new AndPolicy([$compliantPolicy, $nonCompliantPolicy, $notExpectedToBeCalledPolicy]); diff --git a/tests/Unit/Policy/CallbackPolicyTest.php b/tests/Unit/Policy/CallbackPolicyTest.php index 19eab94..416998b 100644 --- a/tests/Unit/Policy/CallbackPolicyTest.php +++ b/tests/Unit/Policy/CallbackPolicyTest.php @@ -4,10 +4,9 @@ namespace Chubbyphp\Tests\Serialization\Unit\Policy; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; use Chubbyphp\Serialization\Policy\CallbackPolicy; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** @@ -17,16 +16,16 @@ */ final class CallbackPolicyTest extends TestCase { - use MockByCallsTrait; - public function testIsCompliantIncludingPathReturnsTrueIfCallbackReturnsTrue(): void { $object = new \stdClass(); $path = ''; - /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class, []); + $builder = new MockObjectBuilder(); + + /** @var NormalizerContextInterface $context */ + $context = $builder->create(NormalizerContextInterface::class, []); $policy = new CallbackPolicy( static function ($pathParameter, $objectParameter, $contextParameter) use ($path, $object, $context) { @@ -47,8 +46,10 @@ public function testIsCompliantIncludingPathReturnsFalseIfCallbackReturnsFalse() $path = ''; - /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class, []); + $builder = new MockObjectBuilder(); + + /** @var NormalizerContextInterface $context */ + $context = $builder->create(NormalizerContextInterface::class, []); $policy = new CallbackPolicy( static function ($pathParameter, $objectParameter, $contextParameter) use ($path, $object, $context) { diff --git a/tests/Unit/Policy/GroupPolicyTest.php b/tests/Unit/Policy/GroupPolicyTest.php index a18f5a3..072cd00 100644 --- a/tests/Unit/Policy/GroupPolicyTest.php +++ b/tests/Unit/Policy/GroupPolicyTest.php @@ -4,11 +4,10 @@ namespace Chubbyphp\Tests\Serialization\Unit\Policy; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; use Chubbyphp\Serialization\Policy\GroupPolicy; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** @@ -18,16 +17,16 @@ */ final class GroupPolicyTest extends TestCase { - use MockByCallsTrait; - public function testIsCompliantIncludingPathReturnsTrueIfNoGroupsAreSet(): void { $object = new \stdClass(); $path = ''; - /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $builder = new MockObjectBuilder(); + + /** @var NormalizerContextInterface $context */ + $context = $builder->create(NormalizerContextInterface::class, []); $policy = new GroupPolicy([]); @@ -40,11 +39,11 @@ public function testIsCompliantIncludingPathReturnsTrueWithDefaultValues(): void $path = ''; - /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class, [ - Call::create('getAttribute') - ->with(GroupPolicy::ATTRIBUTE_GROUPS, [GroupPolicy::GROUP_DEFAULT]) - ->willReturn([GroupPolicy::GROUP_DEFAULT]), + $builder = new MockObjectBuilder(); + + /** @var NormalizerContextInterface $context */ + $context = $builder->create(NormalizerContextInterface::class, [ + new WithReturn('getAttribute', [GroupPolicy::ATTRIBUTE_GROUPS, [GroupPolicy::GROUP_DEFAULT]], [GroupPolicy::GROUP_DEFAULT]), ]); $policy = new GroupPolicy(); @@ -58,11 +57,11 @@ public function testIsCompliantIncludingPathReturnsTrueIfOneGroupMatches(): void $path = ''; - /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class, [ - Call::create('getAttribute') - ->with(GroupPolicy::ATTRIBUTE_GROUPS, [GroupPolicy::GROUP_DEFAULT]) - ->willReturn(['group2']), + $builder = new MockObjectBuilder(); + + /** @var NormalizerContextInterface $context */ + $context = $builder->create(NormalizerContextInterface::class, [ + new WithReturn('getAttribute', [GroupPolicy::ATTRIBUTE_GROUPS, [GroupPolicy::GROUP_DEFAULT]], ['group2']), ]); $policy = new GroupPolicy(['group1', 'group2']); @@ -76,11 +75,11 @@ public function testIsCompliantIncludingPathReturnsFalseIfNoGroupsAreSetInContex $path = ''; - /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class, [ - Call::create('getAttribute') - ->with(GroupPolicy::ATTRIBUTE_GROUPS, [GroupPolicy::GROUP_DEFAULT]) - ->willReturn([]), + $builder = new MockObjectBuilder(); + + /** @var NormalizerContextInterface $context */ + $context = $builder->create(NormalizerContextInterface::class, [ + new WithReturn('getAttribute', [GroupPolicy::ATTRIBUTE_GROUPS, [GroupPolicy::GROUP_DEFAULT]], []), ]); $policy = new GroupPolicy(['group1', 'group2']); @@ -94,11 +93,11 @@ public function testIsCompliantIncludingPathReturnsFalseIfNoGroupsMatch(): void $path = ''; - /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class, [ - Call::create('getAttribute') - ->with(GroupPolicy::ATTRIBUTE_GROUPS, [GroupPolicy::GROUP_DEFAULT]) - ->willReturn(['unknownGroup']), + $builder = new MockObjectBuilder(); + + /** @var NormalizerContextInterface $context */ + $context = $builder->create(NormalizerContextInterface::class, [ + new WithReturn('getAttribute', [GroupPolicy::ATTRIBUTE_GROUPS, [GroupPolicy::GROUP_DEFAULT]], ['unknownGroup']), ]); $policy = new GroupPolicy(['group1', 'group2']); diff --git a/tests/Unit/Policy/NotPolicyTest.php b/tests/Unit/Policy/NotPolicyTest.php index ac445c8..a41d83a 100644 --- a/tests/Unit/Policy/NotPolicyTest.php +++ b/tests/Unit/Policy/NotPolicyTest.php @@ -4,12 +4,11 @@ namespace Chubbyphp\Tests\Serialization\Unit\Policy; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; use Chubbyphp\Serialization\Policy\NotPolicy; use Chubbyphp\Serialization\Policy\PolicyInterface; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** @@ -19,20 +18,20 @@ */ final class NotPolicyTest extends TestCase { - use MockByCallsTrait; - public function testIsCompliantIncludingPathReturnsTrueIfGivenPolicyIncludingPathReturnsFalse(): void { $object = new \stdClass(); $path = ''; - /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class, []); + $builder = new MockObjectBuilder(); + + /** @var NormalizerContextInterface $context */ + $context = $builder->create(NormalizerContextInterface::class, []); - /** @var MockObject|PolicyInterface $nonCompliantPolicy */ - $nonCompliantPolicy = $this->getMockByCalls(PolicyInterface::class, [ - Call::create('isCompliant')->with($path, $object, $context)->willReturn(false), + /** @var PolicyInterface $nonCompliantPolicy */ + $nonCompliantPolicy = $builder->create(PolicyInterface::class, [ + new WithReturn('isCompliant', [$path, $object, $context], false), ]); $policy = new NotPolicy($nonCompliantPolicy); @@ -46,12 +45,14 @@ public function testIsCompliantIncludingPathReturnsFalseIfGivenPolicyIncludingPa $path = ''; - /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class, []); + $builder = new MockObjectBuilder(); + + /** @var NormalizerContextInterface $context */ + $context = $builder->create(NormalizerContextInterface::class, []); - /** @var MockObject|PolicyInterface $nonCompliantPolicy */ - $nonCompliantPolicy = $this->getMockByCalls(PolicyInterface::class, [ - Call::create('isCompliant')->with($path, $object, $context)->willReturn(true), + /** @var PolicyInterface $nonCompliantPolicy */ + $nonCompliantPolicy = $builder->create(PolicyInterface::class, [ + new WithReturn('isCompliant', [$path, $object, $context], true), ]); $policy = new NotPolicy($nonCompliantPolicy); diff --git a/tests/Unit/Policy/NullPolicyTest.php b/tests/Unit/Policy/NullPolicyTest.php index 0f467a4..01ed91d 100644 --- a/tests/Unit/Policy/NullPolicyTest.php +++ b/tests/Unit/Policy/NullPolicyTest.php @@ -4,10 +4,9 @@ namespace Chubbyphp\Tests\Serialization\Unit\Policy; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; use Chubbyphp\Serialization\Policy\NullPolicy; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** @@ -17,16 +16,16 @@ */ final class NullPolicyTest extends TestCase { - use MockByCallsTrait; - public function testIsCompliantIncludingReturnsTrue(): void { $object = new \stdClass(); $path = ''; - /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $builder = new MockObjectBuilder(); + + /** @var NormalizerContextInterface $context */ + $context = $builder->create(NormalizerContextInterface::class, []); $policy = new NullPolicy(); diff --git a/tests/Unit/Policy/OrPolicyTest.php b/tests/Unit/Policy/OrPolicyTest.php index d1c00dd..82407f2 100644 --- a/tests/Unit/Policy/OrPolicyTest.php +++ b/tests/Unit/Policy/OrPolicyTest.php @@ -4,12 +4,11 @@ namespace Chubbyphp\Tests\Serialization\Unit\Policy; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; use Chubbyphp\Serialization\Policy\OrPolicy; use Chubbyphp\Serialization\Policy\PolicyInterface; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** @@ -19,29 +18,29 @@ */ final class OrPolicyTest extends TestCase { - use MockByCallsTrait; - public function testIsCompliantIncludingPathReturnsTrueIfOnePolicyIncludingPathReturnsTrue(): void { $object = new \stdClass(); $path = ''; - /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class, []); + $builder = new MockObjectBuilder(); + + /** @var NormalizerContextInterface $context */ + $context = $builder->create(NormalizerContextInterface::class, []); - /** @var MockObject|PolicyInterface $nonCompliantPolicy */ - $nonCompliantPolicy = $this->getMockByCalls(PolicyInterface::class, [ - Call::create('isCompliant')->with($path, $object, $context)->willReturn(false), + /** @var PolicyInterface $nonCompliantPolicy */ + $nonCompliantPolicy = $builder->create(PolicyInterface::class, [ + new WithReturn('isCompliant', [$path, $object, $context], false), ]); - /** @var MockObject|PolicyInterface $nonCompliantPcompliantPolicyolicy */ - $compliantPolicy = $this->getMockByCalls(PolicyInterface::class, [ - Call::create('isCompliant')->with($path, $object, $context)->willReturn(true), + /** @var PolicyInterface $compliantPolicy */ + $compliantPolicy = $builder->create(PolicyInterface::class, [ + new WithReturn('isCompliant', [$path, $object, $context], true), ]); - /** @var MockObject|PolicyInterface $notToBeCalledPolicy */ - $notToBeCalledPolicy = $this->getMockByCalls(PolicyInterface::class, []); + /** @var PolicyInterface $notToBeCalledPolicy */ + $notToBeCalledPolicy = $builder->create(PolicyInterface::class, []); $policy = new OrPolicy([$nonCompliantPolicy, $compliantPolicy, $notToBeCalledPolicy]); @@ -54,17 +53,19 @@ public function testIsCompliantIncludingReturnsFalseIfAllPoliciesReturnFalse(): $path = ''; - /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class, []); + $builder = new MockObjectBuilder(); + + /** @var NormalizerContextInterface $context */ + $context = $builder->create(NormalizerContextInterface::class, []); - /** @var MockObject|PolicyInterface $nonCompliantPolicy1 */ - $nonCompliantPolicy1 = $this->getMockByCalls(PolicyInterface::class, [ - Call::create('isCompliant')->with($path, $object, $context)->willReturn(false), + /** @var PolicyInterface $nonCompliantPolicy1 */ + $nonCompliantPolicy1 = $builder->create(PolicyInterface::class, [ + new WithReturn('isCompliant', [$path, $object, $context], false), ]); - /** @var MockObject|PolicyInterface $nonCompliantPolicy2 */ - $nonCompliantPolicy2 = $this->getMockByCalls(PolicyInterface::class, [ - Call::create('isCompliant')->with($path, $object, $context)->willReturn(false), + /** @var PolicyInterface $nonCompliantPolicy2 */ + $nonCompliantPolicy2 = $builder->create(PolicyInterface::class, [ + new WithReturn('isCompliant', [$path, $object, $context], false), ]); $policy = new OrPolicy([$nonCompliantPolicy1, $nonCompliantPolicy2]); diff --git a/tests/Unit/SerializerTest.php b/tests/Unit/SerializerTest.php index 32589ad..0889d48 100644 --- a/tests/Unit/SerializerTest.php +++ b/tests/Unit/SerializerTest.php @@ -5,12 +5,11 @@ namespace Chubbyphp\Tests\Serialization\Unit; use Chubbyphp\DecodeEncode\Encoder\EncoderInterface; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; use Chubbyphp\Serialization\Normalizer\NormalizerInterface; use Chubbyphp\Serialization\Serializer; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; /** @@ -20,24 +19,24 @@ */ final class SerializerTest extends TestCase { - use MockByCallsTrait; - public function testSerialize(): void { $object = new \stdClass(); $object->name = 'Name'; - /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $builder = new MockObjectBuilder(); + + /** @var NormalizerContextInterface $context */ + $context = $builder->create(NormalizerContextInterface::class, []); - /** @var MockObject|NormalizerInterface $normalizer */ - $normalizer = $this->getMockByCalls(NormalizerInterface::class, [ - Call::create('normalize')->with($object, $context, 'path')->willReturn(['name' => 'Name']), + /** @var NormalizerInterface $normalizer */ + $normalizer = $builder->create(NormalizerInterface::class, [ + new WithReturn('normalize', [$object, $context, 'path'], ['name' => 'Name']), ]); - /** @var EncoderInterface|MockObject $encoder */ - $encoder = $this->getMockByCalls(EncoderInterface::class, [ - Call::create('encode')->with(['name' => 'Name'], 'application/json')->willReturn('{"name":"Name"}'), + /** @var EncoderInterface $encoder */ + $encoder = $builder->create(EncoderInterface::class, [ + new WithReturn('encode', [['name' => 'Name'], 'application/json'], '{"name":"Name"}'), ]); $serializer = new Serializer($normalizer, $encoder); @@ -52,16 +51,18 @@ public function testNormalize(): void $object = new \stdClass(); $object->name = 'Name'; - /** @var MockObject|NormalizerContextInterface $context */ - $context = $this->getMockByCalls(NormalizerContextInterface::class); + $builder = new MockObjectBuilder(); + + /** @var NormalizerContextInterface $context */ + $context = $builder->create(NormalizerContextInterface::class, []); - /** @var MockObject|NormalizerInterface $normalizer */ - $normalizer = $this->getMockByCalls(NormalizerInterface::class, [ - Call::create('normalize')->with($object, $context, 'path')->willReturn(['name' => 'Name']), + /** @var NormalizerInterface $normalizer */ + $normalizer = $builder->create(NormalizerInterface::class, [ + new WithReturn('normalize', [$object, $context, 'path'], ['name' => 'Name']), ]); - /** @var EncoderInterface|MockObject $encoder */ - $encoder = $this->getMockByCalls(EncoderInterface::class); + /** @var EncoderInterface $encoder */ + $encoder = $builder->create(EncoderInterface::class, []); $serializer = new Serializer($normalizer, $encoder); @@ -72,12 +73,14 @@ public function testNormalize(): void public function testGetContentTypes(): void { - /** @var MockObject|NormalizerInterface $normalizer */ - $normalizer = $this->getMockByCalls(NormalizerInterface::class); + $builder = new MockObjectBuilder(); - /** @var EncoderInterface|MockObject $encoder */ - $encoder = $this->getMockByCalls(EncoderInterface::class, [ - Call::create('getContentTypes')->with()->willReturn(['application/json']), + /** @var NormalizerInterface $normalizer */ + $normalizer = $builder->create(NormalizerInterface::class, []); + + /** @var EncoderInterface $encoder */ + $encoder = $builder->create(EncoderInterface::class, [ + new WithReturn('getContentTypes', [], ['application/json']), ]); $serializer = new Serializer($normalizer, $encoder); @@ -87,12 +90,14 @@ public function testGetContentTypes(): void public function testEncode(): void { - /** @var MockObject|NormalizerInterface $normalizer */ - $normalizer = $this->getMockByCalls(NormalizerInterface::class); + $builder = new MockObjectBuilder(); + + /** @var NormalizerInterface $normalizer */ + $normalizer = $builder->create(NormalizerInterface::class, []); - /** @var EncoderInterface|MockObject $encoder */ - $encoder = $this->getMockByCalls(EncoderInterface::class, [ - Call::create('encode')->with(['name' => 'Name'], 'application/json')->willReturn('{"name":"Name"}'), + /** @var EncoderInterface $encoder */ + $encoder = $builder->create(EncoderInterface::class, [ + new WithReturn('encode', [['name' => 'Name'], 'application/json'], '{"name":"Name"}'), ]); $serializer = new Serializer($normalizer, $encoder); diff --git a/tests/Unit/ServiceFactory/NormalizerFactoryTest.php b/tests/Unit/ServiceFactory/NormalizerFactoryTest.php index 2e96cb4..ed01f30 100644 --- a/tests/Unit/ServiceFactory/NormalizerFactoryTest.php +++ b/tests/Unit/ServiceFactory/NormalizerFactoryTest.php @@ -4,8 +4,8 @@ namespace Chubbyphp\Tests\Serialization\Unit\ServiceFactory; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Normalizer\NormalizerInterface; use Chubbyphp\Serialization\Normalizer\NormalizerObjectMappingRegistryInterface; use Chubbyphp\Serialization\ServiceFactory\NormalizerFactory; @@ -20,20 +20,18 @@ */ final class NormalizerFactoryTest extends TestCase { - use MockByCallsTrait; - public function testInvoke(): void { + $builder = new MockObjectBuilder(); + /** @var NormalizerObjectMappingRegistryInterface $normalizerObjectMappingRegistry */ - $normalizerObjectMappingRegistry = $this->getMockByCalls(NormalizerObjectMappingRegistryInterface::class); + $normalizerObjectMappingRegistry = $builder->create(NormalizerObjectMappingRegistryInterface::class, []); /** @var ContainerInterface $container */ - $container = $this->getMockByCalls(ContainerInterface::class, [ - Call::create('has')->with(NormalizerObjectMappingRegistryInterface::class)->willReturn(true), - Call::create('get') - ->with(NormalizerObjectMappingRegistryInterface::class) - ->willReturn($normalizerObjectMappingRegistry), - Call::create('has')->with(LoggerInterface::class)->willReturn(false), + $container = $builder->create(ContainerInterface::class, [ + new WithReturn('has', [NormalizerObjectMappingRegistryInterface::class], true), + new WithReturn('get', [NormalizerObjectMappingRegistryInterface::class], $normalizerObjectMappingRegistry), + new WithReturn('has', [LoggerInterface::class], false), ]); $factory = new NormalizerFactory(); @@ -45,16 +43,16 @@ public function testInvoke(): void public function testCallStatic(): void { + $builder = new MockObjectBuilder(); + /** @var NormalizerObjectMappingRegistryInterface $normalizerObjectMappingRegistry */ - $normalizerObjectMappingRegistry = $this->getMockByCalls(NormalizerObjectMappingRegistryInterface::class); + $normalizerObjectMappingRegistry = $builder->create(NormalizerObjectMappingRegistryInterface::class, []); /** @var ContainerInterface $container */ - $container = $this->getMockByCalls(ContainerInterface::class, [ - Call::create('has')->with(NormalizerObjectMappingRegistryInterface::class.'default')->willReturn(true), - Call::create('get') - ->with(NormalizerObjectMappingRegistryInterface::class.'default') - ->willReturn($normalizerObjectMappingRegistry), - Call::create('has')->with(LoggerInterface::class)->willReturn(false), + $container = $builder->create(ContainerInterface::class, [ + new WithReturn('has', [NormalizerObjectMappingRegistryInterface::class.'default'], true), + new WithReturn('get', [NormalizerObjectMappingRegistryInterface::class.'default'], $normalizerObjectMappingRegistry), + new WithReturn('has', [LoggerInterface::class], false), ]); $factory = [NormalizerFactory::class, 'default']; diff --git a/tests/Unit/ServiceFactory/NormalizerObjectMappingRegistryFactoryTest.php b/tests/Unit/ServiceFactory/NormalizerObjectMappingRegistryFactoryTest.php index 4bceaa6..4119537 100644 --- a/tests/Unit/ServiceFactory/NormalizerObjectMappingRegistryFactoryTest.php +++ b/tests/Unit/ServiceFactory/NormalizerObjectMappingRegistryFactoryTest.php @@ -4,8 +4,8 @@ namespace Chubbyphp\Tests\Serialization\Unit\ServiceFactory; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Mapping\NormalizationObjectMappingInterface; use Chubbyphp\Serialization\Normalizer\NormalizerObjectMappingRegistryInterface; use Chubbyphp\Serialization\ServiceFactory\NormalizerObjectMappingRegistryFactory; @@ -19,13 +19,13 @@ */ final class NormalizerObjectMappingRegistryFactoryTest extends TestCase { - use MockByCallsTrait; - public function testInvoke(): void { + $builder = new MockObjectBuilder(); + /** @var ContainerInterface $container */ - $container = $this->getMockByCalls(ContainerInterface::class, [ - Call::create('get')->with(NormalizationObjectMappingInterface::class.'[]')->willReturn([]), + $container = $builder->create(ContainerInterface::class, [ + new WithReturn('get', [NormalizationObjectMappingInterface::class.'[]'], []), ]); $factory = new NormalizerObjectMappingRegistryFactory(); @@ -37,9 +37,11 @@ public function testInvoke(): void public function testCallStatic(): void { + $builder = new MockObjectBuilder(); + /** @var ContainerInterface $container */ - $container = $this->getMockByCalls(ContainerInterface::class, [ - Call::create('get')->with(NormalizationObjectMappingInterface::class.'[]default')->willReturn([]), + $container = $builder->create(ContainerInterface::class, [ + new WithReturn('get', [NormalizationObjectMappingInterface::class.'[]default'], []), ]); $factory = [NormalizerObjectMappingRegistryFactory::class, 'default']; diff --git a/tests/Unit/ServiceFactory/SerializationServiceFactoryTest.php b/tests/Unit/ServiceFactory/SerializationServiceFactoryTest.php index aad19e0..f495499 100644 --- a/tests/Unit/ServiceFactory/SerializationServiceFactoryTest.php +++ b/tests/Unit/ServiceFactory/SerializationServiceFactoryTest.php @@ -6,7 +6,7 @@ use Chubbyphp\Container\Container; use Chubbyphp\DecodeEncode\Encoder\Encoder; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Normalizer\Normalizer; use Chubbyphp\Serialization\Normalizer\NormalizerObjectMappingRegistry; use Chubbyphp\Serialization\Serializer; @@ -22,8 +22,6 @@ */ final class SerializationServiceFactoryTest extends TestCase { - use MockByCallsTrait; - public function testRegister(): void { $container = new Container(); @@ -63,8 +61,10 @@ public function testRegister(): void public function testRegisterWithDefinedLogger(): void { - /** @var LoggerInterface|MockObject $logger */ - $logger = $this->getMockByCalls(LoggerInterface::class); + $builder = new MockObjectBuilder(); + + /** @var LoggerInterface $logger */ + $logger = $builder->create(LoggerInterface::class, []); $container = new Container([ 'logger' => static fn () => $logger, diff --git a/tests/Unit/ServiceFactory/SerializerFactoryTest.php b/tests/Unit/ServiceFactory/SerializerFactoryTest.php index bddd6f9..e5c094f 100644 --- a/tests/Unit/ServiceFactory/SerializerFactoryTest.php +++ b/tests/Unit/ServiceFactory/SerializerFactoryTest.php @@ -5,8 +5,8 @@ namespace Chubbyphp\Tests\Serialization\Unit\ServiceFactory; use Chubbyphp\DecodeEncode\Encoder\EncoderInterface; -use Chubbyphp\Mock\Call; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockMethod\WithReturn; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Normalizer\NormalizerInterface; use Chubbyphp\Serialization\SerializerInterface; use Chubbyphp\Serialization\ServiceFactory\SerializerFactory; @@ -20,22 +20,22 @@ */ final class SerializerFactoryTest extends TestCase { - use MockByCallsTrait; - public function testInvoke(): void { + $builder = new MockObjectBuilder(); + /** @var NormalizerInterface $normalizer */ - $normalizer = $this->getMockByCalls(NormalizerInterface::class); + $normalizer = $builder->create(NormalizerInterface::class, []); /** @var EncoderInterface $encoder */ - $encoder = $this->getMockByCalls(EncoderInterface::class); + $encoder = $builder->create(EncoderInterface::class, []); /** @var ContainerInterface $container */ - $container = $this->getMockByCalls(ContainerInterface::class, [ - Call::create('has')->with(NormalizerInterface::class)->willReturn(true), - Call::create('get')->with(NormalizerInterface::class)->willReturn($normalizer), - Call::create('has')->with(EncoderInterface::class)->willReturn(true), - Call::create('get')->with(EncoderInterface::class)->willReturn($encoder), + $container = $builder->create(ContainerInterface::class, [ + new WithReturn('has', [NormalizerInterface::class], true), + new WithReturn('get', [NormalizerInterface::class], $normalizer), + new WithReturn('has', [EncoderInterface::class], true), + new WithReturn('get', [EncoderInterface::class], $encoder), ]); $factory = new SerializerFactory(); @@ -47,18 +47,20 @@ public function testInvoke(): void public function testCallStatic(): void { + $builder = new MockObjectBuilder(); + /** @var NormalizerInterface $normalizer */ - $normalizer = $this->getMockByCalls(NormalizerInterface::class); + $normalizer = $builder->create(NormalizerInterface::class, []); /** @var EncoderInterface $encoder */ - $encoder = $this->getMockByCalls(EncoderInterface::class); + $encoder = $builder->create(EncoderInterface::class, []); /** @var ContainerInterface $container */ - $container = $this->getMockByCalls(ContainerInterface::class, [ - Call::create('has')->with(NormalizerInterface::class.'default')->willReturn(true), - Call::create('get')->with(NormalizerInterface::class.'default')->willReturn($normalizer), - Call::create('has')->with(EncoderInterface::class.'default')->willReturn(true), - Call::create('get')->with(EncoderInterface::class.'default')->willReturn($encoder), + $container = $builder->create(ContainerInterface::class, [ + new WithReturn('has', [NormalizerInterface::class.'default'], true), + new WithReturn('get', [NormalizerInterface::class.'default'], $normalizer), + new WithReturn('has', [EncoderInterface::class.'default'], true), + new WithReturn('get', [EncoderInterface::class.'default'], $encoder), ]); $factory = [SerializerFactory::class, 'default']; diff --git a/tests/Unit/ServiceProvider/SerializationServiceProviderTest.php b/tests/Unit/ServiceProvider/SerializationServiceProviderTest.php index 3d38940..8d36608 100644 --- a/tests/Unit/ServiceProvider/SerializationServiceProviderTest.php +++ b/tests/Unit/ServiceProvider/SerializationServiceProviderTest.php @@ -5,7 +5,7 @@ namespace Chubbyphp\Tests\Serialization\Unit\ServiceProvider; use Chubbyphp\DecodeEncode\Encoder\Encoder; -use Chubbyphp\Mock\MockByCallsTrait; +use Chubbyphp\Mock\MockObjectBuilder; use Chubbyphp\Serialization\Normalizer\Normalizer; use Chubbyphp\Serialization\Normalizer\NormalizerObjectMappingRegistry; use Chubbyphp\Serialization\Serializer; @@ -22,8 +22,6 @@ */ final class SerializationServiceProviderTest extends TestCase { - use MockByCallsTrait; - public function testRegister(): void { $container = new Container(); @@ -60,8 +58,9 @@ public function testRegister(): void public function testRegisterWithDefinedLogger(): void { - /** @var LoggerInterface|MockObject $logger */ - $logger = $this->getMockByCalls(LoggerInterface::class); + $builder = new MockObjectBuilder(); + + $logger = $builder->create(LoggerInterface::class, []); $container = new Container([ 'logger' => $logger,