diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 742ec3d2..ed6dcb9d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -6180,11 +6180,6 @@ parameters: count: 1 path: tests/lib/Output/ValueObjectVisitorBaseTest.php - - - message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\VisitorTest\\:\\:testSetFilteredHeaders\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Output/VisitorTest.php - - message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Server\\\\Input\\\\Parser\\\\BaseTest\\:\\:getParseHrefExpectationsMap\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 diff --git a/phpunit.integration.xml b/phpunit.integration.xml index 2b44e4a9..6f446a6b 100644 --- a/phpunit.integration.xml +++ b/phpunit.integration.xml @@ -5,9 +5,10 @@ beStrictAboutTodoAnnotatedTests="true" verbose="true"> + - + diff --git a/src/bundle/Resources/config/services.yml b/src/bundle/Resources/config/services.yml index edbaabe3..70876873 100644 --- a/src/bundle/Resources/config/services.yml +++ b/src/bundle/Resources/config/services.yml @@ -417,6 +417,8 @@ services: Ibexa\Contracts\Rest\Output\VisitorAdapterNormalizer: arguments: + $jsonEncoder: '@ibexa.rest.serializer.encoder.json' + $xmlEncoder: '@ibexa.rest.serializer.encoder.xml' $valueObjectVisitorResolver: '@Ibexa\Contracts\Rest\Output\ValueObjectVisitorResolver' tags: - { name: ibexa.rest.serializer.normalizer, priority: -1000 } diff --git a/src/contracts/Output/VisitorAdapterNormalizer.php b/src/contracts/Output/VisitorAdapterNormalizer.php index c39420f3..aeaa7678 100644 --- a/src/contracts/Output/VisitorAdapterNormalizer.php +++ b/src/contracts/Output/VisitorAdapterNormalizer.php @@ -8,7 +8,10 @@ namespace Ibexa\Contracts\Rest\Output; +use Ibexa\Contracts\Rest\Output\Generator as BaseGenerator; +use Ibexa\Rest\Output\Generator; use LogicException; +use Symfony\Component\Serializer\Encoder\EncoderInterface; use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; @@ -22,9 +25,9 @@ final class VisitorAdapterNormalizer implements NormalizerInterface, NormalizerA public const string ENCODER_CONTEXT = 'ENCODER_CONTEXT'; - public const string OUTER_ELEMENT = 'outer_element'; - public function __construct( + private readonly EncoderInterface $jsonEncoder, + private readonly EncoderInterface $xmlEncoder, private readonly ValueObjectVisitorResolverInterface $valueObjectVisitorResolver, ) { } @@ -90,11 +93,7 @@ private function visitValueObject( ?string $format, array $context, ): array { - if (!isset($context['visitor'])) { - throw new LogicException('Context must have the "Visitor" instance passed.'); - } - - $visitor = $context['visitor']; + $visitor = $context['visitor'] ?? $this->createVisitor($format); $generator = $visitor->getGenerator(); $generator->reset(); @@ -125,9 +124,37 @@ private function buildContext(array $context, ?string $format): array $context += [self::CALLED_CONTEXT => true]; if ($format === 'xml') { - $context += [self::OUTER_ELEMENT => true]; + $context += [Generator\InMemory\Xml::OUTER_ELEMENT => true]; } return $context; } + + private function createGenerator(string $format): BaseGenerator + { + if ($format === 'xml') { + return new Generator\InMemory\Xml( + new Generator\InMemory\Xml\FieldTypeHashGenerator($this->normalizer), + ); + } + + return new Generator\Json( + new Generator\Json\FieldTypeHashGenerator($this->normalizer), + ); + } + + private function createVisitor(?string $format): Visitor + { + $format = $format ?: 'json'; + + $generator = $this->createGenerator($format); + + return new Visitor( + $generator, + $this->normalizer, + $format === 'xml' ? $this->xmlEncoder : $this->jsonEncoder, + $this->valueObjectVisitorResolver, + $format, + ); + } } diff --git a/src/lib/Output/Generator/InMemory/Xml.php b/src/lib/Output/Generator/InMemory/Xml.php index c3ed9ee2..e61f47a7 100644 --- a/src/lib/Output/Generator/InMemory/Xml.php +++ b/src/lib/Output/Generator/InMemory/Xml.php @@ -8,7 +8,6 @@ namespace Ibexa\Rest\Output\Generator\InMemory; -use Ibexa\Contracts\Rest\Output\VisitorAdapterNormalizer; use Ibexa\Rest\Output\Generator\Data; use Ibexa\Rest\Output\Generator\Data\ArrayList; use Ibexa\Rest\Output\Generator\Json; @@ -21,6 +20,8 @@ final class Xml extends Json { + public const string OUTER_ELEMENT = 'outer_element'; + public function getMediaType($name): string { return $this->generateMediaTypeWithVendor($name, 'xml', $this->vendor); @@ -36,10 +37,6 @@ public function startList($name): void $this->json = $array; } - /** - * @param string $name - * @param string $value - */ public function startAttribute($name, $value): void { $this->checkStartAttribute($name); @@ -74,11 +71,6 @@ public function startValueElement(string $name, $value, array $attributes = []): } } - /** - * End document. - * - * Returns the generated document as a string. - */ public function endDocument(mixed $data): string { parent::endDocument($data); @@ -111,7 +103,8 @@ public function getEncoderContext(array $data): array XmlEncoder::VERSION => '1.0', XmlEncoder::ENCODING => 'UTF-8', XmlEncoder::AS_COLLECTION => true, - VisitorAdapterNormalizer::OUTER_ELEMENT => true, + XmlEncoder::FORMAT_OUTPUT => true, + self::OUTER_ELEMENT => true, ]; } } diff --git a/src/lib/Output/Generator/InMemory/Xml/FieldTypeHashGenerator.php b/src/lib/Output/Generator/InMemory/Xml/FieldTypeHashGenerator.php index c4f000b5..7c7a06a0 100644 --- a/src/lib/Output/Generator/InMemory/Xml/FieldTypeHashGenerator.php +++ b/src/lib/Output/Generator/InMemory/Xml/FieldTypeHashGenerator.php @@ -30,7 +30,7 @@ protected function generateValue($parent, $value): mixed } } - protected function generateArrayValue($parent, $value) + protected function generateArrayValue($parent, $value): JsonObject { if ($this->isNumericArray($value)) { return $this->generateListArray($parent, $value); @@ -39,7 +39,7 @@ protected function generateArrayValue($parent, $value) } } - protected function generateListArray($parent, array $listArray) + protected function generateListArray($parent, array $listArray): JsonObject { $object = new JsonObject($parent); @@ -55,15 +55,7 @@ protected function generateListArray($parent, array $listArray) return $object; } - /** - * Generates a JSON object from the given $hashArray with $parent. - * - * @param \Ibexa\Rest\Output\Generator\Json\ArrayObject|\Ibexa\Rest\Output\Generator\Json\JsonObject $parent - * @param array $hashArray - * - * @return \Ibexa\Rest\Output\Generator\Json\JsonObject - */ - protected function generateHashArray($parent, array $hashArray) + protected function generateHashArray($parent, array $hashArray): JsonObject { $object = new JsonObject($parent); diff --git a/src/lib/Output/Normalizer/JsonObjectNormalizer.php b/src/lib/Output/Normalizer/JsonObjectNormalizer.php index 06dbafb4..f7313c0e 100644 --- a/src/lib/Output/Normalizer/JsonObjectNormalizer.php +++ b/src/lib/Output/Normalizer/JsonObjectNormalizer.php @@ -7,8 +7,8 @@ namespace Ibexa\Rest\Output\Normalizer; -use Ibexa\Contracts\Rest\Output\VisitorAdapterNormalizer; use Ibexa\Rest\Output\Generator\Data\ArrayList; +use Ibexa\Rest\Output\Generator\InMemory; use Ibexa\Rest\Output\Generator\Json\JsonObject; use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; @@ -27,8 +27,8 @@ public function normalize($object, ?string $format = null, array $context = []): { $vars = get_object_vars($object); - $isOuterElement = $context[VisitorAdapterNormalizer::OUTER_ELEMENT] ?? false; - unset($context[VisitorAdapterNormalizer::OUTER_ELEMENT]); + $isOuterElement = $context[InMemory\Xml::OUTER_ELEMENT] ?? false; + unset($context[InMemory\Xml::OUTER_ELEMENT]); $data = []; foreach ($vars as $key => $value) { diff --git a/tests/integration/IbexaTestKernel.php b/tests/integration/IbexaTestKernel.php index 6705424f..0b2e07e6 100644 --- a/tests/integration/IbexaTestKernel.php +++ b/tests/integration/IbexaTestKernel.php @@ -16,6 +16,7 @@ use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\Serializer\Serializer; final class IbexaTestKernel extends CoreIbexaTestKernel { @@ -45,6 +46,12 @@ protected static function getExposedServicesByClass(): iterable yield UriParserInterface::class; } + protected static function getExposedServicesById(): iterable + { + yield from parent::getExposedServicesById(); + yield 'ibexa.rest.serializer' => Serializer::class; + } + private static function loadRouting(ContainerBuilder $container): void { $container->loadFromExtension('framework', [ @@ -53,4 +60,11 @@ private static function loadRouting(ContainerBuilder $container): void ], ]); } + + protected function loadServices(LoaderInterface $loader): void + { + parent::loadServices($loader); + + $loader->load(__DIR__ . '/Resources/services.yaml'); + } } diff --git a/tests/integration/Resources/services.yaml b/tests/integration/Resources/services.yaml new file mode 100644 index 00000000..3f32462b --- /dev/null +++ b/tests/integration/Resources/services.yaml @@ -0,0 +1,4 @@ +services: + Ibexa\Tests\Integration\Rest\Serializer\TestDataObjectNormalizer: + tags: + - { name: ibexa.rest.serializer.normalizer } diff --git a/tests/integration/Serializer/SerializerTest.php b/tests/integration/Serializer/SerializerTest.php new file mode 100644 index 00000000..a9faad95 --- /dev/null +++ b/tests/integration/Serializer/SerializerTest.php @@ -0,0 +1,108 @@ +serializer = $this->getIbexaTestCore()->getServiceByClassName( + Serializer::class, + 'ibexa.rest.serializer', + ); + $this->locationService = $this->getIbexaTestCore()->getServiceByClassName( + LocationService::class, + ); + } + + public function testSerializeTestDataObject(): void + { + $dataObject = new TestDataObject( + 'some_string', + 1, + ); + + $expectedData = [ + 'string' => 'some_string', + 'int' => 1, + 'innerObject' => null, + 'location' => null, + ]; + + $serializedData = $this->serializer->serialize($dataObject, 'json'); + + self::assertSame(json_encode($expectedData), $serializedData); + } + + public function testNormalizeTestDataObjectWithApiLocation(): void + { + $dataObject = new TestDataObject( + 'some_string', + 1, + null, + $this->locationService->loadLocation(2), + ); + + $normalizedData = $this->serializer->normalize($dataObject); + + self::assertSame( + 'application/vnd.ibexa.api.Location+json', + $normalizedData['location']['_media-type'] ?? [], + ); + self::assertSame( + '/api/ibexa/v2/content/locations/1/2', + $normalizedData['location']['_href'] ?? [], + ); + self::assertSame($normalizedData['location']['id'] ?? null, 2); + + self::assertArrayHasKey('Content', $normalizedData['location'] ?? []); + self::assertSame( + 'application/vnd.ibexa.api.Content+json', + $normalizedData['location']['Content']['_media-type'], + ); + } + + public function testSerializeTestDataObjectWithApiLocation(): void + { + $dataObject = new TestDataObject( + 'some_string', + 1, + null, + $this->locationService->loadLocation(2), + ); + + $serializedData = $this->serializer->serialize($dataObject, 'xml'); + self::assertResponseMatchesXmlSnapshot( + $serializedData, + self::SNAPSHOT_DIR . '/TestDataObject.xml', + ); + + $serializedData = $this->serializer->serialize($dataObject, 'json'); + self::assertResponseMatchesJsonSnapshot( + $serializedData, + self::SNAPSHOT_DIR . '/TestDataObject.json', + ); + } +} diff --git a/tests/integration/Serializer/TestDataObject.php b/tests/integration/Serializer/TestDataObject.php new file mode 100644 index 00000000..04b6e3e1 --- /dev/null +++ b/tests/integration/Serializer/TestDataObject.php @@ -0,0 +1,22 @@ + + */ + public function normalize(mixed $object, ?string $format = null, array $context = []): array + { + assert($object instanceof TestDataObject); + + $scalarData = [ + 'string' => $object->string, + 'int' => $object->int, + 'innerObject' => $object->innerObject, + 'location' => null, + ]; + + if ($object->apiLocation instanceof Location) { + $normalizedLocation = $this->normalizer->normalize($object->apiLocation); + $scalarData['location'] = $normalizedLocation['Location'] ?? null; + } + + return $scalarData; + } + + public function supportsNormalization(mixed $data, ?string $format = null): bool + { + return $data instanceof TestDataObject; + } +} diff --git a/tests/integration/Serializer/_snapshot/TestDataObject.json b/tests/integration/Serializer/_snapshot/TestDataObject.json new file mode 100644 index 00000000..713554fc --- /dev/null +++ b/tests/integration/Serializer/_snapshot/TestDataObject.json @@ -0,0 +1,142 @@ +{ + "string": "some_string", + "int": 1, + "innerObject": null, + "location": { + "_media-type": "application\/vnd.ibexa.api.Location+json", + "_href": "\/api\/ibexa\/v2\/content\/locations\/1\/2", + "id": 2, + "priority": 0, + "hidden": false, + "invisible": false, + "ParentLocation": { + "_media-type": "application\/vnd.ibexa.api.Location+json", + "_href": "\/api\/ibexa\/v2\/content\/locations\/1" + }, + "pathString": "\/1\/2\/", + "depth": 1, + "childCount": 1, + "remoteId": "f3e90596361e31d496d4026eb624c983", + "Children": { + "_media-type": "application\/vnd.ibexa.api.LocationList+json", + "_href": "\/api\/ibexa\/v2\/content\/locations\/1\/2\/children" + }, + "Content": { + "_media-type": "application\/vnd.ibexa.api.Content+json", + "_href": "\/api\/ibexa\/v2\/content\/objects\/57" + }, + "sortField": "PRIORITY", + "sortOrder": "ASC", + "UrlAliases": { + "_media-type": "application\/vnd.ibexa.api.UrlAliasRefList+json", + "_href": "\/api\/ibexa\/v2\/content\/locations\/1\/2\/urlaliases" + }, + "ContentInfo": { + "_media-type": "application\/vnd.ibexa.api.ContentInfo+json", + "_href": "\/api\/ibexa\/v2\/content\/objects\/57", + "Content": { + "_media-type": "application\/vnd.ibexa.api.Content+json", + "_href": "\/api\/ibexa\/v2\/content\/objects\/57", + "_remoteId": "8a9c9c761004866fb458d89910f52bee", + "_id": 57, + "ContentType": { + "_media-type": "application\/vnd.ibexa.api.ContentType+json", + "_href": "\/api\/ibexa\/v2\/content\/types\/21" + }, + "Name": "Home", + "TranslatedName": "Home", + "Versions": { + "_media-type": "application\/vnd.ibexa.api.VersionList+json", + "_href": "\/api\/ibexa\/v2\/content\/objects\/57\/versions" + }, + "CurrentVersion": { + "_media-type": "application\/vnd.ibexa.api.Version+json", + "_href": "\/api\/ibexa\/v2\/content\/objects\/57\/currentversion", + "Version": { + "_media-type": "application\/vnd.ibexa.api.Version+json", + "_href": "\/api\/ibexa\/v2\/content\/objects\/57\/versions\/1", + "VersionInfo": { + "id": 504, + "versionNo": 1, + "status": "PUBLISHED", + "modificationDate": "2007-11-28T16:51:36+00:00", + "Creator": { + "_media-type": "application\/vnd.ibexa.api.User+json", + "_href": "\/api\/ibexa\/v2\/user\/users\/14" + }, + "creationDate": "2007-11-28T16:50:55+00:00", + "initialLanguageCode": "eng-GB", + "languageCodes": "eng-GB", + "VersionTranslationInfo": { + "_media-type": "application\/vnd.ibexa.api.VersionTranslationInfo+json", + "Language": [ + { + "languageCode": "eng-GB" + } + ] + }, + "names": { + "value": [ + { + "_languageCode": "eng-GB", + "#text": "Home" + } + ] + }, + "Content": { + "_media-type": "application\/vnd.ibexa.api.ContentInfo+json", + "_href": "\/api\/ibexa\/v2\/content\/objects\/57" + } + }, + "Fields": { + "field": [ + { + "id": 186, + "fieldDefinitionIdentifier": "name", + "languageCode": "eng-GB", + "fieldTypeIdentifier": "ezstring", + "fieldValue": "Home" + } + ] + }, + "Relations": { + "_media-type": "application\/vnd.ibexa.api.RelationList+json", + "_href": "\/api\/ibexa\/v2\/content\/objects\/57\/versions\/1\/relations", + "Relation": [] + }, + "Thumbnail": { + "_media-type": "application\/vnd.ibexa.api.Thumbnail+json" + } + } + }, + "Section": { + "_media-type": "application\/vnd.ibexa.api.Section+json", + "_href": "\/api\/ibexa\/v2\/content\/sections\/1" + }, + "MainLocation": { + "_media-type": "application\/vnd.ibexa.api.Location+json", + "_href": "\/api\/ibexa\/v2\/content\/locations\/1\/2" + }, + "Locations": { + "_media-type": "application\/vnd.ibexa.api.LocationList+json", + "_href": "\/api\/ibexa\/v2\/content\/objects\/57\/locations" + }, + "Owner": { + "_media-type": "application\/vnd.ibexa.api.User+json", + "_href": "\/api\/ibexa\/v2\/user\/users\/14" + }, + "lastModificationDate": "2007-11-28T16:51:36+00:00", + "publishedDate": "2007-11-19T13:54:46+00:00", + "mainLanguageCode": "eng-GB", + "currentVersionNo": 1, + "alwaysAvailable": true, + "isHidden": false, + "status": "PUBLISHED", + "ObjectStates": { + "_media-type": "application\/vnd.ibexa.api.ContentObjectStates+json", + "_href": "\/api\/ibexa\/v2\/content\/objects\/57\/objectstates" + } + } + } + } +} \ No newline at end of file diff --git a/tests/integration/Serializer/_snapshot/TestDataObject.xml b/tests/integration/Serializer/_snapshot/TestDataObject.xml new file mode 100644 index 00000000..d3b038ee --- /dev/null +++ b/tests/integration/Serializer/_snapshot/TestDataObject.xml @@ -0,0 +1,136 @@ + + some_string + 1 + + + <_media-type>application/vnd.ibexa.api.Location+json + <_href>/api/ibexa/v2/content/locations/1/2 + 2 + 0 + 0 + 0 + + <_media-type>application/vnd.ibexa.api.Location+json + <_href>/api/ibexa/v2/content/locations/1 + + /1/2/ + 1 + 1 + f3e90596361e31d496d4026eb624c983 + + <_media-type>application/vnd.ibexa.api.LocationList+json + <_href>/api/ibexa/v2/content/locations/1/2/children + + + <_media-type>application/vnd.ibexa.api.Content+json + <_href>/api/ibexa/v2/content/objects/57 + + PRIORITY + ASC + + <_media-type>application/vnd.ibexa.api.UrlAliasRefList+json + <_href>/api/ibexa/v2/content/locations/1/2/urlaliases + + + <_media-type>application/vnd.ibexa.api.ContentInfo+json + <_href>/api/ibexa/v2/content/objects/57 + + <_media-type>application/vnd.ibexa.api.Content+json + <_href>/api/ibexa/v2/content/objects/57 + <_remoteId>8a9c9c761004866fb458d89910f52bee + <_id>57 + + <_media-type>application/vnd.ibexa.api.ContentType+json + <_href>/api/ibexa/v2/content/types/21 + + Home + Home + + <_media-type>application/vnd.ibexa.api.VersionList+json + <_href>/api/ibexa/v2/content/objects/57/versions + + + <_media-type>application/vnd.ibexa.api.Version+json + <_href>/api/ibexa/v2/content/objects/57/currentversion + + <_media-type>application/vnd.ibexa.api.Version+json + <_href>/api/ibexa/v2/content/objects/57/versions/1 + + 504 + 1 + PUBLISHED + 2007-11-28T16:51:36+00:00 + + <_media-type>application/vnd.ibexa.api.User+json + <_href>/api/ibexa/v2/user/users/14 + + 2007-11-28T16:50:55+00:00 + eng-GB + eng-GB + + <_media-type>application/vnd.ibexa.api.VersionTranslationInfo+json + + eng-GB + + + + + <_languageCode>eng-GB + Home + + + + <_media-type>application/vnd.ibexa.api.ContentInfo+json + <_href>/api/ibexa/v2/content/objects/57 + + + + + 186 + name + eng-GB + ezstring + Home + + + + <_media-type>application/vnd.ibexa.api.RelationList+json + <_href>/api/ibexa/v2/content/objects/57/versions/1/relations + + + + <_media-type>application/vnd.ibexa.api.Thumbnail+json + + + +
+ <_media-type>application/vnd.ibexa.api.Section+json + <_href>/api/ibexa/v2/content/sections/1 +
+ + <_media-type>application/vnd.ibexa.api.Location+json + <_href>/api/ibexa/v2/content/locations/1/2 + + + <_media-type>application/vnd.ibexa.api.LocationList+json + <_href>/api/ibexa/v2/content/objects/57/locations + + + <_media-type>application/vnd.ibexa.api.User+json + <_href>/api/ibexa/v2/user/users/14 + + 2007-11-28T16:51:36+00:00 + 2007-11-19T13:54:46+00:00 + eng-GB + 1 + 1 + 0 + PUBLISHED + + <_media-type>application/vnd.ibexa.api.ContentObjectStates+json + <_href>/api/ibexa/v2/content/objects/57/objectstates + +
+
+
+
diff --git a/tests/integration/bootstrap.php b/tests/integration/bootstrap.php index 34998520..a984004e 100644 --- a/tests/integration/bootstrap.php +++ b/tests/integration/bootstrap.php @@ -6,8 +6,11 @@ */ declare(strict_types=1); +use Ibexa\Contracts\Core\Test\Persistence\Fixture\FixtureImporter; +use Ibexa\Tests\Core\Repository\LegacySchemaImporter; use Ibexa\Tests\Integration\Rest\IbexaTestKernel; use Symfony\Bundle\FrameworkBundle\Console\Application; +use Symfony\Component\Console\Input\ArrayInput; chdir(dirname(__DIR__, 2)); @@ -17,6 +20,30 @@ $application = new Application($kernel); $application->setAutoExit(false); -// Skipping database initialization until really needed by integration tests +$databaseUrl = getenv('DATABASE_URL'); +if ($databaseUrl !== false && strpos($databaseUrl, 'sqlite') !== 0) { + $application->run(new ArrayInput([ + 'command' => 'doctrine:database:drop', + '--if-exists' => '1', + '--force' => '1', + ])); +} + +$application->run(new ArrayInput([ + 'command' => 'doctrine:database:create', +])); + +/** @var \Psr\Container\ContainerInterface $testContainer */ +$testContainer = $kernel->getContainer()->get('test.service_container'); + +$schemaImporter = $testContainer->get(LegacySchemaImporter::class); +foreach ($kernel->getSchemaFiles() as $file) { + $schemaImporter->importSchema($file); +} + +$fixtureImporter = $testContainer->get(FixtureImporter::class); +foreach ($kernel->getFixtures() as $fixture) { + $fixtureImporter->import($fixture); +} $kernel->shutdown(); diff --git a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateBoolValue.out b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateBoolValue.out index 0231e982..ca195a00 100644 --- a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateBoolValue.out +++ b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateBoolValue.out @@ -1,2 +1,4 @@ -true + + true + diff --git a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateComplexValueAuthor.out b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateComplexValueAuthor.out index 50cdf03f..f390ad90 100644 --- a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateComplexValueAuthor.out +++ b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateComplexValueAuthor.out @@ -1,2 +1,15 @@ -1Joe Sindelfingensindelfingen@example.com2Joe Bielefeldbielefeld@example.com + + + + 1 + Joe Sindelfingen + sindelfingen@example.com + + + 2 + Joe Bielefeld + bielefeld@example.com + + + diff --git a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateEmptyStringValue.out b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateEmptyStringValue.out index 0ab663cf..33067672 100644 --- a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateEmptyStringValue.out +++ b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateEmptyStringValue.out @@ -1,2 +1,4 @@ - + + + diff --git a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateFloatValue.out b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateFloatValue.out index 01e8b633..f6a405bd 100644 --- a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateFloatValue.out +++ b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateFloatValue.out @@ -1,2 +1,4 @@ -23.424242 + + 23.424242 + diff --git a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateHashArrayMixedValue.out b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateHashArrayMixedValue.out index 94495bd4..aba5a4d2 100644 --- a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateHashArrayMixedValue.out +++ b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateHashArrayMixedValue.out @@ -1,2 +1,9 @@ -23trueSindelfingen + + + 23 + true + Sindelfingen + + + diff --git a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateHashArrayValue.out b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateHashArrayValue.out index acf8b8c5..82f83d2e 100644 --- a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateHashArrayValue.out +++ b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateHashArrayValue.out @@ -1,2 +1,9 @@ -23trueSindelfingen + + + 23 + true + Sindelfingen + + + diff --git a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateIntegerValue.out b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateIntegerValue.out index d3d45a0a..a5a9ec4e 100644 --- a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateIntegerValue.out +++ b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateIntegerValue.out @@ -1,2 +1,4 @@ -23 + + 23 + diff --git a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateListArrayValue.out b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateListArrayValue.out index 6cf1a0fd..0bd769c0 100644 --- a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateListArrayValue.out +++ b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateListArrayValue.out @@ -1,2 +1,9 @@ -23trueSindelfingen + + + 23 + true + Sindelfingen + + + diff --git a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateNull.out b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateNull.out index 35808898..40d692ad 100644 --- a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateNull.out +++ b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateNull.out @@ -1,2 +1,4 @@ - + + + diff --git a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateObjectUsingNormalizer.out b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateObjectUsingNormalizer.out index 35808898..40d692ad 100644 --- a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateObjectUsingNormalizer.out +++ b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateObjectUsingNormalizer.out @@ -1,2 +1,4 @@ - + + + diff --git a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateStringValue.out b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateStringValue.out index e8101bab..a14540bc 100644 --- a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateStringValue.out +++ b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateStringValue.out @@ -1,2 +1,4 @@ -Sindelfingen + + Sindelfingen + diff --git a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateStringValueWithSpecialChars.out b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateStringValueWithSpecialChars.out index 76207592..00ce20a7 100644 --- a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateStringValueWithSpecialChars.out +++ b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateStringValueWithSpecialChars.out @@ -1,2 +1,4 @@ -Sindelfingen]]> + + Sindelfingen]]> + diff --git a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateUsingNormalizer.out b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateUsingNormalizer.out index 5dfd754a..c57c5eb0 100644 --- a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateUsingNormalizer.out +++ b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateUsingNormalizer.out @@ -1,2 +1,14 @@ -1foobar4256 + + + 1 + foo + + bar + + + 42 + 56 + + + diff --git a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateWithMissingNormalizer.out b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateWithMissingNormalizer.out index 35808898..40d692ad 100644 --- a/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateWithMissingNormalizer.out +++ b/tests/lib/Output/Generator/_fixtures/Xml_FieldTypeHashGeneratorTest__testGenerateWithMissingNormalizer.out @@ -1,2 +1,4 @@ - + + + diff --git a/tests/lib/Output/Generator/_fixtures/testGeneratorElementList.xml b/tests/lib/Output/Generator/_fixtures/testGeneratorElementList.xml index 2172fc8c..81a57359 100644 --- a/tests/lib/Output/Generator/_fixtures/testGeneratorElementList.xml +++ b/tests/lib/Output/Generator/_fixtures/testGeneratorElementList.xml @@ -1,2 +1,5 @@ - + + + + diff --git a/tests/lib/Output/Generator/_fixtures/testGeneratorHashElement.xml b/tests/lib/Output/Generator/_fixtures/testGeneratorHashElement.xml index 34657d61..1157d565 100644 --- a/tests/lib/Output/Generator/_fixtures/testGeneratorHashElement.xml +++ b/tests/lib/Output/Generator/_fixtures/testGeneratorHashElement.xml @@ -1,2 +1,5 @@ -element value 1element value 2 + + element value 1 + element value 2 + diff --git a/tests/lib/Output/Generator/_fixtures/testGeneratorStackedElement.xml b/tests/lib/Output/Generator/_fixtures/testGeneratorStackedElement.xml index 16f32ea2..df816101 100644 --- a/tests/lib/Output/Generator/_fixtures/testGeneratorStackedElement.xml +++ b/tests/lib/Output/Generator/_fixtures/testGeneratorStackedElement.xml @@ -1,2 +1,4 @@ - + + + diff --git a/tests/lib/Output/Generator/_fixtures/testGeneratorStartEndValueElement.xml b/tests/lib/Output/Generator/_fixtures/testGeneratorStartEndValueElement.xml index 90110575..b82f96d4 100644 --- a/tests/lib/Output/Generator/_fixtures/testGeneratorStartEndValueElement.xml +++ b/tests/lib/Output/Generator/_fixtures/testGeneratorStartEndValueElement.xml @@ -1,2 +1,4 @@ -42 + + 42 + diff --git a/tests/lib/Output/Generator/_fixtures/testGeneratorValueElement.xml b/tests/lib/Output/Generator/_fixtures/testGeneratorValueElement.xml index 90110575..b82f96d4 100644 --- a/tests/lib/Output/Generator/_fixtures/testGeneratorValueElement.xml +++ b/tests/lib/Output/Generator/_fixtures/testGeneratorValueElement.xml @@ -1,2 +1,4 @@ -42 + + 42 + diff --git a/tests/lib/Output/Generator/_fixtures/testGeneratorValueList.xml b/tests/lib/Output/Generator/_fixtures/testGeneratorValueList.xml index c744fd8f..7b514ef6 100644 --- a/tests/lib/Output/Generator/_fixtures/testGeneratorValueList.xml +++ b/tests/lib/Output/Generator/_fixtures/testGeneratorValueList.xml @@ -1,2 +1,5 @@ -value1value2 + + value1 + value2 + diff --git a/tests/lib/Output/VisitorTest.php b/tests/lib/Output/VisitorTest.php index baced28e..9db7f877 100644 --- a/tests/lib/Output/VisitorTest.php +++ b/tests/lib/Output/VisitorTest.php @@ -4,7 +4,6 @@ * @copyright Copyright (C) Ibexa AS. All rights reserved. * @license For full copyright and license information view LICENSE file distributed with this source code. */ -declare(strict_types=1); namespace Ibexa\Tests\Rest\Output; @@ -20,26 +19,84 @@ final class VisitorTest extends TestCase { - public function testVisitDocument(): void + private Visitor $visitor; + + private NormalizerInterface&MockObject $normalizer; + + private EncoderInterface&MockObject $encoder; + + private Generator&MockObject $generator; + + private ValueObjectVisitorResolverInterface&MockObject $valueObjectVisitorResolver; + + public function setUp(): void { - //TODO refactor + parent::setUp(); + + $this->generator = $this->createMock(Generator::class); + $this->normalizer = $this->createMock(NormalizerInterface::class); + $this->encoder = $this->createMock(EncoderInterface::class); + $this->valueObjectVisitorResolver = $this->createMock(ValueObjectVisitorResolverInterface::class); + + $this->visitor = new Visitor( + $this->generator, + $this->normalizer, + $this->encoder, + $this->valueObjectVisitorResolver, + 'json', + ); } - public function testVisitEmptyDocument(): void + public function testVisitDocument(): void { - //TODO refactor + $data = new stdClass(); + $content = 'Hello world!'; + + $this->normalizer + ->expects(self::once()) + ->method('normalize') + ->with($data, 'json', ['visitor' => $this->visitor]) + ->willReturn($content); + + $this->encoder + ->expects(self::once()) + ->method('encode') + ->with($content) + ->willReturn($content); + + self::assertEquals( + new Response($content, Response::HTTP_OK, []), + $this->visitor->visit($data), + ); } - public function testVisitValueObject(): void + public function testVisitEmptyDocument(): void { - //TODO refactor + $data = new stdClass(); + + $this->normalizer + ->expects(self::once()) + ->method('normalize') + ->with($data, 'json', ['visitor' => $this->visitor]) + ->willReturn(null); + + $this->encoder + ->expects(self::once()) + ->method('encode') + ->with(null) + ->willReturn(null); + + self::assertEquals( + new Response(null, Response::HTTP_OK, []), + $this->visitor->visit($data), + ); } public function testSetHeaders(): void { $data = new stdClass(); - $visitor = $this->getVisitorMock(); + $visitor = $this->visitor; $visitor->setHeader('Content-Type', 'text/xml'); self::assertEquals( @@ -48,22 +105,20 @@ public function testSetHeaders(): void Response::HTTP_OK, [ 'Content-Type' => 'text/xml', - ] + ], ), - $visitor->visit($data) + $visitor->visit($data), ); } /** * @todo This is a test for a feature that needs refactoring. - * - * @see \Ibexa\Contracts\Rest\Output\Visitor::visit */ - public function testSetFilteredHeaders() + public function testSetFilteredHeaders(): void { $data = new stdClass(); - $visitor = $this->getVisitorMock(); + $visitor = $this->visitor; $visitor->setHeader('Content-Type', 'text/xml'); $visitor->setHeader('Accept-Patch', false); @@ -73,9 +128,9 @@ public function testSetFilteredHeaders() Response::HTTP_OK, [ 'Content-Type' => 'text/xml', - ] + ], ), - $visitor->visit($data) + $visitor->visit($data), ); } @@ -83,7 +138,7 @@ public function testSetHeadersNoOverwrite(): void { $data = new stdClass(); - $visitor = $this->getVisitorMock(); + $visitor = $this->visitor; $visitor->setHeader('Content-Type', 'text/xml'); $visitor->setHeader('Content-Type', 'text/html'); @@ -93,9 +148,9 @@ public function testSetHeadersNoOverwrite(): void Response::HTTP_OK, [ 'Content-Type' => 'text/xml', - ] + ], ), - $visitor->visit($data) + $visitor->visit($data), ); } @@ -103,7 +158,7 @@ public function testSetHeaderResetAfterVisit(): void { $data = new stdClass(); - $visitor = $this->getVisitorMock(); + $visitor = $this->visitor; $visitor->setHeader('Content-Type', 'text/xml'); @@ -114,9 +169,9 @@ public function testSetHeaderResetAfterVisit(): void new Response( null, Response::HTTP_OK, - [] + [], ), - $result + $result, ); } @@ -124,15 +179,15 @@ public function testSetStatusCode(): void { $data = new stdClass(); - $visitor = $this->getVisitorMock(); + $visitor = $this->visitor; $visitor->setStatus(201); self::assertEquals( new Response( null, - Response::HTTP_CREATED + Response::HTTP_CREATED, ), - $visitor->visit($data) + $visitor->visit($data), ); } @@ -140,7 +195,7 @@ public function testSetStatusCodeNoOverride(): void { $data = new stdClass(); - $visitor = $this->getVisitorMock(); + $visitor = $this->visitor; $visitor->setStatus(201); $visitor->setStatus(404); @@ -148,45 +203,9 @@ public function testSetStatusCodeNoOverride(): void self::assertEquals( new Response( null, - Response::HTTP_CREATED + Response::HTTP_CREATED, ), - $visitor->visit($data) + $visitor->visit($data), ); } - - public function getValueObjectVisitorResolverMock(): ValueObjectVisitorResolverInterface&MockObject - { - return $this->createMock(ValueObjectVisitorResolverInterface::class); - } - - protected function getGeneratorMock(): Generator&MockObject - { - return $this->createMock(Generator::class); - } - - protected function getNormalizerMock(): NormalizerInterface&MockObject - { - return $this->createMock(NormalizerInterface::class); - } - - protected function getEncoderMock(): EncoderInterface&MockObject - { - return $this->createMock(EncoderInterface::class); - } - - protected function getVisitorMock(): Visitor&MockObject - { - return $this->getMockBuilder(Visitor::class) - ->setMethods(['visitValueObject']) - ->setConstructorArgs( - [ - $this->getGeneratorMock(), - $this->getNormalizerMock(), - $this->getEncoderMock(), - $this->getValueObjectVisitorResolverMock(), - 'json', - ], - ) - ->getMock(); - } }