From ae907d605bc082b2b0231c35ad9b0a16355d31a3 Mon Sep 17 00:00:00 2001 From: Jordi Sala Morales Date: Tue, 9 Aug 2022 13:47:11 +0200 Subject: [PATCH] Add test for weird cases --- tests/Entity/TransformerTest.php | 130 +++++++++++++++++++++++-------- 1 file changed, 97 insertions(+), 33 deletions(-) diff --git a/tests/Entity/TransformerTest.php b/tests/Entity/TransformerTest.php index 7e7351c3b..19dddaaa8 100644 --- a/tests/Entity/TransformerTest.php +++ b/tests/Entity/TransformerTest.php @@ -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 */ - protected $blockManager; + private $blockManager; - protected TransformerInterface $transformer; + private TransformerInterface $transformer; protected function setUp(): void { @@ -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()); @@ -155,23 +156,94 @@ public function testLoadSnapshotToPage(): void static::assertSame('/get-child', $page->getUrl()); } - public function testLoadBlock(): void + /** + * @dataProvider blockProvider + * + * @param array $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 + */ + 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', @@ -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' => [], + ], + ], ], ], ];