ContainerAwareEventManagerTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Doctrine\Tests;
  11. use Symfony\Bridge\Doctrine\ContainerAwareEventManager;
  12. use Symfony\Component\DependencyInjection\Container;
  13. class ContainerAwareEventManagerTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected function setUp()
  16. {
  17. if (!class_exists('Symfony\Component\DependencyInjection\Container')) {
  18. $this->markTestSkipped('The "DependencyInjection" component is not available');
  19. }
  20. $this->container = new Container();
  21. $this->evm = new ContainerAwareEventManager($this->container);
  22. }
  23. public function testDispatchEvent()
  24. {
  25. $this->container->set('foobar', $listener1 = new MyListener());
  26. $this->evm->addEventListener('foo', 'foobar');
  27. $this->evm->addEventListener('foo', $listener2 = new MyListener());
  28. $this->evm->dispatchEvent('foo');
  29. $this->assertTrue($listener1->called);
  30. $this->assertTrue($listener2->called);
  31. }
  32. public function testRemoveEventListener()
  33. {
  34. $this->evm->addEventListener('foo', 'bar');
  35. $this->evm->addEventListener('foo', $listener = new MyListener());
  36. $listeners = array('foo' => array('_service_bar' => 'bar', spl_object_hash($listener) => $listener));
  37. $this->assertSame($listeners, $this->evm->getListeners());
  38. $this->assertSame($listeners['foo'], $this->evm->getListeners('foo'));
  39. $this->evm->removeEventListener('foo', $listener);
  40. $this->assertSame(array('_service_bar' => 'bar'), $this->evm->getListeners('foo'));
  41. $this->evm->removeEventListener('foo', 'bar');
  42. $this->assertSame(array(), $this->evm->getListeners('foo'));
  43. }
  44. }
  45. class MyListener
  46. {
  47. public $called = false;
  48. public function foo()
  49. {
  50. $this->called = true;
  51. }
  52. }