Skip to content

Commit 30c4cec

Browse files
authored
Merge pull request #33 from 5am-code/feature/retrieve-single-block
Retrieve a single block @johguentner
2 parents d329ad4 + 98eec75 commit 30c4cec

File tree

4 files changed

+150
-81
lines changed

4 files changed

+150
-81
lines changed

src/Endpoints/Block.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
77
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
88
use FiveamCode\LaravelNotionApi\Entities\Collections\BlockCollection;
9-
9+
use FiveamCode\LaravelNotionApi\Entities\Blocks\Block as BlockEntity;
1010
/**
1111
* Class Block
1212
* @package FiveamCode\LaravelNotionApi\Endpoints
@@ -31,6 +31,25 @@ public function __construct(Notion $notion, string $blockId)
3131
$this->blockId = $blockId;
3232
}
3333

34+
/**
35+
* Retrieve a block
36+
* url: https://api.notion.com/{version}/blocks/{block_id}
37+
* notion-api-docs: https://developers.notion.com/reference/retrieve-a-block
38+
*
39+
* @param string $blockId
40+
* @return BlockEntity
41+
* @throws HandlingException
42+
* @throws NotionException
43+
*/
44+
public function retrieve(): BlockEntity {
45+
46+
$response = $this->get(
47+
$this->url(Endpoint::BLOCKS . '/' . $this->blockId)
48+
);
49+
50+
return BlockEntity::fromResponse($response->json());
51+
}
52+
3453
/**
3554
* Retrieve block children
3655
* url: https://api.notion.com/{version}/blocks/{block_id}/children

tests/EndpointBlocksTest.php

Lines changed: 100 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace FiveamCode\LaravelNotionApi\Tests;
44

5+
use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText;
56
use Notion;
67
use Illuminate\Support\Facades\Http;
78
use FiveamCode\LaravelNotionApi\Entities\Blocks\Block;
@@ -54,7 +55,7 @@ public function it_returns_block_collection_with_children()
5455
Http::fake([
5556
'https://api.notion.com/v1/blocks/b55c9c91-384d-452b-81db-d1ef79372b76/children*'
5657
=> Http::response(
57-
json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_200.json'), true),
58+
json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_block_children_200.json'), true),
5859
200,
5960
['Headers']
6061
)
@@ -79,85 +80,85 @@ public function it_returns_block_collection_with_children()
7980
$this->assertEquals('Lacinato kale', $blockChild->getRawContent()['text'][0]['plain_text']);
8081
}
8182

82-
/** @test */
83-
public function it_returns_block_collection_with_children_as_correct_instances()
84-
{
85-
// successful /v1/blocks/BLOCK_DOES_EXIST/children
86-
Http::fake([
87-
'https://api.notion.com/v1/blocks/1d719dd1-563b-4387-b74f-20da92b827fb/children*'
88-
=> Http::response(
89-
json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_supported_blocks_200.json'), true),
90-
200,
91-
['Headers']
92-
)
93-
]);
94-
95-
$blockChildren = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->children();
96-
$this->assertInstanceOf(BlockCollection::class, $blockChildren);
97-
98-
# check collection
99-
$blockChildrenCollection = $blockChildren->asCollection();
100-
$this->assertContainsOnly(Block::class, $blockChildrenCollection);
101-
$this->assertIsIterable($blockChildrenCollection);
102-
$this->assertCount(8, $blockChildrenCollection);
103-
104-
# check paragraph
105-
$blockChild = $blockChildrenCollection[0];
106-
$this->assertInstanceOf(Paragraph::class, $blockChild);
107-
$this->assertEquals('paragraph', $blockChild->getType());
108-
$this->assertFalse($blockChild->hasChildren());
109-
$this->assertEquals('paragraph_block', $blockChild->getContent()->getPlainText());
110-
111-
# check heading_1
112-
$blockChild = $blockChildrenCollection[1];
113-
$this->assertInstanceOf(HeadingOne::class, $blockChild);
114-
$this->assertEquals('heading_1', $blockChild->getType());
115-
$this->assertFalse($blockChild->hasChildren());
116-
$this->assertEquals('heading_one_block', $blockChild->getContent()->getPlainText());
117-
118-
# check heading_2
119-
$blockChild = $blockChildrenCollection[2];
120-
$this->assertInstanceOf(HeadingTwo::class, $blockChild);
121-
$this->assertEquals('heading_2', $blockChild->getType());
122-
$this->assertFalse($blockChild->hasChildren());
123-
$this->assertEquals('heading_two_block', $blockChild->getContent()->getPlainText());
124-
125-
# check heading_3
126-
$blockChild = $blockChildrenCollection[3];
127-
$this->assertInstanceOf(HeadingThree::class, $blockChild);
128-
$this->assertEquals('heading_3', $blockChild->getType());
129-
$this->assertFalse($blockChild->hasChildren());
130-
$this->assertEquals('heading_three_block', $blockChild->getContent()->getPlainText());
131-
132-
# check bulleted_list_item
133-
$blockChild = $blockChildrenCollection[4];
134-
$this->assertInstanceOf(BulletedListItem::class, $blockChild);
135-
$this->assertEquals('bulleted_list_item', $blockChild->getType());
136-
$this->assertFalse($blockChild->hasChildren());
137-
$this->assertEquals('bulleted_list_item_block', $blockChild->getContent()->getPlainText());
138-
139-
# check numbered_list_item
140-
$blockChild = $blockChildrenCollection[5];
141-
$this->assertInstanceOf(NumberedListItem::class, $blockChild);
142-
$this->assertEquals('numbered_list_item', $blockChild->getType());
143-
$this->assertFalse($blockChild->hasChildren());
144-
$this->assertEquals('numbered_list_item_block', $blockChild->getContent()->getPlainText());
145-
146-
# check to_do
147-
$blockChild = $blockChildrenCollection[6];
148-
$this->assertInstanceOf(ToDo::class, $blockChild);
149-
$this->assertEquals('to_do', $blockChild->getType());
150-
$this->assertFalse($blockChild->hasChildren());
151-
$this->assertEquals('to_do_block', $blockChild->getContent()->getPlainText());
152-
153-
# check toggle
154-
$blockChild = $blockChildrenCollection[7];
155-
$this->assertInstanceOf(Toggle::class, $blockChild);
156-
$this->assertEquals('toggle', $blockChild->getType());
157-
$this->assertFalse($blockChild->hasChildren());
158-
$this->assertEquals('toggle_block', $blockChild->getContent()->getPlainText());
159-
160-
}
83+
/** @test */
84+
public function it_returns_block_collection_with_children_as_correct_instances()
85+
{
86+
// successful /v1/blocks/BLOCK_DOES_EXIST/children
87+
Http::fake([
88+
'https://api.notion.com/v1/blocks/1d719dd1-563b-4387-b74f-20da92b827fb/children*'
89+
=> Http::response(
90+
json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_supported_blocks_200.json'), true),
91+
200,
92+
['Headers']
93+
)
94+
]);
95+
96+
$blockChildren = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->children();
97+
$this->assertInstanceOf(BlockCollection::class, $blockChildren);
98+
99+
# check collection
100+
$blockChildrenCollection = $blockChildren->asCollection();
101+
$this->assertContainsOnly(Block::class, $blockChildrenCollection);
102+
$this->assertIsIterable($blockChildrenCollection);
103+
$this->assertCount(8, $blockChildrenCollection);
104+
105+
# check paragraph
106+
$blockChild = $blockChildrenCollection[0];
107+
$this->assertInstanceOf(Paragraph::class, $blockChild);
108+
$this->assertEquals('paragraph', $blockChild->getType());
109+
$this->assertFalse($blockChild->hasChildren());
110+
$this->assertEquals('paragraph_block', $blockChild->getContent()->getPlainText());
111+
112+
# check heading_1
113+
$blockChild = $blockChildrenCollection[1];
114+
$this->assertInstanceOf(HeadingOne::class, $blockChild);
115+
$this->assertEquals('heading_1', $blockChild->getType());
116+
$this->assertFalse($blockChild->hasChildren());
117+
$this->assertEquals('heading_one_block', $blockChild->getContent()->getPlainText());
118+
119+
# check heading_2
120+
$blockChild = $blockChildrenCollection[2];
121+
$this->assertInstanceOf(HeadingTwo::class, $blockChild);
122+
$this->assertEquals('heading_2', $blockChild->getType());
123+
$this->assertFalse($blockChild->hasChildren());
124+
$this->assertEquals('heading_two_block', $blockChild->getContent()->getPlainText());
125+
126+
# check heading_3
127+
$blockChild = $blockChildrenCollection[3];
128+
$this->assertInstanceOf(HeadingThree::class, $blockChild);
129+
$this->assertEquals('heading_3', $blockChild->getType());
130+
$this->assertFalse($blockChild->hasChildren());
131+
$this->assertEquals('heading_three_block', $blockChild->getContent()->getPlainText());
132+
133+
# check bulleted_list_item
134+
$blockChild = $blockChildrenCollection[4];
135+
$this->assertInstanceOf(BulletedListItem::class, $blockChild);
136+
$this->assertEquals('bulleted_list_item', $blockChild->getType());
137+
$this->assertFalse($blockChild->hasChildren());
138+
$this->assertEquals('bulleted_list_item_block', $blockChild->getContent()->getPlainText());
139+
140+
# check numbered_list_item
141+
$blockChild = $blockChildrenCollection[5];
142+
$this->assertInstanceOf(NumberedListItem::class, $blockChild);
143+
$this->assertEquals('numbered_list_item', $blockChild->getType());
144+
$this->assertFalse($blockChild->hasChildren());
145+
$this->assertEquals('numbered_list_item_block', $blockChild->getContent()->getPlainText());
146+
147+
# check to_do
148+
$blockChild = $blockChildrenCollection[6];
149+
$this->assertInstanceOf(ToDo::class, $blockChild);
150+
$this->assertEquals('to_do', $blockChild->getType());
151+
$this->assertFalse($blockChild->hasChildren());
152+
$this->assertEquals('to_do_block', $blockChild->getContent()->getPlainText());
153+
154+
# check toggle
155+
$blockChild = $blockChildrenCollection[7];
156+
$this->assertInstanceOf(Toggle::class, $blockChild);
157+
$this->assertEquals('toggle', $blockChild->getType());
158+
$this->assertFalse($blockChild->hasChildren());
159+
$this->assertEquals('toggle_block', $blockChild->getContent()->getPlainText());
160+
161+
}
161162

