Skip to content

Commit

Permalink
Add database unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Oct 31, 2021
1 parent 69c63bb commit b122ef4
Show file tree
Hide file tree
Showing 2 changed files with 239 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Databases/Database.php
Expand Up @@ -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),
Expand Down Expand Up @@ -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(
Expand Down
236 changes: 236 additions & 0 deletions tests/Unit/Databases/DatabaseTest.php
@@ -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());
}
}

0 comments on commit b122ef4

Please sign in to comment.