Skip to content

Commit

Permalink
Add test for weird cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Aug 9, 2022
1 parent ee734f1 commit ae907d6
Showing 1 changed file with 97 additions and 33 deletions.
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 ae907d6

Please sign in to comment.