Skip to content

Commit c0c4b7f

Browse files
committed
added user test
1 parent b1c320d commit c0c4b7f

File tree

9 files changed

+186
-7
lines changed

9 files changed

+186
-7
lines changed

phpunit.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheResultFile=".phpunit.cache/test-results"
6+
executionOrder="depends,defects"
7+
forceCoversAnnotation="false"
8+
beStrictAboutCoversAnnotation="true"
9+
beStrictAboutOutputDuringTests="true"
10+
beStrictAboutTodoAnnotatedTests="true"
11+
failOnRisky="true"
12+
failOnWarning="true"
13+
verbose="true">
14+
<testsuites>
15+
<testsuite name="default">
16+
<directory suffix="Test.php">tests</directory>
17+
</testsuite>
18+
</testsuites>
19+
20+
<coverage cacheDirectory=".phpunit.cache/code-coverage"
21+
processUncoveredFiles="true">
22+
<include>
23+
<directory suffix=".php">src</directory>
24+
</include>
25+
</coverage>
26+
</phpunit>

src/Endpoints/Users.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ class Users extends Endpoint implements EndpointInterface
1919
*/
2020
public function all(): UserCollection
2121
{
22-
$result = $this->get(
23-
$this->url(Endpoint::USERS . "?{$this->buildPaginationQuery()}")
24-
);
22+
$resultData = $this->getJson($this->url(Endpoint::USERS) . "?{$this->buildPaginationQuery()}");
2523

26-
return new UserCollection($result->json());
24+
return new UserCollection($resultData);
2725
}
2826

