EventTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * EventTest file
  5. *
  6. * Test Case for Event class
  7. *
  8. * CakePHP : Rapid Development Framework (https://cakephp.org)
  9. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * For full copyright and license information, please see the LICENSE.txt
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  16. * @link https://cakephp.org CakePHP Project
  17. * @since 2.1.0
  18. * @license https://opensource.org/licenses/mit-license.php MIT License
  19. */
  20. namespace Cake\Test\TestCase\Event;
  21. use Cake\Core\Exception\CakeException;
  22. use Cake\Event\Event;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * Tests the Cake\Event\Event class functionality
  26. */
  27. class EventTest extends TestCase
  28. {
  29. /**
  30. * Tests the name() method
  31. *
  32. * @triggers fake.event
  33. */
  34. public function testName(): void
  35. {
  36. $event = new Event('fake.event');
  37. $this->assertSame('fake.event', $event->getName());
  38. }
  39. /**
  40. * Tests the subject() method
  41. *
  42. * @triggers fake.event $this
  43. * @triggers fake.event
  44. */
  45. public function testSubject(): void
  46. {
  47. $event = new Event('fake.event', $this);
  48. $this->assertSame($this, $event->getSubject());
  49. $this->expectException(CakeException::class);
  50. $this->expectExceptionMessage('No subject set for this event');
  51. $event = new Event('fake.event');
  52. $this->assertNull($event->getSubject());
  53. }
  54. /**
  55. * Tests the event propagation stopping property
  56. *
  57. * @triggers fake.event
  58. */
  59. public function testPropagation(): void
  60. {
  61. $event = new Event('fake.event');
  62. $this->assertFalse($event->isStopped());
  63. $event->stopPropagation();
  64. $this->assertTrue($event->isStopped());
  65. }
  66. /**
  67. * Tests that it is possible to get/set custom data in a event
  68. *
  69. * @triggers fake.event $this, array('some' => 'data')
  70. */
  71. public function testEventData(): void
  72. {
  73. $event = new Event('fake.event', $this, ['some' => 'data']);
  74. $this->assertEquals(['some' => 'data'], $event->getData());
  75. $this->assertSame('data', $event->getData('some'));
  76. $this->assertNull($event->getData('undef'));
  77. }
  78. /**
  79. * Tests that it is possible to get the name and subject directly
  80. *
  81. * @triggers fake.event $this
  82. */
  83. public function testEventDirectPropertyAccess(): void
  84. {
  85. $event = new Event('fake.event', $this);
  86. $this->assertEquals($this, $event->getSubject());
  87. $this->assertSame('fake.event', $event->getName());
  88. }
  89. }