From fd50780c05d1744ccbb3ee31283dd3dca7f2a244 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Wed, 6 Dec 2023 15:31:51 +0100 Subject: [PATCH 1/6] Added serializeContentFieldValue method to FieldTypeSerializer --- src/lib/Output/FieldTypeSerializer.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/lib/Output/FieldTypeSerializer.php b/src/lib/Output/FieldTypeSerializer.php index 4bc7c07c..eda28ab5 100644 --- a/src/lib/Output/FieldTypeSerializer.php +++ b/src/lib/Output/FieldTypeSerializer.php @@ -41,21 +41,28 @@ public function __construct(FieldTypeService $fieldTypeService, FieldTypeProcess } /** - * Serializes the field value of $field through $generator. + * @deprecated 4.6.0 The "FieldTypeSerializer::serializeFieldValue()" method is deprecated, will be removed in 5.0. + * Use "FieldTypeSerializer::serializeContentFieldValue()" instead. * - * @param \Ibexa\Contracts\Rest\Output\Generator $generator - * @param \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType $contentType - * @param \Ibexa\Contracts\Core\Repository\Values\Content\Field $field + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException + */ + public function serializeFieldValue(Generator $generator, ContentType $contentType, Field $field): void + { + $this->serializeContentFieldValue($generator, $field); + } + + /** + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException */ - public function serializeFieldValue(Generator $generator, ContentType $contentType, Field $field) + public function serializeContentFieldValue(Generator $generator, Field $field): void { $this->serializeValue( 'fieldValue', $generator, $this->fieldTypeService->getFieldType( - $contentType->getFieldDefinition($field->fieldDefIdentifier)->fieldTypeIdentifier + $field->getFieldTypeIdentifier() ), - $field->value + $field->getValue() ); } From 58af0385dc7f86c93155b20453ee265f86b12eb1 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Wed, 6 Dec 2023 15:32:17 +0100 Subject: [PATCH 2/6] Added ValueObjectVisitor for Field --- .../config/value_object_visitors.yml | 6 +++ .../Output/ValueObjectVisitor/Field.php | 53 +++++++++++++++++++ .../Output/ValueObjectVisitor/Version.php | 37 +------------ 3 files changed, 60 insertions(+), 36 deletions(-) create mode 100644 src/lib/Server/Output/ValueObjectVisitor/Field.php diff --git a/src/bundle/Resources/config/value_object_visitors.yml b/src/bundle/Resources/config/value_object_visitors.yml index eeee86f5..6609346b 100644 --- a/src/bundle/Resources/config/value_object_visitors.yml +++ b/src/bundle/Resources/config/value_object_visitors.yml @@ -407,6 +407,12 @@ services: tags: - { name: ibexa.rest.output.value_object.visitor, type: Ibexa\Rest\Server\Values\CreatedFieldDefinition } + Ibexa\Rest\Server\Output\ValueObjectVisitor\Field: + parent: Ibexa\Contracts\Rest\Output\ValueObjectVisitor + arguments: [ '@Ibexa\Rest\Output\FieldTypeSerializer' ] + tags: + - { name: ibexa.rest.output.value_object.visitor, type: Ibexa\Contracts\Core\Repository\Values\Content\Field } + Ibexa\Rest\Server\Output\ValueObjectVisitor\RestFieldDefinition: parent: Ibexa\Contracts\Rest\Output\ValueObjectVisitor class: Ibexa\Rest\Server\Output\ValueObjectVisitor\RestFieldDefinition diff --git a/src/lib/Server/Output/ValueObjectVisitor/Field.php b/src/lib/Server/Output/ValueObjectVisitor/Field.php new file mode 100644 index 00000000..22bbf07d --- /dev/null +++ b/src/lib/Server/Output/ValueObjectVisitor/Field.php @@ -0,0 +1,53 @@ +fieldTypeSerializer = $fieldTypeSerializer; + } + + /** + * @param \Ibexa\Contracts\Core\Repository\Values\Content\Field $data + * + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException + */ + public function visit(Visitor $visitor, Generator $generator, $data): void + { + $generator->startHashElement('field'); + + $generator->startValueElement('id', $data->id); + $generator->endValueElement('id'); + + $generator->startValueElement('fieldDefinitionIdentifier', $data->fieldDefIdentifier); + $generator->endValueElement('fieldDefinitionIdentifier'); + + $generator->startValueElement('languageCode', $data->languageCode); + $generator->endValueElement('languageCode'); + + $generator->startValueElement('fieldTypeIdentifier', $data->fieldTypeIdentifier); + $generator->endValueElement('fieldTypeIdentifier'); + + $this->fieldTypeSerializer->serializeContentFieldValue( + $generator, + $data + ); + + $generator->endHashElement('field'); + } +} diff --git a/src/lib/Server/Output/ValueObjectVisitor/Version.php b/src/lib/Server/Output/ValueObjectVisitor/Version.php index 4e507f28..d3a138b5 100644 --- a/src/lib/Server/Output/ValueObjectVisitor/Version.php +++ b/src/lib/Server/Output/ValueObjectVisitor/Version.php @@ -6,9 +6,7 @@ */ namespace Ibexa\Rest\Server\Output\ValueObjectVisitor; -use Ibexa\Contracts\Core\Repository\Values\Content\Field; use Ibexa\Contracts\Core\Repository\Values\Content\Thumbnail; -use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType; use Ibexa\Contracts\Rest\Output\Generator; use Ibexa\Contracts\Rest\Output\ValueObjectVisitor; use Ibexa\Contracts\Rest\Output\Visitor; @@ -57,44 +55,11 @@ public function visit(Visitor $visitor, Generator $generator, $data) $generator->endObjectElement('Version'); } - /** - * Visits a single content field and generates its content. - * - * @param \Ibexa\Contracts\Rest\Output\Generator $generator - * @param \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType $contentType - * @param \Ibexa\Contracts\Core\Repository\Values\Content\Field $field - */ - public function visitField(Generator $generator, ContentType $contentType, Field $field) - { - $generator->startHashElement('field'); - - $generator->startValueElement('id', $field->id); - $generator->endValueElement('id'); - - $generator->startValueElement('fieldDefinitionIdentifier', $field->fieldDefIdentifier); - $generator->endValueElement('fieldDefinitionIdentifier'); - - $generator->startValueElement('languageCode', $field->languageCode); - $generator->endValueElement('languageCode'); - - $generator->startValueElement('fieldTypeIdentifier', $field->fieldTypeIdentifier); - $generator->endValueElement('fieldTypeIdentifier'); - - $this->fieldTypeSerializer->serializeFieldValue( - $generator, - $contentType, - $field - ); - - $generator->endHashElement('field'); - } - protected function visitVersionAttributes(Visitor $visitor, Generator $generator, VersionValue $data) { $content = $data->content; $versionInfo = $content->getVersionInfo(); - $contentType = $data->contentType; $path = $data->path; if ($path == null) { @@ -115,7 +80,7 @@ protected function visitVersionAttributes(Visitor $visitor, Generator $generator $generator->startHashElement('Fields'); $generator->startList('field'); foreach ($content->getFields() as $field) { - $this->visitField($generator, $contentType, $field); + $visitor->visitValueObject($field); } $generator->endList('field'); $generator->endHashElement('Fields'); From 81a701eb6a0da257ecbbb979203803e6ec899240 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Wed, 6 Dec 2023 15:32:33 +0100 Subject: [PATCH 3/6] [Tests] Adjusted tests --- tests/lib/Output/FieldTypeSerializerTest.php | 256 ++++++++++-------- .../Output/ValueObjectVisitor/VersionTest.php | 12 - 2 files changed, 144 insertions(+), 124 deletions(-) diff --git a/tests/lib/Output/FieldTypeSerializerTest.php b/tests/lib/Output/FieldTypeSerializerTest.php index 38165621..50e6da07 100644 --- a/tests/lib/Output/FieldTypeSerializerTest.php +++ b/tests/lib/Output/FieldTypeSerializerTest.php @@ -12,7 +12,6 @@ use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType as APIContentType; use Ibexa\Contracts\Rest\FieldTypeProcessor; use Ibexa\Contracts\Rest\Output\Generator; -use Ibexa\Core\Repository\Values\ContentType\FieldDefinition; use Ibexa\Rest\FieldTypeProcessorRegistry; use Ibexa\Rest\Output\FieldTypeSerializer; use PHPUnit\Framework\TestCase; @@ -34,78 +33,82 @@ class FieldTypeSerializerTest extends TestCase protected $generatorMock; - public function testSerializeFieldValue() - { + /** + * @dataProvider provideDataWithFieldValueToSerialize + * + * @param mixed $hashValue + * + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException + */ + public function testSerializeFieldValue( + APIFieldType $fieldType, + $hashValue + ): void { $serializer = $this->getFieldTypeSerializer(); - $this->getGeneratorMock()->expects($this->once()) - ->method('generateFieldTypeHash') - ->with( - $this->equalTo('fieldValue'), - $this->equalTo([23, 42]) - ); - - $this->getContentTypeMock()->expects($this->once()) - ->method('getFieldDefinition') - ->with( - $this->equalTo('some-field') - )->willReturn( - new FieldDefinition( - [ - 'fieldTypeIdentifier' => 'myFancyFieldType', - ] - ) - ); - - $fieldTypeMock = $this->getFieldTypeMock(); - $this->getFieldTypeServiceMock()->expects($this->once()) + $this->getFieldTypeServiceMock() ->method('getFieldType') ->with($this->equalTo('myFancyFieldType')) - ->willReturnCallback( - static function () use ($fieldTypeMock) { - return $fieldTypeMock; - } - ); - - $fieldTypeMock->expects($this->once()) - ->method('toHash') - ->with($this->equalTo('my-field-value')) - ->willReturn([23, 42]); + ->willReturn($fieldType); $serializer->serializeFieldValue( - $this->getGeneratorMock(), + $this->mockGeneratorGenerateFieldTypeHash( + 'fieldValue', + $hashValue + ), $this->getContentTypeMock(), - new Field( - [ - 'fieldDefIdentifier' => 'some-field', - 'value' => 'my-field-value', - ] - ) + $this->createFieldMock('myFancyFieldType', 'my-field-value') ); } - public function testSerializeFieldValueWithProcessor() - { + /** + * @dataProvider provideDataWithFieldValueToSerialize + * + * @param mixed $hashValue + * + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException + */ + public function testSerializeContentFieldValue( + APIFieldType $fieldType, + $hashValue + ): void { $serializer = $this->getFieldTypeSerializer(); - $this->getGeneratorMock()->expects($this->once()) - ->method('generateFieldTypeHash') - ->with( - $this->equalTo('fieldValue'), - $this->equalTo(['post-processed']) - ); + $this->getFieldTypeServiceMock() + ->method('getFieldType') + ->with($this->equalTo('myFancyFieldType')) + ->willReturn($fieldType); + + $serializer->serializeContentFieldValue( + $this->mockGeneratorGenerateFieldTypeHash( + 'fieldValue', + $hashValue + ), + $this->createFieldMock('myFancyFieldType', 'my-field-value') + ); + } - $this->getContentTypeMock()->expects($this->once()) - ->method('getFieldDefinition') - ->with( - $this->equalTo('some-field') - )->willReturn( - new FieldDefinition( - [ - 'fieldTypeIdentifier' => 'myFancyFieldType', - ] - ) - ); + /** + * @return iterable, + * }> + */ + public function provideDataWithFieldValueToSerialize(): iterable + { + $hash = [23, 42]; + yield [ + $this->createFieldTypeMock( + 'my-field-value', + $hash + ), + $hash, + ]; + } + + public function testSerializeFieldValueWithProcessor() + { + $serializer = $this->getFieldTypeSerializer(); $processorMock = $this->getFieldTypeProcessorMock(); $this->getFieldTypeProcessorRegistryMock() @@ -117,11 +120,7 @@ public function testSerializeFieldValueWithProcessor() ->expects($this->once()) ->method('getProcessor') ->with('myFancyFieldType') - ->willReturnCallback( - static function () use ($processorMock) { - return $processorMock; - } - ); + ->willReturn($processorMock); $processorMock->expects($this->once()) ->method('postProcessValueHash') ->with($this->equalTo([23, 42])) @@ -146,14 +145,12 @@ static function () use ($fieldTypeMock) { ->willReturn([23, 42]); $serializer->serializeFieldValue( - $this->getGeneratorMock(), + $this->mockGeneratorGenerateFieldTypeHash( + 'fieldValue', + ['post-processed'] + ), $this->getContentTypeMock(), - new Field( - [ - 'fieldDefIdentifier' => 'some-field', - 'value' => 'my-field-value', - ] - ) + $this->createFieldMock('myFancyFieldType', 'my-field-value') ); } @@ -161,13 +158,6 @@ public function testSerializeFieldDefaultValue() { $serializer = $this->getFieldTypeSerializer(); - $this->getGeneratorMock()->expects($this->once()) - ->method('generateFieldTypeHash') - ->with( - $this->equalTo('defaultValue'), - $this->equalTo([23, 42]) - ); - $fieldTypeMock = $this->getFieldTypeMock(); $this->getFieldTypeServiceMock()->expects($this->once()) ->method('getFieldType') @@ -184,7 +174,10 @@ static function () use ($fieldTypeMock) { ->willReturn([23, 42]); $serializer->serializeFieldDefaultValue( - $this->getGeneratorMock(), + $this->mockGeneratorGenerateFieldTypeHash( + 'defaultValue', + [23, 42] + ), 'myFancyFieldType', 'my-field-value' ); @@ -194,13 +187,6 @@ public function testSerializeFieldSettings() { $serializer = $this->getFieldTypeSerializer(); - $this->getGeneratorMock()->expects($this->once()) - ->method('generateFieldTypeHash') - ->with( - $this->equalTo('fieldSettings'), - $this->equalTo(['foo' => 'bar']) - ); - $fieldTypeMock = $this->getFieldTypeMock(); $this->getFieldTypeServiceMock()->expects($this->once()) ->method('getFieldType') @@ -217,7 +203,10 @@ static function () use ($fieldTypeMock) { ->willReturn(['foo' => 'bar']); $serializer->serializeFieldSettings( - $this->getGeneratorMock(), + $this->mockGeneratorGenerateFieldTypeHash( + 'fieldSettings', + ['foo' => 'bar'] + ), 'myFancyFieldType', 'my-field-settings' ); @@ -248,13 +237,6 @@ static function () use ($processorMock) { ->with($this->equalTo(['foo' => 'bar'])) ->willReturn(['post-processed']); - $this->getGeneratorMock()->expects($this->once()) - ->method('generateFieldTypeHash') - ->with( - $this->equalTo('fieldSettings'), - $this->equalTo(['post-processed']) - ); - $this->getFieldTypeServiceMock()->expects($this->once()) ->method('getFieldType') ->with($this->equalTo('myFancyFieldType')) @@ -270,7 +252,10 @@ static function () use ($fieldTypeMock) { ->willReturn(['foo' => 'bar']); $serializer->serializeFieldSettings( - $this->getGeneratorMock(), + $this->mockGeneratorGenerateFieldTypeHash( + 'fieldSettings', + ['post-processed'] + ), 'myFancyFieldType', 'my-field-settings' ); @@ -280,13 +265,6 @@ public function testSerializeValidatorConfiguration() { $serializer = $this->getFieldTypeSerializer(); - $this->getGeneratorMock()->expects($this->once()) - ->method('generateFieldTypeHash') - ->with( - $this->equalTo('validatorConfiguration'), - $this->equalTo(['bar' => 'foo']) - ); - $fieldTypeMock = $this->getFieldTypeMock(); $this->getFieldTypeServiceMock()->expects($this->once()) ->method('getFieldType') @@ -303,7 +281,10 @@ static function () use ($fieldTypeMock) { ->willReturn(['bar' => 'foo']); $serializer->serializeValidatorConfiguration( - $this->getGeneratorMock(), + $this->mockGeneratorGenerateFieldTypeHash( + 'validatorConfiguration', + ['bar' => 'foo'] + ), 'myFancyFieldType', 'validator-config' ); @@ -344,13 +325,6 @@ static function () use ($fieldTypeMock) { } ); - $this->getGeneratorMock()->expects($this->once()) - ->method('generateFieldTypeHash') - ->with( - $this->equalTo('validatorConfiguration'), - $this->equalTo(['post-processed']) - ); - $this->getFieldTypeServiceMock()->expects($this->once()) ->method('getFieldType') ->with($this->equalTo('myFancyFieldType')) @@ -366,7 +340,10 @@ static function () use ($fieldTypeMock) { ->willReturn(['bar' => 'foo']); $serializer->serializeValidatorConfiguration( - $this->getGeneratorMock(), + $this->mockGeneratorGenerateFieldTypeHash( + 'validatorConfiguration', + ['post-processed'] + ), 'myFancyFieldType', 'validator-config' ); @@ -433,6 +410,61 @@ protected function getGeneratorMock() return $this->generatorMock; } + + /** + * @param mixed $value + */ + private function createFieldMock( + string $fieldTypeIdentifier, + $value + ): Field { + $fieldMock = $this->createMock(Field::class); + $fieldMock + ->method('getFieldTypeIdentifier') + ->willReturn($fieldTypeIdentifier); + + $fieldMock + ->method('getValue') + ->willReturn($value); + + return $fieldMock; + } + + /** + * @param mixed $hashElementName + * @param mixed $hashElementValue + */ + private function mockGeneratorGenerateFieldTypeHash( + $hashElementName, + $hashElementValue + ): Generator { + $generator = $this->createMock(Generator::class); + $generator + ->method('generateFieldTypeHash') + ->with( + $hashElementName, + $hashElementValue + ); + + return $generator; + } + + /** + * @param mixed $value + * @param mixed $hashValue + */ + private function createFieldTypeMock( + $value, + $hashValue + ): APIFieldType { + $fieldTypeMock = $this->createMock(APIFieldType::class); + $fieldTypeMock + ->method('toHash') + ->with($value) + ->willReturn($hashValue); + + return $fieldTypeMock; + } } class_alias(FieldTypeSerializerTest::class, 'EzSystems\EzPlatformRest\Tests\Output\FieldTypeSerializerTest'); diff --git a/tests/lib/Server/Output/ValueObjectVisitor/VersionTest.php b/tests/lib/Server/Output/ValueObjectVisitor/VersionTest.php index cea18c01..02dc863a 100644 --- a/tests/lib/Server/Output/ValueObjectVisitor/VersionTest.php +++ b/tests/lib/Server/Output/ValueObjectVisitor/VersionTest.php @@ -9,7 +9,6 @@ use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo; use Ibexa\Contracts\Core\Repository\Values\Content\Field; use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType; -use Ibexa\Contracts\Rest\Output\Generator; use Ibexa\Core\Repository\Values; use Ibexa\Rest\Output\FieldTypeSerializer; use Ibexa\Rest\Server\Output\ValueObjectVisitor; @@ -75,17 +74,6 @@ public function testVisit() [] ); - $this->fieldTypeSerializerMock->expects($this->exactly(2)) - ->method('serializeFieldValue') - ->with( - $this->isInstanceOf(Generator::class), - $this->isInstanceOf(ContentType::class), - $this->isInstanceOf(Field::class) - ); - - $this->getVisitorMock()->expects($this->exactly(2)) - ->method('visitValueObject'); - $this->addRouteExpectation( 'ibexa.rest.load_content_in_version', [ From d33b7c506491c01198591e96e1aeb312adad6999 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Wed, 6 Dec 2023 15:47:37 +0100 Subject: [PATCH 4/6] [PHPStan] Regenerated baseline --- phpstan-baseline.neon | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 1d3ab2ef..1ff6a8cb 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -775,11 +775,6 @@ parameters: count: 1 path: src/lib/Message.php - - - message: "#^Cannot access property \\$fieldTypeIdentifier on Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\FieldDefinition\\|null\\.$#" - count: 1 - path: src/lib/Output/FieldTypeSerializer.php - - message: "#^Method Ibexa\\\\Rest\\\\Output\\\\FieldTypeSerializer\\:\\:serializeFieldDefaultValue\\(\\) has no return type specified\\.$#" count: 1 @@ -790,11 +785,6 @@ parameters: count: 1 path: src/lib/Output/FieldTypeSerializer.php - - - message: "#^Method Ibexa\\\\Rest\\\\Output\\\\FieldTypeSerializer\\:\\:serializeFieldValue\\(\\) has no return type specified\\.$#" - count: 1 - path: src/lib/Output/FieldTypeSerializer.php - - message: "#^Method Ibexa\\\\Rest\\\\Output\\\\FieldTypeSerializer\\:\\:serializeHash\\(\\) has no return type specified\\.$#" count: 1 @@ -3720,11 +3710,6 @@ parameters: count: 1 path: src/lib/Server/Output/ValueObjectVisitor/Version.php - - - message: "#^Method Ibexa\\\\Rest\\\\Server\\\\Output\\\\ValueObjectVisitor\\\\Version\\:\\:visitField\\(\\) has no return type specified\\.$#" - count: 1 - path: src/lib/Server/Output/ValueObjectVisitor/Version.php - - message: "#^Method Ibexa\\\\Rest\\\\Server\\\\Output\\\\ValueObjectVisitor\\\\Version\\:\\:visitVersionAttributes\\(\\) has no return type specified\\.$#" count: 1 @@ -7335,11 +7320,6 @@ parameters: count: 1 path: tests/lib/Output/FieldTypeSerializerTest.php - - - message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeFieldValue\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Output/FieldTypeSerializerTest.php - - message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeFieldValueWithProcessor\\(\\) has no return type specified\\.$#" count: 1 From c7fa4abe471b172cd049fb23cfcd4966f112a370 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Thu, 7 Dec 2023 08:40:54 +0100 Subject: [PATCH 5/6] Refactored FieldTypeSerializerTest --- phpstan-baseline.neon | 40 ------ tests/lib/Output/FieldTypeSerializerTest.php | 131 +++++++------------ 2 files changed, 50 insertions(+), 121 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 1ff6a8cb..7f49668e 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -7290,51 +7290,11 @@ parameters: count: 1 path: tests/lib/Output/FieldTypeSerializerTest.php - - - message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:getFieldTypeSerializer\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Output/FieldTypeSerializerTest.php - - message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:getFieldTypeServiceMock\\(\\) has no return type specified\\.$#" count: 1 path: tests/lib/Output/FieldTypeSerializerTest.php - - - message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:getGeneratorMock\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Output/FieldTypeSerializerTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeFieldDefaultValue\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Output/FieldTypeSerializerTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeFieldSettings\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Output/FieldTypeSerializerTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeFieldSettingsWithPostProcessing\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Output/FieldTypeSerializerTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeFieldValueWithProcessor\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Output/FieldTypeSerializerTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeValidatorConfiguration\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Output/FieldTypeSerializerTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeValidatorConfigurationWithPostProcessing\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Output/FieldTypeSerializerTest.php - - message: "#^Property Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:\\$contentTypeMock has no type specified\\.$#" count: 1 diff --git a/tests/lib/Output/FieldTypeSerializerTest.php b/tests/lib/Output/FieldTypeSerializerTest.php index 50e6da07..3e7f9ce3 100644 --- a/tests/lib/Output/FieldTypeSerializerTest.php +++ b/tests/lib/Output/FieldTypeSerializerTest.php @@ -46,10 +46,10 @@ public function testSerializeFieldValue( ): void { $serializer = $this->getFieldTypeSerializer(); - $this->getFieldTypeServiceMock() - ->method('getFieldType') - ->with($this->equalTo('myFancyFieldType')) - ->willReturn($fieldType); + $this->mockFieldTypeServiceGetFieldType( + 'myFancyFieldType', + $fieldType + ); $serializer->serializeFieldValue( $this->mockGeneratorGenerateFieldTypeHash( @@ -74,10 +74,10 @@ public function testSerializeContentFieldValue( ): void { $serializer = $this->getFieldTypeSerializer(); - $this->getFieldTypeServiceMock() - ->method('getFieldType') - ->with($this->equalTo('myFancyFieldType')) - ->willReturn($fieldType); + $this->mockFieldTypeServiceGetFieldType( + 'myFancyFieldType', + $fieldType + ); $serializer->serializeContentFieldValue( $this->mockGeneratorGenerateFieldTypeHash( @@ -106,7 +106,7 @@ public function provideDataWithFieldValueToSerialize(): iterable ]; } - public function testSerializeFieldValueWithProcessor() + public function testSerializeFieldValueWithProcessor(): void { $serializer = $this->getFieldTypeSerializer(); @@ -127,14 +127,11 @@ public function testSerializeFieldValueWithProcessor() ->willReturn(['post-processed']); $fieldTypeMock = $this->getFieldTypeMock(); - $this->getFieldTypeServiceMock()->expects($this->once()) - ->method('getFieldType') - ->with($this->equalTo('myFancyFieldType')) - ->willReturnCallback( - static function () use ($fieldTypeMock) { - return $fieldTypeMock; - } - ); + + $this->mockFieldTypeServiceGetFieldType( + 'myFancyFieldType', + $fieldTypeMock + ); $fieldTypeMock->expects($this->once()) ->method('getFieldTypeIdentifier') @@ -154,19 +151,15 @@ static function () use ($fieldTypeMock) { ); } - public function testSerializeFieldDefaultValue() + public function testSerializeFieldDefaultValue(): void { $serializer = $this->getFieldTypeSerializer(); $fieldTypeMock = $this->getFieldTypeMock(); - $this->getFieldTypeServiceMock()->expects($this->once()) - ->method('getFieldType') - ->with($this->equalTo('myFancyFieldType')) - ->willReturnCallback( - static function () use ($fieldTypeMock) { - return $fieldTypeMock; - } - ); + $this->mockFieldTypeServiceGetFieldType( + 'myFancyFieldType', + $fieldTypeMock + ); $fieldTypeMock->expects($this->once()) ->method('toHash') @@ -183,19 +176,15 @@ static function () use ($fieldTypeMock) { ); } - public function testSerializeFieldSettings() + public function testSerializeFieldSettings(): void { $serializer = $this->getFieldTypeSerializer(); $fieldTypeMock = $this->getFieldTypeMock(); - $this->getFieldTypeServiceMock()->expects($this->once()) - ->method('getFieldType') - ->with($this->equalTo('myFancyFieldType')) - ->willReturnCallback( - static function () use ($fieldTypeMock) { - return $fieldTypeMock; - } - ); + $this->mockFieldTypeServiceGetFieldType( + 'myFancyFieldType', + $fieldTypeMock + ); $fieldTypeMock->expects($this->once()) ->method('fieldSettingsToHash') @@ -212,7 +201,7 @@ static function () use ($fieldTypeMock) { ); } - public function testSerializeFieldSettingsWithPostProcessing() + public function testSerializeFieldSettingsWithPostProcessing(): void { $serializer = $this->getFieldTypeSerializer(); $fieldTypeMock = $this->getFieldTypeMock(); @@ -237,14 +226,10 @@ static function () use ($processorMock) { ->with($this->equalTo(['foo' => 'bar'])) ->willReturn(['post-processed']); - $this->getFieldTypeServiceMock()->expects($this->once()) - ->method('getFieldType') - ->with($this->equalTo('myFancyFieldType')) - ->willReturnCallback( - static function () use ($fieldTypeMock) { - return $fieldTypeMock; - } - ); + $this->mockFieldTypeServiceGetFieldType( + 'myFancyFieldType', + $fieldTypeMock + ); $fieldTypeMock->expects($this->once()) ->method('fieldSettingsToHash') @@ -261,19 +246,15 @@ static function () use ($fieldTypeMock) { ); } - public function testSerializeValidatorConfiguration() + public function testSerializeValidatorConfiguration(): void { $serializer = $this->getFieldTypeSerializer(); $fieldTypeMock = $this->getFieldTypeMock(); - $this->getFieldTypeServiceMock()->expects($this->once()) - ->method('getFieldType') - ->with($this->equalTo('myFancyFieldType')) - ->willReturnCallback( - static function () use ($fieldTypeMock) { - return $fieldTypeMock; - } - ); + $this->mockFieldTypeServiceGetFieldType( + 'myFancyFieldType', + $fieldTypeMock + ); $fieldTypeMock->expects($this->once()) ->method('validatorConfigurationToHash') @@ -290,7 +271,7 @@ static function () use ($fieldTypeMock) { ); } - public function testSerializeValidatorConfigurationWithPostProcessing() + public function testSerializeValidatorConfigurationWithPostProcessing(): void { $serializer = $this->getFieldTypeSerializer(); $fieldTypeMock = $this->getFieldTypeMock(); @@ -316,23 +297,10 @@ static function () use ($processorMock) { ->willReturn(['post-processed']); $fieldTypeMock = $this->getFieldTypeMock(); - $this->getFieldTypeServiceMock()->expects($this->once()) - ->method('getFieldType') - ->with($this->equalTo('myFancyFieldType')) - ->willReturnCallback( - static function () use ($fieldTypeMock) { - return $fieldTypeMock; - } - ); - - $this->getFieldTypeServiceMock()->expects($this->once()) - ->method('getFieldType') - ->with($this->equalTo('myFancyFieldType')) - ->willReturnCallback( - static function () use ($fieldTypeMock) { - return $fieldTypeMock; - } - ); + $this->mockFieldTypeServiceGetFieldType( + 'myFancyFieldType', + $fieldTypeMock + ); $fieldTypeMock->expects($this->once()) ->method('validatorConfigurationToHash') @@ -349,7 +317,7 @@ static function () use ($fieldTypeMock) { ); } - protected function getFieldTypeSerializer() + protected function getFieldTypeSerializer(): FieldTypeSerializer { return new FieldTypeSerializer( $this->getFieldTypeServiceMock(), @@ -402,15 +370,6 @@ protected function getFieldTypeMock() return $this->fieldTypeMock; } - protected function getGeneratorMock() - { - if (!isset($this->generatorMock)) { - $this->generatorMock = $this->createMock(Generator::class); - } - - return $this->generatorMock; - } - /** * @param mixed $value */ @@ -465,6 +424,16 @@ private function createFieldTypeMock( return $fieldTypeMock; } + + private function mockFieldTypeServiceGetFieldType( + string $identifier, + APIFieldType $fieldType + ): void { + $this->getFieldTypeServiceMock() + ->method('getFieldType') + ->with($identifier) + ->willReturn($fieldType); + } } class_alias(FieldTypeSerializerTest::class, 'EzSystems\EzPlatformRest\Tests\Output\FieldTypeSerializerTest'); From 747f7254ba46e6563b1f348938f4c53226026c37 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Thu, 7 Dec 2023 12:08:35 +0100 Subject: [PATCH 6/6] [Tests] Added test coverage for Field ValueObjectVisitor --- .../Output/ValueObjectVisitor/FieldTest.php | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 tests/lib/Server/Output/ValueObjectVisitor/FieldTest.php diff --git a/tests/lib/Server/Output/ValueObjectVisitor/FieldTest.php b/tests/lib/Server/Output/ValueObjectVisitor/FieldTest.php new file mode 100644 index 00000000..6de3d346 --- /dev/null +++ b/tests/lib/Server/Output/ValueObjectVisitor/FieldTest.php @@ -0,0 +1,101 @@ +fieldTypeSerializer = $this->createMock(FieldTypeSerializer::class); + } + + public function testVisit(): void + { + $visitor = $this->getVisitor(); + $generator = $this->getGenerator(); + + $generator->startDocument(null); + $field = new ApiField( + [ + 'id' => 1, + 'fieldDefIdentifier' => 'foo', + 'value' => 'foo', + 'languageCode' => 'eng-GB', + 'fieldTypeIdentifier' => 'ezfoo', + ] + ); + + $this->mockFieldTypeSerializerSerializeContentFieldValue( + $generator, + $field, + 'foo' + ); + + $visitor->visit( + $this->getVisitorMock(), + $generator, + $field + ); + + $result = $generator->endDocument(null); + + $this->assertContainsTag('field', $result); + $this->assertContainsTag('id', $result); + $this->assertContainsTag('fieldDefinitionIdentifier', $result); + $this->assertContainsTag('value', $result); + $this->assertContainsTag('languageCode', $result); + $this->assertContainsTag('fieldTypeIdentifier', $result); + } + + private function mockFieldTypeSerializerSerializeContentFieldValue( + Generator $generator, + ApiField $field, + string $value + ): void { + $this->fieldTypeSerializer + ->method('serializeContentFieldValue') + ->with( + $generator, + $field + ) + ->willReturn($value); + } + + private function assertContainsTag( + string $tag, + string $result + ): void { + $this->assertXMLTag( + [ + 'tag' => $tag, + ], + $result, + "Invalid <$tag> element.", + ); + } + + protected function internalGetVisitor(): Field + { + return new Field($this->fieldTypeSerializer); + } +}