Skip to content

Commit

Permalink
Add resolver tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Oct 16, 2024
1 parent e2d7c0e commit 226399a
Show file tree
Hide file tree
Showing 10 changed files with 579 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function resolve(mixed $data, string $locale, array $params = []): Conten
{
if (!\is_array($data)
|| 0 === \count($data)
|| !\array_is_list($data)
) {
return ContentView::create([], ['ids' => [], ...$params]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function resolve(mixed $data, string $locale, array $params = []): Conten
{
if (!\is_array($data)
|| 0 === \count($data)
|| !\array_is_list($data)
) {
return ContentView::create([], ['ids' => [], ...$params]);
}
Expand All @@ -40,6 +41,10 @@ public function resolve(mixed $data, string $locale, array $params = []): Conten

$resolvableResources = [];
foreach ($data as $id) {
if (!\is_string($id)) {
continue;
}

$key = \substr($id, 0, 1);
$id = \substr($id, 1);
$id = \is_numeric($id) ? \intval($id) : null;
Expand All @@ -61,7 +66,7 @@ public function resolve(mixed $data, string $locale, array $params = []): Conten
return ContentView::create(
$resolvableResources,
[
'ids' => $data,
'ids' => 0 === \count($resolvableResources) ? [] : $data,
...$params,
],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function resolve(mixed $data, string $locale, array $params = []): Conten
{
if (!\is_array($data)
|| 0 === \count($data)
|| !\array_is_list($data)
) {
return ContentView::create([], ['ids' => [], ...$params]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ class SingleAccountSelectionPropertyResolver implements PropertyResolverInterfac
{
public function resolve(mixed $data, string $locale, array $params = []): ContentView
{
if (!\is_int($data)) {
return ContentView::create([], ['id' => null, ...$params]);
if (!\is_numeric($data)) {
return ContentView::create(null, ['id' => null, ...$params]);
}

/** @var string $resourceLoaderKey */
$resourceLoaderKey = $params['resourceLoader'] ?? AccountResourceLoader::getKey();

return ContentView::createResolvable(
$data,
(int) $data,
$resourceLoaderKey,
[
'ids' => $data,
'id' => $data,
...$params,
],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ class SingleContactSelectionPropertyResolver implements PropertyResolverInterfac
{
public function resolve(mixed $data, string $locale, array $params = []): ContentView
{
if (!\is_int($data)) {
return ContentView::create([], ['id' => null, ...$params]);
if (!\is_numeric($data)) {
return ContentView::create(null, ['id' => null, ...$params]);
}

/** @var string $resourceLoaderKey */
$resourceLoaderKey = $params['resourceLoader'] ?? ContactResourceLoader::getKey();

return ContentView::createResolvable(
$data,
(int) $data,
$resourceLoaderKey,
[
'id' => $data,
Expand Down
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\ContactBundle\Tests\Unit\Infrastructure\Sulu\Content\PropertyResolver;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Sulu\Bundle\ContactBundle\Infrastructure\Sulu\Content\PropertyResolver\AccountSelectionPropertyResolver;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ResolvableResource;

#[CoversClass(AccountSelectionPropertyResolver::class)]
class AccountSelectionPropertyResolverTest extends TestCase
{
private AccountSelectionPropertyResolver $resolver;

public function setUp(): void
{
$this->resolver = new AccountSelectionPropertyResolver();
}

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('account', $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_account']);

$content = $contentView->getContent();
$this->assertIsArray($content);
$resolvable = $content[0] ?? null;
$this->assertInstanceOf(ResolvableResource::class, $resolvable);
$this->assertSame(1, $resolvable->getId());
$this->assertSame('custom_account', $resolvable->getResourceLoaderKey());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?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\ContactBundle\Tests\Unit\Infrastructure\Sulu\Content\PropertyResolver;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Sulu\Bundle\ContactBundle\Infrastructure\Sulu\Content\PropertyResolver\ContactAccountSelectionPropertyResolver;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ResolvableResource;

#[CoversClass(ContactAccountSelectionPropertyResolver::class)]
class ContactAccountSelectionPropertyResolverTest extends TestCase
{
private ContactAccountSelectionPropertyResolver $resolver;

public function setUp(): void
{
$this->resolver = new ContactAccountSelectionPropertyResolver();
}

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]];
yield 'unknown_prefix' => [['e1', 'b2']];
}

/**
* @param string[] $data
* @param string[] $expectedResourceLoaderKeys
*/
#[DataProvider('provideResolvableData')]
public function testResolveResolvableData(array $data, array $expectedResourceLoaderKeys): 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((int) \substr($value, 1), $resolvable->getId());
$expectedResourceLoaderKey = $expectedResourceLoaderKeys[$key] ?? null;
$this->assertSame($expectedResourceLoaderKey, $resolvable->getResourceLoaderKey());
}

$this->assertSame(['ids' => $data], $contentView->getView());
}

/**
* @return iterable<array{
* 0: string[],
* 1: string[],
* }>
*/
public static function provideResolvableData(): iterable
{
yield 'empty' => [[], []];
yield 'contact_list' => [['c1', 'a2'], ['contact', 'account']];
}

public function testCustomResourceLoader(): void
{
$contentView = $this->resolver->resolve(['c1', 'a2'], 'en', [
'contactResourceLoader' => 'custom_contact',
'accountResourceLoader' => 'custom_account',
]);

$content = $contentView->getContent();
$this->assertIsArray($content);

$resolvable = $content[0] ?? null;
$this->assertInstanceOf(ResolvableResource::class, $resolvable);
$this->assertSame(1, $resolvable->getId());
$this->assertSame('custom_contact', $resolvable->getResourceLoaderKey());

$resolvable = $content[1] ?? null;
$this->assertInstanceOf(ResolvableResource::class, $resolvable);
$this->assertSame(2, $resolvable->getId());
$this->assertSame('custom_account', $resolvable->getResourceLoaderKey());
}
}
Loading

0 comments on commit 226399a

Please sign in to comment.