Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-rath committed Feb 17, 2020
1 parent 989f9bd commit 7c734de
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,63 @@

namespace Sulu\Bundle\ContentBundle\Tests\Unit\Content\Infrastructure\Sulu\Admin;

use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Sulu\Bundle\AdminBundle\Admin\View\FormViewBuilderInterface;
use Sulu\Bundle\AdminBundle\Admin\View\PreviewFormViewBuilderInterface;
use Sulu\Bundle\AdminBundle\Admin\View\ViewBuilderFactory;
use Sulu\Bundle\AdminBundle\Admin\View\ViewCollection;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\ContentResolverInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\CategoryFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\TagFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Admin\ContentViewBuilder;
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Admin\ContentViewBuilderInterface;
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Preview\ContentObjectProvider;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example;
use Sulu\Bundle\PreviewBundle\Preview\Object\PreviewObjectProviderInterface;
use Sulu\Bundle\PreviewBundle\Preview\Object\PreviewObjectProviderRegistry;
use Sulu\Bundle\PreviewBundle\Preview\Object\PreviewObjectProviderRegistryInterface;
use Sulu\Component\Content\Compat\Structure\LegacyPropertyFactory;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;

class ContentViewBuilderTest extends TestCase
{
protected function getContentViewBuilder(): ContentViewBuilderInterface
protected function getContentViewBuilder(
PreviewObjectProviderRegistryInterface $previewObjectProviderRegistry = null
): ContentViewBuilderInterface {
if (null === $previewObjectProviderRegistry) {
$previewObjectProviderRegistry = $this->getPreviewObjectProviderRegistry([]);
}

return new ContentViewBuilder(new ViewBuilderFactory(), $previewObjectProviderRegistry);
}

/**
* @param array<string, PreviewObjectProviderInterface> $providers
*/
protected function getPreviewObjectProviderRegistry(array $providers): PreviewObjectProviderRegistryInterface
{
return new ContentViewBuilder(new ViewBuilderFactory(), new PreviewObjectProviderRegistry([]));
return new PreviewObjectProviderRegistry($providers);
}

protected function getContentObjectProvider(
EntityManagerInterface $entityManager,
StructureMetadataFactoryInterface $structureMetadataFactory,
LegacyPropertyFactory $propertyFactory,
ContentResolverInterface $contentResolver,
TagFactoryInterface $tagFactory,
CategoryFactoryInterface $categoryFactory,
string $entityClass
): ContentObjectProvider {
return new ContentObjectProvider(
$entityManager,
$structureMetadataFactory,
$propertyFactory,
$contentResolver,
$tagFactory,
$categoryFactory,
$entityClass
);
}

public function testBuild(): void
Expand Down Expand Up @@ -65,4 +109,61 @@ public function testBuild(): void
$this->assertInstanceOf(FormViewBuilderInterface::class, $addContentProjection);
$this->assertSame('example', $addContentProjection->getView()->getOption('formKey'));
}

