GenericEventTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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->assertAttributeSame(['foo' => 'bar'], 'arguments', $this->event);
  55. $this->assertSame($this->event, $result);
  56. }
  57. public function testSetArgument()
  58. {
  59. $result = $this->event->setArgument('foo2', 'bar2');
  60. $this->assertAttributeSame(['name' => 'Event', 'foo2' => 'bar2'], 'arguments', $this->event);
  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. /**
  69. * @expectedException \InvalidArgumentException
  70. */
  71. public function testGetArgException()
  72. {
  73. $this->event->getArgument('nameNotExist');
  74. }
  75. public function testOffsetGet()
  76. {
  77. // test getting key
  78. $this->assertEquals('Event', $this->event['name']);
  79. // test getting invalid arg
  80. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
  81. $this->assertFalse($this->event['nameNotExist']);
  82. }
  83. public function testOffsetSet()
  84. {
  85. $this->event['foo2'] = 'bar2';
  86. $this->assertAttributeSame(['name' => 'Event', 'foo2' => 'bar2'], 'arguments', $this->event);
  87. }
  88. public function testOffsetUnset()
  89. {
  90. unset($this->event['name']);
  91. $this->assertAttributeSame([], 'arguments', $this->event);
  92. }
  93. public function testOffsetIsset()
  94. {
  95. $this->assertArrayHasKey('name', $this->event);
  96. $this->assertArrayNotHasKey('nameNotExist', $this->event);
  97. }
  98. public function testHasArgument()
  99. {
  100. $this->assertTrue($this->event->hasArgument('name'));
  101. $this->assertFalse($this->event->hasArgument('nameNotExist'));
  102. }
  103. public function testGetSubject()
  104. {
  105. $this->assertSame($this->subject, $this->event->getSubject());
  106. }
  107. public function testHasIterator()
  108. {
  109. $data = [];
  110. foreach ($this->event as $key => $value) {
  111. $data[$key] = $value;
  112. }
  113. $this->assertEquals(['name' => 'Event'], $data);
  114. }
  115. }