Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/Endpoints/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
180 changes: 100 additions & 80 deletions tests/EndpointBlocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']
)
Expand All @@ -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()
Expand Down Expand Up @@ -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);
}

}
30 changes: 30 additions & 0 deletions tests/stubs/endpoints/blocks/response_specific_block_200.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
}