From 5c50917f0934cc6d342f53ae79a9258171636fc1 Mon Sep 17 00:00:00 2001 From: philippe-pixieset <134528031+philippe-pixieset@users.noreply.github.com> Date: Wed, 10 Jan 2024 11:38:30 -0700 Subject: [PATCH] feat(database): inline database (#330) --- src/Databases/Database.php | 52 ++++++++++++++++++++++++++- tests/Integration/DatabasesTest.php | 18 ++++++++++ tests/Unit/Databases/DatabaseTest.php | 17 +++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/src/Databases/Database.php b/src/Databases/Database.php index 1afa87f6..a3ec35d8 100644 --- a/src/Databases/Database.php +++ b/src/Databases/Database.php @@ -34,6 +34,7 @@ * properties: array, * parent: DatabaseParentJson, * url: string, + * is_inline: bool, * } * * @psalm-immutable @@ -56,6 +57,7 @@ private function __construct( public readonly array $properties, public readonly DatabaseParent $parent, public readonly string $url, + public readonly bool $isInline, ) { if ($cover !== null && $cover->isInternal()) { throw DatabaseException::internalCover(); @@ -80,7 +82,8 @@ public static function create(DatabaseParent $parent): self null, [ "Title" => Title::create() ], $parent, - "" + "", + false, ); } @@ -142,6 +145,7 @@ function (array $descriptionArray): RichText { $properties, $parent, $array["url"], + $array["is_inline"], ); } @@ -159,6 +163,7 @@ public function toArray(): array "properties" => $this->propertiesToArray(), "parent" => $this->parent->toArray(), "url" => $this->url, + "is_inline" => $this->isInline, ]; } @@ -183,6 +188,7 @@ public function changeTitle(string $title): self $this->properties, $this->parent, $this->url, + $this->isInline, ); } @@ -199,6 +205,7 @@ public function changeAdvancedTitle(RichText ...$title): self $this->properties, $this->parent, $this->url, + $this->isInline, ); } @@ -223,6 +230,7 @@ public function changeIcon(Emoji|File|Icon $icon): self $this->properties, $this->parent, $this->url, + $this->isInline, ); } @@ -239,6 +247,7 @@ public function removeIcon(): self $this->properties, $this->parent, $this->url, + $this->isInline, ); } @@ -255,6 +264,7 @@ public function changeCover(File $cover): self $this->properties, $this->parent, $this->url, + $this->isInline, ); } @@ -271,6 +281,7 @@ public function removeCover(): self $this->properties, $this->parent, $this->url, + $this->isInline, ); } @@ -292,6 +303,7 @@ public function addProperty(PropertyInterface $property): self $this->properties()->add($property)->getAll(), $this->parent, $this->url, + $this->isInline, ); } @@ -308,6 +320,7 @@ public function removePropertyByName(string $propertyName): self $this->properties()->remove($propertyName)->getAll(), $this->parent, $this->url, + $this->isInline, ); } @@ -324,6 +337,7 @@ public function changeProperty(PropertyInterface $property): self $this->properties()->change($property)->getAll(), $this->parent, $this->url, + $this->isInline, ); } @@ -341,6 +355,7 @@ public function changeProperties(array $properties): self PropertyCollection::create(...$properties)->getAll(), $this->parent, $this->url, + $this->isInline, ); } @@ -357,6 +372,41 @@ public function changeParent(DatabaseParent $parent): self $this->properties, $parent, $this->url, + $this->isInline, + ); + } + + public function enableInline(): self + { + return new self( + $this->id, + $this->createdTime, + $this->lastEditedTime, + $this->title, + $this->description, + $this->icon, + $this->cover, + $this->properties, + $this->parent, + $this->url, + true, + ); + } + + public function disableInline(): self + { + return new self( + $this->id, + $this->createdTime, + $this->lastEditedTime, + $this->title, + $this->description, + $this->icon, + $this->cover, + $this->properties, + $this->parent, + $this->url, + false, ); } diff --git a/tests/Integration/DatabasesTest.php b/tests/Integration/DatabasesTest.php index b0fd028a..affc27ee 100644 --- a/tests/Integration/DatabasesTest.php +++ b/tests/Integration/DatabasesTest.php @@ -49,6 +49,24 @@ public function test_create_empty_database(): void $client->databases()->delete($database); } + public function test_create_inline_database(): void + { + $client = Helper::client(); + + $database = Database::create(DatabaseParent::page(Helper::testPageId())) + ->changeTitle("Inline database") + ->enableInline(); + + $database = $client->databases()->create($database); + + $databaseFound = $client->databases()->find($database->id); + + $this->assertEquals("Inline database", $database->title[0]->plainText); + $this->assertTrue($databaseFound->isInline); + + $client->databases()->delete($database); + } + public function test_update_database(): void { $client = Helper::client(); diff --git a/tests/Unit/Databases/DatabaseTest.php b/tests/Unit/Databases/DatabaseTest.php index cbbeb629..114100a9 100644 --- a/tests/Unit/Databases/DatabaseTest.php +++ b/tests/Unit/Databases/DatabaseTest.php @@ -214,6 +214,7 @@ public function test_array_conversion(): void "page_id" => "1ce62b6f-b7f3-4201-afd0-08acb02e61c6", ], "url" => "https://notion.so/a7e80c0ba76643c3a9e921ce94595e0e", + "is_inline" => true, ]; $database = Database::fromArray($array); @@ -231,6 +232,7 @@ public function test_array_conversion(): void "2020-12-08T12:00:00.000000Z", $database->lastEditedTime->format(Date::FORMAT), ); + $this->assertTrue($database->isInline); } public function test_from_array_change_emoji_icon(): void @@ -273,6 +275,7 @@ public function test_from_array_change_emoji_icon(): void "page_id" => "1ce62b6f-b7f3-4201-afd0-08acb02e61c6", ], "url" => "https://notion.so/a7e80c0ba76643c3a9e921ce94595e0e", + "is_inline" => false, ]; $database = Database::fromArray($array); @@ -321,6 +324,7 @@ public function test_from_array_change_file_icon(): void "page_id" => "1ce62b6f-b7f3-4201-afd0-08acb02e61c6", ], "url" => "https://notion.so/a7e80c0ba76643c3a9e921ce94595e0e", + "is_inline" => false, ]; $database = Database::fromArray($array); @@ -328,4 +332,17 @@ public function test_from_array_change_file_icon(): void $this->assertEquals("https://my-site.com/image.png", $database->icon->file?->url); } } + + public function test_inline(): void + { + $parent = DatabaseParent::page("1ce62b6f-b7f3-4201-afd0-08acb02e61c6"); + $database = Database::create($parent); + $this->assertFalse($database->isInline); + + $database = $database->enableInline(); + $this->assertTrue($database->isInline); + + $database = $database->disableInline(); + $this->assertFalse($database->isInline); + } }