Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/5759' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Mar 5, 2014
168 parents a84ddbd + 415c61b + 1514822 + d9d9b78 + fefc773 + 83cbc8a + 1b9fd60 + 8a8b989 + 10da323 + e6aba5a + b71d0a2 + 2aa926c + 8405746 + bf1a8de + f16339d + ed1dbc8 + dbf56da + bdb277e + 364ced3 + 9fccdf3 + 40f5963 + 9d7ddea + e1e4aa8 + 870703b + 958decf + e49a08e + a051e7e + b9f6e35 + 2bbef72 + aea30ce + faec0a2 + 098e166 + 51d6372 + 8757ace + 42477b6 + 32cf329 + 0ebadc5 + 188fa4d + a41bbf0 + a29bbbc + 941280d + d691226 + e547023 + d91c234 + 006ef75 + 69d1518 + 7dc0d1a + a995715 + e780008 + e810876 + 9e4090a + 269bf01 + 3a1f2fd + 0cab32d + 704cc7f + e20c9e6 + a0dfc1a + 76b3e1c + fd429e8 + 187ea79 + 2b43ea6 + e3b1be1 + 19f0ef6 + 33bf9c0 + 3729984 + a5ce396 + 698dbe0 + 0efca0b + 08b2a78 + ddaa846 + 7fe4493 + 0da7331 + 7226b1d + 851904c + a2eb795 + f658a03 + de83270 + 806df8a + 4e7ff23 + 409b768 + 1b97191 + d2649e3 + f0162d1 + 6f01416 + a2b3753 + 1786961 + d157fcb + 4444c37 + 192d20c + 811122b + 3a2cf9b + eb2029b + 7a6edab + 8d8a05d + c1ddf21 + 0090b4d + ef80e35 + 59b30de + 4656098 + 377b920 + ccba82c + 5d2770e + 41714a1 + 8adef43 + a78628f + b35fa7a + 3953c79 + aa28e42 + 38f9a49 + 47ed633 + 0a6bf6e + b61d89d + a1fbb6f + 238512b + f40a328 + 62dc143 + 328df3b + e12fe2d + e34a942 + 04e956d + a19a8c2 + ac4b6c0 + d28282e + bd5af3d + b976cb5 + 4feb67b + 7164be7 + 40174df + e4918ae + 1d9f9a2 + c77ae57 + 223b881 + c2f4e25 + e08b166 + e58e548 + 85e6bbd + 0ac2052 + c7ba6af + f443c57 + 1380626 + 5316b6e + a6136d4 + d233be3 + c100a2a + c6a0e2c + e7eef3c + 812f35d + 604cdcd + 5472285 + 6e2f420 + 76c4d1c + c60cca9 + 77f718e + 6bc8c90 + 6ec1d0f + 53d9070 + a67cdeb + 5d412ee + 132380c + 2809ef0 + 45fb937 + 00c66d0 + 0b0c3aa + 519c239 + f7ab865 + 6bfd1a6 + e2b5af2 + 701cbff commit 77607e1
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/Helper/FlashMessenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,32 @@ public function render($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, arr
{
$flashMessenger = $this->getPluginFlashMessenger();
$messages = $flashMessenger->getMessagesFromNamespace($namespace);
return $this->renderMessages($namespace, $messages, $classes);
}

if (empty($messages)) {
return '';
}
/**
* Render Current Messages
*
* @param string $namespace
* @param array $classes
* @return string
*/
public function renderCurrent($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, array $classes = array())
{
$flashMessenger = $this->getPluginFlashMessenger();
$messages = $flashMessenger->getCurrentMessagesFromNamespace($namespace);
return $this->renderMessages($namespace, $messages, $classes);
}

/**
* Render Messages
*
* @param array $messages
* @param array $classes
* @return string
*/
protected function renderMessages($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, array $messages = array(), array $classes = array())
{
// Prepare classes for opening tag
if (empty($classes)) {
if (isset($this->classMessages[$namespace])) {
Expand All @@ -116,14 +137,11 @@ public function render($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, arr
}
$classes = array($classes);
}

// Flatten message array
$escapeHtml = $this->getEscapeHtmlHelper();
$messagesToPrint = array();

$translator = $this->getTranslator();
$translatorTextDomain = $this->getTranslatorTextDomain();

array_walk_recursive($messages, function ($item) use (&$messagesToPrint, $escapeHtml, $translator, $translatorTextDomain) {
if ($translator !== null) {
$item = $translator->translate(
Expand All @@ -133,16 +151,13 @@ public function render($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, arr
}
$messagesToPrint[] = $escapeHtml($item);
});

if (empty($messagesToPrint)) {
return '';
}

// Generate markup
$markup = sprintf($this->getMessageOpenFormat(), ' class="' . implode(' ', $classes) . '"');
$markup .= implode(sprintf($this->getMessageSeparatorString(), ' class="' . implode(' ', $classes) . '"'), $messagesToPrint);
$markup .= $this->getMessageCloseString();

return $markup;
}

Expand Down
193 changes: 193 additions & 0 deletions test/Helper/FlashMessengerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ public function seedMessages()
unset($helper);
}

public function seedCurrentMessages()
{
$helper = new FlashMessenger();
$helper->setSessionManager($this->session);
$helper->addMessage('foo');
$helper->addMessage('bar');
$helper->addInfoMessage('bar-info');
$helper->addSuccessMessage('bar-success');
$helper->addErrorMessage('bar-error');
}

public function testCanAssertPluginClass()
{
$this->assertEquals(
Expand Down Expand Up @@ -88,6 +99,28 @@ public function testCanRetrieveMessages()
$this->assertTrue($this->plugin->hasErrorMessages());
}

public function testCanRetrieveCurrentMessages()
{
$helper = $this->helper;

$this->assertFalse($helper()->hasCurrentMessages());
$this->assertFalse($helper()->hasCurrentInfoMessages());
$this->assertFalse($helper()->hasCurrentSuccessMessages());
$this->assertFalse($helper()->hasCurrentErrorMessages());

$this->seedCurrentMessages();

$this->assertTrue(count($helper('default')) > 0);
$this->assertTrue(count($helper('info')) > 0);
$this->assertTrue(count($helper('success')) > 0);
$this->assertTrue(count($helper('error')) > 0);

$this->assertFalse($this->plugin->hasCurrentMessages());
$this->assertFalse($this->plugin->hasCurrentInfoMessages());
$this->assertFalse($this->plugin->hasCurrentSuccessMessages());
$this->assertFalse($this->plugin->hasCurrentErrorMessages());
}

public function testCanProxyAndRetrieveMessagesFromPluginController()
{
$this->assertFalse($this->helper->hasMessages());
Expand All @@ -105,6 +138,21 @@ public function testCanProxyAndRetrieveMessagesFromPluginController()
$this->assertTrue($this->helper->hasErrorMessages());
}

public function testCanProxyAndRetrieveCurrentMessagesFromPluginController()
{
$this->assertFalse($this->helper->hasCurrentMessages());
$this->assertFalse($this->helper->hasCurrentInfoMessages());
$this->assertFalse($this->helper->hasCurrentSuccessMessages());
$this->assertFalse($this->helper->hasCurrentErrorMessages());

$this->seedCurrentMessages();

$this->assertTrue($this->helper->hasCurrentMessages());
$this->assertTrue($this->helper->hasCurrentInfoMessages());
$this->assertTrue($this->helper->hasCurrentSuccessMessages());
$this->assertTrue($this->helper->hasCurrentErrorMessages());
}

public function testCanDisplayListOfMessages()
{
$displayInfoAssertion = '';
Expand All @@ -118,6 +166,19 @@ public function testCanDisplayListOfMessages()
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfCurrentMessages()
{
$displayInfoAssertion = '';
$displayInfo = $this->helper->renderCurrent(PluginFlashMessenger::NAMESPACE_INFO);
$this->assertEquals($displayInfoAssertion, $displayInfo);

$this->seedCurrentMessages();

$displayInfoAssertion = '<ul class="info"><li>bar-info</li></ul>';
$displayInfo = $this->helper->renderCurrent(PluginFlashMessenger::NAMESPACE_INFO);
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfMessagesByDefaultParameters()
{
$helper = $this->helper;
Expand All @@ -128,6 +189,16 @@ public function testCanDisplayListOfMessagesByDefaultParameters()
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfMessagesByDefaultCurrentParameters()
{
$helper = $this->helper;
$this->seedCurrentMessages();

$displayInfoAssertion = '<ul class="default"><li>foo</li><li>bar</li></ul>';
$displayInfo = $helper()->renderCurrent();
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfMessagesByInvoke()
{
$helper = $this->helper;
Expand All @@ -138,6 +209,16 @@ public function testCanDisplayListOfMessagesByInvoke()
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfCurrentMessagesByInvoke()
{
$helper = $this->helper;
$this->seedCurrentMessages();

$displayInfoAssertion = '<ul class="info"><li>bar-info</li></ul>';
$displayInfo = $helper()->renderCurrent(PluginFlashMessenger::NAMESPACE_INFO);
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfMessagesCustomised()
{
$this->seedMessages();
Expand All @@ -151,6 +232,19 @@ public function testCanDisplayListOfMessagesCustomised()
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfCurrentMessagesCustomised()
{
$this->seedCurrentMessages();

$displayInfoAssertion = '<div class="foo-baz foo-bar"><p>bar-info</p></div>';
$displayInfo = $this->helper
->setMessageOpenFormat('<div%s><p>')
->setMessageSeparatorString('</p><p>')
->setMessageCloseString('</p></div>')
->renderCurrent(PluginFlashMessenger::NAMESPACE_INFO, array('foo-baz', 'foo-bar'));
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfMessagesCustomisedSeparator()
{
$this->seedMessages();
Expand All @@ -164,6 +258,19 @@ public function testCanDisplayListOfMessagesCustomisedSeparator()
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfCurrentMessagesCustomisedSeparator()
{
$this->seedCurrentMessages();

$displayInfoAssertion = '<div><p class="foo-baz foo-bar">foo</p><p class="foo-baz foo-bar">bar</p></div>';
$displayInfo = $this->helper
->setMessageOpenFormat('<div><p%s>')
->setMessageSeparatorString('</p><p%s>')
->setMessageCloseString('</p></div>')
->renderCurrent(PluginFlashMessenger::NAMESPACE_DEFAULT, array('foo-baz', 'foo-bar'));
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfMessagesCustomisedByConfig()
{
$this->seedMessages();
Expand Down Expand Up @@ -199,6 +306,40 @@ public function testCanDisplayListOfMessagesCustomisedByConfig()
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfCurrentMessagesCustomisedByConfig()
{
$this->seedCurrentMessages();
$config = array(
'view_helper_config' => array(
'flashmessenger' => array(
'message_open_format' => '<div%s><ul><li>',
'message_separator_string' => '</li><li>',
'message_close_string' => '</li></ul></div>',
),
),
);
$sm = new ServiceManager();
$sm->setService('Config', $config);
$helperPluginManager = new HelperPluginManager(new Config(array(
'factories' => array(
'flashmessenger' => 'Zend\View\Helper\Service\FlashMessengerFactory',
),
)));
$controllerPluginManager = new PluginManager(new Config(array(
'invokables' => array(
'flashmessenger' => 'Zend\Mvc\Controller\Plugin\FlashMessenger',
),
)));
$helperPluginManager->setServiceLocator($sm);
$controllerPluginManager->setServiceLocator($sm);
$sm->setService('ControllerPluginManager', $controllerPluginManager);
$helper = $helperPluginManager->get('flashmessenger');

$displayInfoAssertion = '<div class="info"><ul><li>bar-info</li></ul></div>';
$displayInfo = $helper->renderCurrent(PluginFlashMessenger::NAMESPACE_INFO);
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfMessagesCustomisedByConfigSeparator()
{
$this->seedMessages();
Expand Down Expand Up @@ -234,6 +375,41 @@ public function testCanDisplayListOfMessagesCustomisedByConfigSeparator()
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfCurrentMessagesCustomisedByConfigSeparator()
{
$this->seedCurrentMessages();

$config = array(
'view_helper_config' => array(
'flashmessenger' => array(
'message_open_format' => '<div><ul><li%s>',
'message_separator_string' => '</li><li%s>',
'message_close_string' => '</li></ul></div>',
),
),
);
$sm = new ServiceManager();
$sm->setService('Config', $config);
$helperPluginManager = new HelperPluginManager(new Config(array(
'factories' => array(
'flashmessenger' => 'Zend\View\Helper\Service\FlashMessengerFactory',
),
)));
$controllerPluginManager = new PluginManager(new Config(array(
'invokables' => array(
'flashmessenger' => 'Zend\Mvc\Controller\Plugin\FlashMessenger',
),
)));
$helperPluginManager->setServiceLocator($sm);
$controllerPluginManager->setServiceLocator($sm);
$sm->setService('ControllerPluginManager', $controllerPluginManager);
$helper = $helperPluginManager->get('flashmessenger');

$displayInfoAssertion = '<div><ul><li class="foo-baz foo-bar">foo</li><li class="foo-baz foo-bar">bar</li></ul></div>';
$displayInfo = $helper->renderCurrent(PluginFlashMessenger::NAMESPACE_DEFAULT, array('foo-baz', 'foo-bar'));
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanTranslateMessages()
{
$mockTranslator = $this->getMock('Zend\I18n\Translator\Translator');
Expand All @@ -250,4 +426,21 @@ public function testCanTranslateMessages()
$display = $this->helper->render(PluginFlashMessenger::NAMESPACE_INFO);
$this->assertEquals($displayAssertion, $display);
}

public function testCanTranslateCurrentMessages()
{
$mockTranslator = $this->getMock('Zend\I18n\Translator\Translator');
$mockTranslator->expects($this->exactly(1))
->method('translate')
->will($this->returnValue('translated message'));

$this->helper->setTranslator($mockTranslator);
$this->assertTrue($this->helper->hasTranslator());

$this->seedCurrentMessages();

$displayAssertion = '<ul class="info"><li>translated message</li></ul>';
$display = $this->helper->renderCurrent(PluginFlashMessenger::NAMESPACE_INFO);
$this->assertEquals($displayAssertion, $display);
}
}

0 comments on commit 77607e1

Please sign in to comment.