ZF-8115: FlashMessenger doesn't work in testing

Description

The FlashMessenger action helper is not test-friendly - if a controller test dispatches an action that adds a message, then resets the MVC system as it must, then dispatches an action that should show the message, the message won't be in the response as one would expect.

This is because:

  1. In Zend_Test_PHPUnit_ControllerTestCase::reset(), the session superglobal is always blown away ($_SESSION = array()), eliminating any messages that have been set. I think there should at least be an option to not do this, to support testing multiple requests pertaining to the same session, and dependent on session data. (I worked around it by just stashing the current value of $_SESSION before calling $this->bootstrap() in my test class and restoring it immediately after to simulate a persisted session.)

  2. Even though resetting the MVC system gets rid of the initial FlashMessenger instance and a new one is later created, the class stores its Zend_Session_Namespace object in a static protected variable and uses the existence of this variable as a flag to decide if it needs to retrieve messages from the session, killing the ability to use FlashMessenger in testing.

Comments

Mocking is a much better approach to this type of unit testing. See [~bittarman]'s blog post for an example of how to do that: http://rmauger.co.uk/2012/03/…

With his permission I will adapt the blog post into a manual page section.