Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/composer/php-http/discovery-1.17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed May 6, 2023
2 parents dd2787d + d4fdb47 commit 98f8b17
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## Added
- Support block as page and database parent

## [1.7.0] 2023-04-26

## Added
Expand Down
16 changes: 13 additions & 3 deletions src/Common/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/**
* @psalm-type FileJson = array{
* type: "external"|"file",
* name: string,
* file?: array{ url: string, expiry_time: string },
* external?: array{ url: string },
* }
Expand All @@ -19,19 +20,20 @@ private function __construct(
public readonly FileType $type,
public readonly string $url,
public readonly DateTimeImmutable|null $expiryTime,
public readonly string $name,
) {
}

public static function createExternal(string $url): self
{
return new self(FileType::External, $url, null);
return new self(FileType::External, $url, null, "File");
}

public static function createInternal(
string $url,
DateTimeImmutable|null $expiryTime = null
): self {
return new self(FileType::Internal, $url, $expiryTime);
return new self(FileType::Internal, $url, $expiryTime, "File");
}

/**
Expand All @@ -49,6 +51,7 @@ public static function fromArray(array $array): self
FileType::from($type),
$file["url"] ?? "",
isset($file["expiry_time"]) ? new DateTimeImmutable($file["expiry_time"]) : null,
$array["name"] ?? "File",
);
}

Expand All @@ -60,6 +63,7 @@ public function toArray(): array
if ($type === FileType::Internal) {
$array = [
"type" => "file",
"name" => $this->name,
"file" => [
"url" => $this->url,
"expiry_time" => $this->expiryTime?->format(Date::FORMAT),
Expand All @@ -70,6 +74,7 @@ public function toArray(): array
if ($type === FileType::External) {
$array = [
"type" => "external",
"name" => $this->name,
"external" => [ "url" => $this->url ],
];
}
Expand All @@ -89,6 +94,11 @@ public function isInternal(): bool

public function changeUrl(string $url): self
{
return new self($this->type, $url, $this->expiryTime);
return new self($this->type, $url, $this->expiryTime, $this->name);
}

public function changeName(string $name): self
{
return new self($this->type, $this->url, $this->expiryTime, $name);
}
}
18 changes: 16 additions & 2 deletions src/Databases/DatabaseParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

/**
* @psalm-type DatabaseParentJson = array{
* type: "page_id"|"workspace",
* type: "page_id"|"workspace"|"block_id",
* page_id?: string,
* workspace?: true,
* block_id?: string,
* }
*
* @psalm-immutable
Expand All @@ -29,6 +30,11 @@ public static function workspace(): self
return new self(DatabaseParentType::Workspace, null);
}

public static function block(string $blockId): self
{
return new self(DatabaseParentType::Block, $blockId);
}

/**
* @param DatabaseParentJson $array
*
Expand All @@ -38,7 +44,7 @@ public static function fromArray(array $array): self
{
$type = DatabaseParentType::from($array["type"]);

$id = $array["page_id"] ?? null;
$id = $array["page_id"] ?? $array["block_id"] ?? null;

return new self($type, $id);
}
Expand All @@ -53,6 +59,9 @@ public function toArray(): array
if ($this->isWorkspace()) {
$array["workspace"] = true;
}
if ($this->isBlock()) {
$array["block_id"] = $this->id;
}

return $array;
}
Expand All @@ -66,4 +75,9 @@ public function isWorkspace(): bool
{
return $this->type === DatabaseParentType::Workspace;
}

public function isBlock(): bool
{
return $this->type === DatabaseParentType::Block;
}
}
1 change: 1 addition & 0 deletions src/Databases/DatabaseParentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ enum DatabaseParentType: string
{
case Page = "page_id";
case Workspace = "workspace";
case Block = "block_id";
}
1 change: 1 addition & 0 deletions src/Databases/Properties/NumberFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum NumberFormat: string
{
case Number = "number";
case NumberChangeCommas = "number_change_commas";
case NumberWithCommas = "number_with_commas";
case Percent = "percent";
case Dollar = "dollar";
case CanadianDollar = "canadian_dollar";
Expand Down
18 changes: 16 additions & 2 deletions src/Pages/PageParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

/**
* @psalm-type PageParentJson = array{
* type: "page_id"|"database_id"|"workspace",
* type: "page_id"|"database_id"|"workspace"|"block_id",
* page_id?: string,
* database_id?: string,
* workspace?: true,
* block_id?: string,
* }
*
* @psalm-immutable
Expand Down Expand Up @@ -35,6 +36,11 @@ public static function workspace(): self
return new self(PageParentType::Workspace, null);
}

public static function block(string $blockId): self
{
return new self(PageParentType::Block, $blockId);
}

/**
* @psalm-param PageParentJson $array
*
Expand All @@ -44,7 +50,7 @@ public static function fromArray(array $array): self
{
$type = PageParentType::from($array["type"]);

$id = $array["page_id"] ?? $array["database_id"] ?? null;
$id = $array["page_id"] ?? $array["database_id"] ?? $array["block_id"] ?? null;

return new self($type, $id);
}
Expand All @@ -62,6 +68,9 @@ public function toArray(): array
if ($this->isWorkspace()) {
$array["workspace"] = true;
}
if ($this->isBlock()) {
$array["block_id"] = $this->id;
}

return $array;
}
Expand All @@ -80,4 +89,9 @@ public function isWorkspace(): bool
{
return $this->type === PageParentType::Workspace;
}

public function isBlock(): bool
{
return $this->type === PageParentType::Block;
}
}
1 change: 1 addition & 0 deletions src/Pages/PageParentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ enum PageParentType: string
case Page = "page_id";
case Database = "database_id";
case Workspace = "workspace";
case Block = "block_id";
}
5 changes: 3 additions & 2 deletions tests/Integration/DatabasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@ public function test_query_all_pages_from_database(): void
$this->assertCount(5, $pages);
}

/** @group bigdb */
public function test_query_big_database(): void
{
$client = Helper::client();
$result = $client->search()->search(SearchQuery::title("Big database"));
$result = $client->search()->search(SearchQuery::title("Big dataset")->filterByDatabases());

if (
count($result->results) > 0 &&
Expand Down Expand Up @@ -239,7 +240,7 @@ private static function bigDatabase(): Database
$client = Helper::client();

$database = Database::create(DatabaseParent::page(Helper::testPageId()))
->changeTitle("Big database");
->changeTitle("Big dataset");

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

Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Blocks/FileBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function test_transform_in_array(): void
"type" => "file",
"file" => [
"type" => "external",
"name" => "File",
"external" => [
"url" => "https://my-site.com/file.doc"
],
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Blocks/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function test_transform_in_array(): void
"type" => "image",
"image" => [
"type" => "external",
"name" => "File",
"external" => [
"url" => "https://my-site.com/image.png"
],
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Blocks/PdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function test_transform_in_array(): void
"type" => "pdf",
"pdf" => [
"type" => "external",
"name" => "File",
"external" => [
"url" => "https://my-site.com/document.pdf"
],
Expand Down
3 changes: 1 addition & 2 deletions tests/Unit/Blocks/ToggleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Notion\Test\Unit\Blocks;

use Notion\Blocks\BlockFactory;
use Notion\Blocks\ToDo;
use Notion\Exceptions\BlockException;
use Notion\Blocks\Toggle;
use Notion\Common\Color;
Expand Down Expand Up @@ -190,7 +189,7 @@ public function test_add_child(): void

public function test_change_color(): void
{
$block = ToDo::fromString("Hello World!")->changeColor(Color::Red);
$block = Toggle::fromString("Hello World!")->changeColor(Color::Red);

$this->assertSame(Color::Red, $block->color);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Blocks/VideoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function test_transform_in_array(): void
"type" => "video",
"video" => [
"type" => "external",
"name" => "File",
"external" => [
"url" => "https://my-site.com/video.mp4"
],
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Common/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function test_intenral_array_conversion(): void
{
$array = [
"type" => "file",
"name" => "Test file",
"file" => [
"url" => "https://notion.so/image.png",
"expiry_time" => "2020-12-08T12:00:00.000000Z",
Expand All @@ -48,6 +49,7 @@ public function test_external_array_conversion(): void
{
$array = [
"type" => "external",
"name" => "Test file",
"external" => [ "url" => "https://my-site.com/image.png" ],
];
$file = File::fromArray($array);
Expand All @@ -61,4 +63,11 @@ public function test_change_url(): void

$this->assertEquals("https://my-site.com/image.png", $file->url);
}

public function test_change_name(): void
{
$file = File::createExternal("")->changeName("My file name");

$this->assertSame("My file name", $file->name);
}
}
1 change: 1 addition & 0 deletions tests/Unit/Common/IconTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function test_icon_from_file_array_conversion(): void

$expected = [
"type" => "external",
"name" => "File",
"external" => ["url" => "http://example.com/icon.png"],
];

Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Databases/DatabaseParentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public function test_create_parent_workspace(): void
$this->assertEquals("workspace", $parent->type->value);
}

public function test_create_parent_block(): void
{
$parent = DatabaseParent::block("0181c3aa-1112-489f-b34a-515b4e3583ed");

$this->assertTrue($parent->isBlock());
$this->assertSame("0181c3aa-1112-489f-b34a-515b4e3583ed", $parent->id);
}

public function test_page_array_conversion(): void
{
$array = [
Expand All @@ -45,6 +53,17 @@ public function test_workspace_array_conversion(): void
$this->assertEquals($array["workspace"], $parent->toArray()["workspace"]);
}

public function test_block_array_conversion(): void
{
$array = [
"type" => "block_id",
"block_id" => "7a774b5d-ca74-4679-9f18-689b5a98f138",
];
$parent = DatabaseParent::fromArray($array);

$this->assertEquals($array["block_id"], $parent->toArray()["block_id"]);
}

public function test_invalid_type_array(): void
{
$this->expectException(\ValueError::class);
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Pages/PageParentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public function test_create_parent_page(): void
$this->assertEquals("058d158b-09de-4d69-be07-901c20a7ca5c", $parent->id);
}

public function test_create_parent_block(): void
{
$parent = PageParent::block("0181c3aa-1112-489f-b34a-515b4e3583ed");

$this->assertTrue($parent->isBlock());
$this->assertSame("0181c3aa-1112-489f-b34a-515b4e3583ed", $parent->id);
}

public function test_create_parent_workspace(): void
{
$parent = PageParent::workspace();
Expand Down Expand Up @@ -65,6 +73,17 @@ public function test_workspace_array_conversion(): void
$this->assertEquals($array["workspace"], $parent->toArray()["workspace"]);
}

public function test_block_array_conversion(): void
{
$array = [
"type" => "block_id",
"block_id" => "7a774b5d-ca74-4679-9f18-689b5a98f138",
];
$parent = PageParent::fromArray($array);

$this->assertEquals($array["block_id"], $parent->toArray()["block_id"]);
}

public function test_invalid_type_array(): void
{
$this->expectException(\ValueError::class);
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Pages/Properties/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@ public function test_is_empty(): void
$date = Date::fromArray($array);

$this->assertTrue($date->isEmpty());
$this->assertFalse($date->isRange());
}
}
1 change: 1 addition & 0 deletions tests/Unit/Pages/Properties/FilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function test_array_conversion(): void
"files" => [
[
"type" => "external",
"name" => "Test file",
"external" => [
"url" => "https://example.com/image.png",
],
Expand Down

0 comments on commit 98f8b17

Please sign in to comment.