Skip to content

Commit

Permalink
Adjust ContentViewSerializationSubscriber to be compatible with the A…
Browse files Browse the repository at this point in the history
…rraySerializer (#59)
  • Loading branch information
niklasnatter authored and alexander-schranz committed Dec 12, 2019
1 parent c75a8bc commit 1d30391
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 7 deletions.
17 changes: 10 additions & 7 deletions EventSubscriber/ContentViewSerializationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@ public static function getSubscribedEvents()
return [
[
'event' => Events::POST_SERIALIZE,
'format' => 'json',
'method' => 'onPostSerializeJson',
],
[
'event' => Events::POST_SERIALIZE,
'format' => 'array',
'method' => 'onPostSerializeArray',
'method' => 'onPostSerialize',
],
];
}
Expand Down Expand Up @@ -67,6 +61,15 @@ public function __construct(
$this->contentTypeManager = $contentTypeManager;
}

public function onPostSerialize(ObjectEvent $event): void
{
if ($event->getContext()->hasAttribute('array_serializer')) {
$this->onPostSerializeArray($event);
} else {
$this->onPostSerializeJson($event);
}
}

public function onPostSerializeJson(ObjectEvent $event): void
{
$object = $event->getObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,68 @@ public function testSerializeToJsonMissingField(): void
json_decode($result, true)
);
}

public function testSerializeToArray(): void
{
$contentView = new ContentView(
self::RESOURCE_KEY,
'123-123-123',
'de',
'default',
['title' => 'Sulu', 'article' => '<p>Sulu is awesome</p>']
);

$result = $this->getContainer()->get('sulu_core.array_serializer')->serialize(
$contentView,
SerializationContext::create()->setSerializeNull(true)
);

$this->assertSame(
[
'id' => '123-123-123',
'template' => 'default',
'content' => [
'title' => 'Sulu',
'article' => '<p>Sulu is awesome</p>',
],
'view' => [
'title' => [],
'article' => [],
],
],
$result
);
}

public function testSerializeToArrayMissingField(): void
{
$contentView = new ContentView(
self::RESOURCE_KEY,
'123-123-123',
'de',
'default',
['title' => 'Sulu']
);

$result = $this->getContainer()->get('sulu_core.array_serializer')->serialize(
$contentView,
SerializationContext::create()->setSerializeNull(true)
);

$this->assertSame(
[
'id' => '123-123-123',
'template' => 'default',
'content' => [
'title' => 'Sulu',
'article' => null,
],
'view' => [
'title' => [],
'article' => [],
],
],
$result
);
}
}

0 comments on commit 1d30391

Please sign in to comment.