-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2906332 Refactor message handeling method
- Loading branch information
1 parent
7507595
commit 958f297
Showing
5 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
modules/checkout/src/Plugin/Commerce/CheckoutPane/CompletionMessages.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
namespace Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane; | ||
|
||
use Drupal\Core\StringTranslation\TranslatableMarkup; | ||
use Drupal\Core\TypedData\TranslatableInterface; | ||
|
||
/** | ||
* Acts as a container to collect all completion messages. | ||
* | ||
* @package Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane | ||
*/ | ||
class CompletionMessages implements \Iterator, \Countable { | ||
|
||
/** | ||
* @var \Drupal\Core\TypedData\TranslatableInterface[] | ||
*/ | ||
private $messages; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $position; | ||
|
||
/** | ||
* Sets up the position to 0. | ||
*/ | ||
public function __construct() { | ||
$this->position = 0; | ||
} | ||
|
||
/** | ||
* Return the current element | ||
* | ||
* @link http://php.net/manual/en/iterator.current.php | ||
* @return mixed Can return any type. | ||
* @since 5.0.0 | ||
*/ | ||
public function current() { | ||
return $this->messages[$this->position]; | ||
} | ||
|
||
/** | ||
* Move forward to next element | ||
* | ||
* @link http://php.net/manual/en/iterator.next.php | ||
* @return void Any returned value is ignored. | ||
* @since 5.0.0 | ||
*/ | ||
public function next() { | ||
++$this->position; | ||
} | ||
|
||
/** | ||
* Return the key of the current element | ||
* | ||
* @link http://php.net/manual/en/iterator.key.php | ||
* @return mixed scalar on success, or null on failure. | ||
* @since 5.0.0 | ||
*/ | ||
public function key() { | ||
return $this->position; | ||
} | ||
|
||
/** | ||
* Checks if current position is valid | ||
* | ||
* @link http://php.net/manual/en/iterator.valid.php | ||
* @return boolean The return value will be casted to boolean and then | ||
* evaluated. Returns true on success or false on failure. | ||
* @since 5.0.0 | ||
*/ | ||
public function valid() { | ||
return isset($this->messages[$this->position]); | ||
} | ||
|
||
/** | ||
* Rewind the Iterator to the first element | ||
* | ||
* @link http://php.net/manual/en/iterator.rewind.php | ||
* @return void Any returned value is ignored. | ||
* @since 5.0.0 | ||
*/ | ||
public function rewind() { | ||
$this->position = 0; | ||
} | ||
|
||
/** | ||
* Adds a message to the array. | ||
* | ||
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $message | ||
*/ | ||
public function addMessage(TranslatableMarkup $message) { | ||
$this->messages[] = $message; | ||
} | ||
|
||
/** | ||
* Count elements of an object | ||
* | ||
* @link http://php.net/manual/en/countable.count.php | ||
* @return int The custom count as an integer. | ||
* </p> | ||
* <p> | ||
* The return value is cast to an integer. | ||
* @since 5.1.0 | ||
*/ | ||
public function count() { | ||
return count($this->messages); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
modules/checkout/tests/src/Kernel/CompletionMessagesTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: robharings | ||
* Date: 09/09/2017 | ||
* Time: 13:31 | ||
*/ | ||
|
||
namespace Drupal\Tests\commerce_checkout\Kernel; | ||
|
||
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CompletionMessages; | ||
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase; | ||
|
||
/** | ||
* Tests the completion messages class. | ||
* | ||
* @covers \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CompletionMessages | ||
*/ | ||
class CompletionMessagesTest extends CommerceKernelTestBase { | ||
|
||
/** | ||
* @var CompletionMessages | ||
*/ | ||
private $completionMessages; | ||
|
||
public function setUp() { | ||
parent::setUp(); | ||
$this->completionMessages = new CompletionMessages(); | ||
} | ||
|
||
public function testAddMessage() { | ||
$this->completionMessages->addMessage(t('Message 1')); | ||
$this->completionMessages->addMessage(t('Message 2')); | ||
|
||
$this->assertCount(2, $this->completionMessages); | ||
} | ||
|
||
public function testMessagesIterator() { | ||
$this->completionMessages->addMessage(t('Message 1')); | ||
$this->completionMessages->addMessage(t('Message 2')); | ||
|
||
$this->assertEquals('Message 1', $this->completionMessages->current()->render()); | ||
$this->completionMessages->next(); | ||
$this->assertEquals('Message 2', $this->completionMessages->current()->render()); | ||
} | ||
|
||
} |