From fec38e22c73082a1323b00519b1abf8cf522c3ba Mon Sep 17 00:00:00 2001 From: Luca Rath-Heel Date: Wed, 13 May 2020 10:23:03 +0200 Subject: [PATCH] stop using preview cache (#140) --- .../Sulu/Preview/ContentObjectProvider.php | 18 +++- .../Preview/ContentObjectProviderTest.php | 86 +++++++++++++++++-- 2 files changed, 92 insertions(+), 12 deletions(-) diff --git a/Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php b/Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php index 0cac9814..055df815 100644 --- a/Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php +++ b/Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php @@ -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(), + ]) ?: '[]'; } /** @@ -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 diff --git a/Tests/Unit/Content/Infrastructure/Sulu/Preview/ContentObjectProviderTest.php b/Tests/Unit/Content/Infrastructure/Sulu/Preview/ContentObjectProviderTest.php index 32afaaa1..70d5edde 100644 --- a/Tests/Unit/Content/Infrastructure/Sulu/Preview/ContentObjectProviderTest.php +++ b/Tests/Unit/Content/Infrastructure/Sulu/Preview/ContentObjectProviderTest.php @@ -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; @@ -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); } }