22
33namespace FiveamCode \LaravelNotionApi \Tests ;
44
5- use Carbon \Carbon ;
6- use FiveamCode \LaravelNotionApi \Entities \Page ;
7- use FiveamCode \LaravelNotionApi \Exceptions \NotionException ;
85use Illuminate \Support \Facades \Http ;
6+ use FiveamCode \LaravelNotionApi \Entities \Blocks \Block ;
7+ use FiveamCode \LaravelNotionApi \Exceptions \NotionException ;
8+ use FiveamCode \LaravelNotionApi \Entities \Collections \BlockCollection ;
99
1010/**
11- * Class EndpointPageTest
11+ * Class EndpointBlocksTest
1212 *
1313 * The fake API responses are based on our test environment (since the current Notion examples do not match with the actual calls).
14- * @see https://developers.notion.com/reference/get-page
14+ * @see https://developers.notion.com/reference/get-block-children
1515 *
1616 * @package FiveamCode\LaravelNotionApi\Tests
1717 */
@@ -21,9 +21,9 @@ class EndpointBlocksTest extends NotionApiTest
2121 /** @test */
2222 public function it_throws_a_notion_exception_bad_request ()
2323 {
24- // failing /v1/databases
24+ // failing /v1/blocks
2525 Http::fake ([
26- 'https://api.notion.com/v1/pages * '
26+ 'https://api.notion.com/v1/blocks/b55c9c91-384d-452b-81db-d1ef79372b76/children * '
2727 => Http::response (
2828 json_decode ('{} ' , true ),
2929 400 ,
@@ -34,44 +34,49 @@ public function it_throws_a_notion_exception_bad_request()
3434 $ this ->expectException (NotionException::class);
3535 $ this ->expectExceptionMessage ("Bad Request " );
3636
37- \Notion::pages ()-> find ( " afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8 " );
37+ \Notion::block ( " b55c9c91-384d-452b-81db-d1ef79372b76 " )-> children ( );
3838 }
3939
4040 /** @test */
41- public function it_returns_page_entity_with_filled_properties ()
41+ public function it_returns_block_collection_with_children ()
4242 {
43- // successful /v1/pages/PAGE_DOES_EXIST
43+ // successful /v1/blocks/BLOCK_DOES_EXIST/children
4444 Http::fake ([
45- 'https://api.notion.com/v1/pages/afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8 '
45+ 'https://api.notion.com/v1/blocks/b55c9c91-384d-452b-81db-d1ef79372b76/children* '
4646 => Http::response (
47- json_decode (file_get_contents ('tests/stubs/endpoints/pages /response_specific_200.json ' ), true ),
47+ json_decode (file_get_contents ('tests/stubs/endpoints/blocks /response_specific_200.json ' ), true ),
4848 200 ,
4949 ['Headers ' ]
5050 )
5151 ]);
5252
53- $ pageResult = \Notion::pages ()->find ("afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8 " );
54-
55- $ this ->assertInstanceOf (Page::class, $ pageResult );
56-
57- // check properties
58- $ this ->assertSame ("Notion Is Awesome " , $ pageResult ->getTitle ());
59- $ this ->assertSame ("page " , $ pageResult ->getObjectType ());
60-
61- $ this ->assertCount (6 , $ pageResult ->getRawProperties ());
62-
63- $ this ->assertInstanceOf (Carbon::class, $ pageResult ->getCreatedTime ());
64- $ this ->assertInstanceOf (Carbon::class, $ pageResult ->getLastEditedTime ());
53+ $ blockChildren = \Notion::block ("b55c9c91-384d-452b-81db-d1ef79372b76 " )->children ();
54+ $ this ->assertInstanceOf (BlockCollection::class, $ blockChildren );
55+
56+ $ blockChildrenCollection = $ blockChildren ->asCollection ();
57+ $ this ->assertContainsOnly (Block::class, $ blockChildrenCollection );
58+ $ this ->assertIsIterable ($ blockChildrenCollection );
59+ $ this ->assertCount (3 , $ blockChildrenCollection );
60+
61+ // check first child
62+ $ blockChild = $ blockChildrenCollection ->first ();
63+ $ this ->assertInstanceOf (Block::class, $ blockChild );
64+ $ this ->assertEquals ("heading_2 " , $ blockChild ->getType ());
65+ $ this ->assertFalse ($ blockChild ->hasChildren ());
66+ $ this ->assertArrayHasKey ("text " , $ blockChild ->getRawContent ());
67+ $ this ->assertArrayHasKey (0 , $ blockChild ->getRawContent ()["text " ]);
68+ $ this ->assertArrayHasKey ("plain_text " , $ blockChild ->getRawContent ()["text " ][0 ]);
69+ $ this ->assertEquals ("Lacinato kale " , $ blockChild ->getRawContent ()["text " ][0 ]["plain_text " ]);
6570 }
6671
6772 /** @test */
6873 public function it_throws_a_notion_exception_not_found ()
6974 {
70- // failing /v1/pages/PAGE_DOES_NOT_EXIST
75+ // failing /v1/blocks/BLOCK_DOES_NOT_EXIST/children
7176 Http::fake ([
72- 'https://api.notion.com/v1/pages /b55c9c91-384d-452b-81db-d1ef79372b79 '
77+ 'https://api.notion.com/v1/blocks /b55c9c91-384d-452b-81db-d1ef79372b11* '
7378 => Http::response (
74- json_decode (file_get_contents ('tests/stubs/endpoints/pages /response_specific_404.json ' ), true ),
79+ json_decode (file_get_contents ('tests/stubs/endpoints/blocks /response_specific_404.json ' ), true ),
7580 200 ,
7681 ['Headers ' ]
7782 )
@@ -80,7 +85,7 @@ public function it_throws_a_notion_exception_not_found()
8085 $ this ->expectException (NotionException::class);
8186 $ this ->expectExceptionMessage ("Not found " );
8287
83- \Notion::pages ()-> find ( "b55c9c91-384d-452b-81db-d1ef79372b79 " );
88+ \Notion::block ( "b55c9c91-384d-452b-81db-d1ef79372b11 " )-> children ( );
8489 }
8590
8691}
0 commit comments