public function testBuildWithPreview(): void
{
$entityManager = $this->prophesize(EntityManagerInterface::class);
$structureMetadataFactory = $this->prophesize(StructureMetadataFactoryInterface::class);
$propertyFactory = $this->prophesize(LegacyPropertyFactory::class);
$contentResolver = $this->prophesize(ContentResolverInterface::class);
$tagFactory = $this->prophesize(TagFactoryInterface::class);
$categoryFactory = $this->prophesize(CategoryFactoryInterface::class);

$contentObjectProvider = $this->getContentObjectProvider(
$entityManager->reveal(),
$structureMetadataFactory->reveal(),
$propertyFactory->reveal(),
$contentResolver->reveal(),
$tagFactory->reveal(),
$categoryFactory->reveal(),
Example::class
);

$previewObjectProviders = ['preview-examples' => $contentObjectProvider];
$previewObjectProviderRegistry = $this->getPreviewObjectProviderRegistry($previewObjectProviders);
$contentViewBuilder = $this->getContentViewBuilder($previewObjectProviderRegistry);

$viewCollection = new ViewCollection();
$contentViewBuilder->build(
$viewCollection,
'preview-examples',
'preview-example',
'edit_parent_key',
'add_parent_key'
);

/** @var FormViewBuilderInterface $editContentProjection */
$editContentProjection = $viewCollection->get('edit_parent_key.content');
$editExcerptView = $viewCollection->get('edit_parent_key.excerpt');
$editSeoView = $viewCollection->get('edit_parent_key.seo');
$addContentProjection = $viewCollection->get('add_parent_key.content');

$this->assertCount(4, $viewCollection->all());

// Test Edit Content View
$this->assertInstanceOf(PreviewFormViewBuilderInterface::class, $editContentProjection);
$this->assertSame('preview-example', $editContentProjection->getView()->getOption('formKey'));

// Test Edit Excerpt View
$this->assertInstanceOf(PreviewFormViewBuilderInterface::class, $editExcerptView);
$this->assertSame('content_excerpt', $editExcerptView->getView()->getOption('formKey'));

// Test Edit Seo View
$this->assertInstanceOf(PreviewFormViewBuilderInterface::class, $editSeoView);
$this->assertSame('content_seo', $editSeoView->getView()->getOption('formKey'));

// Test Add Content View
$this->assertInstanceOf(FormViewBuilderInterface::class, $addContentProjection);
$this->assertSame('preview-example', $addContentProjection->getView()->getOption('formKey'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\QueryBuilder;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Sulu\Bundle\CategoryBundle\Entity\Category;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\ContentResolverInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Exception\ContentNotFoundException;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\CategoryFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\TagFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentProjectionInterface;
Expand Down Expand Up @@ -87,7 +89,7 @@ public function testGetObject(int $id = 1, string $locale = 'de'): void
return func_get_arg(\func_num_args() - 2);
})->shouldBeCalledTimes(1);

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

Expand All @@ -111,6 +113,55 @@ public function testGetObject(int $id = 1, string $locale = 'de'): void
$this->assertSame($projection->reveal(), $result);
}

public function testGetNonExistingObject(int $id = 1, string $locale = 'de'): void
{
$this->entityManager->createQueryBuilder()->willThrow(NoResultException::class)->shouldBeCalledTimes(1);

$result = $this->contentObjectProvider->getObject($id, $locale);

$this->assertNull($result);
}

public function testGetObjectWithNonExistingProjection(int $id = 1, string $locale = 'de'): void
{
$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);

$this->contentResolver->resolve(
$entity->reveal(),
Argument::type('array')
)->willThrow(ContentNotFoundException::class)->shouldBeCalledTimes(1);

$result = $this->contentObjectProvider->getObject($id, $locale);

$this->assertNull($result);
}