162163
/** @test */
163164
public function it_throws_a_notion_exception_not_found()
@@ -188,4 +189,23 @@ public function it_throws_a_handling_exception_not_implemented()
188189
Notion::block('')->create();
189190
}
190191

192+
/** @test */
193+
public function it_retrieves_a_single_block()
194+
{
195+
// successful /v1/blocks/BLOCK_DOES_EXIST
196+
Http::fake([
197+
'https://api.notion.com/v1/blocks/a6f8ebe8d5df4ffab543bcd54d1c3bad'
198+
=> Http::response(
199+
json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_block_200.json'), true),
200+
200,
201+
['Headers']
202+
)
203+
]);
204+
205+
$block = \Notion::block("a6f8ebe8d5df4ffab543bcd54d1c3bad")->retrieve();
206+
207+
$this->assertInstanceOf(Block::class, $block);
208+
$this->assertInstanceOf(Paragraph::class, $block);
209+
}
210+
191211
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"object": "block",
3+
"id": "a6f8ebe8-d5df-4ffa-b543-bcd54d1c3bad",
4+
"created_time": "2021-05-17T13:51:00.000Z",
5+
"last_edited_time": "2021-06-10T17:40:00.000Z",
6+
"has_children": false,
7+
"archived": false,
8+
"type": "paragraph",
9+
"paragraph": {
10+
"text": [
11+
{
12+
"type": "text",
13+
"text": {
14+
"content": "C:\\xampp\\php",
15+
"link": null
16+
},
17+
"annotations": {
18+
"bold": false,
19+
"italic": false,
20+
"strikethrough": false,
21+
"underline": false,
22+
"code": false,
23+
"color": "default"
24+
},
25+
"plain_text": "C:\\xampp\\php",
26+
"href": null
27+
}
28+
]
29+
}
30+
}

0 commit comments

Comments
 (0)