Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f95996
commit a276a3d
Showing
3 changed files
with
127 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Notion\Blocks; | ||
|
||
/** | ||
* @psalm-import-type BlockJson from Block | ||
* | ||
* @psalm-type TableOfContentsJson = array{ | ||
* table_of_contents: array<empty, empty> | ||
* } | ||
*/ | ||
class TableOfContents implements BlockInterface | ||
{ | ||
private const TYPE = Block::TYPE_TABLE_OF_CONTENTS; | ||
|
||
private Block $block; | ||
|
||
private function __construct(Block $block) { | ||
if (!$block->isTableOfContents()) { | ||
throw new \Exception("Block must be of type " . self::TYPE); | ||
} | ||
|
||
$this->block = $block; | ||
} | ||
|
||
public static function create(): self | ||
{ | ||
$block = Block::create(self::TYPE); | ||
|
||
return new self($block); | ||
} | ||
|
||
public static function fromArray(array $array): self | ||
{ | ||
/** @psalm-var BlockJson $array */ | ||
$block = Block::fromArray($array); | ||
|
||
return new self($block); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
$array = $this->block->toArray(); | ||
|
||
$array[self::TYPE] = []; | ||
|
||
return $array; | ||
} | ||
|
||
public function block(): Block | ||
{ | ||
return $this->block; | ||
} | ||
} |
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 | ||
|
||
namespace Notion\Test\Unit\Blocks; | ||
|
||
use Notion\Blocks\BlockFactory; | ||
use Notion\Blocks\TableOfContents; | ||
use Notion\Common\Date; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class TableOfContentsTest extends TestCase | ||
{ | ||
public function test_create_table_of_contents(): void | ||
{ | ||
$tableOfContents = TableOfContents::create(); | ||
|
||
$this->assertEquals("table_of_contents", $tableOfContents->block()->type()); | ||
} | ||
|
||
public function test_create_from_array(): void | ||
{ | ||
$array = [ | ||
"object" => "block", | ||
"id" => "04a13895-f072-4814-8af7-cd11af127040", | ||
"created_time" => "2021-10-18T17:09:00.000Z", | ||
"last_edited_time" => "2021-10-18T17:09:00.000Z", | ||
"archived" => false, | ||
"has_children" => false, | ||
"type" => "table_of_contents", | ||
"table_of_contents" => [], | ||
]; | ||
|
||
$tableOfContents = TableOfContents::fromArray($array); | ||
|
||
$this->assertTrue($tableOfContents->block()->isTableOfContents()); | ||
|
||
$this->assertEquals($tableOfContents, BlockFactory::fromArray($array)); | ||
} | ||
|
||
public function test_error_on_wrong_type(): void | ||
{ | ||
$this->expectException(\Exception::class); | ||
$array = [ | ||
"object" => "block", | ||
"id" => "04a13895-f072-4814-8af7-cd11af127040", | ||
"created_time" => "2021-10-18T17:09:00.000Z", | ||
"last_edited_time" => "2021-10-18T17:09:00.000Z", | ||
"archived" => false, | ||
"has_children" => false, | ||
"type" => "wrong-type", | ||
"table_of_contents" => [], | ||
]; | ||
|
||
TableOfContents::fromArray($array); | ||
} | ||
|
||
public function test_transform_in_array(): void | ||
{ | ||
$tableOfContents = TableOfContents::create(); | ||
|
||
$expected = [ | ||
"object" => "block", | ||
"created_time" => $tableOfContents->block()->createdTime()->format(Date::FORMAT), | ||
"last_edited_time" => $tableOfContents->block()->createdTime()->format(Date::FORMAT), | ||
"archived" => false, | ||
"has_children" => false, | ||
"type" => "table_of_contents", | ||
"table_of_contents" => [], | ||
]; | ||
|
||
$this->assertEquals($expected, $tableOfContents->toArray()); | ||
} | ||
} |