Skip to content

Commit

Permalink
Add database integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Nov 1, 2021
1 parent 21ec4b3 commit 92a25ee
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 14 deletions.
10 changes: 10 additions & 0 deletions src/Client.php
Expand Up @@ -3,6 +3,7 @@
namespace Notion;

use GuzzleHttp\Client as GuzzleClient;
use Notion\Databases\Client as DatabasesClient;
use Notion\Pages\Client as PagesClient;
use Notion\Users\Client as UsersClient;
use Psr\Http\Client\ClientInterface;
Expand Down Expand Up @@ -52,6 +53,15 @@ public function pages(): PagesClient
);
}

public function databases(): DatabasesClient
{
return new DatabasesClient(
$this->psrClient,
$this->token,
self::NOTION_VERSION,
);
}

private static function resolvePsrClient(): ClientInterface
{
if (class_exists(GuzzleClient::class)) {
Expand Down
46 changes: 32 additions & 14 deletions src/Databases/Client.php
Expand Up @@ -58,11 +58,8 @@ public function find(string $databaseId): Database

public function create(Database $database): Database
{
$data = json_encode([
"title" => array_map(fn(RichText $t) => $t->toArray(), $database->title()),
"properties" => array_map(fn(PropertyInterface $p) => $p->toArray(), $database->properties()),
"parent" => $database->parent()->toArray(),
]);
$data = $database->toArray();
unset($data["id"]);

$request = new Request(
"POST",
Expand All @@ -72,7 +69,7 @@ public function create(Database $database): Database
"Notion-Version" => $this->version,
"Content-Type" => "application/json",
],
$data,
json_encode($data),
);

$response = $this->psrClient->sendRequest($request);
Expand All @@ -94,13 +91,10 @@ public function create(Database $database): Database

public function update(Database $database): Database
{
$data = json_encode([
"title" => array_map(fn(RichText $t) => $t->toArray(), $database->title()),
"icon" => $database->icon()?->toArray(),
"cover" => $database->cover()?->toArray(),
"properties" => array_map(fn(PropertyInterface $p) => $p->toArray(), $database->properties()),
"parent" => $database->parent()->toArray(),
]);
$data = $database->toArray();
unset($data["parent"]);
unset($data["created_time"]);
unset($data["last_edited_time"]);

$databaseId = $database->id();
$request = new Request(
Expand All @@ -111,7 +105,7 @@ public function update(Database $database): Database
"Notion-Version" => $this->version,
"Content-Type" => "application/json",
],
$data,
json_encode($data),
);

$response = $this->psrClient->sendRequest($request);
Expand All @@ -130,4 +124,28 @@ public function update(Database $database): Database
/** @psalm-var DatabaseJson $body */
return Database::fromArray($body);
}

public function delete(Database $database): void
{
$databaseId = $database->id();
$request = new Request(
"DELETE",
"https://api.notion.com/v1/blocks/{$databaseId}",
[
"Authorization" => "Bearer {$this->token}",
"Notion-Version" => $this->version,
],
);

$response = $this->psrClient->sendRequest($request);

if ($response->getStatusCode() !== 200) {
/** @var array{ message: string, code: string} $body */
$body = json_decode((string) $response->getBody(), true);
$message = $body["message"];
$code = $body["code"];

throw new NotionException($message, $code);
}
}
}
121 changes: 121 additions & 0 deletions tests/Integration/DatabasesTest.php
@@ -0,0 +1,121 @@
<?php

namespace Notion\Test\Integration;

use Notion\Client;
use Notion\Common\Emoji;
use Notion\Common\RichText;
use Notion\Databases\Database;
use Notion\Databases\DatabaseParent;
use Notion\Databases\Properties\CreatedBy;
use Notion\Databases\Properties\Title;
use Notion\NotionException;
use PHPUnit\Framework\TestCase;

class DatabasesTest extends TestCase
{
private const DEFAULT_PARENT_ID = "3f4c46dee17f43b79587094b61407a31";

public function test_create_empty_database(): void
{
$token = getenv("NOTION_TOKEN");
$client = Client::create($token);

$database = Database::create(DatabaseParent::page(self::DEFAULT_PARENT_ID))
->withTitle(RichText::createText("Empty database"))
->withIcon(Emoji::create("🌻"));

$database = $client->databases()->create($database);

$databaseFound = $client->databases()->find($database->id());

$this->assertEquals("Empty database", $database->title()[0]->plainText());
$this->assertEquals("🌻", $databaseFound->icon()->emoji());

$client->databases()->delete($database);
}

public function test_find_database(): void
{
$token = getenv("NOTION_TOKEN");
$client = Client::create($token);

$database = $client->databases()->find("a1acab7aeea2438bb0e9b23b73fb4a25");

$this->assertEquals("Database Sample", $database->title()[0]->plainText());
}

public function test_update_database(): void
{
$token = getenv("NOTION_TOKEN");
$client = Client::create($token);

$database = $client->databases()->find("a1acab7aeea2438bb0e9b23b73fb4a25");
$database = $database->addProperty(CreatedBy::create());

$updatedDatabase = $client->databases()->update($database);

$this->assertEquals(
"CreatedBy",
$updatedDatabase->properties()["CreatedBy"]->property()->name()
);

// Back to original state
$original = $updatedDatabase->withProperties([ "Title" => Title::create() ]);
$client->databases()->update($original);
}

public function test_find_inexistent_database(): void
{
$token = getenv("NOTION_TOKEN");
$client = Client::create($token);

$this->expectException(NotionException::class);
$this->expectErrorMessage("Could not find database with ID: 60e79d42-4742-41ca-8d70-cc51660cbd3c.");
$client->databases()->find("60e79d42-4742-41ca-8d70-cc51660cbd3c");
}

public function test_create_with_inexistent_parent(): void
{
$token = getenv("NOTION_TOKEN");
$client = Client::create($token);

$database = Database::create(DatabaseParent::page("60e79d42-4742-41ca-8d70-cc51660cbd3c"));

$this->expectException(NotionException::class);
$this->expectErrorMessage("Could not find page with ID: 60e79d42-4742-41ca-8d70-cc51660cbd3c.");
$client->databases()->create($database);
}

public function test_update_deleted_database(): void
{
$token = getenv("NOTION_TOKEN");
$client = Client::create($token);

$database = Database::create(DatabaseParent::page(self::DEFAULT_PARENT_ID))
->withTitle(RichText::createText("Dummy database"));

$database = $client->databases()->create($database);

$client->databases()->delete($database);

$this->expectException(NotionException::class);
$client->databases()->update($database);
}

public function test_delete_database_twice(): void
{
$token = getenv("NOTION_TOKEN");
$client = Client::create($token);

$database = Database::create(DatabaseParent::page(self::DEFAULT_PARENT_ID))
->withTitle(RichText::createText("Dummy database"));

$database = $client->databases()->create($database);

$client->databases()->delete($database);

$this->expectException(NotionException::class);
$client->databases()->delete($database);
}
}

0 comments on commit 92a25ee

Please sign in to comment.