|
| 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