EventTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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->assertTrue(!$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 (\InvalidArgumentException $e)
  40. {
  41. }
  42. $event = new Event($this->subject, 'name', $this->parameters);
  43. }
  44. public function testSetGetReturnValue()
  45. {
  46. $event = $this->createEvent();
  47. $event->setReturnValue('foo');
  48. $this->assertEquals('foo', $event->getReturnValue(), '->getReturnValue() returns the return value of the event');
  49. }
  50. public function testSetIsProcessed()
  51. {
  52. $event = $this->createEvent();
  53. $event->setProcessed(true);
  54. $this->assertEquals(true, $event->isProcessed(), '->isProcessed() returns true if the event has been processed');
  55. $event->setProcessed(false);
  56. $this->assertEquals(false, $event->isProcessed(), '->setProcessed() changes the processed status');
  57. }
  58. public function testArrayAccessInterface()
  59. {
  60. $event = $this->createEvent();
  61. $this->assertEquals('bar', $event['foo'], 'Event implements the ArrayAccess interface');
  62. $event['foo'] = 'foo';
  63. $this->assertEquals('foo', $event['foo'], 'Event implements the ArrayAccess interface');
  64. try
  65. {
  66. $event['foobar'];
  67. $this->fail('::offsetGet() throws an \InvalidArgumentException exception when the parameter does not exist');
  68. }
  69. catch (\InvalidArgumentException $e)
  70. {
  71. }
  72. $this->assertTrue(isset($event['foo']), 'Event implements the ArrayAccess interface');
  73. unset($event['foo']);
  74. $this->assertTrue(!isset($event['foo']), 'Event implements the ArrayAccess interface');
  75. }
  76. protected function createEvent()
  77. {
  78. $this->subject = new \stdClass();
  79. $this->parameters = array('foo' => 'bar');
  80. return new Event($this->subject, 'name', $this->parameters);
  81. }
  82. }