|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use StellarWP\AdminNotices\AdminNotice; |
| 6 | +use StellarWP\AdminNotices\AdminNotices; |
| 7 | +use StellarWP\AdminNotices\Contracts\NotificationsRegistrarInterface; |
| 8 | +use StellarWP\AdminNotices\Tests\Support\Helper\TestCase; |
| 9 | +use StellarWP\AdminNotices\Tests\Support\Traits\WithUopz; |
| 10 | + |
| 11 | +/** |
| 12 | + * @coversDefaultClass \StellarWP\AdminNotices\AdminNotices |
| 13 | + */ |
| 14 | +class AdminNoticesTest extends TestCase |
| 15 | +{ |
| 16 | + use WithUopz; |
| 17 | + |
| 18 | + /** |
| 19 | + * @covers ::show |
| 20 | + * |
| 21 | + * @since 1.0.0 |
| 22 | + */ |
| 23 | + public function testShouldRegisterAdminNotice() |
| 24 | + { |
| 25 | + $mockRegistrar = $this->createMock(NotificationsRegistrarInterface::class); |
| 26 | + $mockRegistrar |
| 27 | + ->expects($this->once()) |
| 28 | + ->method('registerNotice') |
| 29 | + ->with($this->isInstanceOf(StellarWP\AdminNotices\AdminNotice::class)); |
| 30 | + |
| 31 | + $this->setUpContainerWithMockRegistrar($mockRegistrar); |
| 32 | + |
| 33 | + $notice = AdminNotices::show('test', 'This is a test message.'); |
| 34 | + $this->assertInstanceOf(StellarWP\AdminNotices\AdminNotice::class, $notice); |
| 35 | + $this->assertEquals('This is a test message.', $notice->getRenderTextOrCallback()); |
| 36 | + $this->assertSame('/test', $notice->getId()); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @covers ::render |
| 41 | + * |
| 42 | + * @since 1.0.0 |
| 43 | + */ |
| 44 | + public function testShouldRenderAdminNotice() |
| 45 | + { |
| 46 | + $notice = new AdminNotice('test', 'This is a test message.'); |
| 47 | + |
| 48 | + AdminNotices::render($notice); |
| 49 | + |
| 50 | + $this->expectOutputString( |
| 51 | + '<div class=\'notice notice-info\' data-notice-id=\'test\'>This is a test message.</div>' |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @covers ::render |
| 57 | + * |
| 58 | + * @since 1.0.0 |
| 59 | + */ |
| 60 | + public function testShouldReturnAdminNoticeHtml() |
| 61 | + { |
| 62 | + $notice = new AdminNotice('test', 'This is a test message.'); |
| 63 | + |
| 64 | + $html = AdminNotices::render($notice, false); |
| 65 | + |
| 66 | + $this->assertEquals( |
| 67 | + '<div class=\'notice notice-info\' data-notice-id=\'test\'>This is a test message.</div>', |
| 68 | + $html |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @covers ::removeNotice |
| 74 | + * |
| 75 | + * @since 1.0.0 |
| 76 | + */ |
| 77 | + public function testShouldRemoveNotice() |
| 78 | + { |
| 79 | + $mockRegistrar = $this->createMock(NotificationsRegistrarInterface::class); |
| 80 | + $mockRegistrar |
| 81 | + ->expects($this->once()) |
| 82 | + ->method('unregisterNotice') |
| 83 | + ->with('test'); |
| 84 | + |
| 85 | + $this->setUpContainerWithMockRegistrar($mockRegistrar); |
| 86 | + |
| 87 | + AdminNotices::removeNotice('test'); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @covers ::initialize |
| 92 | + * |
| 93 | + * @since 1.0.0 |
| 94 | + */ |
| 95 | + public function testInitialize(): void |
| 96 | + { |
| 97 | + AdminNotices::initialize('test', 'https://example.com'); |
| 98 | + |
| 99 | + $this->assertSame(10, has_action('admin_notices', [AdminNotices::class, 'setUpNotices'])); |
| 100 | + $this->assertSame(10, has_action('admin_enqueue_scripts', [AdminNotices::class, 'enqueueScripts'])); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @covers ::initialize |
| 105 | + * |
| 106 | + * @since 1.0.0 |
| 107 | + */ |
| 108 | + public function testShouldThrowExceptionOnEmptyNamespace() |
| 109 | + { |
| 110 | + $this->expectException(RuntimeException::class); |
| 111 | + $this->expectExceptionMessage('Namespace must be provided'); |
| 112 | + |
| 113 | + AdminNotices::initialize('', 'https://example.com'); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Adds a container to the AdminNotices class with a mock registrar |
| 118 | + * |
| 119 | + * @since 1.0.0 |
| 120 | + */ |
| 121 | + private function setUpContainerWithMockRegistrar($mockRegistrar): void |
| 122 | + { |
| 123 | + AdminNotices::setContainer(new ServiceContainer($mockRegistrar)); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * A simple service container for testing |
| 129 | + * |
| 130 | + * @since 1.0.0 |
| 131 | + */ |
| 132 | +class ServiceContainer implements Psr\Container\ContainerInterface |
| 133 | +{ |
| 134 | + private $mockRegistrar; |
| 135 | + |
| 136 | + public function __construct($mockRegistrar) |
| 137 | + { |
| 138 | + $this->mockRegistrar = $mockRegistrar; |
| 139 | + } |
| 140 | + |
| 141 | + public function get(string $id) |
| 142 | + { |
| 143 | + if ($id === NotificationsRegistrarInterface::class) { |
| 144 | + return $this->mockRegistrar; |
| 145 | + } |
| 146 | + |
| 147 | + throw new RuntimeException('Service not found'); |
| 148 | + } |
| 149 | + |
| 150 | + public function has(string $id): bool |
| 151 | + { |
| 152 | + if ($id === NotificationsRegistrarInterface::class) { |
| 153 | + return true; |
| 154 | + } |
| 155 | + |
| 156 | + throw new RuntimeException('Service not found'); |
| 157 | + } |
| 158 | +} |
0 commit comments