EventTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Components\EventDispatcher;
  10. use Symfony\Components\EventDispatcher\Event;
  11. class EventTest extends \PHPUnit_Framework_TestCase
  12. {
  13. protected $subject;
  14. protected $parameters;
  15. public function testGetSubject()
  16. {
  17. $event = $this->createEvent();
  18. $this->assertEquals($this->subject, $event->getSubject(), '->getSubject() returns the event subject');
  19. }
  20. public function testGetName()
  21. {
  22. $this->assertEquals('name', $this->createEvent()->getName(), '->getName() returns the event name');
  23. }
  24. public function testParameters()
  25. {
  26. $event = $this->createEvent();
  27. $this->assertEquals($this->parameters, $event->getParameters(), '->getParameters() returns the event parameters');
  28. $this->assertEquals('bar', $event->getParameter('foo'), '->getParameter() returns the value of a parameter');
  29. $event->setParameter('foo', 'foo');
  30. $this->assertEquals('foo', $event->getParameter('foo'), '->setParameter() changes the value of a parameter');
  31. $this->assertTrue($event->hasParameter('foo'), '->hasParameter() returns true if the parameter is defined');
  32. unset($event['foo']);
  33. $this->assertFalse($event->hasParameter('foo'), '->hasParameter() returns false if the parameter is not defined');
  34. try
  35. {
  36. $event->getParameter('foobar');
  37. $this->fail('->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist');
  38. }
  39. catch (\Exception $e)
  40. {
  41. $this->assertType('\InvalidArgumentException', $e, '->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist');
  42. $this->assertEquals('The event "name" has no "foobar" parameter.', $e->getMessage(), '->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist');
  43. }
  44. $event = new Event($this->subject, 'name', $this->parameters);
  45. }
  46. public function testSetGetReturnValue()
  47. {
  48. $event = $this->createEvent();
  49. $event->setReturnValue('foo');
  50. $this->assertEquals('foo', $event->getReturnValue(), '->getReturnValue() returns the return value of the event');
  51. }
  52. public function testSetIsProcessed()
  53. {
  54. $event = $this->createEvent();
  55. $event->setProcessed(true);
  56. $this->assertTrue($event->isProcessed(), '->isProcessed() returns true if the event has been processed');
  57. $event->setProcessed(false);
  58. $this->assertFalse($event->isProcessed(), '->setProcessed() changes the processed status');
  59. }
  60. public function testArrayAccessInterface()
  61. {
  62. $event = $this->createEvent();
  63. $this->assertEquals('bar', $event['foo'], 'Event implements the ArrayAccess interface');
  64. $event['foo'] = 'foo';
  65. $this->assertEquals('foo', $event['foo'], 'Event implements the ArrayAccess interface');
  66. try
  67. {
  68. $event['foobar'];
  69. $this->fail('::offsetGet() throws an \InvalidArgumentException exception when the parameter does not exist');
  70. }
  71. catch (\Exception $e)
  72. {
  73. $this->assertType('\InvalidArgumentException', $e, '::offsetGet() throws an \InvalidArgumentException exception when the parameter does not exist');
  74. $this->assertEquals('The event "name" has no "foobar" parameter.', $e->getMessage(), '::offsetGet() throws an \InvalidArgumentException exception when the parameter does not exist');
  75. }
  76. $this->assertTrue(isset($event['foo']), 'Event implements the ArrayAccess interface');
  77. unset($event['foo']);
  78. $this->assertFalse(isset($event['foo']), 'Event implements the ArrayAccess interface');
  79. }
  80. protected function createEvent()
  81. {
  82. $this->subject = new \stdClass();
  83. $this->parameters = array('foo' => 'bar');
  84. return new Event($this->subject, 'name', $this->parameters);
  85. }
  86. }