GenericEventTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\EventDispatcher\GenericEvent;
  13. /**
  14. * Test class for Event.
  15. */
  16. class GenericEventTest extends TestCase
  17. {
  18. /**
  19. * @var GenericEvent
  20. */
  21. private $event;
  22. private $subject;
  23. /**
  24. * Prepares the environment before running a test.
  25. */
  26. protected function setUp()
  27. {
  28. $this->subject = new \stdClass();
  29. $this->event = new GenericEvent($this->subject, ['name' => 'Event']);
  30. }
  31. /**
  32. * Cleans up the environment after running a test.
  33. */
  34. protected function tearDown()
  35. {
  36. $this->subject = null;
  37. $this->event = null;
  38. }
  39. public function testConstruct()
  40. {
  41. $this->assertEquals($this->event, new GenericEvent($this->subject, ['name' => 'Event']));
  42. }
  43. /**
  44. * Tests Event->getArgs().
  45. */
  46. public function testGetArguments()
  47. {
  48. // test getting all
  49. $this->assertSame(['name' => 'Event'], $this->event->getArguments());
  50. }
  51. public function testSetArguments()
  52. {
  53. $result = $this->event->setArguments(['foo' => 'bar']);
  54. $this->assertSame(['foo' => 'bar'], $this->event->getArguments());
  55. $this->assertSame($this->event, $result);
  56. }
  57. public function testSetArgument()
  58. {
  59. $result = $this->event->setArgument('foo2', 'bar2');
  60. $this->assertSame(['name' => 'Event', 'foo2' => 'bar2'], $this->event->getArguments());
  61. $this->assertEquals($this->event, $result);
  62. }
  63. public function testGetArgument()
  64. {
  65. // test getting key
  66. $this->assertEquals('Event', $this->event->getArgument('name'));
  67. }
  68. public function testGetArgException()
  69. {
  70. $this->expectException('\InvalidArgumentException');
  71. $this->event->getArgument('nameNotExist');
  72. }
  73. public function testOffsetGet()
  74. {
  75. // test getting key
  76. $this->assertEquals('Event', $this->event['name']);
  77. // test getting invalid arg
  78. $this->expectException('InvalidArgumentException');
  79. $this->assertFalse($this->event['nameNotExist']);
  80. }
  81. public function testOffsetSet()
  82. {
  83. $this->event['foo2'] = 'bar2';
  84. $this->assertSame(['name' => 'Event', 'foo2' => 'bar2'], $this->event->getArguments());
  85. }
  86. public function testOffsetUnset()
  87. {
  88. unset($this->event['name']);
  89. $this->assertSame([], $this->event->getArguments());
  90. }
  91. public function testOffsetIsset()
  92. {
  93. $this->assertArrayHasKey('name', $this->event);
  94. $this->assertArrayNotHasKey('nameNotExist', $this->event);
  95. }
  96. public function testHasArgument()
  97. {
  98. $this->assertTrue($this->event->hasArgument('name'));
  99. $this->assertFalse($this->event->hasArgument('nameNotExist'));
  100. }
  101. public function testGetSubject()
  102. {
  103. $this->assertSame($this->subject, $this->event->getSubject());
  104. }
  105. public function testHasIterator()
  106. {
  107. $data = [];
  108. foreach ($this->event as $key => $value) {
  109. $data[$key] = $value;
  110. }
  111. $this->assertEquals(['name' => 'Event'], $data);
  112. }
  113. }