diff --git a/src/Endpoints/Block.php b/src/Endpoints/Block.php index 6c68eb5..959ed50 100644 --- a/src/Endpoints/Block.php +++ b/src/Endpoints/Block.php @@ -6,7 +6,7 @@ use FiveamCode\LaravelNotionApi\Exceptions\NotionException; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Entities\Collections\BlockCollection; - +use FiveamCode\LaravelNotionApi\Entities\Blocks\Block as BlockEntity; /** * Class Block * @package FiveamCode\LaravelNotionApi\Endpoints @@ -31,6 +31,25 @@ public function __construct(Notion $notion, string $blockId) $this->blockId = $blockId; } + /** + * Retrieve a block + * url: https://api.notion.com/{version}/blocks/{block_id} + * notion-api-docs: https://developers.notion.com/reference/retrieve-a-block + * + * @param string $blockId + * @return BlockEntity + * @throws HandlingException + * @throws NotionException + */ + public function retrieve(): BlockEntity { + + $response = $this->get( + $this->url(Endpoint::BLOCKS . '/' . $this->blockId) + ); + + return BlockEntity::fromResponse($response->json()); + } + /** * Retrieve block children * url: https://api.notion.com/{version}/blocks/{block_id}/children diff --git a/tests/EndpointBlocksTest.php b/tests/EndpointBlocksTest.php index eacd20e..e45a9e5 100644 --- a/tests/EndpointBlocksTest.php +++ b/tests/EndpointBlocksTest.php @@ -2,6 +2,7 @@ namespace FiveamCode\LaravelNotionApi\Tests; +use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; use Notion; use Illuminate\Support\Facades\Http; use FiveamCode\LaravelNotionApi\Entities\Blocks\Block; @@ -54,7 +55,7 @@ public function it_returns_block_collection_with_children() Http::fake([ 'https://api.notion.com/v1/blocks/b55c9c91-384d-452b-81db-d1ef79372b76/children*' => Http::response( - json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_200.json'), true), + json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_block_children_200.json'), true), 200, ['Headers'] ) @@ -79,85 +80,85 @@ public function it_returns_block_collection_with_children() $this->assertEquals('Lacinato kale', $blockChild->getRawContent()['text'][0]['plain_text']); } - /** @test */ - public function it_returns_block_collection_with_children_as_correct_instances() - { - // successful /v1/blocks/BLOCK_DOES_EXIST/children - Http::fake([ - 'https://api.notion.com/v1/blocks/1d719dd1-563b-4387-b74f-20da92b827fb/children*' - => Http::response( - json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_supported_blocks_200.json'), true), - 200, - ['Headers'] - ) - ]); - - $blockChildren = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->children(); - $this->assertInstanceOf(BlockCollection::class, $blockChildren); - - # check collection - $blockChildrenCollection = $blockChildren->asCollection(); - $this->assertContainsOnly(Block::class, $blockChildrenCollection); - $this->assertIsIterable($blockChildrenCollection); - $this->assertCount(8, $blockChildrenCollection); - - # check paragraph - $blockChild = $blockChildrenCollection[0]; - $this->assertInstanceOf(Paragraph::class, $blockChild); - $this->assertEquals('paragraph', $blockChild->getType()); - $this->assertFalse($blockChild->hasChildren()); - $this->assertEquals('paragraph_block', $blockChild->getContent()->getPlainText()); - - # check heading_1 - $blockChild = $blockChildrenCollection[1]; - $this->assertInstanceOf(HeadingOne::class, $blockChild); - $this->assertEquals('heading_1', $blockChild->getType()); - $this->assertFalse($blockChild->hasChildren()); - $this->assertEquals('heading_one_block', $blockChild->getContent()->getPlainText()); - - # check heading_2 - $blockChild = $blockChildrenCollection[2]; - $this->assertInstanceOf(HeadingTwo::class, $blockChild); - $this->assertEquals('heading_2', $blockChild->getType()); - $this->assertFalse($blockChild->hasChildren()); - $this->assertEquals('heading_two_block', $blockChild->getContent()->getPlainText()); - - # check heading_3 - $blockChild = $blockChildrenCollection[3]; - $this->assertInstanceOf(HeadingThree::class, $blockChild); - $this->assertEquals('heading_3', $blockChild->getType()); - $this->assertFalse($blockChild->hasChildren()); - $this->assertEquals('heading_three_block', $blockChild->getContent()->getPlainText()); - - # check bulleted_list_item - $blockChild = $blockChildrenCollection[4]; - $this->assertInstanceOf(BulletedListItem::class, $blockChild); - $this->assertEquals('bulleted_list_item', $blockChild->getType()); - $this->assertFalse($blockChild->hasChildren()); - $this->assertEquals('bulleted_list_item_block', $blockChild->getContent()->getPlainText()); - - # check numbered_list_item - $blockChild = $blockChildrenCollection[5]; - $this->assertInstanceOf(NumberedListItem::class, $blockChild); - $this->assertEquals('numbered_list_item', $blockChild->getType()); - $this->assertFalse($blockChild->hasChildren()); - $this->assertEquals('numbered_list_item_block', $blockChild->getContent()->getPlainText()); - - # check to_do - $blockChild = $blockChildrenCollection[6]; - $this->assertInstanceOf(ToDo::class, $blockChild); - $this->assertEquals('to_do', $blockChild->getType()); - $this->assertFalse($blockChild->hasChildren()); - $this->assertEquals('to_do_block', $blockChild->getContent()->getPlainText()); - - # check toggle - $blockChild = $blockChildrenCollection[7]; - $this->assertInstanceOf(Toggle::class, $blockChild); - $this->assertEquals('toggle', $blockChild->getType()); - $this->assertFalse($blockChild->hasChildren()); - $this->assertEquals('toggle_block', $blockChild->getContent()->getPlainText()); - - } + /** @test */ + public function it_returns_block_collection_with_children_as_correct_instances() + { + // successful /v1/blocks/BLOCK_DOES_EXIST/children + Http::fake([ + 'https://api.notion.com/v1/blocks/1d719dd1-563b-4387-b74f-20da92b827fb/children*' + => Http::response( + json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_supported_blocks_200.json'), true), + 200, + ['Headers'] + ) + ]); + + $blockChildren = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->children(); + $this->assertInstanceOf(BlockCollection::class, $blockChildren); + + # check collection + $blockChildrenCollection = $blockChildren->asCollection(); + $this->assertContainsOnly(Block::class, $blockChildrenCollection); + $this->assertIsIterable($blockChildrenCollection); + $this->assertCount(8, $blockChildrenCollection); + + # check paragraph + $blockChild = $blockChildrenCollection[0]; + $this->assertInstanceOf(Paragraph::class, $blockChild); + $this->assertEquals('paragraph', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('paragraph_block', $blockChild->getContent()->getPlainText()); + + # check heading_1 + $blockChild = $blockChildrenCollection[1]; + $this->assertInstanceOf(HeadingOne::class, $blockChild); + $this->assertEquals('heading_1', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('heading_one_block', $blockChild->getContent()->getPlainText()); + + # check heading_2 + $blockChild = $blockChildrenCollection[2]; + $this->assertInstanceOf(HeadingTwo::class, $blockChild); + $this->assertEquals('heading_2', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('heading_two_block', $blockChild->getContent()->getPlainText()); + + # check heading_3 + $blockChild = $blockChildrenCollection[3]; + $this->assertInstanceOf(HeadingThree::class, $blockChild); + $this->assertEquals('heading_3', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('heading_three_block', $blockChild->getContent()->getPlainText()); + + # check bulleted_list_item + $blockChild = $blockChildrenCollection[4]; + $this->assertInstanceOf(BulletedListItem::class, $blockChild); + $this->assertEquals('bulleted_list_item', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('bulleted_list_item_block', $blockChild->getContent()->getPlainText()); + + # check numbered_list_item + $blockChild = $blockChildrenCollection[5]; + $this->assertInstanceOf(NumberedListItem::class, $blockChild); + $this->assertEquals('numbered_list_item', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('numbered_list_item_block', $blockChild->getContent()->getPlainText()); + + # check to_do + $blockChild = $blockChildrenCollection[6]; + $this->assertInstanceOf(ToDo::class, $blockChild); + $this->assertEquals('to_do', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('to_do_block', $blockChild->getContent()->getPlainText()); + + # check toggle + $blockChild = $blockChildrenCollection[7]; + $this->assertInstanceOf(Toggle::class, $blockChild); + $this->assertEquals('toggle', $blockChild->getType()); + $this->assertFalse($blockChild->hasChildren()); + $this->assertEquals('toggle_block', $blockChild->getContent()->getPlainText()); + + } /** @test */ public function it_throws_a_notion_exception_not_found() @@ -188,4 +189,23 @@ public function it_throws_a_handling_exception_not_implemented() Notion::block('')->create(); } + /** @test */ + public function it_retrieves_a_single_block() + { + // successful /v1/blocks/BLOCK_DOES_EXIST + Http::fake([ + 'https://api.notion.com/v1/blocks/a6f8ebe8d5df4ffab543bcd54d1c3bad' + => Http::response( + json_decode(file_get_contents('tests/stubs/endpoints/blocks/response_specific_block_200.json'), true), + 200, + ['Headers'] + ) + ]); + + $block = \Notion::block("a6f8ebe8d5df4ffab543bcd54d1c3bad")->retrieve(); + + $this->assertInstanceOf(Block::class, $block); + $this->assertInstanceOf(Paragraph::class, $block); + } + } \ No newline at end of file diff --git a/tests/stubs/endpoints/blocks/response_specific_block_200.json b/tests/stubs/endpoints/blocks/response_specific_block_200.json new file mode 100644 index 0000000..1b2a15f --- /dev/null +++ b/tests/stubs/endpoints/blocks/response_specific_block_200.json @@ -0,0 +1,30 @@ +{ + "object": "block", + "id": "a6f8ebe8-d5df-4ffa-b543-bcd54d1c3bad", + "created_time": "2021-05-17T13:51:00.000Z", + "last_edited_time": "2021-06-10T17:40:00.000Z", + "has_children": false, + "archived": false, + "type": "paragraph", + "paragraph": { + "text": [ + { + "type": "text", + "text": { + "content": "C:\\xampp\\php", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "C:\\xampp\\php", + "href": null + } + ] + } +} \ No newline at end of file diff --git a/tests/stubs/endpoints/blocks/response_specific_200.json b/tests/stubs/endpoints/blocks/response_specific_block_children_200.json similarity index 100% rename from tests/stubs/endpoints/blocks/response_specific_200.json rename to tests/stubs/endpoints/blocks/response_specific_block_children_200.json