Skip to content

Commit 79a442b

Browse files
committed
added tests for endpoint block and replaced not found handling exception with proper notion exception
1 parent a58b905 commit 79a442b

File tree

11 files changed

+196
-62
lines changed

11 files changed

+196
-62
lines changed

src/Endpoints/Block.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

5-
use FiveamCode\LaravelNotionApi\Entities\Collections\BlockCollection;
6-
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
75
use FiveamCode\LaravelNotionApi\Notion;
8-
use Illuminate\Support\Collection;
6+
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
7+
use FiveamCode\LaravelNotionApi\Entities\Collections\BlockCollection;
98

109
class Block extends Endpoint
1110
{
@@ -30,6 +29,9 @@ public function children(): BlockCollection
3029
$this->url(Endpoint::BLOCKS . "/" . $this->blockId . "/children" . "?{$this->buildPaginationQuery()}")
3130
);
3231

32+
if ($response->failed())
33+
throw NotionException::fromResponse($response);
34+
3335
return new BlockCollection($response->json());
3436
}
3537

src/Endpoints/Databases.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

55
use FiveamCode\LaravelNotionApi\Entities\Database;
6-
use FiveamCode\LaravelNotionApi\Entities\Collections\DatabaseCollection;
76
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
8-
use FiveamCode\LaravelNotionApi\Notion;
9-
use FiveamCode\LaravelNotionApi\Query\StartCursor;
10-
use Illuminate\Support\Collection;
7+
use FiveamCode\LaravelNotionApi\Entities\Collections\DatabaseCollection;
118

129

