Skip to content

Commit 939c248

Browse files
committed
added tests for property assembling
1 parent 968d5b7 commit 939c248

File tree

1 file changed

+174
-35
lines changed

1 file changed

+174
-35
lines changed

tests/EndpointPagesTest.php

Lines changed: 174 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,27 @@
22

33
namespace FiveamCode\LaravelNotionApi\Tests;
44

5-
use FiveamCode\LaravelNotionApi\Entities\Properties\Checkbox;
6-
use FiveamCode\LaravelNotionApi\Entities\Properties\Date;
7-
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichDate;
85
use Notion;
96
use Carbon\Carbon;
107
use Illuminate\Support\Facades\Http;
118
use FiveamCode\LaravelNotionApi\Entities\Page;
9+
use FiveamCode\LaravelNotionApi\Entities\User;
10+
use FiveamCode\LaravelNotionApi\Entities\Properties\Url;
11+
use FiveamCode\LaravelNotionApi\Entities\Properties\Text;
12+
use FiveamCode\LaravelNotionApi\Entities\Properties\Date;
13+
use FiveamCode\LaravelNotionApi\Entities\Properties\Email;
14+
use FiveamCode\LaravelNotionApi\Entities\Properties\Number;
1215
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
16+
use FiveamCode\LaravelNotionApi\Entities\Properties\People;
17+
use FiveamCode\LaravelNotionApi\Entities\Properties\Select;
18+
use FiveamCode\LaravelNotionApi\Entities\Properties\Relation;
19+
use FiveamCode\LaravelNotionApi\Entities\Properties\Checkbox;
1320
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
21+
use FiveamCode\LaravelNotionApi\Entities\Properties\MultiSelect;
22+
use FiveamCode\LaravelNotionApi\Entities\Properties\PhoneNumber;
23+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichDate;
24+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
25+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\SelectItem;
1426

