Skip to content

Commit

Permalink
Add test for weird cases (#1532)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 authored Aug 9, 2022
1 parent c933cc2 commit d660cf8
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/Entity/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ public function loadBlock(array $content, PageInterface $page): PageBlockInterfa

$block->setEnabled($content['enabled']);

if (isset($content['position'])) {
$block->setPosition($content['position']);
if (isset($content['position']) && is_numeric($content['position'])) {
$block->setPosition((int) $content['position']);
}

$block->setSettings($content['settings']);
Expand Down
4 changes: 2 additions & 2 deletions src/Model/TransformerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* id: int|string|null,
* name?: string|null,
* enabled: boolean,
* position: int|null,
* position: int|string|null,
* settings: array<string, mixed>,
* type: string|null,
* created_at: int|null,
Expand All @@ -32,7 +32,7 @@
* id: int|string|null,
* name?: string|null,
* enabled: boolean,
* position: int|null,
* position: int|string|null,
* settings: array<string, mixed>,
* type: string|null,
* created_at: int|null,
Expand Down
130 changes: 97 additions & 33 deletions tests/Entity/TransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ final class TransformerTest extends TestCase
/**
* @var MockObject&SnapshotManagerInterface
*/
protected $snapshotManager;
private $snapshotManager;

/**
* @var MockObject&PageManagerInterface
*/
protected $pageManager;
private $pageManager;

/**
* @var MockObject&ManagerInterface<PageBlockInterface>
*/
protected $blockManager;
private $blockManager;

protected TransformerInterface $transformer;
private TransformerInterface $transformer;

protected function setUp(): void
{
Expand Down Expand Up @@ -147,6 +147,7 @@ public function testLoadSnapshotToPage(): void
$snapshot = new SonataPageSnapshot();
$snapshot->setContent($this->getTestContent($dateTime));
$snapshot->setUrl('/get-child');

$page = $this->transformer->load($snapshot);

static::assertSame('page_child', $page->getId());
Expand All @@ -155,23 +156,94 @@ public function testLoadSnapshotToPage(): void
static::assertSame('/get-child', $page->getUrl());
}

public function testLoadBlock(): void
/**
* @dataProvider blockProvider
*
* @param array<string, mixed> $content
*
* @phpstan-param BlockContent $content
*/
public function testLoadBlock(array $content): void
{
$this->blockManager->method('create')->willReturnCallback(static fn () => new SonataPageBlock());

$dateTime = new \DateTime();
$block = $this->transformer->loadBlock($content, new SonataPagePage());

$page = new SonataPagePage();
static::assertSame($content['id'], $block->getId());
}

/**
* @phpstan-return iterable<array{BlockContent}>
*/
public function blockProvider(): iterable
{
$datetime = new \DateTime();

$block = $this->transformer->loadBlock($this->getTestBlockArray($dateTime), $page);
// Normal block
yield [[
'id' => 1,
'name' => 'block1',
'enabled' => true,
'position' => 0,
'settings' => [],
'type' => 'type',
'created_at' => (int) $datetime->format('U'),
'updated_at' => (int) $datetime->format('U'),
'blocks' => [[
'id' => 2,
'name' => 'block2',
'enabled' => true,
'position' => 1,
'settings' => [],
'type' => 'type',
'created_at' => (int) $datetime->format('U'),
'updated_at' => (int) $datetime->format('U'),
'blocks' => [],
]],
]];

// Minimal block block
yield [[
'id' => null,
'enabled' => false,
'position' => null,
'settings' => [],
'type' => null,
'created_at' => null,
'updated_at' => null,
'blocks' => [],
]];

// Weird block data
yield [[
'id' => 'random_string',
'name' => 'block1',
'enabled' => true,
'position' => '',
'settings' => [],
'type' => 'type',
'created_at' => null,
'updated_at' => null,
'blocks' => [],
]];

static::assertSame('block123', $block->getId());
// Numeric string position block data
yield [[
'id' => 'block123',
'enabled' => false,
'position' => '0',
'settings' => [],
'type' => null,
'created_at' => null,
'updated_at' => null,
'blocks' => [],
]];
}

/**
* @phpstan-return PageContent
*/
protected function getTestContent(\DateTimeInterface $datetime): array
private function getTestContent(\DateTimeInterface $datetime): array
{
return [
'id' => 'page_child',
Expand All @@ -188,37 +260,29 @@ protected function getTestContent(\DateTimeInterface $datetime): array
'updated_at' => (int) $datetime->format('U'),
'slug' => null,
'parent_id' => 'page_parent',
'blocks' => [
$this->getTestBlockArray($datetime),
],
];
}

/**
* @phpstan-return BlockContent
*/
protected function getTestBlockArray(\DateTimeInterface $datetime): array
{
return [
'id' => 'block123',
'name' => 'block1',
'enabled' => false,
'position' => 0,
'settings' => [],
'type' => 'type',
'created_at' => (int) $datetime->format('U'),
'updated_at' => (int) $datetime->format('U'),
'blocks' => [
[
'id' => 'block234',
'name' => 'block2',
'id' => 'block123',
'name' => 'block1',
'enabled' => false,
'position' => 0,
'settings' => [],
'type' => 'type',
'created_at' => (int) $datetime->format('U'),
'updated_at' => (int) $datetime->format('U'),
'blocks' => [],
'blocks' => [
[
'id' => 'block234',
'name' => 'block2',
'enabled' => false,
'position' => 0,
'settings' => [],
'type' => 'type',
'created_at' => (int) $datetime->format('U'),
'updated_at' => (int) $datetime->format('U'),
'blocks' => [],
],
],
],
],
];
Expand Down

0 comments on commit d660cf8

Please sign in to comment.