From 513bc4242a3bbf691c83adcd00f404cd1ba50210 Mon Sep 17 00:00:00 2001 From: Sasha Alex Romanenko Date: Sun, 30 Oct 2016 11:33:52 -0400 Subject: [PATCH 1/2] Move FlashMessenger view helper to be together with corresponding MVC Controller Plugin --- .travis.yml | 2 +- CHANGELOG.md | 4 + composer.json | 1 + doc/book/helpers/flash-messenger.md | 146 ----- doc/book/helpers/intro.md | 3 +- mkdocs.yml | 1 - src/Helper/FlashMessenger.php | 353 ------------ src/Helper/Service/FlashMessengerFactory.php | 68 --- src/HelperPluginManager.php | 5 - src/Renderer/PhpRenderer.php | 1 - test/Helper/FlashMessengerTest.php | 545 ------------------ test/HelperPluginManagerCompatibilityTest.php | 13 +- 12 files changed, 9 insertions(+), 1133 deletions(-) delete mode 100644 doc/book/helpers/flash-messenger.md delete mode 100644 src/Helper/FlashMessenger.php delete mode 100644 src/Helper/Service/FlashMessengerFactory.php delete mode 100644 test/Helper/FlashMessengerTest.php diff --git a/.travis.yml b/.travis.yml index 5d09e404..d3139324 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,7 @@ cache: env: global: - COMPOSER_ARGS="--no-interaction --ignore-platform-reqs" - - LATEST_DEPS="zendframework/zend-mvc-plugin-flashmessenger zendframework/zend-mvc-i18n zendframework/zend-mvc-console" + - LATEST_DEPS="zendframework/zend-mvc-i18n zendframework/zend-mvc-console" - SITE_URL=https://zendframework.github.io/zend-view - GH_USER_NAME="Matthew Weier O'Phinney" - GH_USER_EMAIL=matthew@weierophinney.net diff --git a/CHANGELOG.md b/CHANGELOG.md index c8583301..5beb93d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. +## 3.0.0 + +- Moved `FlashMessenger` view helper to be together with corresponding [MVC Controller Plugin](https://github.com/zendframework/zend-mvc-plugin-flashmessenger/issues/3) + ## 2.8.2 - TBD ### Added diff --git a/composer.json b/composer.json index f976a089..25c186e3 100644 --- a/composer.json +++ b/composer.json @@ -54,6 +54,7 @@ "zendframework/zend-mvc": "Zend\\Mvc component", "zendframework/zend-navigation": "Zend\\Navigation component", "zendframework/zend-paginator": "Zend\\Paginator component", + "zendframework/zend-mvc-plugin-flashmessenger" : "Zend FlashMessenger plugin component", "zendframework/zend-permissions-acl": "Zend\\Permissions\\Acl component", "zendframework/zend-servicemanager": "Zend\\ServiceManager component", "zendframework/zend-uri": "Zend\\Uri component" diff --git a/doc/book/helpers/flash-messenger.md b/doc/book/helpers/flash-messenger.md deleted file mode 100644 index acb85b50..00000000 --- a/doc/book/helpers/flash-messenger.md +++ /dev/null @@ -1,146 +0,0 @@ -# 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 - -``` - -## 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/doc/book/helpers/intro.md b/doc/book/helpers/intro.md index 34f10de8..d5200c78 100644 --- a/doc/book/helpers/intro.md +++ b/doc/book/helpers/intro.md @@ -58,7 +58,8 @@ for, and rendering, the various HTML `` tags, such as `HeadTitle`, - [BasePath](base-path.md) - [Cycle](cycle.md) - [Doctype](doctype.md) -- [FlashMessenger](flash-messenger.md) + +[comment]: #(TODO Add link to wherever flashmessenger vew helper plugin will end up to help discoverability.) - [Gravatar](gravatar.md) - [HeadLink](head-link.md) - [HeadMeta](head-meta.md) diff --git a/mkdocs.yml b/mkdocs.yml index 0163b550..96ba30a4 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -12,7 +12,6 @@ pages: - BasePath: helpers/base-path.md - Cycle: helpers/cycle.md - Doctype: helpers/doctype.md - - FlashMessenger: helpers/flash-messenger.md - Gravatar: helpers/gravatar.md - HeadLink: helpers/head-link.md - HeadMeta: helpers/head-meta.md diff --git a/src/Helper/FlashMessenger.php b/src/Helper/FlashMessenger.php deleted file mode 100644 index 193b254d..00000000 --- a/src/Helper/FlashMessenger.php +++ /dev/null @@ -1,353 +0,0 @@ - 'info', - 'error' => 'error', - 'success' => 'success', - 'default' => 'default', - 'warning' => '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 V2PluginFlashMessenger|PluginFlashMessenger - */ - protected $pluginFlashMessenger; - - /** - * Returns the flash messenger plugin controller - * - * @param string|null $namespace - * @return FlashMessenger|V2PluginFlashMessenger|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 V2PluginFlashMessenger|PluginFlashMessenger $pluginFlashMessenger - * @return FlashMessenger - * @throws InvalidArgumentException for an invalid $pluginFlashMessenger - */ - public function setPluginFlashMessenger($pluginFlashMessenger) - { - if (! $pluginFlashMessenger instanceof V2PluginFlashMessenger - && ! $pluginFlashMessenger instanceof PluginFlashMessenger - ) { - throw new InvalidArgumentException(sprintf( - '%s expects either a %s or %s instance; received %s', - __METHOD__, - V2PluginFlashMessenger::class, - PluginFlashMessenger::class, - (is_object($pluginFlashMessenger) ? get_class($pluginFlashMessenger) : gettype($pluginFlashMessenger)) - )); - } - - $this->pluginFlashMessenger = $pluginFlashMessenger; - return $this; - } - - /** - * Get the flash messenger plugin - * - * @return V2PluginFlashMessenger|PluginFlashMessenger - */ - public function getPluginFlashMessenger() - { - if (null === $this->pluginFlashMessenger) { - $this->setPluginFlashMessenger( - class_exists(PluginFlashMessenger::class) - ? new PluginFlashMessenger() - : new V2PluginFlashMessenger() - ); - } - - 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/Helper/Service/FlashMessengerFactory.php b/src/Helper/Service/FlashMessengerFactory.php deleted file mode 100644 index 028d1319..00000000 --- a/src/Helper/Service/FlashMessengerFactory.php +++ /dev/null @@ -1,68 +0,0 @@ -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/src/HelperPluginManager.php b/src/HelperPluginManager.php index 652980be..950adf9a 100644 --- a/src/HelperPluginManager.php +++ b/src/HelperPluginManager.php @@ -62,9 +62,6 @@ class HelperPluginManager extends AbstractPluginManager 'escapeUrl' => Helper\EscapeUrl::class, 'EscapeUrl' => Helper\EscapeUrl::class, 'escapeurl' => Helper\EscapeUrl::class, - 'flashmessenger' => Helper\FlashMessenger::class, - 'flashMessenger' => Helper\FlashMessenger::class, - 'FlashMessenger' => Helper\FlashMessenger::class, 'Gravatar' => Helper\Gravatar::class, 'gravatar' => Helper\Gravatar::class, 'headLink' => Helper\HeadLink::class, @@ -148,7 +145,6 @@ class HelperPluginManager extends AbstractPluginManager * @var array */ protected $factories = [ - Helper\FlashMessenger::class => Helper\Service\FlashMessengerFactory::class, Helper\Identity::class => Helper\Service\IdentityFactory::class, Helper\BasePath::class => InvokableFactory::class, Helper\Cycle::class => InvokableFactory::class, @@ -186,7 +182,6 @@ class HelperPluginManager extends AbstractPluginManager // v2 canonical FQCNs - 'zendviewhelperflashmessenger' => Helper\Service\FlashMessengerFactory::class, 'zendviewhelperidentity' => Helper\Service\IdentityFactory::class, 'zendviewhelperbasepath' => InvokableFactory::class, 'zendviewhelpercycle' => InvokableFactory::class, diff --git a/src/Renderer/PhpRenderer.php b/src/Renderer/PhpRenderer.php index aac1fcd9..7d5768ea 100644 --- a/src/Renderer/PhpRenderer.php +++ b/src/Renderer/PhpRenderer.php @@ -40,7 +40,6 @@ * @method mixed escapeHtmlAttr($value, $recurse = \Zend\View\Helper\Escaper\AbstractHelper::RECURSE_NONE) * @method mixed escapeJs($value, $recurse = \Zend\View\Helper\Escaper\AbstractHelper::RECURSE_NONE) * @method mixed escapeUrl($value, $recurse = \Zend\View\Helper\Escaper\AbstractHelper::RECURSE_NONE) - * @method \Zend\View\Helper\FlashMessenger flashMessenger($namespace = null) * @method \Zend\View\Helper\Gravatar gravatar($email = "", $options = array(), $attribs = array()) * @method \Zend\View\Helper\HeadLink headLink(array $attributes = null, $placement = \Zend\View\Helper\Placeholder\Container\AbstractContainer::APPEND) * @method \Zend\View\Helper\HeadMeta headMeta($content = null, $keyValue = null, $keyType = 'name', $modifiers = array(), $placement = \Zend\View\Helper\Placeholder\Container\AbstractContainer::APPEND) diff --git a/test/Helper/FlashMessengerTest.php b/test/Helper/FlashMessengerTest.php deleted file mode 100644 index 4301495d..00000000 --- a/test/Helper/FlashMessengerTest.php +++ /dev/null @@ -1,545 +0,0 @@ -mvcPluginClass = class_exists(V2PluginFlashMessenger::class) - ? V2PluginFlashMessenger::class - : V3PluginFlashMessenger::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, [ - 'invokables' => [ - 'flashmessenger' => $this->mvcPluginClass, - ], - ]); - }, - 'ViewHelperManager' => function ($services, $name, $options) { - return new HelperPluginManager($services); - }, - ], - ]); - $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\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\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\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\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\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\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\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\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); - } -} diff --git a/test/HelperPluginManagerCompatibilityTest.php b/test/HelperPluginManagerCompatibilityTest.php index 8da3079f..927ae39d 100644 --- a/test/HelperPluginManagerCompatibilityTest.php +++ b/test/HelperPluginManagerCompatibilityTest.php @@ -11,9 +11,7 @@ use PHPUnit_Framework_TestCase as TestCase; use ReflectionProperty; -use Zend\Mvc\Controller\Plugin\FlashMessenger as V2FlashMessenger; use Zend\Mvc\Controller\PluginManager as ControllerPluginManager; -use Zend\Mvc\Plugin\FlashMessenger\FlashMessenger; use Zend\ServiceManager\Config; use Zend\ServiceManager\ServiceManager; use Zend\ServiceManager\Test\CommonPluginManagerTrait; @@ -31,11 +29,7 @@ protected function getPluginManager() if (class_exists(ControllerPluginManager::class)) { $factories['ControllerPluginManager'] = function ($services, $name, $options) { return new ControllerPluginManager($services, [ - 'invokables' => [ - 'flashmessenger' => class_exists(FlashMessenger::class) - ? FlashMessenger::class - : V2FlashMessenger::class, - ], + 'invokables' => [], ]); }; } @@ -66,11 +60,6 @@ public function aliasProvider() $aliases = $r->getValue($pluginManager); foreach ($aliases as $alias => $target) { - // Skipping conditionally since it depends on zend-mvc - if (! class_exists(ControllerPluginManager::class) && strpos($target, '\\FlashMessenger')) { - continue; - } - // Skipping conditionally since it depends on zend-mvc if (! class_exists(ControllerPluginManager::class) && strpos($target, '\\Url')) { continue; From e51ded6b838639f7b7a59960c6bd9c791eee75d6 Mon Sep 17 00:00:00 2001 From: Sasha Alex Romanenko Date: Sun, 30 Oct 2016 23:17:26 -0400 Subject: [PATCH 2/2] Add link to flash messenger view helper documention from MVC Plugin docs --- doc/book/helpers/intro.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/doc/book/helpers/intro.md b/doc/book/helpers/intro.md index d5200c78..00b8cbbb 100644 --- a/doc/book/helpers/intro.md +++ b/doc/book/helpers/intro.md @@ -58,8 +58,6 @@ for, and rendering, the various HTML `` tags, such as `HeadTitle`, - [BasePath](base-path.md) - [Cycle](cycle.md) - [Doctype](doctype.md) - -[comment]: #(TODO Add link to wherever flashmessenger vew helper plugin will end up to help discoverability.) - [Gravatar](gravatar.md) - [HeadLink](head-link.md) - [HeadMeta](head-meta.md) @@ -106,6 +104,12 @@ for, and rendering, the various HTML `` tags, such as `HeadTitle`, > [Paginator Usage](https://zendframework.github.io/zend-paginator/usage/#rendering-pages-with-view-scripts) > documentation. +> ### FlashMessenger helper +> +> View helper related to **Flash Messenger** is documented in the +> [FLash Messenger View Helper](https://docs.zendframework.com/zend-mvc-plugin-flashmessenger/view-helper) +> documentation. + > ### Custom helpers > > For documentation on writing **custom view helpers** see the