Skip to content

Commit b523ec2

Browse files
committed
fix missing exception in ->post(...) and add tests for search-endpoint
1 parent ac7592e commit b523ec2

File tree

6 files changed

+294
-2
lines changed

6 files changed

+294
-2
lines changed

src/Endpoints/Endpoint.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,13 @@ protected function get(string $url)
9090
*/
9191
protected function post(string $url, array $body)
9292
{
93-
return $this->notion->getConnection()->post($url, $body);
93+
$response = $this->notion->getConnection()->post($url, $body);
94+
95+
if ($response->failed())
96+
throw NotionException::fromResponse($response);
97+
98+
$this->response = $response;
99+
return $response;
94100
}
95101

96102

tests/EndpointSearchTest.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
}

tests/EndpointUsersTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class EndpointUsersTest extends NotionApiTest
2626
/** @test */
2727
public function it_throws_a_notion_exception_bad_request()
2828
{
29-
// failing /v1
29+
// failing /v1/users
3030
Http::fake([
3131
'https://api.notion.com/v1/users?*'
3232
=> Http::response(
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"has_more": false,
3+
"next_cursor": null,
4+
"object": "list",
5+
"results": [
6+
{
7+
"created_time": "2021-04-22T22:23:26.080Z",
8+
"id": "e6c6f8ff-c70e-4970-91ba-98f03e0d7fc6",
9+
"last_edited_time": "2021-04-23T04:21:00.000Z",
10+
"object": "database",
11+
"properties": {
12+
"Name": {
13+
"id": "title",
14+
"title": {},
15+
"type": "title"
16+
},
17+
"Task Type": {
18+
"id": "vd@l",
19+
"multi_select": {
20+
"options": []
21+
},
22+
"type": "multi_select"
23+
}
24+
},
25+
"title": [
26+
{
27+
"annotations": {
28+
"bold": false,
29+
"code": false,
30+
"color": "default",
31+
"italic": false,
32+
"strikethrough": false,
33+
"underline": false
34+
},
35+
"href": null,
36+
"plain_text": "Tasks",
37+
"text": {
38+
"content": "Tasks",
39+
"link": null
40+
},
41+
"type": "text"
42+
}
43+
]
44+
},
45+
{
46+
"archived": false,
47+
"created_time": "2021-04-23T04:21:00.000Z",
48+
"id": "4f555b50-3a9b-49cb-924c-3746f4ca5522",
49+
"last_edited_time": "2021-04-23T04:21:00.000Z",
50+
"object": "page",
51+
"parent": {
52+
"database_id": "e6c6f8ff-c70e-4970-91ba-98f03e0d7fc6",
53+
"type": "database_id"
54+
},
55+
"properties": {
56+
"Name": {
57+
"id": "title",
58+
"title": [
59+
{
60+
"annotations": {
61+
"bold": false,
62+
"code": false,
63+
"color": "default",
64+
"italic": false,
65+
"strikethrough": false,
66+
"underline": false
67+
},
68+
"href": null,
69+
"plain_text": "Task 1",
70+
"text": {
71+
"content": "Task1 1",
72+
"link": null
73+
},
74+
"type": "text"
75+
}
76+
],
77+
"type": "title"
78+
}
79+
}
80+
}
81+
]
82+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"has_more": false,
3+
"next_cursor": null,
4+
"object": "list",
5+
"results": [
6+
{
7+
"created_time": "2021-04-22T22:23:26.080Z",
8+
"id": "e6c6f8ff-c70e-4970-91ba-98f03e0d7fc6",
9+
"last_edited_time": "2021-04-23T04:21:00.000Z",
10+
"object": "database",
11+
"properties": {
12+
"Name": {
13+
"id": "title",
14+
"title": {},
15+
"type": "title"
16+
},
17+
"Task Type": {
18+
"id": "vd@l",
19+
"multi_select": {
20+
"options": []
21+
},
22+
"type": "multi_select"
23+
}
24+
},
25+
"title": [
26+
{
27+
"annotations": {
28+
"bold": false,
29+
"code": false,
30+
"color": "default",
31+
"italic": false,
32+
"strikethrough": false,
33+
"underline": false
34+
},
35+
"href": null,
36+
"plain_text": "Tasks",
37+
"text": {
38+
"content": "Tasks",
39+
"link": null
40+
},
41+
"type": "text"
42+
}
43+
]
44+
}
45+
]
46+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"has_more": false,
3+
"next_cursor": null,
4+
"object": "list",
5+
"results": [
6+
{
7+
"archived": false,
8+
"created_time": "2021-04-23T04:21:00.000Z",
9+
"id": "4f555b50-3a9b-49cb-924c-3746f4ca5522",
10+
"last_edited_time": "2021-04-23T04:21:00.000Z",
11+
"object": "page",
12+
"parent": {
13+
"database_id": "e6c6f8ff-c70e-4970-91ba-98f03e0d7fc6",
14+
"type": "database_id"
15+
},
16+
"properties": {
17+
"Name": {
18+
"id": "title",
19+
"title": [
20+
{
21+
"annotations": {
22+
"bold": false,
23+
"code": false,
24+
"color": "default",
25+
"italic": false,
26+
"strikethrough": false,
27+
"underline": false
28+
},
29+
"href": null,
30+
"plain_text": "Task 1",
31+
"text": {
32+
"content": "Task1 1",
33+
"link": null
34+
},
35+
"type": "text"
36+
}
37+
],
38+
"type": "title"
39+
}
40+
}
41+
}
42+
]
43+
}

0 commit comments

Comments
 (0)