2927
/**

src/Entities/Collections/EntityCollection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ protected function setResponseData(array $responseData): void
3030
// so we have to check here on the given status code in the paylaod,
3131
// if the object was not found.
3232
if (
33-
$responseData['object'] === 'error'
33+
array_key_exists('object', $responseData)
34+
&& $responseData['object'] === 'error'
3435
&& Arr::exists($responseData, 'status') && $responseData['status'] === 404
3536
) {
3637
throw NotionException::instance("Not found", compact("responseData"));

tests/EndpointBlocksTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* Class EndpointBlocksTest
1212
*
13-
* The fake API responses are based on our test environment (since the current Notion examples do not match with the actual calls).
13+
* The fake API responses are based on Notions documentation.
1414
* @see https://developers.notion.com/reference/get-block-children
1515
*
1616
* @package FiveamCode\LaravelNotionApi\Tests

tests/EndpointDatabaseTest.php renamed to tests/EndpointDatabasesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @package FiveamCode\LaravelNotionApi\Tests
1717
*/
18-
class EndpointDatabaseTest extends NotionApiTest
18+
class EndpointDatabasesTest extends NotionApiTest
1919
{
2020

2121
/** @test */

tests/EndpointUsersTest.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Tests;
4+
5+
use Carbon\Carbon;
6+
use FiveamCode\LaravelNotionApi\Entities\Blocks\Block;
7+
use FiveamCode\LaravelNotionApi\Entities\Collections\BlockCollection;
8+
use FiveamCode\LaravelNotionApi\Entities\Collections\UserCollection;
9+
use FiveamCode\LaravelNotionApi\Entities\Page;
10+
use FiveamCode\LaravelNotionApi\Entities\User;
11+
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
12+
use Illuminate\Support\Facades\Http;
13+
use Orchestra\Testbench\TestCase;
14+
15+
/**
16+
* Class EndpointUsersTest
17+
*
18+
* The fake API responses are based on Notions documentation.
19+
* @see https://developers.notion.com/reference/get-users
20+
*
21+
* @package FiveamCode\LaravelNotionApi\Tests
22+
*/
23+
class EndpointUsersTest extends NotionApiTest
24+
{
25+
26+
/** @test */
27+
public function it_throws_a_notion_exception_bad_request()
28+
{
29+
// failing /v1
30+
Http::fake([
31+
'https://api.notion.com/v1/users?*'
32+
=> Http::response(
33+
json_decode('{}', true),
34+
400,
35+
['Headers']
36+
)
37+
]);
38+
39+
$this->expectException(NotionException::class);
40+
$this->expectExceptionMessage("Bad Request");
41+
42+
\Notion::users()->all();
43+
}
44+
45+
/** @test */
46+
public function it_returns_all_users_of_workspace_as_collection_with_entity_objects()
47+
{
48+
// successful /v1/users
49+
Http::fake([
50+
'https://api.notion.com/v1/users?*'
51+
=> Http::response(
52+
json_decode(file_get_contents('tests/stubs/endpoints/users/response_all_200.json'), true),
53+
200,
54+
['Headers']
55+
)
56+
]);
57+
58+
$users = \Notion::users()->all();
59+
60+
$this->assertInstanceOf(UserCollection::class, $users);
61+
62+
$userCollection = $users->asCollection();
63+
$this->assertContainsOnly(User::class, $userCollection);
64+
$this->assertIsIterable($userCollection);
65+
$this->assertCount(2, $userCollection);
66+
67+
$user = $userCollection->first();
68+
69+
$this->assertInstanceOf(User::class, $user);
70+
$this->assertEquals("Avocado Lovelace", $user->getName());
71+
$this->assertEquals("https://secure.notion-static.com/e6a352a8-8381-44d0-a1dc-9ed80e62b53d.jpg", $user->getAvatarUrl());
72+
}
73+
74+
/** @test */
75+
public function it_returns_a_specific_user_as_entity_object()
76+
{
77+
// successful /v1/users/USER_DOES_EXITS
78+
Http::fake([
79+
'https://api.notion.com/v1/users/d40e767c-d7af-4b18-a86d-55c61f1e39a4'
80+
=> Http::response(
81+
json_decode(file_get_contents('tests/stubs/endpoints/users/response_specific_200.json'), true),
82+
200,
83+
['Headers']
84+
)
85+
]);
86+
87+
$user = \Notion::users()->find("d40e767c-d7af-4b18-a86d-55c61f1e39a4");
88+
89+
$this->assertInstanceOf(User::class, $user);
90+
$this->assertEquals("Avocado Lovelace", $user->getName());
91+
$this->assertEquals("https://secure.notion-static.com/e6a352a8-8381-44d0-a1dc-9ed80e62b53d.jpg", $user->getAvatarUrl());
92+
}
93+
94+
/** @test */
95+
public function it_throws_a_notion_exception_not_found()
96+
{
97+
// failing /v1/pages/PAGE_DOES_NOT_EXIST
98+
Http::fake([
99+
'https://api.notion.com/v1/users/d40e767c-d7af-4b18-a86d-55c61f1e39a1'
100+
=> Http::response(
101+
json_decode(file_get_contents('tests/stubs/endpoints/users/response_specific_404.json'), true),
102+
200,
103+
['Headers']
104+
)
105+
]);
106+
107+
$this->expectException(NotionException::class);
108+
$this->expectExceptionMessage("Not found");
109+
110+
\Notion::users()->find("d40e767c-d7af-4b18-a86d-55c61f1e39a1");
111+
}
112+
113+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"object": "list",
3+
"results": [
4+
{
5+
"object": "user",
6+
"id": "d40e767c-d7af-4b18-a86d-55c61f1e39a4",
7+
"type": "person",
8+
"person": {
9+
"email": "avo@example.org"
10+
},
11+
"name": "Avocado Lovelace",
12+
"avatar_url": "https://secure.notion-static.com/e6a352a8-8381-44d0-a1dc-9ed80e62b53d.jpg"
13+
},
14+
{
15+
"object": "user",
16+
"id": "9a3b5ae0-c6e6-482d-b0e1-ed315ee6dc57",
17+
"type": "bot",
18+
"bot": {},
19+
"name": "Doug Engelbot",
20+
"avatar_url": "https://secure.notion-static.com/6720d746-3402-4171-8ebb-28d15144923c.jpg"
21+
}
22+
],
23+
"next_cursor": "fe2cc560-036c-44cd-90e8-294d5a74cebc",
24+
"has_more": true
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"object": "user",
3+
"id": "d40e767c-d7af-4b18-a86d-55c61f1e39a4",
4+
"type": "person",
5+
"person": {
6+
"email": "avo@example.org"
7+
},
8+
"name": "Avocado Lovelace",
9+
"avatar_url": "https://secure.notion-static.com/e6a352a8-8381-44d0-a1dc-9ed80e62b53d.jpg"
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"object": "error",
3+
"status": 404,
4+
"code": "object_not_found",
5+
"message": "Could not find user with ID: b55c9c91-384d-452b-81db-d1ef79372b79."
6+
}

0 commit comments

Comments
 (0)