EventTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 ArrayObject;
  22. use Cake\Core\Exception\CakeException;
  23. use Cake\Event\Event;
  24. use Cake\TestSuite\TestCase;
  25. /**
  26. * Tests the Cake\Event\Event class functionality
  27. */
  28. class EventTest extends TestCase
  29. {
  30. /**
  31. * Tests the name() method
  32. *
  33. * @triggers fake.event
  34. */
  35. public function testName(): void
  36. {
  37. $event = new Event('fake.event');
  38. $this->assertSame('fake.event', $event->getName());
  39. }
  40. /**
  41. * Tests the subject() method
  42. *
  43. * @triggers fake.event $this
  44. * @triggers fake.event
  45. */
  46. public function testSubject(): void
  47. {
  48. $event = new Event('fake.event', $this);
  49. $this->assertSame($this, $event->getSubject());
  50. $this->expectException(CakeException::class);
  51. $this->expectExceptionMessage('No subject set for this event');
  52. $event = new Event('fake.event');
  53. $this->assertNull($event->getSubject());
  54. }
  55. /**
  56. * Tests the event propagation stopping property
  57. *
  58. * @triggers fake.event
  59. */
  60. public function testPropagation(): void
  61. {
  62. $event = new Event('fake.event');
  63. $this->assertFalse($event->isStopped());
  64. $event->stopPropagation();
  65. $this->assertTrue($event->isStopped());
  66. }
  67. /**
  68. * Tests that it is possible to get/set custom data in a event
  69. *
  70. * @triggers fake.event $this, array('some' => 'data')
  71. */
  72. public function testEventData(): void
  73. {
  74. $event = new Event('fake.event', $this, ['some' => 'data']);
  75. $this->assertEquals(['some' => 'data'], $event->getData());
  76. $this->assertSame('data', $event->getData('some'));
  77. $this->assertNull($event->getData('undef'));
  78. }
  79. /**
  80. * Tests that it is possible to get/set custom data in a event
  81. *
  82. * @triggers fake.event $this, array('some' => 'data')
  83. */
  84. public function testEventDataObject(): void
  85. {
  86. $data = new ArrayObject(['some' => 'data']);
  87. $event = new Event('fake.event', $this, $data);
  88. $this->assertEquals(['some' => 'data'], $event->getData());
  89. $this->assertSame('data', $event->getData('some'));
  90. $this->assertNull($event->getData('undef'));
  91. }
  92. /**
  93. * Tests that it is possible to get the name and subject directly
  94. *
  95. * @triggers fake.event $this
  96. */
  97. public function testEventDirectPropertyAccess(): void
  98. {
  99. $event = new Event('fake.event', $this);
  100. $this->assertEquals($this, $event->getSubject());
  101. $this->assertSame('fake.event', $event->getName());
  102. }
  103. }