Skip to content

Commit

Permalink
Make unit tests use the new XML generator
Browse files Browse the repository at this point in the history
  • Loading branch information
barw4 committed Oct 1, 2024
1 parent 847a54d commit 5cf488f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 29 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6526,7 +6526,7 @@ parameters:
path: tests/lib/Output/ValueObjectVisitorBaseTest.php

-
message: "#^Property Ibexa\\\\Tests\\\\Rest\\\\Output\\\\ValueObjectVisitorBaseTest\\:\\:\\$generator \\(Ibexa\\\\Rest\\\\Output\\\\Generator\\\\Xml\\) in isset\\(\\) is not nullable\\.$#"
message: "#^Property Ibexa\\\\Tests\\\\Rest\\\\Output\\\\ValueObjectVisitorBaseTest\\:\\:\\$generator \\(Ibexa\\\\Rest\\\\Output\\\\Generator\\\\InMemory\\\\Xml\\) in isset\\(\\) is not nullable\\.$#"
count: 1
path: tests/lib/Output/ValueObjectVisitorBaseTest.php

Expand Down
69 changes: 49 additions & 20 deletions src/lib/Output/Generator/InMemory/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

use Ibexa\Rest\Output\Generator\Json;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

final class Xml extends Json
{
Expand All @@ -29,6 +31,11 @@ public function startAttribute($name, $value): void
$this->json->{'@' . $name} = $value;
}

public function serializeBool($boolValue)
{
return $boolValue ? 'true' : 'false';
}

public function startValueElement(string $name, $value, array $attributes = []): void
{
$this->checkStartValueElement($name);
Expand All @@ -51,6 +58,26 @@ 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);

$normalizedData = $this->toArray();

$encoderContext = $this->getEncoderContext($normalizedData);
$encoderContext['as_collection'] = true;
$transformedData = $this->transformData($normalizedData);

$serializer = new Serializer([new ObjectNormalizer()], [new XmlEncoder()]);

return $serializer->encode($transformedData, 'xml', $encoderContext);
}

public function transformData(array $normalizedData): array
{
$topNodeName = array_key_first($normalizedData);
Expand All @@ -64,28 +91,30 @@ public function transformData(array $normalizedData): array
$data['#'] = $normalizedData[$topNodeName];
}

return $this->clearEmptyArrays($data);
return $data;
}

/**
* @param array<mixed> $array
*
* @return array<mixed>
*/
private function clearEmptyArrays(array &$array): array
{
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key] = $this->clearEmptyArrays($value);

if (empty($array[$key])) {
unset($array[$key]);
}
}
}

return $array;
}
// /**
// * @param array<mixed> $array
// *
// * @return array<mixed>
// */
// private function clearEmptyArrays(array &$array): array
// {
// foreach ($array as $key => &$value) {
// if (is_array($value)) {
// // Recursively apply the function to the nested array
// $this->clearEmptyArrays($value);
//
// // Remove the field if it's an empty array after recursion
// if (empty($value)) {
// unset($array[$key]);
// }
// }
// }
//
// return $array;
// }

public function getEncoderContext(array $data): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Output/Generator/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public function generateFieldTypeHash($hashElementName, $hashValue)
*
* @param bool $boolValue
*
* @return bool
* @return mixed
*/
public function serializeBool($boolValue)
{
Expand Down
4 changes: 4 additions & 0 deletions src/lib/Output/Generator/Json/JsonObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
* @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\Rest\Output\Generator\Json;

use AllowDynamicProperties;

/**
* Json object.
*
* Special JSON object (\stdClass) implementation, which allows to access the
* parent object it is assigned to again.
*/
#[AllowDynamicProperties]
class JsonObject
{
/**
Expand Down
12 changes: 6 additions & 6 deletions tests/lib/Output/ValueObjectVisitorBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ abstract class ValueObjectVisitorBaseTest extends Server\BaseTest
/**
* Output generator.
*
* @var \Ibexa\Rest\Output\Generator\Xml
* @var \Ibexa\Rest\Output\Generator\InMemory\Xml
*/
protected $generator;

Expand Down Expand Up @@ -90,15 +90,15 @@ protected function getResponseMock()
/**
* Gets the output generator.
*
* @return \Ibexa\Rest\Output\Generator\Xml
* @return \Ibexa\Rest\Output\Generator\InMemory\Xml
*/
protected function getGenerator()
{
if (!isset($this->generator)) {
$this->generator = new Generator\Xml(
new Generator\Xml\FieldTypeHashGenerator(
$this->createMock(NormalizerInterface::class)
)
$this->generator = new Generator\InMemory\Xml(
new Generator\InMemory\Xml\FieldTypeHashGenerator(
$this->createMock(NormalizerInterface::class),
),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use DOMDocument;
use DOMXPath;
use Ibexa\Contracts\Rest\Output\ValueObjectVisitor;
use Ibexa\Rest\Output\Generator\Xml;
use Ibexa\Rest\Output\Generator\InMemory\Xml;
use Ibexa\Rest\Server\Output\ValueObjectVisitor\Exception as ExceptionValueObjectVisitor;
use Ibexa\Tests\Rest\Output\ValueObjectVisitorBaseTest;
use Symfony\Contracts\Translation\TranslatorInterface;
Expand Down

0 comments on commit 5cf488f

Please sign in to comment.