Skip to content

Commit c3455ea

Browse files
committed
tests for specific page request
1 parent b72001b commit c3455ea

File tree

11 files changed

+272
-28
lines changed

11 files changed

+272
-28
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
2-
vendor
2+
vendor
3+
.phpunit.result.cache

src/Endpoints/Databases.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,9 @@ private function collect(): DatabaseCollection
6565
*/
6666
public function find(string $databaseId): Database
6767
{
68-
$response = $this->get(
69-
$this->url(Endpoint::DATABASES . "/{$databaseId}")
70-
);
68+
$result = $this
69+
->getJson($this->url(Endpoint::DATABASES . "/{$databaseId}"));
7170

72-
if (!$response->ok())
73-
throw HandlingException::instance("Database not found.", ["databaseId" => $databaseId]);
74-
75-
return new Database($response->json());
71+
return new Database($result);
7672
}
7773
}

src/Endpoints/Endpoint.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

5-
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
6-
use FiveamCode\LaravelNotionApi\Query\StartCursor;
7-
use Illuminate\Support\Collection;
5+
use Illuminate\Http\Client\Response;
86
use FiveamCode\LaravelNotionApi\Notion;
7+
use GuzzleHttp\Promise\PromiseInterface;
8+
use FiveamCode\LaravelNotionApi\Query\StartCursor;
9+
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
910
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
11+
use FiveamCode\LaravelNotionApi\Exceptions\LaravelNotionAPIException;
1012

