From 131939a48f06a1066eeac2e37358a1f62c6bcf1a Mon Sep 17 00:00:00 2001 From: Sasha Alex Romanenko Date: Sun, 30 Oct 2016 22:32:04 -0400 Subject: [PATCH 1/6] Port FlashMessenger view helper to be part of MVC plugin. --- composer.json | 9 +- composer.lock | 160 +++++-- src/Module.php | 14 + src/View/Helper/FlashMessenger.php | 345 ++++++++++++++ src/View/Helper/FlashMessengerFactory.php | 67 +++ test/View/Helper/FlashMessengerTest.php | 555 ++++++++++++++++++++++ 6 files changed, 1103 insertions(+), 47 deletions(-) create mode 100644 src/View/Helper/FlashMessenger.php create mode 100644 src/View/Helper/FlashMessengerFactory.php create mode 100644 test/View/Helper/FlashMessengerTest.php diff --git a/composer.json b/composer.json index fa643de..916aabe 100644 --- a/composer.json +++ b/composer.json @@ -19,11 +19,13 @@ "php": "^5.6 || ^7.0", "zendframework/zend-mvc": "^3.0", "zendframework/zend-session": "^2.8.5", - "zendframework/zend-stdlib": "^2.7 || ^3.0" + "zendframework/zend-stdlib": "^2.7 || ^3.0", + "zendframework/zend-view": "^2.10" }, "require-dev": { "phpunit/phpunit": "^5.7.27 || ^6.5.8 || ^7.1.4", - "zendframework/zend-coding-standard": "~1.0.0" + "zendframework/zend-coding-standard": "~1.0.0", + "zendframework/zend-i18n": "^2.6" }, "conflict": { "zendframework/zend-mvc": "<3.0.0" @@ -50,6 +52,9 @@ "component": "Zend\\Mvc\\Plugin\\FlashMessenger" } }, + "suggests": { + "zendframework/zend-i18n": "Zend\\I18n component" + }, "scripts": { "check": [ "@cs-check", diff --git a/composer.lock b/composer.lock index 4f08b93..920b84d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "95df8c54af88165006000f018a2d4e2d", + "content-hash": "845a247730d03148cbb58136613c7197", "packages": [ { "name": "container-interop/container-interop", @@ -302,30 +302,30 @@ }, { "name": "zendframework/zend-loader", - "version": "2.5.1", + "version": "2.6.0", "source": { "type": "git", "url": "https://github.com/zendframework/zend-loader.git", - "reference": "c5fd2f071bde071f4363def7dea8dec7393e135c" + "reference": "78f11749ea340f6ca316bca5958eef80b38f9b6c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/zendframework/zend-loader/zipball/c5fd2f071bde071f4363def7dea8dec7393e135c", - "reference": "c5fd2f071bde071f4363def7dea8dec7393e135c", + "url": "https://api.github.com/repos/zendframework/zend-loader/zipball/78f11749ea340f6ca316bca5958eef80b38f9b6c", + "reference": "78f11749ea340f6ca316bca5958eef80b38f9b6c", "shasum": "" }, "require": { - "php": ">=5.3.23" + "php": "^5.6 || ^7.0" }, "require-dev": { - "fabpot/php-cs-fixer": "1.7.*", - "phpunit/phpunit": "~4.0" + "phpunit/phpunit": "^5.7.27 || ^6.5.8 || ^7.1.4", + "zendframework/zend-coding-standard": "~1.0.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "2.5-dev", - "dev-develop": "2.6-dev" + "dev-master": "2.6.x-dev", + "dev-develop": "2.7.x-dev" } }, "autoload": { @@ -337,12 +337,13 @@ "license": [ "BSD-3-Clause" ], - "homepage": "https://github.com/zendframework/zend-loader", + "description": "Autoloading and plugin loading strategies", "keywords": [ + "ZendFramework", "loader", - "zf2" + "zf" ], - "time": "2015-06-03T14:05:47+00:00" + "time": "2018-04-30T15:20:54+00:00" }, { "name": "zendframework/zend-modulemanager", @@ -674,31 +675,31 @@ }, { "name": "zendframework/zend-stdlib", - "version": "3.1.1", + "version": "3.2.0", "source": { "type": "git", "url": "https://github.com/zendframework/zend-stdlib.git", - "reference": "10ef03144902d1955f935fff5346ed52f7d99bcc" + "reference": "cd164b4a18b5d1aeb69be2c26db035b5ed6925ae" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/zendframework/zend-stdlib/zipball/10ef03144902d1955f935fff5346ed52f7d99bcc", - "reference": "10ef03144902d1955f935fff5346ed52f7d99bcc", + "url": "https://api.github.com/repos/zendframework/zend-stdlib/zipball/cd164b4a18b5d1aeb69be2c26db035b5ed6925ae", + "reference": "cd164b4a18b5d1aeb69be2c26db035b5ed6925ae", "shasum": "" }, "require": { "php": "^5.6 || ^7.0" }, "require-dev": { - "athletic/athletic": "~0.1", - "phpunit/phpunit": "~4.0", + "phpbench/phpbench": "^0.13", + "phpunit/phpunit": "^5.7.27 || ^6.5.8 || ^7.1.2", "zendframework/zend-coding-standard": "~1.0.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "3.1-dev", - "dev-develop": "3.2-dev" + "dev-master": "3.2.x-dev", + "dev-develop": "3.3.x-dev" } }, "autoload": { @@ -710,34 +711,35 @@ "license": [ "BSD-3-Clause" ], - "homepage": "https://github.com/zendframework/zend-stdlib", + "description": "SPL extensions, array utilities, error handlers, and more", "keywords": [ + "ZendFramework", "stdlib", - "zf2" + "zf" ], - "time": "2018-04-12T16:05:42+00:00" + "time": "2018-04-30T13:50:40+00:00" }, { "name": "zendframework/zend-uri", - "version": "2.6.0", + "version": "2.6.1", "source": { "type": "git", "url": "https://github.com/zendframework/zend-uri.git", - "reference": "fb998b9487ea8c5f4aaac0e536190709bdd5353b" + "reference": "3b6463645c6766f78ce537c70cb4fdabee1e725f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/zendframework/zend-uri/zipball/fb998b9487ea8c5f4aaac0e536190709bdd5353b", - "reference": "fb998b9487ea8c5f4aaac0e536190709bdd5353b", + "url": "https://api.github.com/repos/zendframework/zend-uri/zipball/3b6463645c6766f78ce537c70cb4fdabee1e725f", + "reference": "3b6463645c6766f78ce537c70cb4fdabee1e725f", "shasum": "" }, "require": { "php": "^5.6 || ^7.0", "zendframework/zend-escaper": "^2.5", - "zendframework/zend-validator": "^2.5" + "zendframework/zend-validator": "^2.10" }, "require-dev": { - "phpunit/phpunit": "^6.2.1 || ^5.7.15", + "phpunit/phpunit": "^5.7.27 || ^6.5.8 || ^7.1.4", "zendframework/zend-coding-standard": "~1.0.0" }, "type": "library", @@ -756,13 +758,13 @@ "license": [ "BSD-3-Clause" ], - "description": "a component that aids in manipulating and validating » Uniform Resource Identifiers (URIs)", - "homepage": "https://github.com/zendframework/zend-uri", + "description": "A component that aids in manipulating and validating » Uniform Resource Identifiers (URIs)", "keywords": [ + "ZendFramework", "uri", - "zf2" + "zf" ], - "time": "2018-04-10T17:08:10+00:00" + "time": "2018-04-30T13:40:08+00:00" }, { "name": "zendframework/zend-validator", @@ -1342,16 +1344,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "6.0.3", + "version": "6.0.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "774a82c0c5da4c1c7701790c262035d235ab7856" + "reference": "52187754b0eed0b8159f62a6fa30073327e8c2ca" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/774a82c0c5da4c1c7701790c262035d235ab7856", - "reference": "774a82c0c5da4c1c7701790c262035d235ab7856", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/52187754b0eed0b8159f62a6fa30073327e8c2ca", + "reference": "52187754b0eed0b8159f62a6fa30073327e8c2ca", "shasum": "" }, "require": { @@ -1401,7 +1403,7 @@ "testing", "xunit" ], - "time": "2018-04-06T15:39:20+00:00" + "time": "2018-04-29T14:59:09+00:00" }, { "name": "phpunit/php-file-iterator", @@ -1591,16 +1593,16 @@ }, { "name": "phpunit/phpunit", - "version": "7.1.4", + "version": "7.1.5", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "6d51299e307dc510149e0b7cd1931dd11770e1cb" + "reference": "ca64dba53b88aba6af32aebc6b388068db95c435" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/6d51299e307dc510149e0b7cd1931dd11770e1cb", - "reference": "6d51299e307dc510149e0b7cd1931dd11770e1cb", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/ca64dba53b88aba6af32aebc6b388068db95c435", + "reference": "ca64dba53b88aba6af32aebc6b388068db95c435", "shasum": "" }, "require": { @@ -1619,7 +1621,7 @@ "phpunit/php-text-template": "^1.2.1", "phpunit/php-timer": "^2.0", "phpunit/phpunit-mock-objects": "^6.1.1", - "sebastian/comparator": "^2.1 || ^3.0", + "sebastian/comparator": "^3.0", "sebastian/diff": "^3.0", "sebastian/environment": "^3.1", "sebastian/exporter": "^3.1", @@ -1667,7 +1669,7 @@ "testing", "xunit" ], - "time": "2018-04-18T13:41:53+00:00" + "time": "2018-04-29T15:09:19+00:00" }, { "name": "phpunit/phpunit-mock-objects", @@ -2484,6 +2486,74 @@ "zf" ], "time": "2016-11-09T21:30:43+00:00" + }, + { + "name": "zendframework/zend-i18n", + "version": "2.8.0", + "source": { + "type": "git", + "url": "https://github.com/zendframework/zend-i18n.git", + "reference": "cfdb658121e0d7eb969a498c2f67f1eacaab9c63" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/zendframework/zend-i18n/zipball/cfdb658121e0d7eb969a498c2f67f1eacaab9c63", + "reference": "cfdb658121e0d7eb969a498c2f67f1eacaab9c63", + "shasum": "" + }, + "require": { + "php": "^5.6 || ^7.0", + "zendframework/zend-stdlib": "^2.7 || ^3.0" + }, + "require-dev": { + "phpunit/phpunit": "^5.7.27 || ^6.5.8 || ^7.1.2", + "zendframework/zend-cache": "^2.6.1", + "zendframework/zend-coding-standard": "~1.0.0", + "zendframework/zend-config": "^2.6", + "zendframework/zend-eventmanager": "^2.6.2 || ^3.0", + "zendframework/zend-filter": "^2.6.1", + "zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3", + "zendframework/zend-validator": "^2.6", + "zendframework/zend-view": "^2.6.3" + }, + "suggest": { + "ext-intl": "Required for most features of Zend\\I18n; included in default builds of PHP", + "zendframework/zend-cache": "Zend\\Cache component", + "zendframework/zend-config": "Zend\\Config component", + "zendframework/zend-eventmanager": "You should install this package to use the events in the translator", + "zendframework/zend-filter": "You should install this package to use the provided filters", + "zendframework/zend-i18n-resources": "Translation resources", + "zendframework/zend-servicemanager": "Zend\\ServiceManager component", + "zendframework/zend-validator": "You should install this package to use the provided validators", + "zendframework/zend-view": "You should install this package to use the provided view helpers" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.8.x-dev", + "dev-develop": "2.9.x-dev" + }, + "zf": { + "component": "Zend\\I18n", + "config-provider": "Zend\\I18n\\ConfigProvider" + } + }, + "autoload": { + "psr-4": { + "Zend\\I18n\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "description": "Provide translations for your application, and filter and validate internationalized values", + "keywords": [ + "ZendFramework", + "i18n", + "zf" + ], + "time": "2018-04-25T19:32:43+00:00" } ], "aliases": [], diff --git a/src/Module.php b/src/Module.php index b8053a9..a99743a 100644 --- a/src/Module.php +++ b/src/Module.php @@ -7,6 +7,7 @@ namespace Zend\Mvc\Plugin\FlashMessenger; +use Zend\Mvc\Plugin\FlashMessenger\View; use Zend\ServiceManager\Factory\InvokableFactory; class Module @@ -32,6 +33,19 @@ public function getConfig() FlashMessenger::class => InvokableFactory::class, ], ], + 'view_helpers' => [ + 'aliases' => [ + 'flashmessenger' => View\Helper\FlashMessenger::class, + 'flashMessenger' => View\Helper\FlashMessenger::class, + 'FlashMessenger' => View\Helper\FlashMessenger::class, + + ], + 'factories' => [ + View\Helper\FlashMessenger::class => View\Helper\FlashMessengerFactory::class, + View\Helper\FlashMessengerFactory::class =>InvokableFactory::class, + 'zendviewhelperflashmessenger' => View\Helper\FlashMessengerFactory::class, + ] + ] ]; } } diff --git a/src/View/Helper/FlashMessenger.php b/src/View/Helper/FlashMessenger.php new file mode 100644 index 0000000..c4373c6 --- /dev/null +++ b/src/View/Helper/FlashMessenger.php @@ -0,0 +1,345 @@ + PluginFlashMessenger::NAMESPACE_INFO, + 'error' => PluginFlashMessenger::NAMESPACE_ERROR, + 'success' => PluginFlashMessenger::NAMESPACE_SUCCESS, + 'default' => PluginFlashMessenger::NAMESPACE_DEFAULT, + 'warning' => PluginFlashMessenger::NAMESPACE_WARNING, + ]; + + /** + * Templates for the open/close/separators for message tags + * + * @var string + */ + protected $messageCloseString = ''; + protected $messageOpenFormat = '
  • '; + protected $messageSeparatorString = '
  • '; + + /** + * Flag whether to escape messages + * + * @var bool + */ + protected $autoEscape = true; + + /** + * Html escape helper + * + * @var EscapeHtml + */ + protected $escapeHtmlHelper; + + /** + * Flash messenger plugin + * + * @var PluginFlashMessenger + */ + protected $pluginFlashMessenger; + + /** + * Returns the flash messenger plugin controller + * + * @param string|null $namespace + * @return FlashMessenger|PluginFlashMessenger + */ + public function __invoke($namespace = null) + { + if (null === $namespace) { + return $this; + } + $flashMessenger = $this->getPluginFlashMessenger(); + + return $flashMessenger->getMessagesFromNamespace($namespace); + } + + /** + * Proxy the flash messenger plugin controller + * + * @param string $method + * @param array $argv + * @return mixed + */ + public function __call($method, $argv) + { + $flashMessenger = $this->getPluginFlashMessenger(); + return call_user_func_array([$flashMessenger, $method], $argv); + } + + /** + * Render Messages + * + * @param string $namespace + * @param array $classes + * @param null|bool $autoEscape + * @return string + */ + public function render($namespace = 'default', array $classes = [], $autoEscape = null) + { + $flashMessenger = $this->getPluginFlashMessenger(); + $messages = $flashMessenger->getMessagesFromNamespace($namespace); + return $this->renderMessages($namespace, $messages, $classes, $autoEscape); + } + + /** + * Render Current Messages + * + * @param string $namespace + * @param array $classes + * @param bool|null $autoEscape + * @return string + */ + public function renderCurrent($namespace = 'default', array $classes = [], $autoEscape = null) + { + $flashMessenger = $this->getPluginFlashMessenger(); + $messages = $flashMessenger->getCurrentMessagesFromNamespace($namespace); + return $this->renderMessages($namespace, $messages, $classes, $autoEscape); + } + + /** + * Render Messages + * + * @param string $namespace + * @param array $messages + * @param array $classes + * @param bool|null $autoEscape + * @return string + */ + protected function renderMessages( + $namespace = 'default', + array $messages = [], + array $classes = [], + $autoEscape = null + ) { + if (empty($messages)) { + return ''; + } + + // Prepare classes for opening tag + if (empty($classes)) { + if (isset($this->classMessages[$namespace])) { + $classes = $this->classMessages[$namespace]; + } else { + $classes = $this->classMessages['default']; + } + $classes = [$classes]; + } + + if (null === $autoEscape) { + $autoEscape = $this->getAutoEscape(); + } + + // Flatten message array + $escapeHtml = $this->getEscapeHtmlHelper(); + $messagesToPrint = []; + $translator = $this->getTranslator(); + $translatorTextDomain = $this->getTranslatorTextDomain(); + array_walk_recursive( + $messages, + function ($item) use (& $messagesToPrint, $escapeHtml, $autoEscape, $translator, $translatorTextDomain) { + if ($translator !== null) { + $item = $translator->translate( + $item, + $translatorTextDomain + ); + } + + if ($autoEscape) { + $messagesToPrint[] = $escapeHtml($item); + return; + } + + $messagesToPrint[] = $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; + } + + /** + * Set whether or not auto escaping should be used + * + * @param bool $autoEscape + * @return self + */ + public function setAutoEscape($autoEscape = true) + { + $this->autoEscape = (bool) $autoEscape; + return $this; + } + + /** + * Return whether auto escaping is enabled or disabled + * + * return bool + */ + public function getAutoEscape() + { + return $this->autoEscape; + } + + /** + * Set the string used to close message representation + * + * @param string $messageCloseString + * @return FlashMessenger + */ + public function setMessageCloseString($messageCloseString) + { + $this->messageCloseString = (string) $messageCloseString; + return $this; + } + + /** + * Get the string used to close message representation + * + * @return string + */ + public function getMessageCloseString() + { + return $this->messageCloseString; + } + + /** + * Set the formatted string used to open message representation + * + * @param string $messageOpenFormat + * @return FlashMessenger + */ + public function setMessageOpenFormat($messageOpenFormat) + { + $this->messageOpenFormat = (string) $messageOpenFormat; + return $this; + } + + /** + * Get the formatted string used to open message representation + * + * @return string + */ + public function getMessageOpenFormat() + { + return $this->messageOpenFormat; + } + + /** + * Set the string used to separate messages + * + * @param string $messageSeparatorString + * @return FlashMessenger + */ + public function setMessageSeparatorString($messageSeparatorString) + { + $this->messageSeparatorString = (string) $messageSeparatorString; + return $this; + } + + /** + * Get the string used to separate messages + * + * @return string + */ + public function getMessageSeparatorString() + { + return $this->messageSeparatorString; + } + + /** + * Set the flash messenger plugin + * + * @param PluginFlashMessenger $pluginFlashMessenger + * @return FlashMessenger + * @throws InvalidArgumentException for an invalid $pluginFlashMessenger + */ + public function setPluginFlashMessenger($pluginFlashMessenger) + { + if (! $pluginFlashMessenger instanceof PluginFlashMessenger + ) { + throw new InvalidArgumentException(sprintf( + '%s expects a %s instance; received %s', + __METHOD__, + PluginFlashMessenger::class, + (is_object($pluginFlashMessenger) ? get_class($pluginFlashMessenger) : gettype($pluginFlashMessenger)) + )); + } + + $this->pluginFlashMessenger = $pluginFlashMessenger; + return $this; + } + + /** + * Get the flash messenger plugin + * + * @return PluginFlashMessenger + */ + public function getPluginFlashMessenger() + { + if (null === $this->pluginFlashMessenger) { + $this->setPluginFlashMessenger(new PluginFlashMessenger()); + } + + return $this->pluginFlashMessenger; + } + + /** + * Retrieve the escapeHtml helper + * + * @return EscapeHtml + */ + protected function getEscapeHtmlHelper() + { + if ($this->escapeHtmlHelper) { + return $this->escapeHtmlHelper; + } + + if (method_exists($this->getView(), 'plugin')) { + $this->escapeHtmlHelper = $this->view->plugin('escapehtml'); + } + + if (!$this->escapeHtmlHelper instanceof EscapeHtml) { + $this->escapeHtmlHelper = new EscapeHtml(); + } + + return $this->escapeHtmlHelper; + } +} diff --git a/src/View/Helper/FlashMessengerFactory.php b/src/View/Helper/FlashMessengerFactory.php new file mode 100644 index 0000000..33991a4 --- /dev/null +++ b/src/View/Helper/FlashMessengerFactory.php @@ -0,0 +1,67 @@ +getServiceLocator(); + } + $helper = new FlashMessenger(); + $controllerPluginManager = $container->get('ControllerPluginManager'); + $flashMessenger = $controllerPluginManager->get('flashmessenger'); + + $helper->setPluginFlashMessenger($flashMessenger); + + $config = $container->get('config'); + if (isset($config['view_helper_config']['flashmessenger'])) { + $configHelper = $config['view_helper_config']['flashmessenger']; + if (isset($configHelper['message_open_format'])) { + $helper->setMessageOpenFormat($configHelper['message_open_format']); + } + if (isset($configHelper['message_separator_string'])) { + $helper->setMessageSeparatorString($configHelper['message_separator_string']); + } + if (isset($configHelper['message_close_string'])) { + $helper->setMessageCloseString($configHelper['message_close_string']); + } + } + + return $helper; + } + + /** + * Create service (v2) + * + * @param ServiceLocatorInterface $container + * @param string $normalizedName + * @param string $requestedName + * @return FlashMessenger + */ + public function createService(ServiceLocatorInterface $container, $normalizedName = null, $requestedName = null) + { + return $this($container, $requestedName); + } +} diff --git a/test/View/Helper/FlashMessengerTest.php b/test/View/Helper/FlashMessengerTest.php new file mode 100644 index 0000000..dbe78ad --- /dev/null +++ b/test/View/Helper/FlashMessengerTest.php @@ -0,0 +1,555 @@ +mvcPluginClass = PluginFlashMessenger::class; + $this->helper = new FlashMessenger(); + $this->plugin = $this->helper->getPluginFlashMessenger(); + } + + public function seedMessages() + { + $helper = new FlashMessenger(); + $helper->addMessage('foo'); + $helper->addMessage('bar'); + $helper->addInfoMessage('bar-info'); + $helper->addSuccessMessage('bar-success'); + $helper->addWarningMessage('bar-warning'); + $helper->addErrorMessage('bar-error'); + unset($helper); + } + + public function seedCurrentMessages() + { + $helper = new FlashMessenger(); + $helper->addMessage('foo'); + $helper->addMessage('bar'); + $helper->addInfoMessage('bar-info'); + $helper->addSuccessMessage('bar-success'); + $helper->addErrorMessage('bar-error'); + } + + public function createServiceManager(array $config = []) + { + $config = new Config([ + 'services' => [ + 'config' => $config, + ], + 'factories' => [ + 'ControllerPluginManager' => function ($services, $name, $options) { + return new PluginManager($services, [ + 'aliases' => [ + 'flashmessenger' => $this->mvcPluginClass, + ], + 'factories' => [ + $this->mvcPluginClass => InvokableFactory::class + ] + ]); + }, + 'ViewHelperManager' => function ($services, $name, $options) { + return new HelperPluginManager($services, [ + 'factories' => [ + 'flashmessenger' => FlashMessengerFactory::class + ], + ]); + }, + ], + ]); + $sm = new ServiceManager(); + $config->configureServiceManager($sm); + return $sm; + } + + public function testCanAssertPluginClass() + { + $this->assertEquals($this->mvcPluginClass, get_class($this->plugin)); + $this->assertEquals($this->mvcPluginClass, get_class($this->helper->getPluginFlashMessenger())); + $this->assertSame($this->plugin, $this->helper->getPluginFlashMessenger()); + } + + public function testCanRetrieveMessages() + { + $helper = $this->helper; + + $this->assertFalse($helper()->hasMessages()); + $this->assertFalse($helper()->hasInfoMessages()); + $this->assertFalse($helper()->hasSuccessMessages()); + $this->assertFalse($helper()->hasWarningMessages()); + $this->assertFalse($helper()->hasErrorMessages()); + + $this->seedMessages(); + + $this->assertNotEmpty($helper('default')); + $this->assertNotEmpty($helper('info')); + $this->assertNotEmpty($helper('success')); + $this->assertNotEmpty($helper('warning')); + $this->assertNotEmpty($helper('error')); + + $this->assertTrue($this->plugin->hasMessages()); + $this->assertTrue($this->plugin->hasInfoMessages()); + $this->assertTrue($this->plugin->hasSuccessMessages()); + $this->assertTrue($this->plugin->hasWarningMessages()); + $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->assertNotEmpty($helper('default')); + $this->assertNotEmpty($helper('info')); + $this->assertNotEmpty($helper('success')); + $this->assertNotEmpty($helper('error')); + + $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()); + $this->assertFalse($this->helper->hasInfoMessages()); + $this->assertFalse($this->helper->hasSuccessMessages()); + $this->assertFalse($this->helper->hasWarningMessages()); + $this->assertFalse($this->helper->hasErrorMessages()); + + $this->seedMessages(); + + $this->assertTrue($this->helper->hasMessages()); + $this->assertTrue($this->helper->hasInfoMessages()); + $this->assertTrue($this->helper->hasSuccessMessages()); + $this->assertTrue($this->helper->hasWarningMessages()); + $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 = ''; + $displayInfo = $this->helper->render('info'); + $this->assertEquals($displayInfoAssertion, $displayInfo); + + $this->seedMessages(); + + $displayInfoAssertion = '
    • bar-info
    '; + $displayInfo = $this->helper->render('info'); + $this->assertEquals($displayInfoAssertion, $displayInfo); + } + + public function testCanDisplayListOfCurrentMessages() + { + $displayInfoAssertion = ''; + $displayInfo = $this->helper->renderCurrent('info'); + $this->assertEquals($displayInfoAssertion, $displayInfo); + + $this->seedCurrentMessages(); + + $displayInfoAssertion = '
    • bar-info
    '; + $displayInfo = $this->helper->renderCurrent('info'); + $this->assertEquals($displayInfoAssertion, $displayInfo); + } + + public function testCanDisplayListOfMessagesByDefaultParameters() + { + $helper = $this->helper; + $this->seedMessages(); + + $displayInfoAssertion = '
    • foo
    • bar
    '; + $displayInfo = $helper()->render(); + $this->assertEquals($displayInfoAssertion, $displayInfo); + } + + public function testCanDisplayListOfMessagesByDefaultCurrentParameters() + { + $helper = $this->helper; + $this->seedCurrentMessages(); + + $displayInfoAssertion = '
    • foo
    • bar
    '; + $displayInfo = $helper()->renderCurrent(); + $this->assertEquals($displayInfoAssertion, $displayInfo); + } + + public function testCanDisplayListOfMessagesByInvoke() + { + $helper = $this->helper; + $this->seedMessages(); + + $displayInfoAssertion = '
    • bar-info
    '; + $displayInfo = $helper()->render('info'); + $this->assertEquals($displayInfoAssertion, $displayInfo); + } + + public function testCanDisplayListOfCurrentMessagesByInvoke() + { + $helper = $this->helper; + $this->seedCurrentMessages(); + + $displayInfoAssertion = '
    • bar-info
    '; + $displayInfo = $helper()->renderCurrent('info'); + $this->assertEquals($displayInfoAssertion, $displayInfo); + } + + public function testCanDisplayListOfMessagesCustomised() + { + $this->seedMessages(); + + $displayInfoAssertion = '

    bar-info

    '; + $displayInfo = $this->helper + ->setMessageOpenFormat('

    ') + ->setMessageSeparatorString('

    ') + ->setMessageCloseString('

    ') + ->render('info', ['foo-baz', 'foo-bar']); + $this->assertEquals($displayInfoAssertion, $displayInfo); + } + + public function testCanDisplayListOfCurrentMessagesCustomised() + { + $this->seedCurrentMessages(); + + $displayInfoAssertion = '

    bar-info

    '; + $displayInfo = $this->helper + ->setMessageOpenFormat('

    ') + ->setMessageSeparatorString('

    ') + ->setMessageCloseString('

    ') + ->renderCurrent('info', ['foo-baz', 'foo-bar']); + $this->assertEquals($displayInfoAssertion, $displayInfo); + } + + public function testCanDisplayListOfMessagesCustomisedSeparator() + { + $this->seedMessages(); + + $displayInfoAssertion = '

    foo

    bar

    '; + $displayInfo = $this->helper + ->setMessageOpenFormat('
    ') + ->setMessageSeparatorString('

    ') + ->setMessageCloseString('

    ') + ->render('default', ['foo-baz', 'foo-bar']); + $this->assertEquals($displayInfoAssertion, $displayInfo); + } + + public function testCanDisplayListOfCurrentMessagesCustomisedSeparator() + { + $this->seedCurrentMessages(); + + $displayInfoAssertion = '

    foo

    bar

    '; + $displayInfo = $this->helper + ->setMessageOpenFormat('
    ') + ->setMessageSeparatorString('

    ') + ->setMessageCloseString('

    ') + ->renderCurrent('default', ['foo-baz', 'foo-bar']); + $this->assertEquals($displayInfoAssertion, $displayInfo); + } + + public function testCanDisplayListOfMessagesCustomisedByConfig() + { + $this->seedMessages(); + + $config = [ + 'view_helper_config' => [ + 'flashmessenger' => [ + 'message_open_format' => '
    • ', + 'message_separator_string' => '
    • ', + 'message_close_string' => '
    ', + ], + ], + ]; + + $services = $this->createServiceManager($config); + $helperPluginManager = $services->get('ViewHelperManager'); + $helper = $helperPluginManager->get('flashmessenger'); + + $displayInfoAssertion = '
    • bar-info
    '; + $displayInfo = $helper->render('info'); + $this->assertEquals($displayInfoAssertion, $displayInfo); + } + + public function testCanDisplayListOfCurrentMessagesCustomisedByConfig() + { + $this->seedCurrentMessages(); + $config = [ + 'view_helper_config' => [ + 'flashmessenger' => [ + 'message_open_format' => '
    • ', + 'message_separator_string' => '
    • ', + 'message_close_string' => '
    ', + ], + ], + ]; + $services = $this->createServiceManager($config); + $helperPluginManager = $services->get('ViewHelperManager'); + $helper = $helperPluginManager->get('flashmessenger'); + + $displayInfoAssertion = '
    • bar-info
    '; + $displayInfo = $helper->renderCurrent('info'); + $this->assertEquals($displayInfoAssertion, $displayInfo); + } + + public function testCanDisplayListOfMessagesCustomisedByConfigSeparator() + { + $this->seedMessages(); + + $config = [ + 'view_helper_config' => [ + 'flashmessenger' => [ + 'message_open_format' => '
      ', + 'message_separator_string' => '', + 'message_close_string' => '
    ', + ], + ], + ]; + $services = $this->createServiceManager($config); + $helperPluginManager = $services->get('ViewHelperManager'); + $helper = $helperPluginManager->get('flashmessenger'); + + $displayInfoAssertion = '
    • foo
    • bar
    '; + $displayInfo = $helper->render('default', ['foo-baz', 'foo-bar']); + $this->assertEquals($displayInfoAssertion, $displayInfo); + } + + public function testCanDisplayListOfCurrentMessagesCustomisedByConfigSeparator() + { + $this->seedCurrentMessages(); + + $config = [ + 'view_helper_config' => [ + 'flashmessenger' => [ + 'message_open_format' => '
      ', + 'message_separator_string' => '', + 'message_close_string' => '
    ', + ], + ], + ]; + $services = $this->createServiceManager($config); + $helperPluginManager = $services->get('ViewHelperManager'); + $helper = $helperPluginManager->get('flashmessenger'); + + $displayInfoAssertion = '
    • foo
    • bar
    '; + $displayInfo = $helper->renderCurrent('default', ['foo-baz', 'foo-bar']); + $this->assertEquals($displayInfoAssertion, $displayInfo); + } + + public function testCanTranslateMessages() + { + $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->seedMessages(); + + $displayAssertion = '
    • translated message
    '; + $display = $this->helper->render('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 = '
    • translated message
    '; + $display = $this->helper->renderCurrent('info'); + $this->assertEquals($displayAssertion, $display); + } + + public function testAutoEscapeDefaultsToTrue() + { + $this->assertTrue($this->helper->getAutoEscape()); + } + + public function testCanSetAutoEscape() + { + $this->helper->setAutoEscape(false); + $this->assertFalse($this->helper->getAutoEscape()); + + $this->helper->setAutoEscape(true); + $this->assertTrue($this->helper->getAutoEscape()); + } + + /** + * @covers Zend\Mvc\Plugin\FlashMessenger\View\Helper\FlashMessenger::render; + */ + public function testMessageIsEscapedByDefault() + { + $helper = new FlashMessenger; + $helper->addMessage('Foo
    bar'); + unset($helper); + + $displayAssertion = '
    • Foo<br />bar
    '; + $display = $this->helper->render('default'); + $this->assertSame($displayAssertion, $display); + } + + /** + * @covers Zend\Mvc\Plugin\FlashMessenger\View\Helper\FlashMessenger::render + */ + public function testMessageIsNotEscapedWhenAutoEscapeIsFalse() + { + $helper = new FlashMessenger; + $helper->addMessage('Foo
    bar'); + unset($helper); + + $displayAssertion = '
    • Foo
      bar
    '; + $display = $this->helper->setAutoEscape(false) + ->render('default'); + $this->assertSame($displayAssertion, $display); + } + + /** + * @covers Zend\Mvc\Plugin\FlashMessenger\View\Helper\FlashMessenger::render + */ + public function testCanSetAutoEscapeOnRender() + { + $helper = new FlashMessenger; + $helper->addMessage('Foo
    bar'); + unset($helper); + + $displayAssertion = '
    • Foo
      bar
    '; + $display = $this->helper->render('default', [], false); + $this->assertSame($displayAssertion, $display); + } + + /** + * @covers Zend\Mvc\Plugin\FlashMessenger\View\Helper\FlashMessenger::render + */ + public function testRenderUsesCurrentAutoEscapeByDefault() + { + $helper = new FlashMessenger; + $helper->addMessage('Foo
    bar'); + unset($helper); + + $this->helper->setAutoEscape(false); + $displayAssertion = '
    • Foo
      bar
    '; + $display = $this->helper->render('default'); + $this->assertSame($displayAssertion, $display); + + $helper = new FlashMessenger; + $helper->addMessage('Foo
    bar'); + unset($helper); + + $this->helper->setAutoEscape(true); + $displayAssertion = '
    • Foo<br />bar
    '; + $display = $this->helper->render('default'); + $this->assertSame($displayAssertion, $display); + } + + /** + * @covers Zend\Mvc\Plugin\FlashMessenger\View\Helper\FlashMessenger::renderCurrent + */ + public function testCurrentMessageIsEscapedByDefault() + { + $this->helper->addMessage('Foo
    bar'); + + $displayAssertion = '
    • Foo<br />bar
    '; + $display = $this->helper->renderCurrent('default'); + $this->assertSame($displayAssertion, $display); + } + + /** + * @covers Zend\Mvc\Plugin\FlashMessenger\View\Helper\FlashMessenger::renderCurrent + */ + public function testCurrentMessageIsNotEscapedWhenAutoEscapeIsFalse() + { + $this->helper->addMessage('Foo
    bar'); + + $displayAssertion = '
    • Foo
      bar
    '; + $display = $this->helper->setAutoEscape(false) + ->renderCurrent('default'); + $this->assertSame($displayAssertion, $display); + } + + /** + * @covers Zend\Mvc\Plugin\FlashMessenger\View\Helper\FlashMessenger::renderCurrent + */ + public function testCanSetAutoEscapeOnRenderCurrent() + { + $this->helper->addMessage('Foo
    bar'); + + $displayAssertion = '
    • Foo
      bar
    '; + $display = $this->helper->renderCurrent('default', [], false); + $this->assertSame($displayAssertion, $display); + } + + /** + * @covers Zend\Mvc\Plugin\FlashMessenger\View\Helper\FlashMessenger::renderCurrent + */ + public function testRenderCurrentUsesCurrentAutoEscapeByDefault() + { + $this->helper->addMessage('Foo
    bar'); + + $this->helper->setAutoEscape(false); + $displayAssertion = '
    • Foo
      bar
    '; + $display = $this->helper->renderCurrent('default'); + $this->assertSame($displayAssertion, $display); + + $this->helper->setAutoEscape(true); + $displayAssertion = '
    • Foo<br />bar
    '; + $display = $this->helper->renderCurrent('default'); + $this->assertSame($displayAssertion, $display); + } +} From 51103278a646ca9270c546ad9a31a41fab0a9dc2 Mon Sep 17 00:00:00 2001 From: Sasha Alex Romanenko Date: Sun, 30 Oct 2016 23:16:37 -0400 Subject: [PATCH 2/6] Add view helper documentation --- doc/book/flash-messenger-view-helper.md | 146 ++++++++++++++++++++++++ mkdocs.yml | 1 + 2 files changed, 147 insertions(+) create mode 100644 doc/book/flash-messenger-view-helper.md diff --git a/doc/book/flash-messenger-view-helper.md b/doc/book/flash-messenger-view-helper.md new file mode 100644 index 0000000..acb85b5 --- /dev/null +++ b/doc/book/flash-messenger-view-helper.md @@ -0,0 +1,146 @@ +# FlashMessenger + +The `FlashMessenger` helper is used to render the messages of the +[FlashMessenger MVC plugin](http://zendframework.github.io/zend-mvc-plugin-flashmessenger/). + +## Basic Usage + +When only using the default `namespace` for the `FlashMessenger`, you can do the +following: + +```php +// Usable in any of your .phtml files +echo $this->flashMessenger()->render(); +``` + +The first argument of the `render()` function is the `namespace`. If no +`namespace` is defined, the default +`Zend\Mvc\Controller\Plugin\FlashMessenger::NAMESPACE_DEFAULT` will be used, +which translates to `default`. + +```php +// Usable in any of your .phtml files +echo $this->flashMessenger()->render('error'); + +// Alternatively use one of the pre-defined namespaces +// (aka: use Zend\Mvc\Controller\Plugin\FlashMessenger;) +echo $this->flashMessenger()->render(FlashMessenger::NAMESPACE_SUCCESS); +``` + +## CSS Layout + +The `FlashMessenger` default rendering adds a CSS class to the generated HTML, +that matches the defined `namespace` that should be rendered. While it may work +well for the default cases, every so often you may want to add specific CSS +classes to the HTML output. This can be done while making use of the second +parameter of the `render()` function. + +```php +// Usable in any of your .phtml files +echo $this->flashMessenger()->render('error', ['alert', 'alert-danger']); +``` + +The output of this example, using the default HTML rendering settings, would +look like this: + +```html +
      +
    • Some FlashMessenger Content
    • +
    • You, the developer, are AWESOME!
    • +
    +``` + +## HTML Layout + +Aside from modifying the rendered CSS classes of the `FlashMessenger`, you are +furthermore able to modify the generated HTML as a whole to create even more +distinct visuals for your flash messages. The default output format is defined +within the source code of the `FlashMessenger` view helper itself. + +```php +// Zend/View/Helper/FlashMessenger.php#L41-L43 +protected $messageCloseString = '
  • '; +protected $messageOpenFormat = '
  • '; +protected $messageSeparatorString = '
  • '; +``` + +These defaults exactly match what we're trying to do. The placeholder `%s` will +be filled with the CSS classes output. + +To change this, all we need to do is call the respective setter methods of these +variables and give them new strings; for example: + +```php +// In any of your .phtml files: +echo $this->flashMessenger() + ->setMessageOpenFormat('

    ') + ->setMessageSeparatorString('

    ') + ->setMessageCloseString('

    ') + ->render('success'); +``` + +The above code sample then would then generate the following output: + +```html +
    +

    Some FlashMessenger Content

    +

    You, who's reading the docs, are AWESOME!

    +
    +``` + +## Sample Modification for Twitter Bootstrap 3 + +Taking all the above knowledge into account, we can create a nice, highly usable +and user-friendly rendering strategy using the +[Bootstrap front-end framework](http://getbootstrap.com/) version 3 layouts: + +```php +// In any of your .phtml files: +$flash = $this->flashMessenger(); +$flash->setMessageOpenFormat(' + +
    • ') + ->setMessageSeparatorString('
    • ') + ->setMessageCloseString('
    '); + +echo $flash->render('error', array('alert', 'alert-dismissible', 'alert-danger')); +echo $flash->render('info', array('alert', 'alert-dismissible', 'alert-info')); +echo $flash->render('default', array('alert', 'alert-dismissible', 'alert-warning')); +echo $flash->render('success', array('alert', 'alert-dismissible', 'alert-success')); +``` + +The output of the above example would create dismissable `FlashMessages` with +the following HTML markup. The example only covers one type of `FlashMessenger` +output; if you would have several `FlashMessages` available in each of the +rendered `namespaces`, then you would receive the same output multiple times +only having different CSS classes applied. + +```html +
    + +
      +
    • Some FlashMessenger Content
    • +
    • You, who's reading the docs, are AWESOME!
    • +
    +
    +``` + +## Alternative Configuration of the ViewHelper Layout + +`Zend\View\Helper\Service\FlashMessengerFactory` checks the application +configuration, making it possible to set up the `FlashMessenger` strings through +your `module.config.php`, too. The next example will set up the output to be +identical with the above Twitter Bootstrap 3 Example + +```php +'view_helper_config' => [ + 'flashmessenger' => [ + 'message_open_format' => '
    • ', + 'message_close_string' => '
    ', + 'message_separator_string' => '
  • ', + ], +], +``` diff --git a/mkdocs.yml b/mkdocs.yml index 73a052f..d752a5e 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -2,6 +2,7 @@ docs_dir: docs/book site_dir: docs/html pages: - index.md + - View Helper: flash-messenger-view-helper.md site_name: zend-mvc-plugin-flashmessenger site_description: 'zend-mvc-plugin-flashmessenger: Flash messages for zend-mvc controllers' repo_url: 'https://github.com/zendframework/zend-mvc-plugin-flashmessenger' From 35c15142eb2c37ea8c44bfd13f4612f6ffa81abd Mon Sep 17 00:00:00 2001 From: Sasha Alex Romanenko Date: Sun, 30 Oct 2016 23:22:00 -0400 Subject: [PATCH 3/6] changelog update --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7ce159..74d00a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,7 @@ First stable release. ### Added -- Nothing. +- View Helper plugin [ported](https://github.com/zendframework/zend-mvc-plugin-flashmessenger/issues/3) from Zend View ### Deprecated From 43b8a478d22f0d47c1891acad7bfcfc92627c05e Mon Sep 17 00:00:00 2001 From: Sasha Alex Romanenko Date: Sun, 30 Oct 2016 23:50:57 -0400 Subject: [PATCH 4/6] missed private repository version number for testing --- composer.json | 2 +- composer.lock | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 916aabe..2329b34 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "require-dev": { "phpunit/phpunit": "^5.7.27 || ^6.5.8 || ^7.1.4", "zendframework/zend-coding-standard": "~1.0.0", - "zendframework/zend-i18n": "^2.6" + "zendframework/zend-i18n": "^2.8" }, "conflict": { "zendframework/zend-mvc": "<3.0.0" diff --git a/composer.lock b/composer.lock index 920b84d..1de1796 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "845a247730d03148cbb58136613c7197", + "content-hash": "fba1dfa52e41d5d5bfdfe5002cd3331e", "packages": [ { "name": "container-interop/container-interop", From 710a2949ed709cb88a0cd2d27cd4df8c9c223e86 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 30 Apr 2018 13:22:39 -0500 Subject: [PATCH 5/6] CS and compatibility fixes - CS fixes per phpcs, including whitespace additions and wrapping long lines. - Use PHPUnit 5+ namespaces. - Use getMockBuilder + getMock. --- src/Module.php | 2 +- src/View/Helper/FlashMessenger.php | 9 ++- src/View/Helper/FlashMessengerFactory.php | 8 +-- test/View/Helper/FlashMessengerTest.php | 85 ++++++++++++----------- 4 files changed, 51 insertions(+), 53 deletions(-) diff --git a/src/Module.php b/src/Module.php index a99743a..e64d430 100644 --- a/src/Module.php +++ b/src/Module.php @@ -42,7 +42,7 @@ public function getConfig() ], 'factories' => [ View\Helper\FlashMessenger::class => View\Helper\FlashMessengerFactory::class, - View\Helper\FlashMessengerFactory::class =>InvokableFactory::class, + View\Helper\FlashMessengerFactory::class => InvokableFactory::class, 'zendviewhelperflashmessenger' => View\Helper\FlashMessengerFactory::class, ] ] diff --git a/src/View/Helper/FlashMessenger.php b/src/View/Helper/FlashMessenger.php index c4373c6..703c749 100644 --- a/src/View/Helper/FlashMessenger.php +++ b/src/View/Helper/FlashMessenger.php @@ -1,8 +1,8 @@ escapeHtmlHelper = $this->view->plugin('escapehtml'); } - if (!$this->escapeHtmlHelper instanceof EscapeHtml) { + if (! $this->escapeHtmlHelper instanceof EscapeHtml) { $this->escapeHtmlHelper = new EscapeHtml(); } diff --git a/src/View/Helper/FlashMessengerFactory.php b/src/View/Helper/FlashMessengerFactory.php index 33991a4..096b8f6 100644 --- a/src/View/Helper/FlashMessengerFactory.php +++ b/src/View/Helper/FlashMessengerFactory.php @@ -1,10 +1,8 @@ function ($services, $name, $options) { return new PluginManager($services, [ 'aliases' => [ - 'flashmessenger' => $this->mvcPluginClass, + 'flashmessenger' => $this->mvcPluginClass, ], 'factories' => [ - $this->mvcPluginClass => InvokableFactory::class - ] + $this->mvcPluginClass => InvokableFactory::class, + ], ]); }, 'ViewHelperManager' => function ($services, $name, $options) { return new HelperPluginManager($services, [ 'factories' => [ - 'flashmessenger' => FlashMessengerFactory::class + 'flashmessenger' => FlashMessengerFactory::class, ], ]); }, @@ -249,10 +242,10 @@ public function testCanDisplayListOfMessagesCustomised() $displayInfoAssertion = '

    bar-info

    '; $displayInfo = $this->helper - ->setMessageOpenFormat('

    ') - ->setMessageSeparatorString('

    ') - ->setMessageCloseString('

    ') - ->render('info', ['foo-baz', 'foo-bar']); + ->setMessageOpenFormat('

    ') + ->setMessageSeparatorString('

    ') + ->setMessageCloseString('

    ') + ->render('info', ['foo-baz', 'foo-bar']); $this->assertEquals($displayInfoAssertion, $displayInfo); } @@ -262,10 +255,10 @@ public function testCanDisplayListOfCurrentMessagesCustomised() $displayInfoAssertion = '

    bar-info

    '; $displayInfo = $this->helper - ->setMessageOpenFormat('

    ') - ->setMessageSeparatorString('

    ') - ->setMessageCloseString('

    ') - ->renderCurrent('info', ['foo-baz', 'foo-bar']); + ->setMessageOpenFormat('

    ') + ->setMessageSeparatorString('

    ') + ->setMessageCloseString('

    ') + ->renderCurrent('info', ['foo-baz', 'foo-bar']); $this->assertEquals($displayInfoAssertion, $displayInfo); } @@ -275,10 +268,10 @@ public function testCanDisplayListOfMessagesCustomisedSeparator() $displayInfoAssertion = '

    foo

    bar

    '; $displayInfo = $this->helper - ->setMessageOpenFormat('
    ') - ->setMessageSeparatorString('

    ') - ->setMessageCloseString('

    ') - ->render('default', ['foo-baz', 'foo-bar']); + ->setMessageOpenFormat('
    ') + ->setMessageSeparatorString('

    ') + ->setMessageCloseString('

    ') + ->render('default', ['foo-baz', 'foo-bar']); $this->assertEquals($displayInfoAssertion, $displayInfo); } @@ -288,10 +281,10 @@ public function testCanDisplayListOfCurrentMessagesCustomisedSeparator() $displayInfoAssertion = '

    foo

    bar

    '; $displayInfo = $this->helper - ->setMessageOpenFormat('
    ') - ->setMessageSeparatorString('

    ') - ->setMessageCloseString('

    ') - ->renderCurrent('default', ['foo-baz', 'foo-bar']); + ->setMessageOpenFormat('
    ') + ->setMessageSeparatorString('

    ') + ->setMessageCloseString('

    ') + ->renderCurrent('default', ['foo-baz', 'foo-bar']); $this->assertEquals($displayInfoAssertion, $displayInfo); } @@ -356,7 +349,10 @@ public function testCanDisplayListOfMessagesCustomisedByConfigSeparator() $helperPluginManager = $services->get('ViewHelperManager'); $helper = $helperPluginManager->get('flashmessenger'); - $displayInfoAssertion = '
    • foo
    • bar
    '; + $displayInfoAssertion = '
      ' + . '
    • foo
    • ' + . '
    • bar
    • ' + . '
    '; $displayInfo = $helper->render('default', ['foo-baz', 'foo-bar']); $this->assertEquals($displayInfoAssertion, $displayInfo); } @@ -378,17 +374,21 @@ public function testCanDisplayListOfCurrentMessagesCustomisedByConfigSeparator() $helperPluginManager = $services->get('ViewHelperManager'); $helper = $helperPluginManager->get('flashmessenger'); - $displayInfoAssertion = '
    • foo
    • bar
    '; + $displayInfoAssertion = '
      ' + . '
    • foo
    • ' + . '
    • bar
    • ' + . '
    '; $displayInfo = $helper->renderCurrent('default', ['foo-baz', 'foo-bar']); $this->assertEquals($displayInfoAssertion, $displayInfo); } public function testCanTranslateMessages() { - $mockTranslator = $this->getMock('Zend\I18n\Translator\Translator'); - $mockTranslator->expects($this->exactly(1)) - ->method('translate') - ->will($this->returnValue('translated message')); + $mockTranslator = $this->getMockBuilder(Translator::class)->getMock(); + $mockTranslator + ->expects($this->exactly(1)) + ->method('translate') + ->will($this->returnValue('translated message')); $this->helper->setTranslator($mockTranslator); $this->assertTrue($this->helper->hasTranslator()); @@ -402,10 +402,11 @@ public function testCanTranslateMessages() public function testCanTranslateCurrentMessages() { - $mockTranslator = $this->getMock('Zend\I18n\Translator\Translator'); - $mockTranslator->expects($this->exactly(1)) - ->method('translate') - ->will($this->returnValue('translated message')); + $mockTranslator = $this->getMockBuilder(Translator::class)->getMock(); + $mockTranslator + ->expects($this->exactly(1)) + ->method('translate') + ->will($this->returnValue('translated message')); $this->helper->setTranslator($mockTranslator); $this->assertTrue($this->helper->hasTranslator()); From e3bf3cd9dba72ab8827b77df1b47a81221171593 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 30 Apr 2018 13:37:02 -0500 Subject: [PATCH 6/6] Fix failing unit test Tests were failing on PHPUnit 6 and 7, due to a change in how those versions persist superglobals, and, specifically, $_SESSION. This patch resets the value of $_SESSION between tests, which solves the problems observed. --- test/View/Helper/FlashMessengerTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/test/View/Helper/FlashMessengerTest.php b/test/View/Helper/FlashMessengerTest.php index 55eda8b..9891d11 100644 --- a/test/View/Helper/FlashMessengerTest.php +++ b/test/View/Helper/FlashMessengerTest.php @@ -26,6 +26,7 @@ class FlashMessengerTest extends TestCase public function setUp() { + $_SESSION = []; $this->mvcPluginClass = PluginFlashMessenger::class; $this->helper = new FlashMessenger(); $this->plugin = $this->helper->getPluginFlashMessenger();