Skip to content

Commit 5db62f4

Browse files
committed
Add block tests
1 parent f9b9c2d commit 5db62f4

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

tests/Stub/Gutenberg/Block.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WpOop\WordPress\Test\Stub\Gutenberg;
6+
7+
use WpOop\WordPress\Gutenberg\BlockInterface;
8+
9+
class Block implements BlockInterface
10+
{
11+
protected $blockName;
12+
13+
protected $attributes;
14+
15+
protected $innerBlocks;
16+
17+
protected $innerHtml;
18+
19+
protected $innerContent;
20+
21+
public function __construct(
22+
string $blockName,
23+
array $attributes,
24+
array $innerBlocks,
25+
string $innerHtml,
26+
array $innerContent
27+
) {
28+
$this->blockName = $blockName;
29+
$this->attributes = $attributes;
30+
$this->innerBlocks = $innerBlocks;
31+
$this->innerHtml = $innerHtml;
32+
$this->innerContent = $innerContent;
33+
}
34+
35+
public function getBlockName(): string
36+
{
37+
return $this->blockName;
38+
}
39+
40+
public function getAttributes(): array
41+
{
42+
return $this->attributes;
43+
}
44+
45+
public function getInnerBlocks(): array
46+
{
47+
return $this->innerBlocks;
48+
}
49+
50+
public function getInnerHtml(): string
51+
{
52+
return $this->innerHtml;
53+
}
54+
55+
public function getInnerContent(): array
56+
{
57+
return $this->innerContent;
58+
}
59+
}

tests/Stub/Gutenberg/BlockParser.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WpOop\WordPress\Test\Stub\Gutenberg;
6+
7+
use WpOop\WordPress\Gutenberg\BlockParserInterface;
8+
9+
class BlockParser implements BlockParserInterface
10+
{
11+
public $blocks = [];
12+
13+
public function parseBlocks(string $postContent): array
14+
{
15+
return $this->blocks;
16+
}
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace WpOop\WordPress\Test\Unit\Gutenberg;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use WpOop\WordPress\Test\Stub\Gutenberg\Block;
7+
8+
class BlockInterfaceTest extends TestCase
9+
{
10+
public function testUsage()
11+
{
12+
$innerBlock = new Block('core/quote', [], [], '', []);
13+
$subject = new Block(
14+
'core/paragraph',
15+
['foo' => 'bar', 'foobar' => 42],
16+
[$innerBlock],
17+
'html',
18+
['content', null, 'content2']
19+
);
20+
21+
$this->assertInstanceOf(Block::class, $subject);
22+
23+
$this->assertEquals('core/paragraph', $subject->getBlockName());
24+
$this->assertEquals(['foo' => 'bar', 'foobar' => 42], $subject->getAttributes());
25+
$this->assertEquals([$innerBlock], $subject->getInnerBlocks());
26+
$this->assertEquals('html', $subject->getInnerHtml());
27+
$this->assertEquals(['content', null, 'content2'], $subject->getInnerContent());
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace WpOop\WordPress\Test\Unit\Gutenberg;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use WpOop\WordPress\Gutenberg\BlockParserInterface;
7+
use WpOop\WordPress\Test\Stub\Gutenberg\Block;
8+
use WpOop\WordPress\Test\Stub\Gutenberg\BlockParser;
9+
10+
class BlockParserInterfaceTest extends TestCase
11+
{
12+
public function testUsage()
13+
{
14+
$subject = new BlockParser();
15+
16+
$this->assertInstanceOf(BlockParserInterface::class, $subject);
17+
18+
$blocks = [
19+
new Block('core/paragraph', [], [], '', []),
20+
new Block('core/quote', [], [], '', []),
21+
];
22+
$subject->blocks = $blocks;
23+
24+
$ret = $subject->parseBlocks('wp post content');
25+
26+
$this->assertEquals($blocks, $ret);
27+
}
28+
}

0 commit comments

Comments
 (0)