1113
class Endpoint
1214
{
@@ -22,8 +24,14 @@ class Endpoint
2224
protected ?StartCursor $startCursor = null;
2325
protected int $pageSize = 100;
2426

25-
protected ?\Illuminate\Http\Client\Response $response = null;
27+
protected ?Response $response = null;
2628

29+
/**
30+
* Endpoint constructor.
31+
* @param Notion $notion
32+
* @throws HandlingException
33+
* @throws LaravelNotionAPIException
34+
*/
2735
public function __construct(Notion $notion)
2836
{
2937
$this->notion = $notion;
@@ -49,6 +57,7 @@ protected function url(string $endpoint): string
4957
*
5058
* @param string $url
5159
* @return array
60+
* @throws NotionException|HandlingException
5261
*/
5362
protected function getJson(string $url): array
5463
{
@@ -59,7 +68,9 @@ protected function getJson(string $url): array
5968
}
6069

6170
/**
62-
*
71+
* @param string $url
72+
* @throws HandlingException
73+
* @throws NotionException
6374
*/
6475
protected function get(string $url)
6576
{
@@ -72,14 +83,19 @@ protected function get(string $url)
7283
}
7384

7485
/**
75-
*
86+
* @param string $url
87+
* @param array $body
88+
* @return PromiseInterface|Response
7689
*/
7790
protected function post(string $url, array $body)
7891
{
7992
return $this->notion->getConnection()->post($url, $body);
8093
}
8194

8295

96+
/**
97+
* @return string
98+
*/
8399
protected function buildPaginationQuery(): string
84100
{
85101
$paginationQuery = "";
@@ -93,20 +109,27 @@ protected function buildPaginationQuery(): string
93109
return $paginationQuery;
94110
}
95111

112+
/**
113+
* @param int $limit
114+
* @return $this
115+
*/
96116
public function limit(int $limit): Endpoint
97117
{
98118
$this->pageSize = min($limit, 100);
99119

100120
return $this;
101121
}
102122

123+
/**
124+
* @param StartCursor $startCursor
125+
* @return Endpoint
126+
* @throws HandlingException
127+
* @throws LaravelNotionAPIException
128+
*/
103129
public function offset(StartCursor $startCursor): Endpoint
104130
{
105131
// toDo
106132
throw HandlingException::instance("Not implemented yet.");
107-
108-
$this->startCursor = $startCursor;
109-
return $this;
110133
}
111134

112135
}

src/Entities/Database.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class Database extends Entity
2121
protected function setResponseData(array $responseData): void
2222
{
2323
parent::setResponseData($responseData);
24-
if ($responseData['object'] !== 'database') throw HandlingException::instance("invalid json-array: the given object is not a database");
24+
if ($responseData['object'] !== 'database')
25+
throw HandlingException::instance("invalid json-array: the given object is not a database");
2526
$this->fillFromRaw();
2627
}
2728

src/Entities/Entity.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace FiveamCode\LaravelNotionApi\Entities;
44

55
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
6+
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
67
use FiveamCode\LaravelNotionApi\Notion;
78
use Illuminate\Support\Arr;
89
use Carbon\Carbon;
@@ -20,7 +21,20 @@ public function __construct(array $responseData = null)
2021

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

2640
$this->responseData = $responseData;

src/Exceptions/HandlingException.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@
55
class HandlingException extends LaravelNotionAPIException
66
{
77

8+
public static function instance(string $message, array $payload = []): HandlingException
9+
{
10+
$e = new HandlingException($message);
11+
$e->payload = $payload;
812

13+
return $e;
14+
}
915
}

src/Exceptions/LaravelNotionAPIException.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ abstract class LaravelNotionAPIException extends \Exception
1717
*
1818
* @var array
1919
*/
20-
private array $payload = [];
20+
protected array $payload = [];
2121

2222
/**
2323
* Handy method to create a *Exception with payload.
@@ -26,13 +26,7 @@ abstract class LaravelNotionAPIException extends \Exception
2626
* @param array $payload
2727
* @return HandlingException
2828
*/
29-
public static function instance(string $message, array $payload = []): LaravelNotionAPIException
30-
{
31-
$e = new HandlingException($message);
32-
$e->payload = $payload;
33-
34-
return $e;
35-
}
29+
public abstract static function instance(string $message, array $payload = []): LaravelNotionAPIException;
3630

3731

3832
/**

src/Exceptions/NotionException.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
class NotionException extends LaravelNotionAPIException
88
{
99

10+
public static function instance(string $message, array $payload = []): NotionException
11+
{
12+
$e = new NotionException($message);
13+
$e->payload = $payload;
14+
15+
return $e;
16+
}
1017
/**
1118
* Handy method to create a NotionException
1219
* from a failed request.
@@ -18,7 +25,7 @@ class NotionException extends LaravelNotionAPIException
1825
public static function fromResponse(Response $response): NotionException
1926
{
2027
$e = new NotionException(
21-
"{$response->getStatusCode()} {$response->getReasonPhrase()}", 0,
28+
$response->getReasonPhrase(), 0,
2229
$response->toException()
2330
);
2431

tests/EndpointDatabaseTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace FiveamCode\LaravelNotionApi\Tests;
44

5+
use Carbon\Carbon;
6+
use FiveamCode\LaravelNotionApi\Entities\Database;
57
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
68
use FiveamCode\LaravelNotionApi\Notion;
79
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
@@ -83,9 +85,62 @@ public function it_throws_a_notion_exception_bad_request()
8385

8486

8587
$this->expectException(NotionException::class);
88+
$this->expectExceptionMessage("Bad Request");
8689

8790
$result = $notion->databases()->all();
8891
}
8992

93+
/** @test */
94+
public function it_returns_database_entity_with_filled_properties()
95+
{
96+
// successful /v1/databases/DATABASE_DOES_EXIST
97+
Http::fake([
98+
'https://api.notion.com/v1/databases/668d797c-76fa-4934-9b05-ad288df2d136'
99+
=> Http::response(
100+
json_decode(file_get_contents('tests/stubs/endpoints/databases/response_specific_200.json'), true),
101+
200,
102+
['Headers']
103+
)
104+
]);
105+
106+
$notion = new Notion();
107+
$notion->v1()->setToken("secret_*");
108+
109+
$databaseResult = $notion->databases()->find("668d797c-76fa-4934-9b05-ad288df2d136");
110+
111+
$this->assertInstanceOf(Database::class, $databaseResult);
112+
113+
// check properties
114+
$this->assertSame("Grocery List", $databaseResult->getTitle());
115+
$this->assertSame("database", $databaseResult->getObjectType());
116+
117+
$this->assertCount(1, $databaseResult->getRawTitle());
118+
$this->assertCount(12, $databaseResult->getRawProperties());
119+
120+
$this->assertInstanceOf(Carbon::class, $databaseResult->getCreatedTime());
121+
$this->assertInstanceOf(Carbon::class, $databaseResult->getLastEditedTime());
122+
}
123+
124+
/** @test */
125+
public function it_throws_a_notion_exception_not_found()
126+
{
127+
// failing /v1/databases/DATABASE_DOES_NOT_EXIST
128+
Http::fake([
129+
'https://api.notion.com/v1/databases/b55c9c91-384d-452b-81db-d1ef79372b79'
130+
=> Http::response(
131+
json_decode(file_get_contents('tests/stubs/endpoints/databases/response_specific_404.json'), true),
132+
200,
133+
['Headers']
134+
)
135+
]);
136+
137+
$notion = new Notion();
138+
$notion->v1()->setToken("secret_*");
139+
140+
$this->expectException(NotionException::class);
141+
$this->expectExceptionMessage("Not found");
142+
$databaseResult = $notion->databases()->find("b55c9c91-384d-452b-81db-d1ef79372b79");
143+
144+
}
90145

91146
}

0 commit comments

Comments
 (0)