LegacyEventDispatcherTest.php 2.6 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\Component\EventDispatcher\Tests;
  11. use Symfony\Component\EventDispatcher\Event;
  12. use Symfony\Component\EventDispatcher\EventDispatcher;
  13. use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
  14. use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
  15. /**
  16. * @group legacy
  17. */
  18. class LegacyEventDispatcherTest extends EventDispatcherTest
  19. {
  20. /**
  21. * @group legacy
  22. * @expectedDeprecation The signature of the "Symfony\Component\EventDispatcher\Tests\TestLegacyEventDispatcher::dispatch()" method should be updated to "dispatch($event, string $eventName = null)", not doing so is deprecated since Symfony 4.3.
  23. * @expectedDeprecation Calling the "Symfony\Contracts\EventDispatcher\EventDispatcherInterface::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead.
  24. */
  25. public function testLegacySignatureWithoutEvent()
  26. {
  27. $this->createEventDispatcher()->dispatch('foo');
  28. }
  29. /**
  30. * @group legacy
  31. * @expectedDeprecation The signature of the "Symfony\Component\EventDispatcher\Tests\TestLegacyEventDispatcher::dispatch()" method should be updated to "dispatch($event, string $eventName = null)", not doing so is deprecated since Symfony 4.3.
  32. * @expectedDeprecation Calling the "Symfony\Contracts\EventDispatcher\EventDispatcherInterface::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead.
  33. */
  34. public function testLegacySignatureWithEvent()
  35. {
  36. $this->createEventDispatcher()->dispatch('foo', new Event());
  37. }
  38. /**
  39. * @expectedException \TypeError
  40. * @expectedExceptionMessage Argument 1 passed to "Symfony\Contracts\EventDispatcher\EventDispatcherInterface::dispatch()" must be an object, string given.
  41. */
  42. public function testLegacySignatureWithNewEventObject()
  43. {
  44. $this->createEventDispatcher()->dispatch('foo', new ContractsEvent());
  45. }
  46. protected function createEventDispatcher()
  47. {
  48. return LegacyEventDispatcherProxy::decorate(new TestLegacyEventDispatcher());
  49. }
  50. }
  51. class TestLegacyEventDispatcher extends EventDispatcher
  52. {
  53. public function dispatch($eventName, Event $event = null)
  54. {
  55. return parent::dispatch($event, $eventName);
  56. }
  57. }