Commit 1430346c authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Aide de vue Messenger : ajout de la possibilité de spécifier le namespace...

Aide de vue Messenger : ajout de la possibilité de spécifier le namespace 'xxxxx/*' lors de l'importation des messages du FlashMessenger ; test unitaires.
parent 3b502067
......@@ -14,7 +14,7 @@ use Zend\View\Renderer\PhpRenderer;
* et de générer le code HTML pour les afficher (affublés d'un icône correspondant à leur sévérité).
*
* Possibilité d'importer les messages du FlashMessenger pour les mettre en forme de la même manière
* (avec support du namespace).
* (avec support du namespace, ex: 'these/info' ou 'these/*').
*
* @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
*/
......@@ -149,7 +149,7 @@ class Messenger extends AbstractHelper implements MessageAwareInterface
/**
* Importe les messages n-1 du FlashMessenger.
*
* @param string $namespace
* @param string $namespace Facultatif, ex: 'these/danger' ou 'these/*'
* @return static
*/
protected function importFlashMessages($namespace = null)
......@@ -157,16 +157,12 @@ class Messenger extends AbstractHelper implements MessageAwareInterface
$fm = $this->getPluginFlashMessenger();
if ($namespace) {
// $namespace peut être de forme 'namespace/severity' (ex: 'these/danger')
if (($pos = strrpos($namespace, $sep = self::NAMESPACED_SEVERITY_SEPARATOR)) !== false) {
$parts = explode($sep, $namespace);
$severity = array_pop($parts);
}
else {
$severity = null;
}
foreach ($fm->getMessagesFromNamespace($namespace) as $message) {
$this->addMessage($message, $severity);
$array = $this->extractSeveritiesAndNamespaces($namespace);
foreach ($array as $sev => $ns) {
foreach ($fm->getMessagesFromNamespace($ns) as $message) {
$this->addMessage($message, $sev);
}
}
}
else {
......@@ -190,7 +186,7 @@ class Messenger extends AbstractHelper implements MessageAwareInterface
/**
* Importe les messages courants du FlashMessenger.
*
* @param string $namespace
* @param string $namespace Facultatif, ex: 'these/danger' ou 'these/*'
* @return static
*/
protected function importCurrentFlashMessages($namespace = null)
......@@ -198,13 +194,16 @@ class Messenger extends AbstractHelper implements MessageAwareInterface
$fm = $this->getPluginFlashMessenger();
if ($namespace) {
$severity = $this->extractSeverityFromNamespace($namespace);
foreach ($fm->getCurrentMessagesFromNamespace($namespace) as $message) {
$this->addMessage($message, $severity);
}
$array = $this->extractSeveritiesAndNamespaces($namespace);
/* Si on importe alors on nettoie pour éviter un deuxième affichage */
$fm->clearCurrentMessagesFromNamespace($namespace);
foreach ($array as $sev => $ns) {
foreach ($fm->getCurrentMessagesFromNamespace($ns) as $message) {
$this->addMessage($message, $sev);
}
/* Si on importe alors on nettoie pour éviter un deuxième affichage */
$fm->clearCurrentMessagesFromNamespace($ns);
}
}
else {
foreach ($fm->getCurrentErrorMessages() as $message) {
......@@ -228,22 +227,38 @@ class Messenger extends AbstractHelper implements MessageAwareInterface
}
/**
* Si $namespace est de la forme 'namespace/severity', on retourne la sévérité, sinon null.
*
* @param string $namespace ex: 'these/danger'
* @return string|null ex: 'danger'
* @param string $namespace Ex: 'these/danger' ou 'these/*'
* @return array 'severity' => 'namespace'
*/
private function extractSeverityFromNamespace($namespace)
private function extractSeveritiesAndNamespaces($namespace)
{
// $namespace peut être de forme 'namespace/severity' (ex: 'these/danger')
// $namespace peut être de la forme 'namespace/severity' (ex: 'these/danger')
if (($pos = strrpos($namespace, $sep = self::NAMESPACED_SEVERITY_SEPARATOR)) !== false) {
$severity = array_pop(explode($sep, $namespace));
$parts = explode($sep, $namespace);
$severity = array_pop($parts);
// si $namespace est de la forme 'namespace/*' (ex: 'these/*'), cela revient à importer les namespaces
// 'namespace/danger', 'namespace/success', 'namespace/info' et 'namespace/warning'
if ($severity === '*') {
$namespacePrefix = implode(self::NAMESPACED_SEVERITY_SEPARATOR, $parts) . '/';
$array = [
$s = self::ERROR => $namespacePrefix . $s,
$s = self::SUCCESS => $namespacePrefix . $s,
$s = self::INFO => $namespacePrefix . $s,
$s = self::WARNING => $namespacePrefix . $s,
];
} else {
$array = [
$severity => $namespace,
];
}
}
else {
$severity = null;
$array = [
self::INFO => $namespace,
];
}
return $severity;
return $array;
}
/**
......
......@@ -234,69 +234,26 @@ class MessengerText extends AbstractTest
$this->assertEmpty($this->helper->getMessages());
}
/**
* @param $getInfoMessagesMethod
* @param $getSuccessMessages
* @param $getErrorMessages
* @return mixed
* @deprecated
*/
private function createFlashMessengerMock($getInfoMessagesMethod, $getSuccessMessages, $getErrorMessages)
{
$flashMessengerPlugin = $this->createPartialMock(
FlashMessengerPlugin::class,
[$getInfoMessagesMethod, $getSuccessMessages, $getErrorMessages]
);
$flashMessengerPlugin
->expects($this->once())
->method($getInfoMessagesMethod)
->will($this->returnValue(["Information message from FlashMessenger."]));
$flashMessengerPlugin
->expects($this->once())
->method($getSuccessMessages)
->will($this->returnValue(["Success message from FlashMessenger."]));
$flashMessengerPlugin
->expects($this->once())
->method($getErrorMessages)
->will($this->returnValue(["Error message from FlashMessenger."]));
$flashMessengerViewHelper = $this->createPartialMock(
FlashMessengerViewHelper::class,
['getPluginFlashMessenger']
);
$flashMessengerViewHelper->expects($this->once())
->method('getPluginFlashMessenger')
->will($this->returnValue($flashMessengerPlugin));
return $flashMessengerViewHelper;
}
public function testCanReplaceMessagesWithImportedFlashMessages()
public function testCanImportMessagesFromFlashMessenger()
{
$flashMessengerPlugin = $this->createPartialMock(
FlashMessengerPlugin::class,
['getInfoMessages', 'getSuccessMessages', 'getErrorMessages']
);
$flashMessengerPlugin
->expects($this->once())
->expects($this->exactly(2))
->method('getInfoMessages')
->will($this->returnValue(["Information message from FlashMessenger."]));
$flashMessengerPlugin
->expects($this->once())
->expects($this->exactly(2))
->method('getSuccessMessages')
->will($this->returnValue(["Success message from FlashMessenger."]));
$flashMessengerPlugin
->expects($this->once())
->expects($this->exactly(2))
->method('getErrorMessages')
->will($this->returnValue(["Error message from FlashMessenger."]));
$flashMessengerViewHelper = $this->createPartialMock(
FlashMessengerViewHelper::class,
['getPluginFlashMessenger']
);
$flashMessengerViewHelper->expects($this->once())
->method('getPluginFlashMessenger')
->will($this->returnValue($flashMessengerPlugin));
$flashMessengerViewHelper = $this->createMockForFlashMessengerViewHelper($flashMessengerPlugin);
/** @var PhpRenderer $view */
$view = $this->helper->getView();
......@@ -308,81 +265,41 @@ class MessengerText extends AbstractTest
'danger' => "Error message."
]);
// replaces
$this->helper->setMessagesFromFlashMessenger();
// appends
$this->helper->addMessagesFromFlashMessenger();
$expected = [
'info' => [
"Information message.",
"Information message from FlashMessenger."
],
'success' => [
"Success message.",
"Success message from FlashMessenger."
],
'danger' => [
"Error message.",
"Error message from FlashMessenger."
]
];
$this->assertEquals($expected, $this->helper->getMessages());
}
public function testCanAppendImportedFlashMessages()
{
$flashMessengerPlugin = $this->createPartialMock(
FlashMessengerPlugin::class,
['getInfoMessages', 'getSuccessMessages', 'getErrorMessages']
);
$flashMessengerPlugin
->expects($this->once())
->method('getInfoMessages')
->will($this->returnValue(["Information message from FlashMessenger."]));
$flashMessengerPlugin
->expects($this->once())
->method('getSuccessMessages')
->will($this->returnValue(["Success message from FlashMessenger."]));
$flashMessengerPlugin
->expects($this->once())
->method('getErrorMessages')
->will($this->returnValue(["Error message from FlashMessenger."]));
$flashMessengerViewHelper = $this->createPartialMock(
FlashMessengerViewHelper::class,
['getPluginFlashMessenger']
);
$flashMessengerViewHelper->expects($this->once())
->method('getPluginFlashMessenger')
->will($this->returnValue($flashMessengerPlugin));
/** @var PhpRenderer $view */
$view = $this->helper->getView();
$view->getHelperPluginManager()->setService('flashMessenger', $flashMessengerViewHelper);
$this->helper->setMessages([
'info' => "Information message.",
'success' => "Success message.",
'danger' => "Error message."
]);
// appends
$this->helper->addMessagesFromFlashMessenger();
// replaces
$this->helper->setMessagesFromFlashMessenger();
$expected = [
'info' => [
"Information message.",
"Information message from FlashMessenger."
],
'success' => [
"Success message.",
"Success message from FlashMessenger."
],
'danger' => [
"Error message.",
"Error message from FlashMessenger."
]
];
$this->assertEquals($expected, $this->helper->getMessages());
}
public function testCanAppendImportedFlashMessagesWithNamespace()
public function testCanImportMessagesFromFlashMessengerWithPreciseNamespace()
{
$namespace = 'these/info';
......@@ -391,18 +308,12 @@ class MessengerText extends AbstractTest
['getMessagesFromNamespace']
);
$flashMessengerPlugin
->expects($this->once())
->expects($this->exactly(2))
->method('getMessagesFromNamespace')
->with($namespace)
->will($this->returnValue(["Information message from FlashMessenger namespace."]));
$flashMessengerViewHelper = $this->createPartialMock(
FlashMessengerViewHelper::class,
['getPluginFlashMessenger']
);
$flashMessengerViewHelper->expects($this->once())
->method('getPluginFlashMessenger')
->will($this->returnValue($flashMessengerPlugin));
$flashMessengerViewHelper = $this->createMockForFlashMessengerViewHelper($flashMessengerPlugin);
/** @var PhpRenderer $view */
$view = $this->helper->getView();
......@@ -414,9 +325,10 @@ class MessengerText extends AbstractTest
'danger' => "Error message."
]);
// appends
/**
* appends
*/
$this->helper->addMessagesFromFlashMessenger($namespace);
$expected = [
'info' => [
"Information message.",
......@@ -430,34 +342,36 @@ class MessengerText extends AbstractTest
]
];
$this->assertEquals($expected, $this->helper->getMessages());
// replaces
$this->helper->setMessagesFromFlashMessenger($namespace);
$expected = [
'info' => [
"Information message from FlashMessenger namespace."
],
];
$this->assertEquals($expected, $this->helper->getMessages());
}
public function testCanReplaceMessagesWithImportedFlashCurrentMessages()
public function testCanImportMessagesFromFlashMessengerWithWildcardedNamespace()
{
$wildcardedNamespace = 'these/*';
$flashMessengerPlugin = $this->createPartialMock(
FlashMessengerPlugin::class,
['getCurrentInfoMessages', 'getCurrentSuccessMessages', 'getCurrentErrorMessages']
['getMessagesFromNamespace']
);
$flashMessengerPlugin
->expects($this->once())
->method('getCurrentInfoMessages')
->will($this->returnValue(["Information message from FlashMessenger."]));
$flashMessengerPlugin
->expects($this->once())
->method('getCurrentSuccessMessages')
->will($this->returnValue(["Success message from FlashMessenger."]));
$flashMessengerPlugin
->expects($this->once())
->method('getCurrentErrorMessages')
->will($this->returnValue(["Error message from FlashMessenger."]));
->expects($this->exactly(8))
->method('getMessagesFromNamespace')
->will($this->returnValueMap([
['these/danger', ["Message from FlashMessenger namespace these/danger"]],
['these/success', ["Message from FlashMessenger namespace these/success"]],
['these/info', ["Message from FlashMessenger namespace these/info"]],
['these/warning', ["Message from FlashMessenger namespace these/warning"]],
]));
$flashMessengerViewHelper = $this->createPartialMock(
FlashMessengerViewHelper::class,
['getPluginFlashMessenger']
);
$flashMessengerViewHelper->expects($this->once())
->method('getPluginFlashMessenger')
->will($this->returnValue($flashMessengerPlugin));
$flashMessengerViewHelper = $this->createMockForFlashMessengerViewHelper($flashMessengerPlugin);
/** @var PhpRenderer $view */
$view = $this->helper->getView();
......@@ -469,49 +383,68 @@ class MessengerText extends AbstractTest
'danger' => "Error message."
]);
// replaces
$this->helper->setCurrentMessagesFromFlashMessenger();
/**
* appends
*/
$this->helper->addMessagesFromFlashMessenger($wildcardedNamespace);
$expected = [
'info' => [
"Information message from FlashMessenger."
'danger' => [
"Error message.",
"Message from FlashMessenger namespace these/danger"
],
'success' => [
"Success message from FlashMessenger."
"Success message.",
"Message from FlashMessenger namespace these/success"
],
'info' => [
"Information message.",
"Message from FlashMessenger namespace these/info"
],
'warning' => [
"Message from FlashMessenger namespace these/warning"
],
];
$this->assertEquals($expected, $this->helper->getMessages());
// replaces
$this->helper->setMessagesFromFlashMessenger($wildcardedNamespace);
$expected = [
'danger' => [
"Error message from FlashMessenger."
]
"Message from FlashMessenger namespace these/danger"
],
'success' => [
"Message from FlashMessenger namespace these/success"
],
'info' => [
"Message from FlashMessenger namespace these/info"
],
'warning' => [
"Message from FlashMessenger namespace these/warning"
],
];
$this->assertEquals($expected, $this->helper->getMessages());
}
public function testCanAppendImportedFlashCurrentMessages()
public function testCanImportCurrentMessagesFromFlashMessenger()
{
$flashMessengerPlugin = $this->createPartialMock(
FlashMessengerPlugin::class,
['getCurrentInfoMessages', 'getCurrentSuccessMessages', 'getCurrentErrorMessages']
);
$flashMessengerPlugin
->expects($this->once())
->expects($this->exactly(2))
->method('getCurrentInfoMessages')
->will($this->returnValue(["Information message from FlashMessenger."]));
$flashMessengerPlugin
->expects($this->once())
->expects($this->exactly(2))
->method('getCurrentSuccessMessages')
->will($this->returnValue(["Success message from FlashMessenger."]));
$flashMessengerPlugin
->expects($this->once())
->expects($this->exactly(2))
->method('getCurrentErrorMessages')
->will($this->returnValue(["Error message from FlashMessenger."]));
$flashMessengerViewHelper = $this->createPartialMock(
FlashMessengerViewHelper::class,
['getPluginFlashMessenger']
);
$flashMessengerViewHelper->expects($this->once())
->method('getPluginFlashMessenger')
->will($this->returnValue($flashMessengerPlugin));
$flashMessengerViewHelper = $this->createMockForFlashMessengerViewHelper($flashMessengerPlugin);
/** @var PhpRenderer $view */
$view = $this->helper->getView();
......@@ -523,9 +456,10 @@ class MessengerText extends AbstractTest
'danger' => "Error message."
]);
// appends
/**
* appends
*/
$this->helper->addCurrentMessagesFromFlashMessenger();
$expected = [
'info' => [
"Information message.",
......@@ -541,6 +475,21 @@ class MessengerText extends AbstractTest
]
];
$this->assertEquals($expected, $this->helper->getMessages());
// replaces
$this->helper->setCurrentMessagesFromFlashMessenger();
$expected = [
'info' => [
"Information message from FlashMessenger."
],
'success' => [
"Success message from FlashMessenger."
],
'danger' => [
"Error message from FlashMessenger."
]
];
$this->assertEquals($expected, $this->helper->getMessages());
}
public function testRenderingReturnsEmptyStringIfNoMessageSpecified()
......@@ -596,13 +545,7 @@ class MessengerText extends AbstractTest
->with($namespace)
->will($this->returnValue(["Error message from FlashMessenger namespace."]));
$flashMessengerViewHelper = $this->createPartialMock(
FlashMessengerViewHelper::class,
['getPluginFlashMessenger']
);
$flashMessengerViewHelper->expects($this->once())
->method('getPluginFlashMessenger')
->will($this->returnValue($flashMessengerPlugin));
$flashMessengerViewHelper = $this->createMockForFlashMessengerViewHelper($flashMessengerPlugin);
/** @var PhpRenderer $view */
$view = $this->helper->getView();
......@@ -612,7 +555,6 @@ class MessengerText extends AbstractTest
'danger' => "Error message."
]);
// appends
$this->helper->addMessagesFromFlashMessenger($namespace);
$markup = (string) $this->helper;
......@@ -634,4 +576,23 @@ class MessengerText extends AbstractTest
$markup = (string) $this->helper;
$this->assertEquals($this->getExpected('messenger/custom.phtml'), $markup);
}
/**
* @param $flashMessengerPlugin
* @return \PHPUnit_Framework_MockObject_MockObject
*/
private function createMockForFlashMessengerViewHelper($flashMessengerPlugin)
{
$flashMessengerViewHelper = $this->createPartialMock(
FlashMessengerViewHelper::class,
['getPluginFlashMessenger']
);
$flashMessengerViewHelper->expects($this->once())
->method('getPluginFlashMessenger')
->will($this->returnValue($flashMessengerPlugin));
return $flashMessengerViewHelper;
}
}
......@@ -13,7 +13,7 @@
<logging>
<!--<log type="junit" target="/tmp/phpunit/logs/junit.xml" />-->
<!--<log type="coverage-clover" target="/tmp/phpunit/logs/clover.xml" />-->
<log type="coverage-html" target="/tmp/phpunit/coverage" />
<!--<log type="coverage-html" target="/tmp/phpunit/coverage" />-->
</logging>
<filter>
......
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