| 1 |
<?php |
| 2 |
|
| 3 |
namespace Extendify\Tests\Integration\QuickEdit\Schemas; |
| 4 |
|
| 5 |
use Extendify\QuickEdit\Schemas\Button; |
| 6 |
use Extendify\QuickEdit\Schemas\Cover; |
| 7 |
use Extendify\QuickEdit\Schemas\Heading; |
| 8 |
use Extendify\QuickEdit\Schemas\Image; |
| 9 |
use Extendify\QuickEdit\Schemas\MediaText; |
| 10 |
use Extendify\QuickEdit\Schemas\NavigationLink; |
| 11 |
use Extendify\QuickEdit\Schemas\Paragraph; |
| 12 |
use Extendify\QuickEdit\Schemas\Registry; |
| 13 |
use Extendify\QuickEdit\Schemas\Schema; |
| 14 |
use Extendify\QuickEdit\Schemas\SocialLink; |
| 15 |
use ReflectionClass; |
| 16 |
use WP_UnitTestCase; |
| 17 |
|
| 18 |
class RegistryTest extends WP_UnitTestCase |
| 19 |
{ |
| 20 |
public function setUp(): void |
| 21 |
{ |
| 22 |
parent::setUp(); |
| 23 |
$this->resetRegistry(); |
| 24 |
} |
| 25 |
|
| 26 |
public function tearDown(): void |
| 27 |
{ |
| 28 |
$this->resetRegistry(); |
| 29 |
parent::tearDown(); |
| 30 |
} |
| 31 |
|
| 32 |
public function test_get_returns_null_for_unknown_block() |
| 33 |
{ |
| 34 |
$this->assertNull(Registry::get('core/paragraph')); |
| 35 |
$this->assertNull(Registry::get('not/a-block')); |
| 36 |
} |
| 37 |
|
| 38 |
public function test_register_then_get_returns_the_instance() |
| 39 |
{ |
| 40 |
$schema = new Paragraph(); |
| 41 |
Registry::register('core/paragraph', $schema); |
| 42 |
|
| 43 |
$this->assertSame($schema, Registry::get('core/paragraph')); |
| 44 |
} |
| 45 |
|
| 46 |
public function test_register_overwrites_previous_entry() |
| 47 |
{ |
| 48 |
$first = new Paragraph(); |
| 49 |
$second = new Paragraph(); |
| 50 |
|
| 51 |
Registry::register('core/paragraph', $first); |
| 52 |
Registry::register('core/paragraph', $second); |
| 53 |
|
| 54 |
$this->assertSame($second, Registry::get('core/paragraph')); |
| 55 |
} |
| 56 |
|
| 57 |
public function test_init_registers_the_expected_block_names() |
| 58 |
{ |
| 59 |
Registry::init(); |
| 60 |
|
| 61 |
$expected = [ |
| 62 |
'core/paragraph' => Paragraph::class, |
| 63 |
'core/heading' => Heading::class, |
| 64 |
'core/image' => Image::class, |
| 65 |
'core/button' => Button::class, |
| 66 |
'core/cover' => Cover::class, |
| 67 |
'core/media-text' => MediaText::class, |
| 68 |
'core/social-link' => SocialLink::class, |
| 69 |
'core/navigation-link' => NavigationLink::class, |
| 70 |
'core/navigation-submenu' => NavigationLink::class, |
| 71 |
]; |
| 72 |
|
| 73 |
foreach ($expected as $blockName => $class) { |
| 74 |
$schema = Registry::get($blockName); |
| 75 |
$this->assertInstanceOf(Schema::class, $schema, "missing schema for $blockName"); |
| 76 |
$this->assertInstanceOf($class, $schema, "wrong class for $blockName"); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
public function test_describe_returns_blockName_and_fields_per_registered_schema() |
| 81 |
{ |
| 82 |
Registry::init(); |
| 83 |
|
| 84 |
$described = Registry::describe(); |
| 85 |
|
| 86 |
$this->assertArrayHasKey('core/paragraph', $described); |
| 87 |
$this->assertSame('core/paragraph', $described['core/paragraph']['blockName']); |
| 88 |
$this->assertSame((new Paragraph())->fields(), $described['core/paragraph']['fields']); |
| 89 |
|
| 90 |
$this->assertSame( |
| 91 |
[ |
| 92 |
'core/paragraph', |
| 93 |
'core/heading', |
| 94 |
'core/image', |
| 95 |
'core/button', |
| 96 |
'core/cover', |
| 97 |
'core/media-text', |
| 98 |
'core/social-link', |
| 99 |
'core/navigation-link', |
| 100 |
'core/navigation-submenu', |
| 101 |
], |
| 102 |
array_keys($described) |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
public function test_describe_is_empty_when_nothing_registered() |
| 107 |
{ |
| 108 |
$this->assertSame([], Registry::describe()); |
| 109 |
} |
| 110 |
|
| 111 |
private function resetRegistry(): void |
| 112 |
{ |
| 113 |
$reflection = new ReflectionClass(Registry::class); |
| 114 |
$prop = $reflection->getProperty('schemas'); |
| 115 |
$prop->setAccessible(true); |
| 116 |
$prop->setValue(null, []); |
| 117 |
} |
| 118 |
} |
| 119 |
|