Skip to content

Commit e73207a

Browse files
committed
add tests for Notion::resolve(); polish record
- polish `PestHttpRecorder::class`
1 parent c83c9b1 commit e73207a

File tree

4 files changed

+100
-5
lines changed

4 files changed

+100
-5
lines changed

src/Endpoints/Resolve.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function parentOf(Entity $entity)
7474
* @throws HandlingException
7575
* @throws NotionException
7676
*/
77-
public function parent(NotionParent $parent): Page|Database|Block
77+
public function parent(NotionParent $parent): Page|Database|Block|NotionParent
7878
{
7979
switch ($parent->getObjectType()) {
8080
case 'page_id':
@@ -83,8 +83,9 @@ public function parent(NotionParent $parent): Page|Database|Block
8383
return $this->notion->databases()->find($parent->getId());
8484
case 'block_id':
8585
return $this->notion->block($parent->getId())->retrieve();
86-
case 'workspace_id':
87-
throw new HandlingException('A Notion Workspace cannot be resolved by the Notion API.');
86+
case 'workspace':
87+
return $parent;
88+
// throw new HandlingException('A Notion Workspace cannot be resolved by the Notion API.');
8889
default:
8990
throw new HandlingException('Unknown parent type while resolving the notion parent');
9091
}

src/Entities/NotionParent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected function setResponseData(array $responseData): void
2222
if (
2323
$responseData['object'] !== 'page_id'
2424
&& $responseData['object'] !== 'database_id'
25-
&& $responseData['object'] !== 'workspace_id'
25+
&& $responseData['object'] !== 'workspace'
2626
&& $responseData['object'] !== 'block_id'
2727
) {
2828
throw HandlingException::instance('invalid json-array: the given object is not a valid parent');

src/Macros/PestHttpRecorder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function handle(Request $request)
7272
$header = $request->headers();
7373
$method = Str::lower($request->method());
7474
$name = Str::slug(Str::replace('/', '-', $urlInfo['path']));
75-
$payload = ($method === 'get') ? $urlInfo['query'] : $request->body();
75+
$payload = ($method === 'get') ? ($urlInfo['query'] ?? null) : $request->body();
7676
$queryName = array_pop($this->requestNames) ?? hash('adler32', $payload);
7777

7878
$fileName = "{$method}_{$name}_{$queryName}.json";
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
use FiveamCode\LaravelNotionApi\Entities\NotionParent;
4+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
5+
use Illuminate\Support\Facades\Http;
6+
7+
$httpRecorder = null;
8+
9+
beforeEach(function () {
10+
$this->httpRecorder = Http::recordAndFakeLater([
11+
'https://api.notion.com/v1/databases*',
12+
'https://api.notion.com/v1/pages*',
13+
'https://api.notion.com/v1/blocks*',
14+
'https://api.notion.com/v1/users*',
15+
])->storeIn('snapshots/resolve');
16+
});
17+
18+
it('should resolve the users of specific page properties', function () {
19+
$this->httpRecorder->nameForNextRequest('for-user-resolve');
20+
$page = Notion::pages()->find('8890c263e97c45339ef5616d5e75360e');
21+
22+
$createdBy = $page->getProperty('Created by');
23+
$lastEditedBy = $page->getProperty('Last edited by');
24+
$person = $page->getProperty('Person');
25+
26+
$createdByUser = Notion::resolve()->user($createdBy->getUser());
27+
$lastEditedByUser = Notion::resolve()->user($lastEditedBy->getUser());
28+
$personUser = Notion::resolve()->user($person->getPeople()->first());
29+
30+
expect($createdByUser)->toBeInstanceOf(\FiveamCode\LaravelNotionApi\Entities\User::class);
31+
expect($createdByUser->getName())->toBe('TestUser for NotionForLaravel');
32+
expect($createdByUser->getId())->toBe('455aad58-7aec-4a39-8c0f-37cab3ca38f5');
33+
34+
expect($lastEditedByUser)->toBeInstanceOf(\FiveamCode\LaravelNotionApi\Entities\User::class);
35+
expect($lastEditedByUser->getName())->toBe('TestUser for NotionForLaravel');
36+
expect($lastEditedByUser->getId())->toBe('455aad58-7aec-4a39-8c0f-37cab3ca38f5');
37+
38+
expect($personUser)->toBeInstanceOf(\FiveamCode\LaravelNotionApi\Entities\User::class);
39+
expect($personUser->getName())->toBe('TestUser for NotionForLaravel');
40+
expect($personUser->getId())->toBe('455aad58-7aec-4a39-8c0f-37cab3ca38f5');
41+
});
42+
43+
it('should resolve the page parent of a page', function () {
44+
$page = Notion::pages()->find('a652fac351cc4cc79f5b17eb702793ed');
45+
$parentPage = Notion::resolve()->parent($page->getParent());
46+
47+
expect($parentPage)->toBeInstanceOf(\FiveamCode\LaravelNotionApi\Entities\Page::class);
48+
expect($parentPage->getId())->toBe('5ac149b9-d8f1-4d8d-ac05-facefc16ebf7');
49+
expect($parentPage->getTitle())->toBe('Resolve Endpoint - Testing Suite');
50+
});
51+
52+
it('should resolve the database parent of a page', function(){
53+
$page = Notion::pages()->find('415d9b6c6e454f42aab2b6e13804cfe9');
54+
55+
$database = Notion::resolve()->parent($page->getParent());
56+
expect($database)->toBeInstanceOf(\FiveamCode\LaravelNotionApi\Entities\Database::class);
57+
expect($database->getId())->toBe('8a0ef209-8c8a-4fd1-a21c-db7ab327e870');
58+
expect($database->getTitle())->toBe('Test Table as Parent');
59+
});
60+
61+
it('should resolve the block parent of a block', function(){
62+
$block = Notion::block('d5f9419b44204c909501b1e2b7569503')->retrieve();
63+
64+
$parentBlock = Notion::resolve()->parent($block->getParent());
65+
expect($parentBlock)->toBeInstanceOf(\FiveamCode\LaravelNotionApi\Entities\Blocks\Block::class);
66+
expect($parentBlock->getId())->toBe('0971ac1a-b6f2-4acc-b706-f5f2ed16ffd6');
67+
expect($parentBlock->getType())->toBe('paragraph');
68+
});
69+
70+
it('should resolve the page parent of a block', function(){
71+
$block = Notion::block('0971ac1a-b6f2-4acc-b706-f5f2ed16ffd6')->retrieve();
72+
73+
$pageParent = Notion::resolve()->parent($block->getParent());
74+
expect($pageParent)->toBeInstanceOf(\FiveamCode\LaravelNotionApi\Entities\Page::class);
75+
expect($pageParent->getId())->toBe('d946d011-966d-4b14-973f-dc5580f5b024');
76+
expect($pageParent->getTitle())->toBe('Page for Block Parent Resolve Testing');
77+
});
78+
79+
it('should throw a handling exception when unknown parent type', function(){
80+
expect(fn() => new NotionParent(['object' => 'unknown', 'id' => '1234']))->toThrow('invalid json-array: the given object is not a valid parent');
81+
});
82+
83+
it('should resolve the pages of a database relation', function(){
84+
$page = Notion::pages()->find('1c56e2ad3d95458c935dae6d57769037');
85+
86+
$relationPropertyItems = $page->getProperty('Parent Relation Database');
87+
$relationPages = Notion::resolve()->relations($relationPropertyItems);
88+
89+
expect($relationPages)->toBeInstanceOf(\Illuminate\Support\Collection::class);
90+
expect($relationPages->count())->toBe(3);
91+
expect($relationPages->first())->toBeInstanceOf(\FiveamCode\LaravelNotionApi\Entities\Page::class);
92+
expect($relationPages->first()->getId())->toBe('cfb10a19-30cc-43a9-8db0-04c43f8cf315');
93+
expect($relationPages->first()->getTitle())->toBe('test 1');
94+
});

0 commit comments

Comments
 (0)