EventTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\Component\EventDispatcher;
  10. use Symfony\Component\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. $this->assertFalse($event->hasParameter('oof'), '->hasParameter() returns false if the parameter is not defined');
  33. try {
  34. $event->getParameter('foobar');
  35. $this->fail('->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist');
  36. } catch (\Exception $e) {
  37. $this->assertInstanceOf('\InvalidArgumentException', $e, '->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist');
  38. $this->assertEquals('The event "name" has no "foobar" parameter.', $e->getMessage(), '->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist');
  39. }
  40. $event = new Event($this->subject, 'name', $this->parameters);
  41. }
  42. public function testSetGetReturnValue()
  43. {
  44. $event = $this->createEvent();
  45. $event->setReturnValue('foo');
  46. $this->assertEquals('foo', $event->getReturnValue(), '->getReturnValue() returns the return value of the event');
  47. }
  48. public function testSetIsProcessed()
  49. {
  50. $event = $this->createEvent();
  51. $event->setProcessed(true);
  52. $this->assertTrue($event->isProcessed(), '->isProcessed() returns true if the event has been processed');
  53. $event->setProcessed(false);
  54. $this->assertFalse($event->isProcessed(), '->setProcessed() changes the processed status');
  55. }
  56. protected function createEvent()
  57. {
  58. $this->subject = new \stdClass();
  59. $this->parameters = array('foo' => 'bar');
  60. return new Event($this->subject, 'name', $this->parameters);
  61. }
  62. }