|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace FiveamCode\LaravelNotionApi\Tests; |
| 4 | + |
| 5 | +use FiveamCode\LaravelNotionApi\Entities\Collections\EntityCollection; |
| 6 | +use FiveamCode\LaravelNotionApi\Entities\Database; |
| 7 | +use FiveamCode\LaravelNotionApi\Entities\Page; |
| 8 | +use FiveamCode\LaravelNotionApi\Exceptions\NotionException; |
| 9 | +use Illuminate\Support\Facades\Http; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class EndpointSearchTest |
| 13 | + * |
| 14 | + * The fake API responses are based on Notions documentation. |
| 15 | + * @see https://developers.notion.com/reference/post-search |
| 16 | + * |
| 17 | + * @package FiveamCode\LaravelNotionApi\Tests |
| 18 | + */ |
| 19 | +class EndpointSearchTest extends NotionApiTest |
| 20 | +{ |
| 21 | + |
| 22 | + /** @test */ |
| 23 | + public function it_throws_a_notion_exception_bad_request() |
| 24 | + { |
| 25 | + // failing /v1/search |
| 26 | + Http::fake([ |
| 27 | + 'https://api.notion.com/v1/search' |
| 28 | + => Http::response( |
| 29 | + json_decode('{}', true), |
| 30 | + 400, |
| 31 | + ['Headers'] |
| 32 | + ) |
| 33 | + ]); |
| 34 | + |
| 35 | + $this->expectException(NotionException::class); |
| 36 | + $this->expectExceptionMessage("Bad Request"); |
| 37 | + |
| 38 | + \Notion::search()->query(); |
| 39 | + } |
| 40 | + |
| 41 | + /** @test */ |
| 42 | + public function it_returns_all_pages_and_databases_of_the_workspace_as_collection_with_entity_objects() |
| 43 | + { |
| 44 | + // successful /v1/search |
| 45 | + Http::fake([ |
| 46 | + 'https://api.notion.com/v1/search' |
| 47 | + => Http::response( |
| 48 | + json_decode(file_get_contents('tests/stubs/endpoints/search/response_all_200.json'), true), |
| 49 | + 200, |
| 50 | + ['Headers'] |
| 51 | + ) |
| 52 | + ]); |
| 53 | + |
| 54 | + $searchResult = \Notion::search()->query(); |
| 55 | + $entityCollection = $searchResult->asCollection(); |
| 56 | + $this->assertInstanceOf(EntityCollection::class, $searchResult); |
| 57 | + $this->assertIsIterable($entityCollection); |
| 58 | + $this->assertCount(2, $entityCollection); |
| 59 | + |
| 60 | + $database = $entityCollection[0]; |
| 61 | + $page = $entityCollection[1]; |
| 62 | + |
| 63 | + $this->assertInstanceOf(Database::class, $database); |
| 64 | + $this->assertInstanceOf(Page::class, $page); |
| 65 | + } |
| 66 | + |
| 67 | + /** @test */ |
| 68 | + public function it_returns_only_pages_of_the_workspace_as_collection_with_entity_objects() |
| 69 | + { |
| 70 | + // successful /v1/search |
| 71 | + Http::fake([ |
| 72 | + 'https://api.notion.com/v1/search' |
| 73 | + => Http::response( |
| 74 | + json_decode(file_get_contents('tests/stubs/endpoints/search/response_pages_200.json'), true), |
| 75 | + 200, |
| 76 | + ['Headers'] |
| 77 | + ) |
| 78 | + ]); |
| 79 | + |
| 80 | + $searchResult = \Notion::search()->onlyPages()->query(); |
| 81 | + $entityCollection = $searchResult->asCollection(); |
| 82 | + $this->assertInstanceOf(EntityCollection::class, $searchResult); |
| 83 | + $this->assertIsIterable($entityCollection); |
| 84 | + $this->assertCount(1, $entityCollection); |
| 85 | + |
| 86 | + $page = $entityCollection->first(); |
| 87 | + |
| 88 | + $this->assertInstanceOf(Page::class, $page); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /** @test */ |
| 93 | + public function it_returns_only_databases_of_the_workspace_as_collection_with_entity_objects() |
| 94 | + { |
| 95 | + // successful /v1/search |
| 96 | + Http::fake([ |
| 97 | + 'https://api.notion.com/v1/search' |
| 98 | + => Http::response( |
| 99 | + json_decode(file_get_contents('tests/stubs/endpoints/search/response_databases_200.json'), true), |
| 100 | + 200, |
| 101 | + ['Headers'] |
| 102 | + ) |
| 103 | + ]); |
| 104 | + |
| 105 | + $searchResult = \Notion::search()->onlyDatabases()->query(); |
| 106 | + $entityCollection = $searchResult->asCollection(); |
| 107 | + $this->assertInstanceOf(EntityCollection::class, $searchResult); |
| 108 | + $this->assertIsIterable($entityCollection); |
| 109 | + $this->assertCount(1, $entityCollection); |
| 110 | + |
| 111 | + $database = $entityCollection->first(); |
| 112 | + |
| 113 | + $this->assertInstanceOf(Database::class, $database); |
| 114 | + } |
| 115 | +} |
0 commit comments