Skip to content

Commit

Permalink
add test for ContentObjectProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-rath committed Feb 17, 2020
1 parent fb4d15d commit 1364223
Showing 1 changed file with 264 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

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

use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
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\Factory\CategoryFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\TagFactoryInterface;
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\ExcerptInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ExcerptTrait;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\SeoInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\SeoTrait;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateTrait;
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Preview\ContentObjectProvider;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example;
use Sulu\Bundle\TagBundle\Entity\Tag;
use Sulu\Component\Content\Compat\Structure\LegacyPropertyFactory;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;

class ContentObjectProviderTest extends TestCase
{
/**
* @var EntityManagerInterface
*/
private $entityManager;

/**
* @var StructureMetadataFactoryInterface
*/
private $structureMetadataFactory;

/**
* @var LegacyPropertyFactory
*/
private $propertyFactory;

/**
* @var ContentResolverInterface
*/
private $contentResolver;

/**
* @var TagFactoryInterface
*/
private $tagFactory;

/**
* @var CategoryFactoryInterface
*/
private $categoryFactory;

/**
* @var string
*/
private $entityClass;

/**
* @var ContentObjectProvider
*/
private $contentObjectProvider;

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

$this->contentObjectProvider = new ContentObjectProvider(
$this->entityManager->reveal(),
$this->structureMetadataFactory->reveal(),
$this->propertyFactory->reveal(),
$this->contentResolver->reveal(),
$this->tagFactory->reveal(),
$this->categoryFactory->reveal(),
$this->entityClass
);
}

public function testGetObject($id = '123-123-123', $locale = 'de'): void
{
/**
* @var QueryBuilder
*/
$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::type('string'))->will(function () {
return func_get_arg(\func_num_args() - 2);
})->shouldBeCalledTimes(1);

/**
* @var Query
*/
$query = $this->prophesize(AbstractQuery::class);

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

/**
* @var ContentRichEntityInterface
*/
$entity = $this->prophesize(ContentRichEntityInterface::class);

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

/**
* @var ContentProjectionInterface
*/
$projection = $this->prophesize(ContentProjectionInterface::class);

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

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

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

public function testGetId($id = 1)
{
/**
* @var ContentProjectionInterface
*/
$projection = $this->prophesize(ContentProjectionInterface::class);

$projection->getId()->willReturn($id);

$this->assertSame($id, $this->contentObjectProvider->getId($projection->reveal()));
}

public function testSetValues(
$locale = 'de',
$data = [
'title' => 'Title',
'seoTitle' => 'Seo Title',
'excerptTitle' => 'Excerpt Title',
'excerptTags' => ['foo', 'bar'],
'excerptCategories' => [1, 2],
]
) {
$tags = array_map(function (string $name) {
$tag = new Tag();
$tag->setName($name);

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

$this->tagFactory->create($data['excerptTags'])->willReturn($tags);

$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;
use TemplateTrait;
use SeoTrait;
use ExcerptTrait;

public function getContentId()
{
return 1;
}

public function getTemplateType(): string
{
return 'example';
}
});

$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['excerptTags'], $projection->getExcerptTagNames());
$this->assertSame($tags, $projection->getExcerptTags());
$this->assertSame($data['excerptCategories'], $projection->getExcerptCategoryIds());
$this->assertSame($categories, $projection->getExcerptCategories());
}

public function testSetContext($locale = 'de', array $context = ['template' => 'overview'])
{
$projection = (new class() implements ContentProjectionInterface, TemplateInterface {
use ContentProjectionTrait;
use TemplateTrait;

public function getContentId()
{
return 1;
}

public function getTemplateType(): string
{
return 'example';
}
});

$this->contentObjectProvider->setContext($projection, $locale, $context);

$this->assertSame($context['template'], $projection->getTemplateKey());
}

public function testSerialize()
{
// Just for Code Coverage
$this->contentObjectProvider->serialize(null);

$this->markTestSkipped();
}

public function testDeserialize()
{
// Just for Code Coverage
$this->contentObjectProvider->deserialize('', null);

$this->markTestSkipped();
}
}

0 comments on commit 1364223

Please sign in to comment.