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
69c63bb
commit b122ef4
Showing
2 changed files
with
239 additions
and
1 deletion.
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,236 @@ | ||
<?php | ||
|
||
namespace Notion\Test\Unit\Databases; | ||
|
||
use Notion\Common\Date; | ||
use Notion\Common\Emoji; | ||
use Notion\Common\File; | ||
use Notion\Common\RichText; | ||
use Notion\Databases\Database; | ||
use Notion\Databases\DatabaseParent; | ||
use Notion\Databases\Properties\Title; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DatabaseTest extends TestCase | ||
{ | ||
public function test_create_database(): void | ||
{ | ||
$parent = DatabaseParent::page("1ce62b6f-b7f3-4201-afd0-08acb02e61c6"); | ||
$database = Database::create($parent); | ||
|
||
$this->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()); | ||
} | ||
} |