Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Prokyonn committed Feb 8, 2023
1 parent 8ab75e1 commit c986650
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Tests/Application/ExampleTestBundle/Link/ExampleLinkProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\Application\ExampleTestBundle\Link;

use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentManager\ContentManagerInterface;
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Link\ContentLinkProvider;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example;
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkConfiguration;
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkConfigurationBuilder;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;

class ExampleLinkProvider extends ContentLinkProvider
{
public function __construct(
ContentManagerInterface $contentManager,
StructureMetadataFactoryInterface $structureMetadataFactory,
EntityManagerInterface $entityManager
) {
parent::__construct($contentManager, $structureMetadataFactory, $entityManager, Example::class);
}

public function getConfiguration(): LinkConfiguration
{
return LinkConfigurationBuilder::create()
->setTitle('Example')
->setResourceKey(Example::RESOURCE_KEY)
->setListAdapter('table')
->setDisplayProperties(['id'])
->setOverlayTitle('Select Example')
->setEmptyText('No example selected')
->setIcon('su-document')
->getLinkConfiguration();
}
}
11 changes: 11 additions & 0 deletions Tests/Application/ExampleTestBundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ services:
tags:
- { name: sulu.teaser.provider, alias: examples }

example_test.example_link_provider:
class: Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Link\ExampleLinkProvider
public: true
arguments:
- '@sulu_content.content_manager'
- '@sulu_page.structure.factory'
- '@doctrine.orm.entity_manager'
tags:
- { name: sulu.link.provider, alias: examples }


example_test.example_data_provider_repository:
class: Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\SmartContent\Repository\ContentDataProviderRepository
public: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?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\Infrastructure\Sulu\Link;

use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Link\ExampleLinkProvider;
use Sulu\Bundle\ContentBundle\Tests\Traits\AssertSnapshotTrait;
use Sulu\Bundle\ContentBundle\Tests\Traits\CreateExampleTrait;
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkItem;
use Sulu\Bundle\TestBundle\Testing\WebsiteTestCase;

class ContentLinkProviderTest extends WebsiteTestCase
{
use AssertSnapshotTrait;
use CreateExampleTrait;
private ExampleLinkProvider $exampleLinkProvider;

/**
* @var mixed[]
*/
private static $exampleIds = [];

public static function setUpBeforeClass(): void
{
static::purgeDatabase();
parent::setUpBeforeClass();

// Example 1 (both locales, both published)
$example1 = static::createExample([
'en' => [
'live' => [
'title' => 'example-1',
'article' => 'example-1-article',
],
],
'de' => [
'live' => [
'title' => 'beispiel-1',
'article' => null,
],
],
]);

// Example 2 (only en, published)
$example2 = static::createExample([
'en' => [
'live' => [
'title' => 'example-2',
],
],
]);

// Example 3 (both locales, only en published)
$example3 = static::createExample([
'en' => [
'live' => [
'title' => 'example-3',
'article' => '<p>Test article</p>',
],
],
'de' => [
'draft' => [
'title' => 'beispiel-3',
],
],
]);

// Example 4 (only de, published)
$example4 = static::createExample([
'de' => [
'live' => [
'title' => 'beispiel-4',
'article' => '<p>Test article</p>',
],
],
]);

// Example 5 (only en, not published)
$example5 = static::createExample([
'en' => [
'draft' => [
'title' => 'example-5',
],
],
]);

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

static::$exampleIds[] = $example1->getId();
static::$exampleIds[] = $example2->getId();
static::$exampleIds[] = $example3->getId();
static::$exampleIds[] = $example4->getId();
static::$exampleIds[] = $example5->getId();
}

protected function setUp(): void
{
/** @var ExampleLinkProvider $provider */
$provider = self::getContainer()->get('example_test.example_link_provider');
$this->exampleLinkProvider = $provider;
}

public function testEmpty(): void
{
$links = $this->exampleLinkProvider->preload([], 'de');

$this->assertCount(0, $links);
}

public function testPreloadDE(): void
{
$links = $this->exampleLinkProvider->preload(static::$exampleIds, 'de');
$this->assertArraySnapshot('links_de.json', $this->mapLinks($links));
}

public function testPreloadEN(): void
{
$links = $this->exampleLinkProvider->preload(static::$exampleIds, 'en');
$this->assertArraySnapshot('links_en.json', $this->mapLinks($links));
}

/**
* @param LinkItem[] $links
*
* @return array<string, mixed>
*/
private function mapLinks(array $links): array
{
return \array_map(function(LinkItem $linkItem) {
return [
'id' => $linkItem->getId(),
'url' => $linkItem->getUrl(),
'title' => $linkItem->getTitle(),
];
}, $links);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"id": "@integer@",
"url": "/beispiel-1",
"title": "beispiel-1"
},
{
"id": "@integer@",
"url": "/beispiel-4",
"title": "beispiel-4"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"id": "@integer@",
"url": "/example-1",
"title": "example-1"
},
{
"id": "@integer@",
"url": "/example-2",
"title": "example-2"
},
{
"id": "@integer@",
"url": "/example-3",
"title": "example-3"
}
]

0 comments on commit c986650

Please sign in to comment.