-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Tag and add Category loader tests
- Loading branch information
1 parent
32e3639
commit 02dadf0
Showing
8 changed files
with
389 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...it/Infrastructure/Sulu/Content/PropertyResolver/CategorySelectionPropertyResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
/* | ||
* 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\CategoryBundle\Tests\Unit\Infrastructure\Sulu\Content\PropertyResolver; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ResolvableResource; | ||
use Sulu\Bundle\CategoryBundle\Infrastructure\Sulu\Content\PropertyResolver\CategorySelectionPropertyResolver; | ||
|
||
#[CoversClass(CategorySelectionPropertyResolver::class)] | ||
class CategorySelectionPropertyResolverTest extends TestCase | ||
{ | ||
private CategorySelectionPropertyResolver $resolver; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->resolver = new CategorySelectionPropertyResolver(); | ||
} | ||
|
||
public function testResolveEmpty(): void | ||
{ | ||
$contentView = $this->resolver->resolve([], 'en'); | ||
|
||
$this->assertSame([], $contentView->getContent()); | ||
$this->assertSame(['ids' => []], $contentView->getView()); | ||
} | ||
|
||
public function testResolveParams(): void | ||
{ | ||
$contentView = $this->resolver->resolve([], 'en', ['custom' => 'params']); | ||
|
||
$this->assertSame([], $contentView->getContent()); | ||
$this->assertSame([ | ||
'ids' => [], | ||
'custom' => 'params', | ||
], $contentView->getView()); | ||
} | ||
|
||
#[DataProvider('provideUnresolvableData')] | ||
public function testResolveUnresolvableData(mixed $data): void | ||
{ | ||
$contentView = $this->resolver->resolve($data, 'en'); | ||
|
||
$this->assertSame([], $contentView->getContent()); | ||
$this->assertSame(['ids' => []], $contentView->getView()); | ||
} | ||
|
||
/** | ||
* @return iterable<array{ | ||
* 0: mixed, | ||
* }> | ||
*/ | ||
public static function provideUnresolvableData(): iterable | ||
{ | ||
yield 'null' => [null]; | ||
yield 'smart_content' => [['source' => '123']]; | ||
yield 'single_value' => [1]; | ||
yield 'object' => [(object) [1, 2]]; | ||
} | ||
|
||
/** | ||
* @param array<string|int> $data | ||
*/ | ||
#[DataProvider('provideResolvableData')] | ||
public function testResolveResolvableData(array $data): void | ||
{ | ||
$contentView = $this->resolver->resolve($data, 'en'); | ||
|
||
$content = $contentView->getContent(); | ||
$this->assertIsArray($content); | ||
foreach ($data as $key => $value) { | ||
$resolvable = $content[$key] ?? null; | ||
$this->assertInstanceOf(ResolvableResource::class, $resolvable); | ||
$this->assertSame($value, $resolvable->getId()); | ||
$this->assertSame('category', $resolvable->getResourceLoaderKey()); | ||
} | ||
|
||
$this->assertSame(['ids' => $data], $contentView->getView()); | ||
} | ||
|
||
/** | ||
* @return iterable<array{ | ||
* 0: array<string|int>, | ||
* }> | ||
*/ | ||
public static function provideResolvableData(): iterable | ||
{ | ||
yield 'empty' => [[]]; | ||
yield 'int_list' => [[1, 2]]; | ||
yield 'string_list' => [['1', '2']]; | ||
} | ||
|
||
public function testCustomResourceLoader(): void | ||
{ | ||
$contentView = $this->resolver->resolve([1], 'en', ['resourceLoader' => 'custom_category']); | ||
|
||
$content = $contentView->getContent(); | ||
$this->assertIsArray($content); | ||
$resolvable = $content[0] ?? null; | ||
$this->assertInstanceOf(ResolvableResource::class, $resolvable); | ||
$this->assertSame(1, $resolvable->getId()); | ||
$this->assertSame('custom_category', $resolvable->getResourceLoaderKey()); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...ndle/Tests/Unit/Infrastructure/Sulu/Content/ResourceLoader/CategoryResourceLoaderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/* | ||
* 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\CategoryBundle\Tests\Unit\Infrastructure\Sulu\Content\ResourceLoader; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Sulu\Bundle\CategoryBundle\Category\CategoryManagerInterface; | ||
use Sulu\Bundle\CategoryBundle\Entity\Category; | ||
use Sulu\Bundle\CategoryBundle\Infrastructure\Sulu\Content\ResourceLoader\CategoryResourceLoader; | ||
use Sulu\Bundle\TestBundle\Testing\SetGetPrivatePropertyTrait; | ||
|
||
class CategoryResourceLoaderTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
use SetGetPrivatePropertyTrait; | ||
|
||
/** | ||
* @var ObjectProphecy<CategoryManagerInterface> | ||
*/ | ||
private ObjectProphecy $categoryManager; | ||
|
||
private CategoryResourceLoader $loader; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->categoryManager = $this->prophesize(CategoryManagerInterface::class); | ||
$this->loader = new CategoryResourceLoader($this->categoryManager->reveal()); | ||
} | ||
|
||
public function testGetKey(): void | ||
{ | ||
$this->assertSame('category', $this->loader::getKey()); | ||
} | ||
|
||
public function testLoad(): void | ||
{ | ||
$category1 = $this->createCategory(1); | ||
$category2 = $this->createCategory(3); | ||
|
||
$this->categoryManager->findByIds([1, 3])->willReturn([ | ||
$category1, | ||
$category2, | ||
]) | ||
->shouldBeCalled(); | ||
|
||
$result = $this->loader->load([1, 3], 'en', []); | ||
|
||
$this->assertSame([ | ||
1 => $category1, | ||
3 => $category2, | ||
], $result); | ||
} | ||
|
||
private static function createCategory(int $id): Category | ||
{ | ||
$category = new Category(); | ||
static::setPrivateProperty($category, 'id', $id); | ||
$category->setKey('category-' . $id); | ||
|
||
return $category; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...ts/Unit/Infrastructure/Sulu/Content/PropertyResolver/TagSelectionPropertyResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
/* | ||
* 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\TagBundle\Tests\Unit\Infrastructure\Sulu\Content\PropertyResolver; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ResolvableResource; | ||
use Sulu\Bundle\TagBundle\Infrastructure\Sulu\Content\PropertyResolver\TagSelectionPropertyResolver; | ||
|
||
#[CoversClass(TagSelectionPropertyResolver::class)] | ||
class TagSelectionPropertyResolverTest extends TestCase | ||
{ | ||
private TagSelectionPropertyResolver $resolver; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->resolver = new TagSelectionPropertyResolver(); | ||
} | ||
|
||
public function testResolveEmpty(): void | ||
{ | ||
$contentView = $this->resolver->resolve([], 'en'); | ||
|
||
$this->assertSame([], $contentView->getContent()); | ||
$this->assertSame(['ids' => []], $contentView->getView()); | ||
} | ||
|
||
public function testResolveParams(): void | ||
{ | ||
$contentView = $this->resolver->resolve([], 'en', ['custom' => 'params']); | ||
|
||
$this->assertSame([], $contentView->getContent()); | ||
$this->assertSame([ | ||
'ids' => [], | ||
'custom' => 'params', | ||
], $contentView->getView()); | ||
} | ||
|
||
#[DataProvider('provideUnresolvableData')] | ||
public function testResolveUnresolvableData(mixed $data): void | ||
{ | ||
$contentView = $this->resolver->resolve($data, 'en'); | ||
|
||
$this->assertSame([], $contentView->getContent()); | ||
$this->assertSame(['ids' => []], $contentView->getView()); | ||
} | ||
|
||
/** | ||
* @return iterable<array{ | ||
* 0: mixed, | ||
* }> | ||
*/ | ||
public static function provideUnresolvableData(): iterable | ||
{ | ||
yield 'null' => [null]; | ||
yield 'smart_content' => [['source' => '123']]; | ||
yield 'single_value' => [1]; | ||
yield 'object' => [(object) [1, 2]]; | ||
} | ||
|
||
/** | ||
* @param array<string|int> $data | ||
*/ | ||
#[DataProvider('provideResolvableData')] | ||
public function testResolveResolvableData(array $data): void | ||
{ | ||
$contentView = $this->resolver->resolve($data, 'en'); | ||
|
||
$content = $contentView->getContent(); | ||
$this->assertIsArray($content); | ||
foreach ($data as $key => $value) { | ||
$resolvable = $content[$key] ?? null; | ||
$this->assertInstanceOf(ResolvableResource::class, $resolvable); | ||
$this->assertSame($value, $resolvable->getId()); | ||
$this->assertSame('tag', $resolvable->getResourceLoaderKey()); | ||
} | ||
|
||
$this->assertSame(['ids' => $data], $contentView->getView()); | ||
} | ||
|
||
/** | ||
* @return iterable<array{ | ||
* 0: array<string|int>, | ||
* }> | ||
*/ | ||
public static function provideResolvableData(): iterable | ||
{ | ||
yield 'empty' => [[]]; | ||
yield 'int_list' => [[1, 2]]; | ||
yield 'string_list' => [['1', '2']]; | ||
} | ||
|
||
public function testCustomResourceLoader(): void | ||
{ | ||
$contentView = $this->resolver->resolve([1], 'en', ['resourceLoader' => 'custom_tag']); | ||
|
||
$content = $contentView->getContent(); | ||
$this->assertIsArray($content); | ||
$resolvable = $content[0] ?? null; | ||
$this->assertInstanceOf(ResolvableResource::class, $resolvable); | ||
$this->assertSame(1, $resolvable->getId()); | ||
$this->assertSame('custom_tag', $resolvable->getResourceLoaderKey()); | ||
} | ||
} |
Oops, something went wrong.