GenericEventTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. parent::setUp();
  29. $this->subject = new \stdClass();
  30. $this->event = new GenericEvent($this->subject, array('name' => 'Event'));
  31. }
  32. /**
  33. * Cleans up the environment after running a test.
  34. */
  35. protected function tearDown()
  36. {
  37. $this->subject = null;
  38. $this->event = null;
  39. parent::tearDown();
  40. }
  41. public function testConstruct()
  42. {
  43. $this->assertEquals($this->event, new GenericEvent($this->subject, array('name' => 'Event')));
  44. }
  45. /**
  46. * Tests Event->getArgs().
  47. */
  48. public function testGetArguments()
  49. {
  50. // test getting all
  51. $this->assertSame(array('name' => 'Event'), $this->event->getArguments());
  52. }
  53. public function testSetArguments()
  54. {
  55. $result = $this->event->setArguments(array('foo' => 'bar'));
  56. $this->assertAttributeSame(array('foo' => 'bar'), 'arguments', $this->event);
  57. $this->assertSame($this->event, $result);
  58. }
  59. public function testSetArgument()
  60. {
  61. $result = $this->event->setArgument('foo2', 'bar2');
  62. $this->assertAttributeSame(array('name' => 'Event', 'foo2' => 'bar2'), 'arguments', $this->event);
  63. $this->assertEquals($this->event, $result);
  64. }
  65. public function testGetArgument()
  66. {
  67. // test getting key
  68. $this->assertEquals('Event', $this->event->getArgument('name'));
  69. }
  70. /**
  71. * @expectedException \InvalidArgumentException
  72. */
  73. public function testGetArgException()
  74. {
  75. $this->event->getArgument('nameNotExist');
  76. }
  77. public function testOffsetGet()
  78. {
  79. // test getting key
  80. $this->assertEquals('Event', $this->event['name']);
  81. // test getting invalid arg
  82. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
  83. $this->assertFalse($this->event['nameNotExist']);
  84. }
  85. public function testOffsetSet()
  86. {
  87. $this->event['foo2'] = 'bar2';
  88. $this->assertAttributeSame(array('name' => 'Event', 'foo2' => 'bar2'), 'arguments', $this->event);
  89. }
  90. public function testOffsetUnset()
  91. {
  92. unset($this->event['name']);
  93. $this->assertAttributeSame(array(), 'arguments', $this->event);
  94. }
  95. public function testOffsetIsset()
  96. {
  97. $this->assertTrue(isset($this->event['name']));
  98. $this->assertFalse(isset($this->event['nameNotExist']));
  99. }
  100. public function testHasArgument()
  101. {
  102. $this->assertTrue($this->event->hasArgument('name'));
  103. $this->assertFalse($this->event->hasArgument('nameNotExist'));
  104. }
  105. public function testGetSubject()
  106. {
  107. $this->assertSame($this->subject, $this->event->getSubject());
  108. }
  109. public function testHasIterator()
  110. {
  111. $data = array();
  112. foreach ($this->event as $key => $value) {
  113. $data[$key] = $value;
  114. }
  115. $this->assertEquals(array('name' => 'Event'), $data);
  116. }
  117. }