public function testGetId(int $id = 1): void
{
$projection = $this->prophesize(ContentProjectionInterface::class);
Expand All @@ -130,28 +181,38 @@ public function testSetValues(
array $data = [
'title' => 'Title',
'seoTitle' => 'Seo Title',
'seoDescription' => 'Seo Description',
'seoKeywords' => 'Seo Keywords',
'seoCanonicalUrl' => 'Seo Canonical Url',
'seoNoIndex' => true,
'seoNoFollow' => false,
'seoHideInSitemap' => true,
'excerptTitle' => 'Excerpt Title',
'excerptDescription' => 'Excerpt Description',
'excerptMore' => 'Excerpt More',
'excerptTags' => ['foo', 'bar'],
'excerptCategories' => [1, 2],
'excerptImage' => ['id' => 3],
'excerptIcon' => ['id' => 4],
]
): void {
$this->tagFactory->create(Argument::type('array'))->will(function (array $tags) {
return array_map(function ($name) {
$tag = new Tag();
$tag->setName($name);
$tags = array_map(function (string $name) {
$tag = new Tag();
$tag->setName($name);

return $tag;
}, $tags);
});
return $tag;
}, $data['excerptTags']);

$this->categoryFactory->create(Argument::type('array'))->will(function (array $categories) {
return array_map(function ($id) {
$category = new Category();
$category->setId($id);
$this->tagFactory->create($data['excerptTags'])->willReturn($tags);

return $category;
}, $categories);
});
$categories = array_map(function (int $id) {
$category = new Category();
$category->setId($id);

return $category;
}, $data['excerptCategories']);

$this->categoryFactory->create($data['excerptCategories'])->willReturn($categories);

$projection = (new class() implements ContentProjectionInterface, TemplateInterface, SeoInterface, ExcerptInterface {
use ContentProjectionTrait;
Expand All @@ -173,11 +234,25 @@ public function getTemplateType(): string
$newData = $data;
$this->contentObjectProvider->setValues($projection, $locale, $newData);

$this->assertSame('Title', $projection->getTemplateData()['title']);
$this->assertSame('Seo Title', $projection->getSeoTitle());
$this->assertSame('Excerpt Title', $projection->getExcerptTitle());
$this->assertSame($data['title'], $projection->getTemplateData()['title']);

$this->assertSame($data['seoTitle'], $projection->getSeoTitle());
$this->assertSame($data['seoDescription'], $projection->getSeoDescription());
$this->assertSame($data['seoKeywords'], $projection->getSeoKeywords());
$this->assertSame($data['seoCanonicalUrl'], $projection->getSeoCanonicalUrl());
$this->assertSame($data['seoNoFollow'], $projection->getSeoNoFollow());
$this->assertSame($data['seoNoIndex'], $projection->getSeoNoIndex());
$this->assertSame($data['seoHideInSitemap'], $projection->getSeoHideInSitemap());

$this->assertSame($data['excerptTitle'], $projection->getExcerptTitle());
$this->assertSame($data['excerptDescription'], $projection->getExcerptDescription());
$this->assertSame($data['excerptMore'], $projection->getExcerptMore());
$this->assertSame($data['excerptTags'], $projection->getExcerptTagNames());
$this->assertSame($tags, $projection->getExcerptTags());
$this->assertSame($data['excerptCategories'], $projection->getExcerptCategoryIds());
$this->assertSame($categories, $projection->getExcerptCategories());
$this->assertSame($data['excerptImage'], $projection->getExcerptImage());
$this->assertSame($data['excerptIcon'], $projection->getExcerptIcon());
}

/**
Expand Down Expand Up @@ -208,6 +283,7 @@ public function getTemplateType(): string
public function testSerialize(): void
{
$object = new \stdClass();
$object->foo = 'bar';
$serializedObject = serialize($object);

$result = $this->contentObjectProvider->serialize($object);
Expand All @@ -218,10 +294,11 @@ public function testSerialize(): void
public function testDeserialize(): void
{
$object = new \stdClass();
$object->foo = 'bar';
$serializedObject = serialize($object);

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

$this->assertSame($deserializedObject, $object);
$this->assertSame($deserializedObject->foo, $object->foo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,37 @@ public function testGetByEntityReturnNoneTemplate(): void
$contentRouteDefaultsProvider->getByEntity(Example::class, '123-123-123', 'en');
}

public function testGetByEntityReturnNoneTemplateFromPreview(): void
{
$contentRichEntity = $this->prophesize(ContentRichEntityInterface::class);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage(sprintf(
'Expected to get "%s" from ContentResolver but "%s" given.',
TemplateInterface::class,
\get_class($contentRichEntity->reveal())
));

$entityManager = $this->prophesize(EntityManagerInterface::class);
$contentResolver = $this->prophesize(ContentResolverInterface::class);
$structureMetadataFactory = $this->prophesize(StructureMetadataFactoryInterface::class);
$propertyFactory = $this->prophesize(LegacyPropertyFactory::class);

$contentRouteDefaultsProvider = $this->getContentRouteDefaultsProvider(
$entityManager->reveal(),
$contentResolver->reveal(),
$structureMetadataFactory->reveal(),
$propertyFactory->reveal()
);

/**
* @var ContentProjectionInterface
*/
$entity = $contentRichEntity->reveal();

$contentRouteDefaultsProvider->getByEntity(Example::class, '123-123-123', 'en', $entity);
}

public function testIsPublishedNotExists(): void
{
$entityManager = $this->prophesize(EntityManagerInterface::class);
Expand Down

0 comments on commit 7c734de

Please sign in to comment.