Skip to content
Snippets Groups Projects
Commit 8b1be169 authored by Grzegorz Rola's avatar Grzegorz Rola
Browse files

Added factory, functions, and changed order of parameters.

parent 0046b113
No related branches found
No related tags found
1 merge request!4Feature/factory
Pipeline #7383 passed
...@@ -45,7 +45,15 @@ require_once('/path/to/notice/src/init.php'); ...@@ -45,7 +45,15 @@ require_once('/path/to/notice/src/init.php');
Simple usage looks like: Simple usage looks like:
```php ```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). 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).
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
<directory suffix=".php">src</directory> <directory suffix=".php">src</directory>
<exclude> <exclude>
<file>src/init.php</file> <file>src/init.php</file>
<directory suffix=".php">tests</directory>
<directory suffix=".php">vendor</directory>
</exclude> </exclude>
</whitelist> </whitelist>
</filter> </filter>
......
...@@ -34,7 +34,7 @@ class Notice ...@@ -34,7 +34,7 @@ class Notice
* *
* @var bool * @var bool
*/ */
protected $isDismissible; protected $dismissible;
/** /**
* Notice hook priority. * Notice hook priority.
...@@ -46,7 +46,7 @@ class Notice ...@@ -46,7 +46,7 @@ class Notice
* Is action added? * Is action added?
* @var bool * @var bool
*/ */
private $isActionAdded = false; private $actionAdded = false;
/** /**
* Attributes. * Attributes.
...@@ -61,14 +61,14 @@ class Notice ...@@ -61,14 +61,14 @@ class Notice
* *
* @param string $noticeContent Notice content. * @param string $noticeContent Notice content.
* @param string $noticeType Notice type. * @param string $noticeType Notice type.
* @param bool $isDismissible Is dismissible. * @param bool $dismissible Is dismissible.
* @param int $priority Notice priority. * @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->noticeContent = $noticeContent;
$this->noticeType = $noticeType; $this->noticeType = $noticeType;
$this->isDismissible = $isDismissible; $this->dismissible = $dismissible;
$this->priority = $priority; $this->priority = $priority;
$this->addAction(); $this->addAction();
} }
...@@ -110,15 +110,15 @@ class Notice ...@@ -110,15 +110,15 @@ class Notice
*/ */
public function isDismissible() 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 ...@@ -135,7 +135,7 @@ class Notice
public function setPriority($priority) public function setPriority($priority)
{ {
$this->priority = $priority; $this->priority = $priority;
if ($this->isActionAdded) { if ($this->actionAdded) {
$this->removeAction(); $this->removeAction();
$this->addAction(); $this->addAction();
} }
...@@ -146,17 +146,17 @@ class Notice ...@@ -146,17 +146,17 @@ class Notice
*/ */
protected function addAction() protected function addAction()
{ {
if (!$this->isActionAdded) { if (!$this->actionAdded) {
add_action('admin_notices', [$this, 'showNotice'], $this->priority); add_action('admin_notices', [$this, 'showNotice'], $this->priority);
$this->isActionAdded = true; $this->actionAdded = true;
} }
} }
protected function removeAction() protected function removeAction()
{ {
if ($this->isActionAdded) { if ($this->actionAdded) {
remove_action('admin_notices', [$this, 'showNotice'], $this->priority); remove_action('admin_notices', [$this, 'showNotice'], $this->priority);
$this->isActionAdded = false; $this->actionAdded = false;
} }
} }
...@@ -172,7 +172,7 @@ class Notice ...@@ -172,7 +172,7 @@ class Notice
} else { } else {
$notice_class = 'notice notice-' . $this->noticeType; $notice_class = 'notice notice-' . $this->noticeType;
} }
if ($this->isDismissible) { if ($this->dismissible) {
$notice_class .= ' is-dismissible'; $notice_class .= ' is-dismissible';
} }
return $notice_class; return $notice_class;
......
<?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);
}
}
...@@ -16,13 +16,40 @@ class TestNotice extends WP_UnitTestCase ...@@ -16,13 +16,40 @@ class TestNotice extends WP_UnitTestCase
public function testShowNotice() 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>'); $this->expectOutputString('<div class="notice notice-info"><p>test</p></div>');
$notice->showNotice(); $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() public function testShowNoticeDismissible()
{ {
$notice = new Notice('test', Notice::NOTICE_TYPE_INFO, true); $notice = new Notice('test', Notice::NOTICE_TYPE_INFO, true);
...@@ -32,4 +59,47 @@ class TestNotice extends WP_UnitTestCase ...@@ -32,4 +59,47 @@ class TestNotice extends WP_UnitTestCase
$notice->showNotice(); $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());
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment