-
Notifications
You must be signed in to change notification settings - Fork 183
Added an optional flashmessage listener #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9e02547
Added a flashmessage listener
Miliooo f21d3ea
some updates
Miliooo ef1ee68
some refactoring with the key and the config
Miliooo 8e1d37b
refactored to use parameter
Miliooo 31de539
fixed naming of test
Miliooo 97fd7dd
updated config reference
Miliooo fb105f3
added config tests
Miliooo ac283df
test updates
Miliooo f884e39
small updates, just hope this time the travis build passes
Miliooo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| <?php | ||
| namespace FOS\MessageBundle\EventListener; | ||
|
|
||
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
| use FOS\MessageBundle\Event\FOSMessageEvents; | ||
| use Symfony\Component\Translation\TranslatorInterface; | ||
| use Symfony\Component\HttpFoundation\Session\Session; | ||
| use Symfony\Component\EventDispatcher\Event; | ||
|
|
||
| /** | ||
| * The flash listener adds flash messages when certain message events occur | ||
| * | ||
| * @author Michiel Boeckaert <[email protected]> | ||
| */ | ||
| class FlashListener implements EventSubscriberInterface | ||
| { | ||
| private static $successMessages = array( | ||
| FOSMessageEvents::POST_SEND => 'flash_post_send_success', | ||
| FOSMessageEvents::POST_DELETE => 'flash_thread_delete_success', | ||
| FOSMessageEvents::POST_UNDELETE => 'flash_thread_undelete_success', | ||
| ); | ||
|
|
||
| /** | ||
| * Translator | ||
| * | ||
| * @var TranslatorInterface | ||
| */ | ||
| protected $translator; | ||
|
|
||
| /** | ||
| * Session | ||
| * | ||
| * @var Session | ||
| */ | ||
| protected $session; | ||
|
|
||
| /** | ||
| * The flash key | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $key; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
| * @param Session $session The current session | ||
| * @param TranslatorInterface $translator A translator instance | ||
| * @param string $key The flash key | ||
| */ | ||
| public function __construct(Session $session, TranslatorInterface $translator, $key) | ||
| { | ||
| $this->session = $session; | ||
| $this->translator = $translator; | ||
| $this->key = $key; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public static function getSubscribedEvents() | ||
| { | ||
| return array( | ||
| FOSMessageEvents::POST_SEND => 'addSuccessFlash', | ||
| FOSMessageEvents::POST_DELETE => 'addSuccessFlash', | ||
| FOSMessageEvents::POST_UNDELETE => 'addSuccessFlash' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a flashmessage to the session | ||
| * | ||
| * @param Event $event The current event | ||
| * | ||
| * @throws \InvalidArgumentException | ||
| */ | ||
| public function addSuccessFlash(Event $event) | ||
| { | ||
| $eventName = $event->getname(); | ||
|
|
||
| if (!isset(self::$successMessages[$eventName])) { | ||
| throw new \InvalidArgumentException('This event does not correspond to a known flash message'); | ||
| } | ||
|
|
||
| $this->session->getFlashBag()->add($this->key, $this->trans(self::$successMessages[$eventName])); | ||
| } | ||
|
|
||
| private function trans($message, array $params = array()) | ||
| { | ||
| return $this->translator->trans($message, $params, 'FOSMessageBundle'); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
|
|
||
| <container xmlns="http://symfony.com/schema/dic/services" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
|
||
| <parameters> | ||
| <parameter key="fos_message.flash_listener.class">FOS\MessageBundle\EventListener\FlashListener</parameter> | ||
| </parameters> | ||
|
|
||
| <services> | ||
|
|
||
| <service id="fos_message.flash_listener" class="%fos_message.flash_listener.class%"> | ||
| <argument type="service" id="session" /> | ||
| <argument type="service" id="translator" /> | ||
| <argument>%fos_message.flash_messages_key%</argument> | ||
| <tag name="kernel.event_subscriber" /> | ||
| </service> | ||
|
|
||
| </services> | ||
|
|
||
| </container> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| <?php | ||
|
|
||
| namespace FOS\MessageBundle\Tests\DependencyInjection; | ||
|
|
||
| use FOS\MessageBundle\DependencyInjection\FOSMessageExtension; | ||
| use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
| use Symfony\Component\Yaml\Parser; | ||
|
|
||
| class FOSMessageExtensionTest extends \PHPUnit_Framework_TestCase | ||
| { | ||
|
|
||
| /** @var ContainerBuilder */ | ||
| protected $containerBuilder; | ||
|
|
||
| /** | ||
| * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException | ||
| */ | ||
| public function testMessageLoadThrowsExceptionUnlessDbDriverSet() | ||
| { | ||
| $loader = new FOSMessageExtension(); | ||
| $config = $this->getEmptyConfig(); | ||
| unset($config['db_driver']); | ||
| $loader->load(array($config), new ContainerBuilder()); | ||
| } | ||
|
|
||
| /** | ||
| * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException | ||
| */ | ||
| public function testMessageLoadThrowsExceptionUnlessThreadClassSet() | ||
| { | ||
| $loader = new FOSMessageExtension(); | ||
| $config = $this->getEmptyConfig(); | ||
| unset($config['thread_class']); | ||
| $loader->load(array($config), new ContainerBuilder()); | ||
| } | ||
|
|
||
| /** | ||
| * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException | ||
| */ | ||
| public function testMessageLoadThrowsExceptionUnlessMessageClassSet() | ||
| { | ||
| $loader = new FOSMessageExtension(); | ||
| $config = $this->getEmptyConfig(); | ||
| unset($config['message_class']); | ||
| $loader->load(array($config), new ContainerBuilder()); | ||
| } | ||
|
|
||
| /** | ||
| * @expectedException \InvalidArgumentException | ||
| * @expectedExceptionMessage Invalid db driver "foo" | ||
| */ | ||
| public function testUnsupportedDbDriverThrowsException() | ||
| { | ||
| $loader = new FOSMessageExtension(); | ||
| $config = $this->getEmptyConfig(); | ||
| $config['db_driver'] = "foo"; | ||
| $loader->load(array($config), new ContainerBuilder()); | ||
| } | ||
|
|
||
| public function testFlashesAreDisabledByDefault() | ||
| { | ||
| $this->createEmptyConfiguration(); | ||
| $this->assertNotHasDefinition('fos_message.flash_listener'); | ||
| } | ||
|
|
||
| public function testUserEnablesFlashes() | ||
| { | ||
| $this->containerBuilder = new ContainerBuilder(); | ||
| $loader = new FOSMessageExtension(); | ||
| $config = $this->getEmptyConfigWithFlashesEnabled(); | ||
| $loader->load(array($config), $this->containerBuilder); | ||
|
|
||
| $this->assertHasDefinition('fos_message.flash_listener'); | ||
| $this->assertParameter('success', 'fos_message.flash_messages_key'); | ||
| } | ||
|
|
||
| protected function createEmptyConfiguration() | ||
| { | ||
| $this->containerBuilder = new ContainerBuilder(); | ||
| $loader = new FOSMessageExtension(); | ||
| $config = $this->getEmptyConfig(); | ||
| $loader->load(array($config), $this->containerBuilder); | ||
| $this->assertTrue($this->containerBuilder instanceof ContainerBuilder); | ||
| } | ||
|
|
||
| /** | ||
| * gets an empty config | ||
| * | ||
| * @return array | ||
| */ | ||
| protected function getEmptyConfig() | ||
| { | ||
| $yaml = <<<EOF | ||
| db_driver: mongodb | ||
| thread_class: Acme\MyBundle\Entity\Thread | ||
| message_class: Acme\MyBundle\Entity\Message | ||
| EOF; | ||
| $parser = new Parser(); | ||
|
|
||
| return $parser->parse($yaml); | ||
| } | ||
|
|
||
| /** | ||
| * Gets an empty config but with the optional flash settings enabled | ||
| * | ||
| * @return array | ||
| */ | ||
| protected function getEmptyConfigWithFlashesEnabled() | ||
| { | ||
| $yaml = <<<EOF | ||
| db_driver: mongodb | ||
| thread_class: Acme\MyBundle\Entity\Thread | ||
| message_class: Acme\MyBundle\Entity\Message | ||
| flash_messages: | ||
| enabled: true | ||
| EOF; | ||
| $parser = new Parser(); | ||
|
|
||
| return $parser->parse($yaml); | ||
| } | ||
|
|
||
| /** | ||
| * Asserts that a parameter key has a certain value | ||
| * | ||
| * @param mixed $value | ||
| * @param string $key | ||
| */ | ||
| private function assertParameter($value, $key) | ||
| { | ||
| $this->assertEquals($value, $this->containerBuilder->getParameter($key), sprintf('%s parameter is correct', $key)); | ||
| } | ||
|
|
||
| /** | ||
| * Asserts that a definition exists | ||
| * | ||
| * @param string $id | ||
| */ | ||
| private function assertHasDefinition($id) | ||
| { | ||
| $this->assertTrue(($this->containerBuilder->hasDefinition($id) || $this->containerBuilder->hasAlias($id))); | ||
| } | ||
|
|
||
| /** | ||
| * Asserts that a definition does not exist | ||
| * | ||
| * @param string $id | ||
| */ | ||
| private function assertNotHasDefinition($id) | ||
| { | ||
| $this->assertFalse(($this->containerBuilder->hasDefinition($id) || $this->containerBuilder->hasAlias($id))); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
| namespace FOS\MessageBundle\Tests\EventListener; | ||
|
|
||
| use FOS\MessageBundle\EventListener\FlashListener; | ||
| use FOS\MessageBundle\Event\FOSMessageEvents; | ||
|
|
||
| /** | ||
| * Test for the Flash listener | ||
| * | ||
| * @author Michiel Boeckaert <[email protected]> | ||
| */ | ||
| class FlashListenerTest extends \PHPUnit_Framework_TestCase | ||
| { | ||
| private $listener; | ||
| private $session; | ||
| private $translator; | ||
| private $event; | ||
| private $key; | ||
|
|
||
| public function setUp() | ||
| { | ||
| $this->event = $this->getMock('Symfony\Component\EventDispatcher\Event'); | ||
|
|
||
| //if we use the interface getflashbag returns an error... | ||
| $this->session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->disableOriginalConstructor()->getMock(); | ||
| $this->translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface'); | ||
| $this->key = 'success'; | ||
| $this->listener = new FlashListener($this->session, $this->translator, $this->key); | ||
| } | ||
|
|
||
| /** | ||
| * @expectedException \InvalidArgumentException | ||
| * @expectedExceptionMessage This event does not correspond to a known flash message | ||
| */ | ||
| public function testAddFlashWithNonSupportedEvent() | ||
| { | ||
| $this->event->expects($this->once())->method('getName')->will($this->returnValue('foo')); | ||
| $this->listener->addSuccessFlash($this->event); | ||
| } | ||
|
|
||
| public function testAddFlashOnValidEvent() | ||
| { | ||
| $flashbagMock = $this->getMock('Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface'); | ||
|
|
||
| $this->event->expects($this->once())->method('getName')->will($this->returnValue(FOSMessageEvents::POST_SEND)); | ||
|
|
||
| $this->translator->expects($this->once()) | ||
| ->method('trans') | ||
| ->with('flash_post_send_success', array(), 'FOSMessageBundle') | ||
| ->will($this->returnValue('translatedString')); | ||
|
|
||
| $this->session->expects($this->once()) | ||
| ->method('getFlashBag') | ||
| ->will($this->returnValue($flashbagMock)); | ||
|
|
||
| $flashbagMock->expects($this->once()) | ||
| ->method('add') | ||
| ->with('success', 'translatedString'); | ||
|
|
||
| $this->listener->addSuccessFlash($this->event); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spacing