diff --git a/README.md b/README.md index b96ce4a16b77277c951ae31a2f52362fa71f2f2a..cef12b930c5b7188663d28e316cc829b54bbc121 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,15 @@ require_once('/path/to/notice/src/init.php'); Simple usage looks like: ```php -$notice = new \WPDesk\Notice\Notice( 'info', 'Notice text goes here' ); +$notice = WPDeskNotice('Notice text goes here'); + +// Is equivalent to: + +$notice = \WPDesk\Notice\Factory::notice('Notice text goes here'); + +// Is equivalent to: + +$notice = new \WPDesk\Notice\Notice('Notice text goes here'); ``` Notice must be used before WordPress action `admin_notices`. WordPress admin actions order is listed [here](https://codex.wordpress.org/Plugin_API/Action_Reference#Actions_Run_During_an_Admin_Page_Request). diff --git a/phpunit-integration.xml b/phpunit-integration.xml index b989fa53c2b5307d4f55789c5353b45f2748f8d1..4ab26660b6b026743711f94b76a0f2866493ed5f 100644 --- a/phpunit-integration.xml +++ b/phpunit-integration.xml @@ -12,6 +12,8 @@ <directory suffix=".php">src</directory> <exclude> <file>src/init.php</file> + <directory suffix=".php">tests</directory> + <directory suffix=".php">vendor</directory> </exclude> </whitelist> </filter> diff --git a/src/WPDesk/Notice/Notice.php b/src/WPDesk/Notice/Notice.php index 3e397310c59f45e338f10e31b40b872001532065..b0f394d4f35b98a7133ca0979fb15e6ae1cc4d6d 100644 --- a/src/WPDesk/Notice/Notice.php +++ b/src/WPDesk/Notice/Notice.php @@ -34,7 +34,7 @@ class Notice * * @var bool */ - protected $isDismissible; + protected $dismissible; /** * Notice hook priority. @@ -46,7 +46,7 @@ class Notice * Is action added? * @var bool */ - private $isActionAdded = false; + private $actionAdded = false; /** * Attributes. @@ -61,15 +61,15 @@ class Notice * * @param string $noticeContent Notice content. * @param string $noticeType Notice type. - * @param bool $isDismissible Is dismissible. + * @param bool $dismissible Is dismissible. * @param int $priority Notice priority. */ - public function __construct($noticeContent, $noticeType = 'info', $isDismissible = false, $priority = 10) + public function __construct($noticeContent, $noticeType = 'info', $dismissible = false, $priority = 10) { $this->noticeContent = $noticeContent; - $this->noticeType = $noticeType; - $this->isDismissible = $isDismissible; - $this->priority = $priority; + $this->noticeType = $noticeType; + $this->dismissible = $dismissible; + $this->priority = $priority; $this->addAction(); } @@ -110,15 +110,15 @@ class Notice */ public function isDismissible() { - return $this->isDismissible; + return $this->dismissible; } /** - * @param bool $isDismissible + * @param bool $dismissible */ - public function setIsDismissible($isDismissible) + public function setDismissible($dismissible) { - $this->isDismissible = $isDismissible; + $this->dismissible = $dismissible; } /** @@ -135,7 +135,7 @@ class Notice public function setPriority($priority) { $this->priority = $priority; - if ($this->isActionAdded) { + if ($this->actionAdded) { $this->removeAction(); $this->addAction(); } @@ -146,17 +146,17 @@ class Notice */ protected function addAction() { - if (!$this->isActionAdded) { + if (!$this->actionAdded) { add_action('admin_notices', [$this, 'showNotice'], $this->priority); - $this->isActionAdded = true; + $this->actionAdded = true; } } protected function removeAction() { - if ($this->isActionAdded) { + if ($this->actionAdded) { remove_action('admin_notices', [$this, 'showNotice'], $this->priority); - $this->isActionAdded = false; + $this->actionAdded = false; } } @@ -172,7 +172,7 @@ class Notice } else { $notice_class = 'notice notice-' . $this->noticeType; } - if ($this->isDismissible) { + if ($this->dismissible) { $notice_class .= ' is-dismissible'; } return $notice_class; diff --git a/tests/integration/TestFunctions.php b/tests/integration/TestFunctions.php new file mode 100644 index 0000000000000000000000000000000000000000..e4c616b3f76e0c3bef00d98b79ed03f6692ac59b --- /dev/null +++ b/tests/integration/TestFunctions.php @@ -0,0 +1,32 @@ +<?php + +use \WPDesk\Notice\Notice; +use \WPDesk\Notice\PermanentDismissibleNotice; + +/** + * Class TestFunctions + */ +class TestFunctions extends WP_UnitTestCase +{ + + /** + * Test WPDeskNotice function. + */ + public function testWPDeskNotice() + { + $notice = WPDeskNotice('test'); + + $this->assertInstanceOf(Notice::class, $notice); + } + + /** + * Test WPDeskPermanentDismissibleNotice function. + */ + public function testWPDeskPermanentDismissibleNotice() + { + $notice = WPDeskPermanentDismissibleNotice('test'); + + $this->assertInstanceOf(PermanentDismissibleNotice::class, $notice); + } + +} diff --git a/tests/integration/TestNotice.php b/tests/integration/TestNotice.php index 9035800ad972081c1a5fd6c626e0bdbb3b1fbb79..3cdad40a27110414ffccb6e795931a33584a145d 100644 --- a/tests/integration/TestNotice.php +++ b/tests/integration/TestNotice.php @@ -16,13 +16,40 @@ class TestNotice extends WP_UnitTestCase public function testShowNotice() { - $notice = new Notice('test', Notice::NOTICE_TYPE_INFO ); + $notice = new Notice('test'); $this->expectOutputString('<div class="notice notice-info"><p>test</p></div>'); $notice->showNotice(); } + public function testShowNoticeError() + { + $notice = new Notice('test', Notice::NOTICE_TYPE_ERROR); + + $this->expectOutputString('<div class="notice notice-error"><p>test</p></div>'); + + $notice->showNotice(); + } + + public function testShowNoticeWarning() + { + $notice = new Notice('test', Notice::NOTICE_TYPE_WARNING); + + $this->expectOutputString('<div class="notice notice-warning"><p>test</p></div>'); + + $notice->showNotice(); + } + + public function testShowNoticeSuccess() + { + $notice = new Notice('test', Notice::NOTICE_TYPE_SUCCESS); + + $this->expectOutputString('<div class="notice notice-success"><p>test</p></div>'); + + $notice->showNotice(); + } + public function testShowNoticeDismissible() { $notice = new Notice('test', Notice::NOTICE_TYPE_INFO, true); @@ -32,4 +59,47 @@ class TestNotice extends WP_UnitTestCase $notice->showNotice(); } + public function testNoticeContent() + { + $noticeContent = 'test'; + + $notice = new Notice($noticeContent); + + $this->assertEquals($noticeContent, $notice->getNoticeContent()); + + $noticeContent = 'test 2'; + $notice->setNoticeContent($noticeContent); + $this->assertEquals($noticeContent, $notice->getNoticeContent()); + } + + public function testNoticeType() + { + $notice = new Notice('test', Notice::NOTICE_TYPE_INFO); + + $this->assertEquals(Notice::NOTICE_TYPE_INFO, $notice->getNoticeType()); + + $notice->setNoticeType(Notice::NOTICE_TYPE_ERROR); + $this->assertEquals(Notice::NOTICE_TYPE_ERROR, $notice->getNoticeType()); + } + + public function testDismissible() + { + $notice = new Notice('test'); + + $this->assertFalse($notice->isDismissible()); + + $notice->setDismissible(true); + $this->assertTrue($notice->isDismissible()); + } + + public function testPriority() + { + $notice = new Notice('test'); + + $this->assertEquals(10, $notice->getPriority()); + + $notice->setPriority(20); + $this->assertEquals(20, $notice->getPriority()); + } + }