Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Prokyonn committed Oct 14, 2024
1 parent 53343e0 commit 53abf16
Show file tree
Hide file tree
Showing 15 changed files with 681 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver;
namespace Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\Resolver;

use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FieldMetadata;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\MetadataResolver\MetadataResolver;
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface;

class BlockPropertyResolver implements PropertyResolverInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver;
namespace Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\Resolver;

use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface;

class DefaultPropertyResolver implements PropertyResolverInterface
{
Expand Down
22 changes: 11 additions & 11 deletions Content/UserInterface/Controller/Website/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
*/
class ContentController extends AbstractController
{
public function __construct(private ContentResolverInterface $contentResolver)
{
}

/**
* @param T $object
*/
Expand Down Expand Up @@ -70,15 +66,10 @@ public function indexAction(
protected function resolveSuluParameters(DimensionContentInterface $object, bool $normalize): array
{
if (false === $normalize) {
return $this->contentResolver->resolve($object);
return $this->container->get('sulu_content.content_resolver')->resolve($object);
}

return []; // TODO
// return [
// 'resource' => $object->getResource(), // TODO normalize JSON response
//
// // TODO resolve content
// ];
return []; // TODO normalize JSON response
}

/**
Expand Down Expand Up @@ -109,4 +100,13 @@ protected function renderSuluView(

return $this->renderView($viewTemplate, $parameters);
}

public static function getSubscribedServices(): array
{
$services = parent::getSubscribedServices();

$services['sulu_content.content_resolver'] = ContentResolverInterface::class;

return $services;
}
}
1 change: 0 additions & 1 deletion Resources/config/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<call method="setContainer">
<argument type="service" id="Psr\Container\ContainerInterface" />
</call>
<argument type="service" id="sulu_content.content_resolver" />
</service>

<service id="Sulu\Bundle\ContentBundle\Content\UserInterface\Controller\Website\ContentController" alias="sulu_content.content_controller" public="true" />
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/resolvers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@
</service>

<service id="sulu_content.property_resolver.default"
class="Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\DefaultPropertyResolver">
class="Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\Resolver\DefaultPropertyResolver">
<tag name="sulu_content.property_resolver"/>
</service>

<!-- TODO move to admin bundle -->
<service id="sulu_content.property_resolver.block"
class="Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\BlockPropertyResolver">
class="Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\Resolver\BlockPropertyResolver">
<call method="setMetadataResolver">
<argument type="service" id="sulu_content.metadata_resolver"/>
</call>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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\Functional\Content\Application\ContentResolver;

use Sulu\Bundle\ContentBundle\Content\Application\ContentAggregator\ContentAggregatorInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\ContentResolverInterface;
use Sulu\Bundle\ContentBundle\Tests\Traits\CreateExampleTrait;
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;

class ContentResolverTest extends SuluTestCase
{
use CreateExampleTrait;

private ContentResolverInterface $contentResolver;
private ContentAggregatorInterface $contentAggregator;

protected function setUp(): void
{
self::purgeDatabase();
self::initPhpcr();

$this->contentResolver = self::getContainer()->get('sulu_content.content_resolver');
$this->contentAggregator = self::getContainer()->get('sulu_content.content_aggregator');
}

public function testResolveContent(): void
{
$example1 = static::createExample(
[
'en' => [
'live' => [
'title' => 'example-1',
'description' => 'text-area-1',
'article' => 'editor-1',
'blocks' => [
[
'type' => 'heading',
'heading' => 'heading-1',
],
[
'type' => 'text',
'text' => 'text-1',
],
],
],
],
],
[
'create_route' => true,
]
);

static::getEntityManager()->flush();

$dimensionContent = $this->contentAggregator->aggregate($example1, ['locale' => 'en', 'stage' => 'live']);
/** @var mixed[] $result */
$result = $this->contentResolver->resolve($dimensionContent);

/** @var mixed[] $content */
$content = $result['content'];

self::assertSame('example-1', $content['title']);
self::assertSame('text-area-1', $content['description']);
self::assertSame('editor-1', $content['article']);
self::assertCount(2, $content['blocks']);
self::assertSame('heading-1', $content['blocks'][0]['heading']);
self::assertSame('heading', $content['blocks'][0]['type']);
self::assertSame('text-1', $content['blocks'][1]['text']);
self::assertSame('text', $content['blocks'][1]['type']);

// TODO add tests for categories / tags / images / ...
self::assertNull($content['image']);

// TODO add excerpt / seo test
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?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\Application\ContentResolver\Resolver;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FormMetadata;
use Sulu\Bundle\AdminBundle\Metadata\MetadataProviderInterface;
use Sulu\Bundle\CategoryBundle\Entity\Category;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Resolver\ExcerptResolver;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\MetadataResolver\MetadataResolver;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ExcerptInterface;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\ExampleDimensionContent;
use Sulu\Bundle\ContentBundle\Tests\Traits\SetGetPrivatePropertyTrait;
use Sulu\Bundle\TagBundle\Entity\Tag;

class ExcerptResolverTest extends TestCase
{
use ProphecyTrait;
use SetGetPrivatePropertyTrait;

public function testResolveWithNonTemplateInterface(): void
{
$templateResolver = new ExcerptResolver(
$this->prophesize(MetadataProviderInterface::class)->reveal(),
$this->prophesize(MetadataResolver::class)->reveal()
);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('DimensionContent needs to extend the ' . ExcerptInterface::class);

$templateResolver->resolve($this->prophesize(DimensionContentInterface::class)->reveal());
}

public function testResolve(): void
{
$example = new Example();
$dimensionContent = new ExampleDimensionContent($example);
$example->addDimensionContent($dimensionContent);
$dimensionContent->setLocale('en');

$dimensionContent->setExcerptTitle('Sulu');
$dimensionContent->setExcerptDescription('Sulu is awesome');
$dimensionContent->setExcerptMore('Sulu is more awesome');
$dimensionContent->setExcerptIcon(['id' => 1]);
$dimensionContent->setExcerptImage(['id' => 2]);
$tag = new Tag();
$this->setPrivateProperty($tag, 'id', 1);
$dimensionContent->setExcerptTags([$tag]);
$category = new Category();
$this->setPrivateProperty($category, 'id', 1);
$dimensionContent->setExcerptCategories([$category]);

$formMetadata = $this->prophesize(FormMetadata::class);
$formMetadata->getItems()
->willReturn([]);
$formMetadataProvider = $this->prophesize(MetadataProviderInterface::class);
$formMetadataProvider->getMetadata('content_excerpt', 'en', [])
->willReturn($formMetadata->reveal());

$metadataResolver = $this->prophesize(MetadataResolver::class);
$metadataResolver->resolveItems([], [
'excerptTitle' => 'Sulu',
'excerptDescription' => 'Sulu is awesome',
'excerptMore' => 'Sulu is more awesome',
'excerptIcon' => ['id' => 1],
'excerptImage' => ['id' => 2],
'excerptTags' => [1],
'excerptCategories' => [1],
], 'en')
->willReturn(
[
ContentView::create(['dummy' => 'data'], []),
]
);

$templateResolver = new ExcerptResolver(
$formMetadataProvider->reveal(),
$metadataResolver->reveal()
);

$contentView = $templateResolver->resolve($dimensionContent);

$this->assertInstanceOf(ContentView::class, $contentView);
$content = $contentView->getContent();
$this->assertIsArray($content);
$this->assertCount(1, $content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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\Application\ContentResolver\Resolver;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FormMetadata;
use Sulu\Bundle\AdminBundle\Metadata\MetadataProviderInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Resolver\SeoResolver;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\MetadataResolver\MetadataResolver;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\SeoInterface;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\ExampleDimensionContent;

class SeoResolverTest extends TestCase
{
use ProphecyTrait;

public function testResolveWithNonTemplateInterface(): void
{
$templateResolver = new SeoResolver(
$this->prophesize(MetadataProviderInterface::class)->reveal(),
$this->prophesize(MetadataResolver::class)->reveal()
);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('DimensionContent needs to extend the ' . SeoInterface::class);

$templateResolver->resolve($this->prophesize(DimensionContentInterface::class)->reveal());
}

public function testResolve(): void
{
$example = new Example();
$dimensionContent = new ExampleDimensionContent($example);
$example->addDimensionContent($dimensionContent);
$dimensionContent->setLocale('en');

$dimensionContent->setSeoTitle('Sulu');
$dimensionContent->setSeoDescription('Sulu is awesome');
$dimensionContent->setSeoKeywords('Sulu, awesome');
$dimensionContent->setSeoCanonicalUrl('https://sulu.io');
$dimensionContent->setSeoNoIndex(true);
$dimensionContent->setSeoNoFollow(true);
$dimensionContent->setSeoHideInSitemap(true);

$formMetadata = $this->prophesize(FormMetadata::class);
$formMetadata->getItems()
->willReturn([]);
$formMetadataProvider = $this->prophesize(MetadataProviderInterface::class);
$formMetadataProvider->getMetadata('content_seo', 'en', [])
->willReturn($formMetadata->reveal());

$metadataResolver = $this->prophesize(MetadataResolver::class);
$metadataResolver->resolveItems([], [
'seoTitle' => 'Sulu',
'seoDescription' => 'Sulu is awesome',
'seoKeywords' => 'Sulu, awesome',
'seoCanonicalUrl' => 'https://sulu.io',
'seoNoIndex' => true,
'seoNoFollow' => true,
'seoHideInSitemap' => true,
], 'en')
->willReturn(
[
ContentView::create(['dummy' => 'data'], []),
]
);

$templateResolver = new SeoResolver(
$formMetadataProvider->reveal(),
$metadataResolver->reveal()
);

$contentView = $templateResolver->resolve($dimensionContent);

$this->assertInstanceOf(ContentView::class, $contentView);
$content = $contentView->getContent();
$this->assertIsArray($content);
$this->assertCount(1, $content);
}
}
Loading

0 comments on commit 53abf16

Please sign in to comment.