Commit 7fb61365 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Aide de vue Messenger : tests unitaires + améliorations suite à ces tests.

parent fbacb22c
......@@ -26,7 +26,7 @@ class Messenger extends AbstractHelper
self::WARNING => array('warning', 'warning-sign'),
self::ERROR => array('error', 'exclamation-sign'),
);
/**
* Messages courants
* @var array
......@@ -38,7 +38,11 @@ class Messenger extends AbstractHelper
* @var string
*/
protected $severity = null;
/**
* @var string
*/
protected $containerInnerTemplate = '%s';
/**
* Helper entry point.
......@@ -54,29 +58,6 @@ class Messenger extends AbstractHelper
return $this;
}
/**
* Importe les messages du FlashMessenger.
*
* @return self
*/
protected function importFlashMessages()
{
/* @var $fm \Zend\View\Helper\FlashMessenger */
$fm = $this->getView()->flashMessenger();
foreach ($fm->getInfoMessages() as $message) {
$this->addMessage($message, self::INFO);
}
foreach ($fm->getErrorMessages() as $message) {
$this->addMessage($message, self::ERROR);
}
foreach ($fm->getSuccessMessages() as $message) {
$this->addMessage($message, self::SUCCESS);
}
return $this;
}
/**
* Retourne le code HTML généré par cette aide de vue.
......@@ -109,33 +90,57 @@ class Messenger extends AbstractHelper
$messages = (array)$this->getMessages();
$out = '';
if ($this->severity && isset($messages[$this->severity])) {
// on ne conserve que la sévérité éventuellement demandée
$messages = array($this->severity => $messages[$this->severity]);
}
// if ($this->severity && isset($messages[$this->severity])) {
// // on ne conserve que la sévérité éventuellement demandée
// $messages = array($this->severity => $messages[$this->severity]);
// }
foreach ($messages as $key => $message) {
if (is_array($message)) {
if ($this->severity && isset($message[$this->severity])) {
// on ne conserve que la sévérité éventuellement demandée
$message = array($this->severity => $message[$this->severity]);
}
// if (is_array($message)) {
// if ($this->severity && isset($message[$this->severity])) {
// // on ne conserve que la sévérité éventuellement demandée
// $message = array($this->severity => $message[$this->severity]);
// }
foreach ($message as $s => $m) {
$out .= sprintf(
$this->getTemplate(is_string($key) ? $key : (is_string($s) ? $s : 'info')),
implode('<br />', (array)$m)
);
}
}
else {
$s = is_string($key) ? $key : ($this->severity ? $this->severity : 'info');
if ($this->severity && $this->severity == $s)
$out .= sprintf($this->getTemplate($s), $message) . '<br />';
}
// }
// else {
// $s = is_string($key) ? $key : ($this->severity ? $this->severity : 'info');
//// if ($this->severity && $this->severity == $s) {
//// $out .= sprintf($this->getTemplate($s), $message) . '<br />';
//// }
// }
}
return $out;
}
/**
* Importe les messages du FlashMessenger.
*
* @return self
*/
protected function importFlashMessages()
{
/* @var $fm \Zend\View\Helper\FlashMessenger */
$fm = $this->getView()->getHelperPluginManager()->get('flashMessenger');
foreach ($fm->getInfoMessages() as $message) {
$this->addMessage($message, self::INFO);
}
foreach ($fm->getErrorMessages() as $message) {
$this->addMessage($message, self::ERROR);
}
foreach ($fm->getSuccessMessages() as $message) {
$this->addMessage($message, self::SUCCESS);
}
return $this;
}
/**
* Spécifie les messages courants (remplaçant les messages existants).
*
......@@ -224,29 +229,40 @@ class Messenger extends AbstractHelper
}
/**
* Retourne le motif utilisé pour générer le conteneur des messages à afficher.
* Retourne le motif utilisé pour générer le conteneur de chaque message à afficher.
*
* @param string $severity Ex: Messenger::INFO
* @param string $divId Dom id éventuel à utiliser pour la div englobante
* @param string $innerContainerTemplate Template du container éventuel
* contenant le message.
* @param string $containerId Dom id éventuel à utiliser pour la div englobante
* @return string
*/
public function getTemplate($severity, $divId = null, $innerContainerTemplate = '%s')
public function getTemplate($severity, $containerId = null)
{
if (!isset($this->uiClasses[$severity])) {
throw new Unicaen_Exception ("Sévérité inconnue: " . $severity);
}
$divId = $divId ? sprintf('id="%s"', $divId) : '';
$innerTemplate = $this->containerInnerTemplate ?: '%s';
$containerId = $containerId ? sprintf('id="%s"', $containerId) : '';
$template = <<<EOT
<p class="messenger alert alert-{$this->uiClasses[$severity][0]}" {$divId}>
<p class="messenger alert alert-{$this->uiClasses[$severity][0]}" {$containerId}>
<button type="button" class="close" data-dismiss="alert">&times;</button>
<!--<i class="icon-{$this->uiClasses[$severity][1]}"></i>-->
$innerContainerTemplate
$innerTemplate
</p>
EOT;
return $template . PHP_EOL;
}
/**
*
* @param string $containerInnerTemplate
* @return self
*/
public function setContainerInnerTemplate($containerInnerTemplate = '%s')
{
$this->containerInnerTemplate = $containerInnerTemplate;
return $this;
}
}
\ No newline at end of file
<?php
namespace UnicaenAppTest\View\Helper;
use UnicaenApp\View\Helper\Messenger;
/**
* Description of MessengerText
*
* @property Messenger $helper Description
* @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
*/
class MessengerText extends AbstractTest
{
protected $helperClass = 'UnicaenApp\View\Helper\Messenger';
protected $router;
protected $routeMatch;
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
parent::setUp();
}
public function testReturnsSelfWhenInvoked()
{
$this->assertSame($this->helper, $this->helper->__invoke());
}
public function getMessages()
{
$info = "Information message";
$success = "Success message";
$error = "Error message";
return array(
array(
'severity' => null,
'messages' => array(
'info' => $info,
'success' => $success,
'error' => $error),
'expected' => array(
'info' => array($info),
'success' => array($success),
'error' => array($error)),
),
array(
'severity' => null,
'messages' => $messages = array(
'info' => array($info, $info . ' n°2'),
'success' => array($success),
'error' => array($error)),
'expected' => $messages,
),
array(
'severity' => 'info',
'messages' => $info,
'expected' => array($info),
),
array(
'severity' => 'info',
'messages' => array($info, $info . ' n°2'),
'expected' => array($info, $info . ' n°2'),
),
array(
'severity' => 'info',
'messages' => array('info' => array($info, $info . ' n°2')),
'expected' => array($info, $info . ' n°2'),
),
array(
'severity' => 'info',
'messages' => array('info' => $info),
'expected' => array($info),
),
array(
'severity' => 'success',
'messages' => array('success' => $success),
'expected' => array($success),
),
array(
'severity' => 'error',
'messages' => array('error' => $error),
'expected' => array($error),
),
);
}
/**
* @dataProvider getMessages
*/
public function testThrowsExceptionIfNoTitleSpecified($severity, $messages, $expected)
{
$this->helper->setMessages($messages);
$this->assertTrue($this->helper->hasMessages($severity));
$this->assertEquals($expected, $this->helper->getMessages($severity));
}
public function testSettingUniqueMessageReplaceExistingMessages()
{
$this->helper->setMessages(array(
'info' => "Information message.",
'error' => "Error message."));
$this->helper->setMessage(array('info' => $message = "Unique information message."));
$this->assertEquals(array('info' => array($message)), $this->helper->getMessages());
}
public function testcanAddMessage()
{
$this->helper->setMessages(array(
'info' => "Information message.",
'error' => "Error message."));
$this->helper->addMessage("Success message.", 'success');
$expected = array(
'info' => array("Information message."),
'success' => array("Success message."),
'error' => array("Error message."));
$this->assertEquals($expected, $this->helper->getMessages());
$this->helper->addMessage("Another information message.");
$expected = array(
'info' => array("Information message.", "Another information message."),
'success' => array("Success message."),
'error' => array("Error message."));
$this->assertEquals($expected, $this->helper->getMessages());
}
public function testCanClearMessages()
{
$this->helper->setMessages(array(
'info' => "Information message.",
'success' => "Success message.",
'error' => "Error message."));
$this->helper->clearMessages('info');
$expected = array(
'info' => array(),
'success' => array("Success message."),
'error' => array("Error message."));
$this->assertEquals($expected, $this->helper->getMessages());
$this->helper->clearMessages();
$this->assertEmpty($this->helper->getMessages());
}
public function testCanImportFlashMessages()
{
$flashMessenger = $this->getMock('\Zend\View\Helper\FlashMessenger', array('getInfoMessages', 'getSuccessMessages', 'getErrorMessages'));
$this->helper->getView()->getHelperPluginManager()->setService('flashMessenger', $flashMessenger);
$flashMessenger->expects($this->any())
->method('getInfoMessages')
->will($this->returnValue(array("Information message from FlashMessenger.")));
$flashMessenger->expects($this->any())
->method('getSuccessMessages')
->will($this->returnValue(array("Success message from FlashMessenger.")));
$flashMessenger->expects($this->any())
->method('getErrorMessages')
->will($this->returnValue(array("Error message from FlashMessenger.")));
$this->helper->setMessages(array(
'info' => "Information message.",
'success' => "Success message.",
'error' => "Error message."));
$this->helper->__invoke(true);
$expected = array(
'info' => array("Information message.", "Information message from FlashMessenger."),
'success' => array("Success message.", "Success message from FlashMessenger."),
'error' => array("Error message.", "Error message from FlashMessenger."));
$this->assertEquals($expected, $this->helper->getMessages());
}
public function testRenderingReturnsEmptyStringIfNoMessageSpecified()
{
$this->assertEquals('', "" . $this->helper);
}
public function getMessagesAndExpectedScript()
{
return array(
'one-info' => array(
"Information message.",
'messenger/one-info.phtml'
),
'two-errors' => array(
array(
'error' => array("Error message.", "Another error message.")),
'messenger/two-errors.phtml'
),
'all' => array(
array(
'info' => "Information message.",
'success' => "Success message.",
'error' => "Error message."),
'messenger/all.phtml'
),
);
}
/**
* @dataProvider getMessagesAndExpectedScript
*/
public function testRenderingReturnsCorrectMarkup($messages, $expectedScript)
{
$this->helper->setMessages($messages);
$markup = (string) $this->helper;
$this->assertEquals($this->getExpected($expectedScript), $markup);
}
public function testCanCustomizeMarkupTemplate()
{
$this->helper->addMessage("Information message.")
->setContainerInnerTemplate('<em>%s</em>');
$markup = (string) $this->helper;
$this->assertEquals($this->getExpected('messenger/custom.phtml'), $markup);
}
}
\ No newline at end of file
<p class="messenger alert alert-info" >
<button type="button" class="close" data-dismiss="alert">&times;</button>
<!--<i class="icon-info-sign"></i>-->
Information message.
</p>
<p class="messenger alert alert-success" >
<button type="button" class="close" data-dismiss="alert">&times;</button>
<!--<i class="icon-ok-sign"></i>-->
Success message.
</p>
<p class="messenger alert alert-error" >
<button type="button" class="close" data-dismiss="alert">&times;</button>
<!--<i class="icon-exclamation-sign"></i>-->
Error message.
</p>
<p class="messenger alert alert-info" >
<button type="button" class="close" data-dismiss="alert">&times;</button>
<!--<i class="icon-info-sign"></i>-->
<em>Information message.</em>
</p>
<p class="messenger alert alert-info" >
<button type="button" class="close" data-dismiss="alert">&times;</button>
<!--<i class="icon-info-sign"></i>-->
Information message.
</p>
<p class="messenger alert alert-error" >
<button type="button" class="close" data-dismiss="alert">&times;</button>
<!--<i class="icon-exclamation-sign"></i>-->
Error message.
</p>
<p class="messenger alert alert-error" >
<button type="button" class="close" data-dismiss="alert">&times;</button>
<!--<i class="icon-exclamation-sign"></i>-->
Another error message.
</p>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment