Skip to content

Commit 86e37f9

Browse files
committed
add embed-creation and add tests for embed-block
1 parent 9c7d72d commit 86e37f9

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

src/Entities/Blocks/Embed.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,27 @@ class Embed extends Block
1717
private RichText $caption;
1818
private string $url = "";
1919

20-
function __construct(array $responseData = null){
20+
public static function create(string $url, string $caption): Embed
21+
{
22+
$embed = new Embed();
23+
24+
$embed->rawContent = [
25+
'url' => $url,
26+
'caption' => [
27+
[
28+
'type' => 'text',
29+
'text' => [
30+
'content' => $caption
31+
]
32+
]
33+
]
34+
];
35+
36+
return $embed;
37+
}
38+
39+
function __construct(array $responseData = null)
40+
{
2141
$this->type = "embed";
2242
parent::__construct($responseData);
2343
}
@@ -41,11 +61,13 @@ protected function fillContent(): void
4161
$this->content = $this->url;
4262
}
4363

44-
public function getUrl(){
64+
public function getUrl()
65+
{
4566
return $this->url;
4667
}
4768

48-
public function getCaption(){
69+
public function getCaption()
70+
{
4971
return $this->caption;
5072
}
5173
}

tests/EndpointBlocksTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Facades\Http;
77
use FiveamCode\LaravelNotionApi\Entities\Blocks\Block;
88
use FiveamCode\LaravelNotionApi\Entities\Blocks\BulletedListItem;
9+
use FiveamCode\LaravelNotionApi\Entities\Blocks\Embed;
910
use FiveamCode\LaravelNotionApi\Entities\Blocks\HeadingOne;
1011
use FiveamCode\LaravelNotionApi\Entities\Blocks\HeadingThree;
1112
use FiveamCode\LaravelNotionApi\Entities\Blocks\HeadingTwo;
@@ -99,7 +100,7 @@ public function it_returns_block_collection_with_children_as_correct_instances()
99100
$blockChildrenCollection = $blockChildren->asCollection();
100101
$this->assertContainsOnly(Block::class, $blockChildrenCollection);
101102
$this->assertIsIterable($blockChildrenCollection);
102-
$this->assertCount(8, $blockChildrenCollection);
103+
$this->assertCount(9, $blockChildrenCollection);
103104

104105
# check paragraph
105106
$blockChild = $blockChildrenCollection[0];
@@ -156,6 +157,14 @@ public function it_returns_block_collection_with_children_as_correct_instances()
156157
$this->assertEquals('toggle', $blockChild->getType());
157158
$this->assertFalse($blockChild->hasChildren());
158159
$this->assertEquals('toggle_block', $blockChild->getContent()->getPlainText());
160+
161+
# check embed
162+
$blockChild = $blockChildrenCollection[8];
163+
$this->assertInstanceOf(Embed::class, $blockChild);
164+
$this->assertEquals('embed', $blockChild->getType());
165+
$this->assertFalse($blockChild->hasChildren());
166+
$this->assertEquals('Testcaption', $blockChild->getCaption()->getPlainText());
167+
$this->assertEquals('https://notion.so', $blockChild->getUrl());
159168
}
160169

161170
/** @test */
@@ -202,6 +211,7 @@ public function it_returns_parent_block_in_which_new_blocks_have_been_successful
202211
$numberedListItem = NumberedListItem::create("New TextBlock");
203212
$toDo = ToDo::create("New TextBlock");
204213
$toggle = Toggle::create(["New TextBlock"]);
214+
$embed = Embed::create("https://5amco.de", "Testcaption");
205215

206216
$parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($paragraph);
207217
$this->assertInstanceOf(Block::class, $parentBlock);
@@ -227,8 +237,11 @@ public function it_returns_parent_block_in_which_new_blocks_have_been_successful
227237
$parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($toggle);
228238
$this->assertInstanceOf(Block::class, $parentBlock);
229239

240+
$parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append($embed);
241+
$this->assertInstanceOf(Block::class, $parentBlock);
242+
230243

231-
$parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append([$paragraph, $bulletedListItem, $headingOne, $headingTwo, $headingThree, $numberedListItem, $toDo, $toggle]);
244+
$parentBlock = Notion::block('1d719dd1-563b-4387-b74f-20da92b827fb')->append([$paragraph, $bulletedListItem, $headingOne, $headingTwo, $headingThree, $numberedListItem, $toDo, $toggle, $embed]);
232245
$this->assertInstanceOf(Block::class, $parentBlock);
233246
}
234247
}

tests/stubs/endpoints/blocks/response_specific_supported_blocks_200.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,37 @@
233233
}
234234
]
235235
}
236+
},
237+
{
238+
"object": "block",
239+
"id": "1750f17f-d2bd-105a-bedc-c6b5808b98be",
240+
"created_time": "2021-09-11T18:52:00.000Z",
241+
"last_edited_time": "2021-09-11T19:01:00.000Z",
242+
"has_children": false,
243+
"archived": false,
244+
"type": "embed",
245+
"embed": {
246+
"caption": [
247+
{
248+
"type": "text",
249+
"text": {
250+
"content": "Testcaption",
251+
"link": null
252+
},
253+
"annotations": {
254+
"bold": false,
255+
"italic": false,
256+
"strikethrough": false,
257+
"underline": false,
258+
"code": false,
259+
"color": "default"
260+
},
261+
"plain_text": "Testcaption",
262+
"href": null
263+
}
264+
],
265+
"url": "https://notion.so"
266+
}
236267
}
237268
],
238269
"next_cursor": null,

0 commit comments

Comments
 (0)