diff --git a/src/Databases/Database.php b/src/Databases/Database.php index 29429f01..95cd2e54 100644 --- a/src/Databases/Database.php +++ b/src/Databases/Database.php @@ -137,6 +137,7 @@ function(array $richTextArray): RichText { public function toArray(): array { return [ + "object" => "database", "id" => $this->id, "created_time" => $this->createdTime->format(Date::FORMAT), "last_edited_time" => $this->lastEditedTime->format(Date::FORMAT), @@ -271,9 +272,10 @@ public function withoutCover(): self ); } - public function withAddedProperty(string $name, PropertyInterface $property): self + public function addProperty(PropertyInterface $property): self { $properties = $this->properties; + $name = $property->property()->name(); $properties[$name] = $property; return new self( diff --git a/tests/Unit/Databases/DatabaseTest.php b/tests/Unit/Databases/DatabaseTest.php new file mode 100644 index 00000000..7346c246 --- /dev/null +++ b/tests/Unit/Databases/DatabaseTest.php @@ -0,0 +1,236 @@ +assertEquals("1ce62b6f-b7f3-4201-afd0-08acb02e61c6", $database->parent()->id()); + $this->assertEmpty($database->properties()); + } + + public function test_add_title(): void + { + $parent = DatabaseParent::page("1ce62b6f-b7f3-4201-afd0-08acb02e61c6"); + $database = Database::create($parent)->withTitle(RichText::createText("Database title")); + + $this->assertEquals("Database title", $database->title()[0]->plainText()); + } + + public function test_add_icon(): void + { + $parent = DatabaseParent::page("1ce62b6f-b7f3-4201-afd0-08acb02e61c6"); + $database = Database::create($parent)->withIcon(Emoji::create("⭐")); + + $this->assertEquals("⭐", $database->icon()->emoji()); + } + + public function test_remove_icon(): void + { + $parent = DatabaseParent::page("1ce62b6f-b7f3-4201-afd0-08acb02e61c6"); + $database = Database::create($parent) + ->withIcon(Emoji::create("⭐")) + ->withoutIcon(); + + $this->assertNull($database->icon()); + } + + public function test_add_cover(): void + { + $parent = DatabaseParent::page("1ce62b6f-b7f3-4201-afd0-08acb02e61c6"); + $coverImage = File::createExternal("https://my-site.com/image.png"); + $database = Database::create($parent)->withCover($coverImage); + + $this->assertEquals($coverImage, $database->cover()); + } + + public function test_remove_cover(): void + { + $parent = DatabaseParent::page("1ce62b6f-b7f3-4201-afd0-08acb02e61c6"); + $coverImage = File::createExternal("https://my-site.com/image.png"); + $database = Database::create($parent)->withCover($coverImage)->withoutCover(); + + $this->assertNull($database->cover()); + } + + public function test_move_page(): void + { + $oldParent = DatabaseParent::page("1ce62b6f-b7f3-4201-afd0-08acb02e61c6"); + $newParent = DatabaseParent::page("08da99e5-f11d-4d26-827d-112a3a9bd07d"); + $database = Database::create($oldParent)->withParent($newParent); + + $this->assertSame($newParent, $database->parent()); + } + + public function test_error_with_internal_cover_image(): void + { + $parent = DatabaseParent::page("1ce62b6f-b7f3-4201-afd0-08acb02e61c6"); + $cover = FIle::createInternal("https://notion.so/image.png"); + + $this->expectException(\Exception::class); + Database::create($parent)->withCover($cover); + } + + public function test_replace_properties(): void + { + $parent = DatabaseParent::page("1ce62b6f-b7f3-4201-afd0-08acb02e61c6"); + $properties = [ + "Dummy prop name" => Title::create("Dummy prop name") + ]; + $database = Database::create($parent)->withProperties($properties); + + $this->assertCount(1, $database->properties()); + } + + public function test_add_property(): void + { + $parent = DatabaseParent::page("1ce62b6f-b7f3-4201-afd0-08acb02e61c6"); + $database = Database::create($parent)->addProperty( + Title::create("Dummy prop name") + ); + + $this->assertCount(1, $database->properties()); + } + + public function test_array_conversion(): void + { + $array = [ + "object" => "database", + "id" => "a7e80c0b-a766-43c3-a9e9-21ce94595e0e", + "created_time" => "2020-12-08T12:00:00.000000Z", + "last_edited_time" => "2020-12-08T12:00:00.000000Z", + "title" => [[ + "plain_text" => "Page title", + "href" => null, + "annotations" => [ + "bold" => false, + "italic" => false, + "strikethrough" => false, + "underline" => false, + "code" => false, + "color" => "default", + ], + "type" => "text", + "text" => [ "content" => "Database title", "link" => null ], + ]], + "icon" => null, + "cover" => null, + "properties" => [ + "title" => [ + "id" => "title", + "name" => "Dummy prop name", + "type" => "title", + "title" => [], + ], + ], + "parent" => [ + "type" => "page_id", + "page_id" => "1ce62b6f-b7f3-4201-afd0-08acb02e61c6", + ], + "url" => "https://notion.so/a7e80c0ba76643c3a9e921ce94595e0e", + ]; + $database = Database::fromArray($array); + + $outArray = $array; + unset($outArray["parent"]["type"]); + + $this->assertSame($outArray, $database->toArray()); + $this->assertSame("a7e80c0b-a766-43c3-a9e9-21ce94595e0e", $database->id()); + $this->assertSame("https://notion.so/a7e80c0ba76643c3a9e921ce94595e0e", $database->url()); + $this->assertEquals( + "2020-12-08T12:00:00.000000Z", + $database->createdTime()->format(Date::FORMAT), + ); + $this->assertEquals( + "2020-12-08T12:00:00.000000Z", + $database->lastEditedTime()->format(Date::FORMAT), + ); + } + + public function test_from_array_with_emoji_icon(): void + { + $array = [ + "id" => "a7e80c0b-a766-43c3-a9e9-21ce94595e0e", + "created_time" => "2020-12-08T12:00:00.000000Z", + "last_edited_time" => "2020-12-08T12:00:00.000000Z", + "title" => [[ + "plain_text" => "Page title", + "href" => null, + "annotations" => [ + "bold" => false, + "italic" => false, + "strikethrough" => false, + "underline" => false, + "code" => false, + "color" => "default", + ], + "type" => "text", + "text" => [ "content" => "Database title", "link" => null ], + ]], + "icon" => [ + "type" => "emoji", + "emoji" => "⭐", + ], + "cover" => null, + "properties" => [], + "parent" => [ + "type" => "page_id", + "page_id" => "1ce62b6f-b7f3-4201-afd0-08acb02e61c6", + ], + "url" => "https://notion.so/a7e80c0ba76643c3a9e921ce94595e0e", + ]; + $database = Database::fromArray($array); + + $this->assertEquals("⭐", $database->icon()->emoji()); + } + + public function test_from_array_with_file_icon(): void + { + $array = [ + "id" => "a7e80c0b-a766-43c3-a9e9-21ce94595e0e", + "created_time" => "2020-12-08T12:00:00.000000Z", + "last_edited_time" => "2020-12-08T12:00:00.000000Z", + "title" => [[ + "plain_text" => "Page title", + "href" => null, + "annotations" => [ + "bold" => false, + "italic" => false, + "strikethrough" => false, + "underline" => false, + "code" => false, + "color" => "default", + ], + "type" => "text", + "text" => [ "content" => "Database title", "link" => null ], + ]], + "icon" => [ + "type" => "external", + "external" => [ "url" => "https://my-site.com/image.png" ], + ], + "cover" => null, + "properties" => [], + "parent" => [ + "type" => "page_id", + "page_id" => "1ce62b6f-b7f3-4201-afd0-08acb02e61c6", + ], + "url" => "https://notion.so/a7e80c0ba76643c3a9e921ce94595e0e", + ]; + $database = Database::fromArray($array); + + $this->assertEquals("https://my-site.com/image.png", $database->icon()->url()); + } +}