vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Tests/ContainerAwareEventDispatcherTest.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony framework.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * This source file is subject to the MIT license that is bundled
       
     9  * with this source code in the file LICENSE.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\FrameworkBundle\Tests;
       
    13 
       
    14 use Symfony\Component\DependencyInjection\Container;
       
    15 use Symfony\Bundle\FrameworkBundle\ContainerAwareEventDispatcher;
       
    16 use Symfony\Component\EventDispatcher\Event;
       
    17 use Symfony\Component\DependencyInjection\Scope;
       
    18 
       
    19 class ContainerAwareEventDispatcherTest extends \PHPUnit_Framework_TestCase
       
    20 {
       
    21     public function testAddAListenerService()
       
    22     {
       
    23         $event = new Event();
       
    24 
       
    25         $service = $this->getMock('Symfony\Bundle\FrameworkBundle\Tests\Service');
       
    26 
       
    27         $service
       
    28             ->expects($this->once())
       
    29             ->method('onEvent')
       
    30             ->with($event)
       
    31         ;
       
    32 
       
    33         $container = new Container();
       
    34         $container->set('service.listener', $service);
       
    35 
       
    36         $dispatcher = new ContainerAwareEventDispatcher($container);
       
    37         $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
       
    38 
       
    39         $dispatcher->dispatch('onEvent', $event);
       
    40     }
       
    41 
       
    42     public function testPreventDuplicateListenerService()
       
    43     {
       
    44         $event = new Event();
       
    45 
       
    46         $service = $this->getMock('Symfony\Bundle\FrameworkBundle\Tests\Service');
       
    47 
       
    48         $service
       
    49             ->expects($this->once())
       
    50             ->method('onEvent')
       
    51             ->with($event)
       
    52         ;
       
    53 
       
    54         $container = new Container();
       
    55         $container->set('service.listener', $service);
       
    56 
       
    57         $dispatcher = new ContainerAwareEventDispatcher($container);
       
    58         $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 5);
       
    59         $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 10);
       
    60 
       
    61         $dispatcher->dispatch('onEvent', $event);
       
    62     }
       
    63 
       
    64     /**
       
    65      * @expectedException \InvalidArgumentException
       
    66      */
       
    67     public function testTriggerAListenerServiceOutOfScope()
       
    68     {
       
    69         $service = $this->getMock('Symfony\Bundle\FrameworkBundle\Tests\Service');
       
    70 
       
    71         $scope = new Scope('scope');
       
    72         $container = new Container();
       
    73         $container->addScope($scope);
       
    74         $container->enterScope('scope');
       
    75 
       
    76         $container->set('service.listener', $service, 'scope');
       
    77 
       
    78         $dispatcher = new ContainerAwareEventDispatcher($container);
       
    79         $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
       
    80 
       
    81         $container->leaveScope('scope');
       
    82         $dispatcher->dispatch('onEvent');
       
    83     }
       
    84 
       
    85     public function testReEnteringAScope()
       
    86     {
       
    87         $event = new Event();
       
    88 
       
    89         $service1 = $this->getMock('Symfony\Bundle\FrameworkBundle\Tests\Service');
       
    90 
       
    91         $service1
       
    92             ->expects($this->exactly(2))
       
    93             ->method('onEvent')
       
    94             ->with($event)
       
    95         ;
       
    96 
       
    97         $scope = new Scope('scope');
       
    98         $container = new Container();
       
    99         $container->addScope($scope);
       
   100         $container->enterScope('scope');
       
   101 
       
   102         $container->set('service.listener', $service1, 'scope');
       
   103 
       
   104         $dispatcher = new ContainerAwareEventDispatcher($container);
       
   105         $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
       
   106         $dispatcher->dispatch('onEvent', $event);
       
   107 
       
   108         $service2 = $this->getMock('Symfony\Bundle\FrameworkBundle\Tests\Service');
       
   109 
       
   110         $service2
       
   111             ->expects($this->once())
       
   112             ->method('onEvent')
       
   113             ->with($event)
       
   114         ;
       
   115 
       
   116         $container->enterScope('scope');
       
   117         $container->set('service.listener', $service2, 'scope');
       
   118 
       
   119         $dispatcher->dispatch('onEvent', $event);
       
   120 
       
   121         $container->leaveScope('scope');
       
   122 
       
   123         $dispatcher->dispatch('onEvent');
       
   124     }
       
   125 }
       
   126 
       
   127 class Service
       
   128 {
       
   129     function onEvent(Event $e)
       
   130     {
       
   131     }
       
   132 }