ContainerAwareEventDispatcherTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Symfony\Bundle\FrameworkBundle\Tests;
  3. use Symfony\Component\DependencyInjection\Container;
  4. use Symfony\Bundle\FrameworkBundle\ContainerAwareEventDispatcher;
  5. use Symfony\Component\EventDispatcher\Event;
  6. use Symfony\Component\DependencyInjection\Scope;
  7. class ContainerAwareEventDispatcherTest extends \PHPUnit_Framework_TestCase
  8. {
  9. public function testAddAListenerService()
  10. {
  11. $event = new Event();
  12. $service = $this->getMock('Symfony\Bundle\FrameworkBundle\Tests\Service');
  13. $service
  14. ->expects($this->once())
  15. ->method('onEvent')
  16. ->with($event)
  17. ;
  18. $container = new Container();
  19. $container->set('service.listener', $service);
  20. $dispatcher = new ContainerAwareEventDispatcher($container);
  21. $dispatcher->addListenerService('onEvent', 'service.listener');
  22. $dispatcher->dispatch('onEvent', $event);
  23. }
  24. public function testPreventDuplicateListenerService()
  25. {
  26. $event = new Event();
  27. $service = $this->getMock('Symfony\Bundle\FrameworkBundle\Tests\Service');
  28. $service
  29. ->expects($this->once())
  30. ->method('onEvent')
  31. ->with($event)
  32. ;
  33. $container = new Container();
  34. $container->set('service.listener', $service);
  35. $dispatcher = new ContainerAwareEventDispatcher($container);
  36. $dispatcher->addListenerService('onEvent', 'service.listener', 5);
  37. $dispatcher->addListenerService('onEvent', 'service.listener', 10);
  38. $dispatcher->dispatch('onEvent', $event);
  39. }
  40. /**
  41. * @expectedException \InvalidArgumentException
  42. */
  43. public function testTriggerAListenerServiceOutOfScope()
  44. {
  45. $service = $this->getMock('Symfony\Bundle\FrameworkBundle\Tests\Service');
  46. $scope = new Scope('scope');
  47. $container = new Container();
  48. $container->addScope($scope);
  49. $container->enterScope('scope');
  50. $container->set('service.listener', $service, 'scope');
  51. $dispatcher = new ContainerAwareEventDispatcher($container);
  52. $dispatcher->addListenerService('onEvent', 'service.listener');
  53. $container->leaveScope('scope');
  54. $dispatcher->dispatch('onEvent');
  55. }
  56. public function testReEnteringAScope()
  57. {
  58. $event = new Event();
  59. $service1 = $this->getMock('Symfony\Bundle\FrameworkBundle\Tests\Service');
  60. $service1
  61. ->expects($this->exactly(2))
  62. ->method('onEvent')
  63. ->with($event)
  64. ;
  65. $scope = new Scope('scope');
  66. $container = new Container();
  67. $container->addScope($scope);
  68. $container->enterScope('scope');
  69. $container->set('service.listener', $service1, 'scope');
  70. $dispatcher = new ContainerAwareEventDispatcher($container);
  71. $dispatcher->addListenerService('onEvent', 'service.listener');
  72. $dispatcher->dispatch('onEvent', $event);
  73. $service2 = $this->getMock('Symfony\Bundle\FrameworkBundle\Tests\Service');
  74. $service2
  75. ->expects($this->once())
  76. ->method('onEvent')
  77. ->with($event)
  78. ;
  79. $container->enterScope('scope');
  80. $container->set('service.listener', $service2, 'scope');
  81. $dispatcher->dispatch('onEvent', $event);
  82. $container->leaveScope('scope');
  83. $dispatcher->dispatch('onEvent');
  84. }
  85. }
  86. class Service
  87. {
  88. function onEvent(Event $e)
  89. {
  90. }
  91. }