From 7bdb2f7ec101f61c59caf7d41210677bb0a5a685 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 14 Jul 2020 15:27:25 +0700 Subject: [PATCH] Fixes #16 remove FlashMessenger view helper Signed-off-by: Abdul Malik Ikhsan --- docs/book/helpers/flash-messenger.md | 146 ----- docs/book/helpers/intro.md | 7 +- src/Helper/FlashMessenger.php | 354 ----------- src/Helper/Service/FlashMessengerFactory.php | 57 -- src/HelperPluginManager.php | 4 - src/Renderer/PhpRenderer.php | 1 - test/Helper/FlashMessengerTest.php | 558 ------------------ test/HelperPluginManagerCompatibilityTest.php | 10 +- 8 files changed, 7 insertions(+), 1130 deletions(-) delete mode 100644 docs/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/docs/book/helpers/flash-messenger.md b/docs/book/helpers/flash-messenger.md deleted file mode 100644 index 95a21fb99..000000000 --- a/docs/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](https://docs.laminas.dev/laminas-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 -`Laminas\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 Laminas\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 -// Laminas/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', ['alert', 'alert-dismissible', 'alert-danger']); -echo $flash->render('info', ['alert', 'alert-dismissible', 'alert-info']); -echo $flash->render('default', ['alert', 'alert-dismissible', 'alert-warning']); -echo $flash->render('success', ['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 - -`Laminas\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/docs/book/helpers/intro.md b/docs/book/helpers/intro.md index 5402f9774..699f9f8c7 100644 --- a/docs/book/helpers/intro.md +++ b/docs/book/helpers/intro.md @@ -60,7 +60,6 @@ for, and rendering, the various HTML `` tags, such as `HeadTitle`, - [BasePath](base-path.md) - [Cycle](cycle.md) - [Doctype](doctype.md) -- [FlashMessenger](flash-messenger.md) - [Gravatar](gravatar.md) - [HeadLink](head-link.md) - [HeadMeta](head-meta.md) @@ -109,6 +108,12 @@ for, and rendering, the various HTML `` tags, such as `HeadTitle`, > [Paginator Usage](https://docs.laminas.dev/laminas-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.laminas.dev/laminas-mvc-plugin-flashmessenger/view-helper/) +> documentation. + > ### Custom Helpers > > For documentation on writing **custom view helpers** see the diff --git a/src/Helper/FlashMessenger.php b/src/Helper/FlashMessenger.php deleted file mode 100644 index 8e12f5cd7..000000000 --- a/src/Helper/FlashMessenger.php +++ /dev/null @@ -1,354 +0,0 @@ - 'info', - 'error' => 'error', - 'success' => 'success', - 'default' => 'default', - 'warning' => 'warning', - ]; - - /** - * Templates for the open/close/separators for message tags - * - * @var string - */ - protected $messageCloseString = '
  • '; - /** @var string */ - protected $messageOpenFormat = '
  • '; - /** @var string */ - 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 array|static - */ - 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. - * @psalm-suppress RedundantConditionGivenDocblockType, DocblockTypeContradiction - */ - 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 (null !== $this->view && method_exists($this->view, '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 8d605efdf..000000000 --- a/src/Helper/Service/FlashMessengerFactory.php +++ /dev/null @@ -1,57 +0,0 @@ -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 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 775d1e560..f5cc770bd 100644 --- a/src/HelperPluginManager.php +++ b/src/HelperPluginManager.php @@ -71,9 +71,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, @@ -158,7 +155,6 @@ class HelperPluginManager extends AbstractPluginManager protected $factories = [ Helper\Asset::class => Helper\Service\AssetFactory::class, Helper\HtmlAttributes::class => InvokableFactory::class, - Helper\FlashMessenger::class => Helper\Service\FlashMessengerFactory::class, Helper\Identity::class => Helper\Service\IdentityFactory::class, Helper\BasePath::class => InvokableFactory::class, Helper\Cycle::class => InvokableFactory::class, diff --git a/src/Renderer/PhpRenderer.php b/src/Renderer/PhpRenderer.php index 7e3aeee47..6f55baa73 100644 --- a/src/Renderer/PhpRenderer.php +++ b/src/Renderer/PhpRenderer.php @@ -55,7 +55,6 @@ * @method mixed escapeHtmlAttr($value, $recurse = \Laminas\View\Helper\Escaper\AbstractHelper::RECURSE_NONE) * @method mixed escapeJs($value, $recurse = \Laminas\View\Helper\Escaper\AbstractHelper::RECURSE_NONE) * @method mixed escapeUrl($value, $recurse = \Laminas\View\Helper\Escaper\AbstractHelper::RECURSE_NONE) - * @method \Laminas\View\Helper\FlashMessenger flashMessenger($namespace = null) * @method \Laminas\View\Helper\Gravatar gravatar($email = "", $options = array(), $attribs = array()) * @method \Laminas\View\Helper\HeadLink headLink(array $attributes = null, $placement = \Laminas\View\Helper\Placeholder\Container\AbstractContainer::APPEND) * @method \Laminas\View\Helper\HeadMeta headMeta($content = null, $keyValue = null, $keyType = 'name', $modifiers = array(), $placement = \Laminas\View\Helper\Placeholder\Container\AbstractContainer::APPEND) diff --git a/test/Helper/FlashMessengerTest.php b/test/Helper/FlashMessengerTest.php deleted file mode 100644 index 11ff5c401..000000000 --- a/test/Helper/FlashMessengerTest.php +++ /dev/null @@ -1,558 +0,0 @@ -mvcPluginClass = V3PluginFlashMessenger::class; - /** @psalm-suppress DeprecatedClass */ - $this->helper = new FlashMessenger(); - $this->plugin = $this->helper->getPluginFlashMessenger(); - } - - public function seedMessages(): void - { - /** @psalm-suppress DeprecatedClass */ - $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(): void - { - /** @psalm-suppress DeprecatedClass */ - $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 = []): ServiceManager - { - $config = new Config([ - 'services' => [ - 'config' => $config, - ], - 'factories' => [ - 'ControllerPluginManager' => fn(ContainerInterface $services) => new PluginManager($services, [ - 'invokables' => [ - 'flashmessenger' => $this->mvcPluginClass, - ], - ]), - 'ViewHelperManager' => static fn(ContainerInterface $services) - => new HelperPluginManager($services), - ], - ]); - $sm = new ServiceManager(); - $config->configureServiceManager($sm); - return $sm; - } - - public function testCanAssertPluginClass(): void - { - $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(): void - { - $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(): void - { - $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(): void - { - $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(): void - { - $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(): void - { - $plugin = $this->prophesize($this->mvcPluginClass); - $plugin->getMessagesFromNamespace('info')->will(fn() => []); - $plugin->addInfoMessage('bar-info')->will(function ($args) { - $this->getMessagesFromNamespace('info')->willReturn([$args[0]]); - return null; - }); - - $this->helper->setPluginFlashMessenger($plugin->reveal()); - - $displayInfoAssertion = ''; - $displayInfo = $this->helper->render('info'); - $this->assertEquals($displayInfoAssertion, $displayInfo); - - $helper = new FlashMessenger(); - $helper->setPluginFlashMessenger($plugin->reveal()); - $helper->addInfoMessage('bar-info'); - unset($helper); - - $displayInfoAssertion = '
    • bar-info
    '; - $displayInfo = $this->helper->render('info'); - $this->assertEquals($displayInfoAssertion, $displayInfo); - } - - /** - * @runInSeparateProcess - * @preserveGlobalState disabled - */ - public function testCanDisplayListOfCurrentMessages(): void - { - $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(): void - { - $helper = $this->helper; - $this->seedMessages(); - - $displayInfoAssertion = '
    • foo
    • bar
    '; - $displayInfo = $helper()->render(); - $this->assertEquals($displayInfoAssertion, $displayInfo); - } - - public function testCanDisplayListOfMessagesByDefaultCurrentParameters(): void - { - $helper = $this->helper; - $this->seedCurrentMessages(); - - $displayInfoAssertion = '
    • foo
    • bar
    '; - $displayInfo = $helper()->renderCurrent(); - $this->assertEquals($displayInfoAssertion, $displayInfo); - } - - public function testCanDisplayListOfMessagesByInvoke(): void - { - $helper = $this->helper; - $this->seedMessages(); - - $displayInfoAssertion = '
    • bar-info
    '; - $displayInfo = $helper()->render('info'); - $this->assertEquals($displayInfoAssertion, $displayInfo); - } - - public function testCanDisplayListOfCurrentMessagesByInvoke(): void - { - $helper = $this->helper; - $this->seedCurrentMessages(); - - $displayInfoAssertion = '
    • bar-info
    '; - $displayInfo = $helper()->renderCurrent('info'); - $this->assertEquals($displayInfoAssertion, $displayInfo); - } - - public function testCanDisplayListOfMessagesCustomised(): void - { - $this->seedMessages(); - - $displayInfoAssertion = '

    bar-info

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

    ') - ->setMessageSeparatorString('

    ') - ->setMessageCloseString('

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

    bar-info

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

    ') - ->setMessageSeparatorString('

    ') - ->setMessageCloseString('

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

    foo

    bar

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

    ') - ->setMessageCloseString('

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

    foo

    bar

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

    ') - ->setMessageCloseString('

    ') - ->renderCurrent('default', ['foo-baz', 'foo-bar']); - $this->assertEquals($displayInfoAssertion, $displayInfo); - } - - public function testCanDisplayListOfMessagesCustomisedByConfig(): void - { - $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(): void - { - $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(): void - { - $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'); - - // @codingStandardsIgnoreStart - $displayInfoAssertion = '
    • foo
    • bar
    '; - // @codingStandardsIgnoreEnd - $displayInfo = $helper->render('default', ['foo-baz', 'foo-bar']); - $this->assertEquals($displayInfoAssertion, $displayInfo); - } - - public function testCanDisplayListOfCurrentMessagesCustomisedByConfigSeparator(): void - { - $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'); - - // @codingStandardsIgnoreStart - $displayInfoAssertion = '
    • foo
    • bar
    '; - // @codingStandardsIgnoreEnd - $displayInfo = $helper->renderCurrent('default', ['foo-baz', 'foo-bar']); - $this->assertEquals($displayInfoAssertion, $displayInfo); - } - - public function testCanTranslateMessages(): void - { - $mockTranslator = $this->prophesize(Translator::class); - $mockTranslator->translate('bar-info', 'default')->willReturn('translated message')->shouldBeCalledTimes(1); - - $this->helper->setTranslator($mockTranslator->reveal()); - $this->assertTrue($this->helper->hasTranslator()); - - $this->seedMessages(); - - $displayAssertion = '
    • translated message
    '; - $display = $this->helper->render('info'); - $this->assertEquals($displayAssertion, $display); - } - - public function testCanTranslateCurrentMessages(): void - { - $mockTranslator = $this->prophesize(Translator::class); - $mockTranslator->translate('bar-info', 'default')->willReturn('translated message')->shouldBeCalledTimes(1); - - $this->helper->setTranslator($mockTranslator->reveal()); - $this->assertTrue($this->helper->hasTranslator()); - - $this->seedCurrentMessages(); - - $displayAssertion = '
    • translated message
    '; - $display = $this->helper->renderCurrent('info'); - $this->assertEquals($displayAssertion, $display); - } - - public function testAutoEscapeDefaultsToTrue(): void - { - $this->assertTrue($this->helper->getAutoEscape()); - } - - public function testCanSetAutoEscape(): void - { - $this->helper->setAutoEscape(false); - $this->assertFalse($this->helper->getAutoEscape()); - - $this->helper->setAutoEscape(true); - $this->assertTrue($this->helper->getAutoEscape()); - } - - /** - * @covers \Laminas\View\Helper\FlashMessenger::render - */ - public function testMessageIsEscapedByDefault(): void - { - $helper = new FlashMessenger(); - $helper->addMessage('Foo
    bar'); - unset($helper); - - $displayAssertion = '
    • Foo<br />bar
    '; - $display = $this->helper->render('default'); - $this->assertSame($displayAssertion, $display); - } - - /** - * @covers \Laminas\View\Helper\FlashMessenger::render - */ - public function testMessageIsNotEscapedWhenAutoEscapeIsFalse(): void - { - $helper = new FlashMessenger(); - $helper->addMessage('Foo
    bar'); - unset($helper); - - $displayAssertion = '
    • Foo
      bar
    '; - $display = $this->helper->setAutoEscape(false) - ->render('default'); - $this->assertSame($displayAssertion, $display); - } - - /** - * @covers \Laminas\View\Helper\FlashMessenger::render - */ - public function testCanSetAutoEscapeOnRender(): void - { - $helper = new FlashMessenger(); - $helper->addMessage('Foo
    bar'); - unset($helper); - - $displayAssertion = '
    • Foo
      bar
    '; - $display = $this->helper->render('default', [], false); - $this->assertSame($displayAssertion, $display); - } - - /** - * @covers \Laminas\View\Helper\FlashMessenger::render - */ - public function testRenderUsesCurrentAutoEscapeByDefault(): void - { - $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 \Laminas\View\Helper\FlashMessenger::renderCurrent - */ - public function testCurrentMessageIsEscapedByDefault(): void - { - $this->helper->addMessage('Foo
    bar'); - - $displayAssertion = '
    • Foo<br />bar
    '; - $display = $this->helper->renderCurrent('default'); - $this->assertSame($displayAssertion, $display); - } - - /** - * @covers \Laminas\View\Helper\FlashMessenger::renderCurrent - */ - public function testCurrentMessageIsNotEscapedWhenAutoEscapeIsFalse(): void - { - $this->helper->addMessage('Foo
    bar'); - - $displayAssertion = '
    • Foo
      bar
    '; - $display = $this->helper->setAutoEscape(false) - ->renderCurrent('default'); - $this->assertSame($displayAssertion, $display); - } - - /** - * @covers \Laminas\View\Helper\FlashMessenger::renderCurrent - */ - public function testCanSetAutoEscapeOnRenderCurrent(): void - { - $this->helper->addMessage('Foo
    bar'); - - $displayAssertion = '
    • Foo
      bar
    '; - $display = $this->helper->renderCurrent('default', [], false); - $this->assertSame($displayAssertion, $display); - } - - /** - * @covers \Laminas\View\Helper\FlashMessenger::renderCurrent - */ - public function testRenderCurrentUsesCurrentAutoEscapeByDefault(): void - { - $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 50ccba2e6..cdbb2fb67 100644 --- a/test/HelperPluginManagerCompatibilityTest.php +++ b/test/HelperPluginManagerCompatibilityTest.php @@ -6,7 +6,6 @@ use Generator; use Laminas\Mvc\Controller\PluginManager as ControllerPluginManager; -use Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger; use Laminas\ServiceManager\Config; use Laminas\ServiceManager\ServiceManager; use Laminas\ServiceManager\Test\CommonPluginManagerTrait; @@ -30,9 +29,7 @@ protected function getPluginManager(): HelperPluginManager if (class_exists(ControllerPluginManager::class)) { // @codingStandardsIgnoreLine $factories['ControllerPluginManager'] = static fn(ContainerInterface $services): ControllerPluginManager => new ControllerPluginManager($services, [ - 'invokables' => [ - 'flashmessenger' => FlashMessenger::class, - ], + 'invokables' => [], ]); } @@ -63,11 +60,6 @@ public function aliasProvider(): Generator $aliases = $r->getValue($pluginManager); foreach ($aliases as $alias => $target) { - // Skipping conditionally since it depends on laminas-mvc - if (! class_exists(ControllerPluginManager::class) && strpos($target, '\\FlashMessenger')) { - continue; - } - // Skipping conditionally since it depends on laminas-mvc if (! class_exists(ControllerPluginManager::class) && strpos($target, '\\Url')) { continue;