GenericEventTest.php 3.5 KB

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