1310
/**

src/Endpoints/Pages.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

55
use FiveamCode\LaravelNotionApi\Entities\Page;
6-
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
7-
use FiveamCode\LaravelNotionApi\Notion;
6+
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
87

98
class Pages extends Endpoint implements EndpointInterface
109
{
@@ -23,19 +22,21 @@ public function find(string $pageId): Page
2322
$this->url(Endpoint::PAGES . "/" . $pageId)
2423
);
2524

26-
if(!$response->ok())
27-
throw HandlingException::instance("Page not found.", ["pageId" => $pageId]);
25+
if ($response->failed())
26+
throw NotionException::fromResponse($response);
2827

2928
return new Page($response->json());
3029
}
3130

32-
public function create(): array{
31+
public function create(): array
32+
{
3333
//toDo
3434
throw new \Exception("not implemented yet");
3535
}
3636

3737

38-
public function updateProperties(): array{
38+
public function updateProperties(): array
39+
{
3940
//toDo
4041
throw new \Exception("not implemented yet");
4142
}

src/Endpoints/Users.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use FiveamCode\LaravelNotionApi\Entities\User;
66
use FiveamCode\LaravelNotionApi\Entities\Collections\UserCollection;
77
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
8+
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
89
use FiveamCode\LaravelNotionApi\Notion;
910
use FiveamCode\LaravelNotionApi\Query\StartCursor;
1011
use Illuminate\Support\Collection;
@@ -24,7 +25,7 @@ public function all(): UserCollection
2425
$result = $this->get(
2526
$this->url(Endpoint::USERS . "?{$this->buildPaginationQuery()}")
2627
);
27-
28+
2829
return new UserCollection($result->json());
2930
}
3031

@@ -42,8 +43,8 @@ public function find(string $userId): User
4243
$this->url(Endpoint::USERS . "/" . $userId)
4344
);
4445

45-
if (!$response->ok())
46-
throw HandlingException::instance("User not found.", ["userId" => $userId]);
46+
if ($response->failed())
47+
throw NotionException::fromResponse($response);
4748

4849

4950
return new User($response->json());

src/Entities/Blocks/Block.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
namespace FiveamCode\LaravelNotionApi\Entities\Blocks;
44

55
use DateTime;
6+
use Illuminate\Support\Arr;
67
use FiveamCode\LaravelNotionApi\Entities\Entity;
78
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
8-
use FiveamCode\LaravelNotionApi\Notion;
9-
use Illuminate\Support\Arr;
109

1110
class Block extends Entity
1211
{

src/Entities/Collections/BlockCollection.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
namespace FiveamCode\LaravelNotionApi\Entities\Collections;
44

5-
use FiveamCode\LaravelNotionApi\Entities\Blocks\Block;
6-
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
7-
use FiveamCode\LaravelNotionApi\Notion;
8-
use Illuminate\Support\Arr;
95
use Illuminate\Support\Collection;
6+
use FiveamCode\LaravelNotionApi\Entities\Blocks\Block;
107

118

129
class BlockCollection extends EntityCollection

src/Entities/Collections/EntityCollection.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
namespace FiveamCode\LaravelNotionApi\Entities\Collections;
44

55

6-
use FiveamCode\LaravelNotionApi\Entities\Entity;
76
use Illuminate\Support\Arr;
87
use Illuminate\Support\Collection;
98
use FiveamCode\LaravelNotionApi\Entities\Page;
9+
use FiveamCode\LaravelNotionApi\Entities\Entity;
1010
use FiveamCode\LaravelNotionApi\Entities\Database;
11+
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
1112
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
1213

1314

@@ -17,18 +18,29 @@ class EntityCollection
1718
protected array $rawResults = [];
1819
protected Collection $collection;
1920

20-
public function __construct(array $reponseData = null)
21+
public function __construct(array $responseData = null)
2122
{
22-
$this->setResponseData($reponseData);
23+
$this->setResponseData($responseData);
2324
}
2425

25-
protected function setResponseData(array $reponseData): void
26+
protected function setResponseData(array $responseData): void
2627
{
27-
if (!Arr::exists($reponseData, 'object')) throw HandlingException::instance("invalid json-array: no object given");
28-
if (!Arr::exists($reponseData, 'results')) throw HandlingException::instance("invalid json-array: no results given");
29-
if ($reponseData['object'] !== 'list') throw HandlingException::instance("invalid json-array: the given object is not a list");
28+
// TODO
29+
// Currently, the API returns not-found objects with status code 200 -
30+
// so we have to check here on the given status code in the paylaod,
31+
// if the object was not found.
32+
if (
33+
$responseData['object'] === 'error'
34+
&& Arr::exists($responseData, 'status') && $responseData['status'] === 404
35+
) {
36+
throw NotionException::instance("Not found", compact("responseData"));
37+
}
3038

31-
$this->responseData = $reponseData;
39+
if (!Arr::exists($responseData, 'object')) throw HandlingException::instance("invalid json-array: no object given");
40+
if (!Arr::exists($responseData, 'results')) throw HandlingException::instance("invalid json-array: no results given");
41+
if ($responseData['object'] !== 'list') throw HandlingException::instance("invalid json-array: the given object is not a list");
42+
43+
$this->responseData = $responseData;
3244
$this->fillFromRaw();
3345
$this->collectChildren();
3446
}
@@ -38,8 +50,8 @@ protected function collectChildren(): void
3850
$this->collection = new Collection();
3951
foreach ($this->rawResults as $pageChild) {
4052
if (Arr::exists($pageChild, 'object')) {
41-
if($pageChild['object'] == 'page') $this->collection->add(new Page($pageChild));
42-
if($pageChild['object'] == 'database') $this->collection->add(new Database($pageChild));
53+
if ($pageChild['object'] == 'page') $this->collection->add(new Page($pageChild));
54+
if ($pageChild['object'] == 'database') $this->collection->add(new Database($pageChild));
4355
}
4456
}
4557
}
@@ -65,7 +77,8 @@ public function asCollection(): Collection
6577
return $this->collection;
6678
}
6779

68-
public function asJson(): string {
80+
public function asJson(): string
81+
{
6982
return $this->collection->map(function (Entity $item) {
7083
return $item->toArray();
7184
});

tests/EndpointBlocksTest.php

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
namespace FiveamCode\LaravelNotionApi\Tests;
44

5-
use Carbon\Carbon;
6-
use FiveamCode\LaravelNotionApi\Entities\Page;
7-
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
85
use Illuminate\Support\Facades\Http;
6+
use FiveamCode\LaravelNotionApi\Entities\Blocks\Block;
7+
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
8+
use FiveamCode\LaravelNotionApi\Entities\Collections\BlockCollection;
99

1010
/**
11-
* Class EndpointPageTest
11+
* Class EndpointBlocksTest
1212
*
1313
* The fake API responses are based on our test environment (since the current Notion examples do not match with the actual calls).
14-
* @see https://developers.notion.com/reference/get-page
14+
* @see https://developers.notion.com/reference/get-block-children
1515
*
1616
* @package FiveamCode\LaravelNotionApi\Tests
1717
*/
@@ -21,9 +21,9 @@ class EndpointBlocksTest extends NotionApiTest
2121
/** @test */
2222
public function it_throws_a_notion_exception_bad_request()
2323
{
24-
// failing /v1/databases
24+
// failing /v1/blocks
2525
Http::fake([
26-
'https://api.notion.com/v1/pages*'
26+
'https://api.notion.com/v1/blocks/b55c9c91-384d-452b-81db-d1ef79372b76/children*'
2727
=> Http::response(
2828
json_decode('{}', true),
2929
400,
@@ -34,44 +34,49 @@ public function it_throws_a_notion_exception_bad_request()
3434
$this->expectException(NotionException::class);
3535
$this->expectExceptionMessage("Bad Request");
3636

37-
\Notion::pages()->find("afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8");
37+
\Notion::block("b55c9c91-384d-452b-81db-d1ef79372b76")->children();
3838
}
3939

4040
/** @test */
41-
public function it_returns_page_entity_with_filled_properties()
41+
public function it_returns_block_collection_with_children()
4242
{
43-
// successful /v1/pages/PAGE_DOES_EXIST
43+
// successful /v1/blocks/BLOCK_DOES_EXIST/children
4444
Http::fake([
45-
'https://api.notion.com/v1/pages/afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8'
45+
'https://api.notion.com/v1/blocks/b55c9c91-384d-452b-81db-d1ef79372b76/children*'
4646
=> Http::response(
47-
json_decode(file_get_contents('tests/stubs/endpoints/pages/response_specific_200.json'), true),
47+
json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_200.json'), true),
4848
200,
4949
['Headers']
5050
)
5151
]);
5252

53-
$pageResult = \Notion::pages()->find("afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8");
54-
55-
$this->assertInstanceOf(Page::class, $pageResult);
56-
57-
// check properties
58-
$this->assertSame("Notion Is Awesome", $pageResult->getTitle());
59-
$this->assertSame("page", $pageResult->getObjectType());
60-
61-
$this->assertCount(6, $pageResult->getRawProperties());
62-
63-
$this->assertInstanceOf(Carbon::class, $pageResult->getCreatedTime());
64-
$this->assertInstanceOf(Carbon::class, $pageResult->getLastEditedTime());
53+
$blockChildren = \Notion::block("b55c9c91-384d-452b-81db-d1ef79372b76")->children();
54+
$this->assertInstanceOf(BlockCollection::class, $blockChildren);
55+
56+
$blockChildrenCollection = $blockChildren->asCollection();
57+
$this->assertContainsOnly(Block::class, $blockChildrenCollection);
58+
$this->assertIsIterable($blockChildrenCollection);
59+
$this->assertCount(3, $blockChildrenCollection);
60+
61+
// check first child
62+
$blockChild = $blockChildrenCollection->first();
63+
$this->assertInstanceOf(Block::class, $blockChild);
64+
$this->assertEquals("heading_2", $blockChild->getType());
65+
$this->assertFalse($blockChild->hasChildren());
66+
$this->assertArrayHasKey("text", $blockChild->getRawContent());
67+
$this->assertArrayHasKey(0, $blockChild->getRawContent()["text"]);
68+
$this->assertArrayHasKey("plain_text", $blockChild->getRawContent()["text"][0]);
69+
$this->assertEquals("Lacinato kale", $blockChild->getRawContent()["text"][0]["plain_text"]);
6570
}
6671

6772
/** @test */
6873
public function it_throws_a_notion_exception_not_found()
6974
{
70-
// failing /v1/pages/PAGE_DOES_NOT_EXIST
75+
// failing /v1/blocks/BLOCK_DOES_NOT_EXIST/children
7176
Http::fake([
72-
'https://api.notion.com/v1/pages/b55c9c91-384d-452b-81db-d1ef79372b79'
77+
'https://api.notion.com/v1/blocks/b55c9c91-384d-452b-81db-d1ef79372b11*'
7378
=> Http::response(
74-
json_decode(file_get_contents('tests/stubs/endpoints/pages/response_specific_404.json'), true),
79+
json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_404.json'), true),
7580
200,
7681
['Headers']
7782
)
@@ -80,7 +85,7 @@ public function it_throws_a_notion_exception_not_found()
8085
$this->expectException(NotionException::class);
8186
$this->expectExceptionMessage("Not found");
8287

83-
\Notion::pages()->find("b55c9c91-384d-452b-81db-d1ef79372b79");
88+
\Notion::block("b55c9c91-384d-452b-81db-d1ef79372b11")->children();
8489
}
8590

8691
}

tests/EndpointPagesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class EndpointPagesTest extends NotionApiTest
2323
/** @test */
2424
public function it_throws_a_notion_exception_bad_request()
2525
{
26-
// failing /v1/databases
26+
// failing /v1
2727
Http::fake([
2828
'https://api.notion.com/v1/pages*'
2929
=> Http::response(

0 commit comments

Comments
 (0)