Skip to content

Commit

Permalink
stop using preview cache (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-rath authored May 13, 2020
1 parent 509af1d commit fec38e2
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 12 deletions.
18 changes: 15 additions & 3 deletions Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,16 @@ public function setContext($object, $locale, array $context): ContentProjectionI
}

/**
* @param mixed $object
* @param ContentProjectionInterface $object
*
* @return string
*/
public function serialize($object)
{
return serialize($object);
return json_encode([
'id' => $object->getContentId(),
'locale' => $object->getDimension()->getLocale(),
]) ?: '[]';
}

/**
Expand All @@ -136,7 +139,16 @@ public function serialize($object)
*/
public function deserialize($serializedObject, $objectClass)
{
return unserialize($serializedObject);
$data = json_decode($serializedObject, true);

$id = $data['id'] ?? null;
$locale = $data['locale'] ?? null;

if (!$id || !$locale) {
return null;
}

return $this->getObject($id, $locale);
}

protected function loadProjection(ContentRichEntityInterface $contentRichEntity, string $locale): ?ContentProjectionInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentProjectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentProjectionTrait;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ExcerptInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ExcerptTrait;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\SeoInterface;
Expand Down Expand Up @@ -245,23 +246,90 @@ public static function getTemplateType(): string

public function testSerialize(): void
{
$object = new \stdClass();
$object->foo = 'bar';
$serializedObject = serialize($object);
$projection = $this->prophesize(ContentProjectionInterface::class);
$projection->getContentId()->willReturn('123-456');
$dimension = $this->prophesize(DimensionInterface::class);
$dimension->getLocale()->willReturn('en');
$projection->getDimension()->willReturn($dimension->reveal());

$serializedObject = json_encode([
'id' => '123-456',
'locale' => 'en',
]);

$result = $this->contentObjectProvider->serialize($object);
$result = $this->contentObjectProvider->serialize($projection->reveal());

$this->assertSame($serializedObject, $result);
}

public function testDeserialize(): void
{
$object = new \stdClass();
$object->foo = 'bar';
$serializedObject = serialize($object);
$queryBuilder = $this->prophesize(QueryBuilder::class);

$this->entityManager->createQueryBuilder()->willReturn($queryBuilder->reveal())->shouldBeCalledTimes(1);

$queryBuilder->select(Argument::type('string'))->will(function () {
return func_get_arg(\func_num_args() - 2);
})->shouldBeCalledTimes(1);

$queryBuilder->from(Argument::type('string'), Argument::type('string'))->will(function () {
return func_get_arg(\func_num_args() - 2);
})->shouldBeCalledTimes(1);

$queryBuilder->where(Argument::type('string'))->will(function () {
return func_get_arg(\func_num_args() - 2);
})->shouldBeCalledTimes(1);

$queryBuilder->setParameter(Argument::type('string'), Argument::any())->will(function () {
return func_get_arg(\func_num_args() - 2);
})->shouldBeCalledTimes(1);

$query = $this->prophesize(AbstractQuery::class);

$queryBuilder->getQuery()->willReturn($query->reveal())->shouldBeCalledTimes(1);

$entity = $this->prophesize(ContentRichEntityInterface::class);

$query->getSingleResult()->willReturn($entity->reveal())->shouldBeCalledTimes(1);

$projection = $this->prophesize(ContentProjectionInterface::class);

$this->contentResolver->resolve(
$entity->reveal(),
Argument::type('array')
)->willReturn($projection->reveal())->shouldBeCalledTimes(1);

$serializedObject = json_encode([
'id' => '123-456',
'locale' => 'en',
]) ?: '';

$result = $this->contentObjectProvider->deserialize($serializedObject, ContentProjectionInterface::class);

$this->assertSame($projection->reveal(), $result);
}

public function testDeserializeIdNull(): void
{
$serializedObject = json_encode([
'id' => null,
'locale' => 'en',
]) ?: '';

$result = $this->contentObjectProvider->deserialize($serializedObject, ContentProjectionInterface::class);

$this->assertNull($result);
}

public function testDeserializeLocaleNull(): void
{
$serializedObject = json_encode([
'id' => '123-456',
'locale' => null,
]) ?: '';

$deserializedObject = $this->contentObjectProvider->deserialize($serializedObject, \get_class($object));
$result = $this->contentObjectProvider->deserialize($serializedObject, ContentProjectionInterface::class);

$this->assertSame($deserializedObject->foo, $object->foo);
$this->assertNull($result);
}
}

0 comments on commit fec38e2

Please sign in to comment.