1527
/**
1628
* Class EndpointPageTest
@@ -91,8 +103,10 @@ public function it_throws_a_notion_exception_not_found()
91103
}
92104

93105
/** @test */
94-
public function it_assembles_properties_for_a_new_page() {
106+
public function it_assembles_properties_for_a_new_page()
107+
{
95108

109+
# test values
96110
$pageId = "0349b883a1c64539b435289ea62b6eab";
97111
$pageTitle = "I was updated from Tinkerwell";
98112

@@ -103,16 +117,48 @@ public function it_assembles_properties_for_a_new_page() {
103117
$dateRangeEndValue = Carbon::tomorrow()->toDateTime();
104118
$dateKey = "DateProperty";
105119
$dateValue = Carbon::yesterday()->toDateTime();
106-
120+
$emailKey = "EmailProperty";
121+
$emailValue = "notion-is-awesome@example.org";
122+
$multiSelectKey = "MultiSelectProperty";
123+
$multiSelectValues = ["Laravel", "Notion"];
124+
$numberKey = "NumberProperty";
125+
$numberValue = 42.42;
126+
$peopleKey = "PeopleProperty";
127+
$peopleValue = ["04536682-603a-4531-a18f-4fa89fdfb4a8", "2fc9a200-e932-4428-baab-ba14526a139b"];
128+
$phoneKey = "PhoneKey";
129+
$phoneValue = "999-888-777-666";
130+
$relationKey = "RelationProperty";
131+
$relationValue = ["dc4ce5b0-0f51-4f6e-b130-5ac7a1b5101d"];
132+
$selectKey = "SelectProperty";
133+
$selectValue = "I choose you, Pikachu";
134+
$textKey = "TextProperty";
135+
$textValue = "Isn't this awesome?";
136+
$urlKey = "UrlProperty";
137+
$urlValue = "https://5amco.de";
138+
139+
140+
# build the page with properties
107141
$page = new Page();
108142
$page->setId($pageId);
109143
$page->setTitle("Name", $pageTitle);
110144
$page->setCheckbox($checkboxKey, $checkboxValue);
111145
$page->setDate($dateRangeKey, $dateRangeStartValue, $dateRangeEndValue);
112146
$page->setDate($dateKey, $dateValue);
113-
147+
$page->setEmail($emailKey, $emailValue);
148+
$page->setMultiSelect($multiSelectKey, $multiSelectValues);
149+
$page->setNumber($numberKey, $numberValue);
150+
$page->setPeople($peopleKey, $peopleValue);
151+
$page->setPhoneNumber($phoneKey, $phoneValue);
152+
$page->setRelation($relationKey, $relationValue);
153+
$page->setSelect($selectKey, $selectValue);
154+
$page->setText($textKey, $textValue);
155+
$page->setUrl($urlKey, $urlValue);
156+
157+
158+
# read the set properties
114159
$properties = $page->getProperties();
115160

161+
# id, title
116162
$this->assertEquals($page->getId(), $pageId);
117163
$this->assertEquals($page->getTitle(), $pageTitle);
118164

@@ -126,52 +172,145 @@ public function it_assembles_properties_for_a_new_page() {
126172
$this->assertArrayHasKey("checkbox", $checkboxContent);
127173
$this->assertEquals($checkboxContent["checkbox"], $checkboxValue);
128174
$this->assertEquals($checkboxProp->getContent(), $checkboxValue);
129-
$this->assertEquals($checkboxProp->asText(), $checkboxValue? "true" : "false");
175+
$this->assertEquals($checkboxProp->asText(), $checkboxValue ? "true" : "false");
130176

131177
# date range
132178
$this->assertTrue(
133179
$this->assertContainsInstanceOf(Date::class, $properties)
134180
);
135181
$dateRangeProp = $page->getProperty($dateRangeKey);
136-
$this->assertEquals($dateRangeKey, $dateRangeProp->getTitle());
137182
$this->assertInstanceOf(RichDate::class, $dateRangeProp->getContent());
138183
$dateRangeContent = $dateRangeProp->getContent();
139184
$this->assertTrue($dateRangeProp->isRange());
140185
$this->assertEquals($dateRangeStartValue, $dateRangeProp->getStart());
141186
$this->assertEquals($dateRangeEndValue, $dateRangeProp->getEnd());
187+
$this->assertJson($dateRangeProp->asText());
188+
$this->assertStringContainsString($dateRangeStartValue->format("Y-m-d H:i:s"), $dateRangeProp->asText());
189+
$this->assertStringContainsString($dateRangeEndValue->format("Y-m-d H:i:s"), $dateRangeProp->asText());
190+
$dateRangeContent = $dateRangeProp->getRawContent();
191+
$this->assertArrayHasKey("date", $dateRangeContent);
192+
$this->assertCount(2, $dateRangeContent["date"]);
193+
$this->assertArrayHasKey("start", $dateRangeContent["date"]);
194+
$this->assertEquals($dateRangeStartValue->format("c"), $dateRangeContent["date"]["start"]);
195+
$this->assertArrayHasKey("end", $dateRangeContent["date"]);
196+
$this->assertEquals($dateRangeEndValue->format("c"), $dateRangeContent["date"]["end"]);
142197

143198
# date
144199
$dateProp = $page->getProperty($dateKey);
145-
$this->assertEquals($dateKey, $dateProp->getTitle());
146200
$this->assertInstanceOf(RichDate::class, $dateProp->getContent());
147-
$dateContent = $dateProp->getContent();
148201
$this->assertFalse($dateProp->isRange());
149202
$this->assertEquals($dateValue, $dateProp->getStart());
150-
151-
152-
153-
203+
$dateContent = $dateProp->getRawContent();
204+
$this->assertArrayHasKey("date", $dateContent);
205+
$this->assertCount(1, $dateContent["date"]);
206+
$this->assertArrayHasKey("start", $dateContent["date"]);
207+
$this->assertEquals($dateValue->format("c"), $dateContent["date"]["start"]);
208+
209+
# email
210+
$this->assertTrue($this->assertContainsInstanceOf(Email::class, $properties));
211+
$mailProp = $page->getProperty($emailKey);
212+
$this->assertInstanceOf(Email::class, $mailProp);
213+
$this->assertEquals($emailValue, $mailProp->getContent());
214+
$this->assertEquals($emailValue, $mailProp->getEmail());
215+
$mailContent = $mailProp->getRawContent();
216+
$this->assertArrayHasKey("email", $mailContent);
217+
$this->assertEquals($mailContent["email"], $emailValue);
218+
219+
# multi-select
220+
$this->assertTrue($this->assertContainsInstanceOf(MultiSelect::class, $properties));
221+
$multiSelectProp = $page->getProperty($multiSelectKey);
222+
$this->assertIsIterable($multiSelectProp->getContent());
223+
$this->assertContainsOnlyInstancesOf(SelectItem::class, $multiSelectProp->getContent());
224+
$this->assertEquals("Laravel", $multiSelectProp->getContent()->first()->getName());
225+
226+
$multiSelectContent = $multiSelectProp->getRawContent();
227+
$this->assertArrayHasKey("multi_select", $multiSelectContent);
228+
$this->assertCount(2, $multiSelectContent["multi_select"]);
229+
$this->assertIsIterable($multiSelectContent["multi_select"][0]);
230+
$this->assertArrayHasKey("name", $multiSelectContent["multi_select"][0]);
231+
$this->assertEquals("Laravel", $multiSelectContent["multi_select"][0]["name"]);
232+
$this->assertIsIterable($multiSelectContent["multi_select"][1]);
233+
$this->assertArrayHasKey("name", $multiSelectContent["multi_select"][1]);
234+
$this->assertEquals("Notion", $multiSelectContent["multi_select"][1]["name"]);
235+
236+
# number
237+
$this->assertTrue($this->assertContainsInstanceOf(Number::class, $properties));
238+
$numberProp = $page->getProperty($numberKey);
239+
$this->assertEquals($numberValue, $numberProp->getContent());
240+
$this->assertEquals($numberValue, $numberProp->getNumber());
241+
$numberContent = $numberProp->getRawContent();
242+
$this->assertArrayHasKey("number", $numberContent);
243+
$this->assertEquals($numberContent["number"], $numberValue);
244+
245+
# people
246+
$this->assertTrue($this->assertContainsInstanceOf(People::class, $properties));
247+
$peopleProp = $page->getProperty($peopleKey);
248+
$this->assertEquals($peopleProp->getContent(), $peopleProp->getPeople());
249+
$this->assertCount(2, $peopleProp->getPeople());
250+
$this->assertContainsOnlyInstancesOf(User::class, $peopleProp->getPeople());
251+
$this->assertEquals($peopleValue[0], $peopleProp->getPeople()->first()->getId());
252+
$peopleContent = $peopleProp->getRawContent();
253+
$this->assertArrayHasKey("people", $peopleContent);
254+
$this->assertArrayHasKey("object", $peopleContent["people"][0]);
255+
$this->assertArrayHasKey("id", $peopleContent["people"][0]);
256+
$this->assertEquals($peopleContent["people"][0]["object"], "user");
257+
$this->assertEquals($peopleContent["people"][0]["id"], $peopleValue[0]);
258+
$this->assertArrayHasKey("object", $peopleContent["people"][1]);
259+
$this->assertArrayHasKey("id", $peopleContent["people"][1]);
260+
$this->assertEquals("user", $peopleContent["people"][1]["object"]);
261+
$this->assertEquals($peopleValue[1], $peopleContent["people"][1]["id"]);
262+
263+
# phone number
264+
$this->assertTrue($this->assertContainsInstanceOf(PhoneNumber::class, $properties));
265+
$phoneProp = $page->getProperty($phoneKey);
266+
$this->assertEquals($phoneValue, $phoneProp->getPhoneNumber());
267+
$this->assertEquals($phoneProp->getContent(), $phoneProp->getPhoneNumber());
268+
$phoneContent = $phoneProp->getRawContent();
269+
$this->assertArrayHasKey("phone_number", $phoneContent);
270+
$this->assertEquals($phoneContent["phone_number"], $phoneValue);
271+
272+
# relation
273+
$this->assertTrue($this->assertContainsInstanceOf(Relation::class, $properties));
274+
$relationProp = $page->getProperty($relationKey);
275+
$this->assertEquals($relationValue[0], $relationProp->getRelation()->first());
276+
$this->assertEquals($relationProp->getContent(), $relationProp->getRelation());
277+
$relationContent = $relationProp->getRawContent();
278+
$this->assertArrayHasKey("relation", $relationContent);
279+
$this->assertArrayHasKey("id", $relationContent["relation"][0]);
280+
$this->assertEquals($relationValue[0], $relationContent["relation"][0]["id"]);
281+
282+
# select
283+
$this->assertTrue($this->assertContainsInstanceOf(Select::class, $properties));
284+
$selectProp = $page->getProperty($selectKey);
285+
$this->assertInstanceOf(SelectItem::class, $selectProp->getContent());
286+
$this->assertEquals($selectValue, $selectProp->getContent()->getName());
287+
$selectContent = $selectProp->getRawContent();
288+
$this->assertArrayHasKey("select", $selectContent);
289+
$this->assertArrayHasKey("name", $selectContent["select"]);
290+
$this->assertEquals($selectValue, $selectContent["select"]["name"]);
291+
292+
# text
293+
$this->assertTrue($this->assertContainsInstanceOf(Text::class, $properties));
294+
$textProp = $page->getProperty($textKey);
295+
$this->assertInstanceOf(RichText::class, $textProp->getContent());
296+
$this->assertEquals($textValue, $textProp->getContent()->getPlainText());
297+
$textContent = $textProp->getRawContent();
298+
$this->assertArrayHasKey("rich_text", $textContent);
299+
$this->assertCount(1, $textContent["rich_text"]);
300+
$this->assertArrayHasKey("type", $textContent["rich_text"][0]);
301+
$this->assertArrayHasKey("text", $textContent["rich_text"][0]);
302+
$this->assertEquals("text", $textContent["rich_text"][0]["type"]);
303+
$this->assertArrayHasKey("content", $textContent["rich_text"][0]["text"]);
304+
$this->assertEquals($textValue, $textContent["rich_text"][0]["text"]["content"]);
305+
306+
# url
307+
$this->assertTrue($this->assertContainsInstanceOf(Url::class, $properties));
308+
$urlProp = $page->getProperty($urlKey);
309+
$this->assertEquals($urlValue, $urlProp->getUrl());
310+
$this->assertEquals($urlProp->getContent(), $urlProp->getUrl());
311+
$urlContent = $urlProp->getRawContent();
312+
$this->assertArrayHasKey("url", $urlContent);
313+
$this->assertEquals($urlValue, $urlContent["url"]);
154314
}
155315

156-
157-
// /** @test */
158-
// public function it_throws_a_handling_exception_not_implemented_for_create()
159-
// {
160-
161-
// $this->expectException(HandlingException::class);
162-
// $this->expectExceptionMessage('Not implemented');
163-
164-
// Notion::pages()->create();
165-
// }
166-
167-
// /** @test */
168-
// public function it_throws_a_handling_exception_not_implemented_for_update_properties()
169-
// {
170-
171-
// $this->expectException(HandlingException::class);
172-
// $this->expectExceptionMessage('Not implemented');
173-
174-
// Notion::pages()->updateProperties();
175-
// }
176-
177316
}

0 commit comments

Comments
 (0)