EventTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * EventTest file
  4. *
  5. * Test Case for Event class
  6. *
  7. * CakePHP : Rapid Development Framework (https://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  15. * @link https://cakephp.org CakePHP Project
  16. * @since 2.1.0
  17. * @license https://opensource.org/licenses/mit-license.php MIT License
  18. */
  19. namespace Cake\Test\TestCase\Event;
  20. use ArrayObject;
  21. use Cake\Event\Event;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * Tests the Cake\Event\Event class functionality
  25. */
  26. class EventTest extends TestCase
  27. {
  28. /**
  29. * Tests the name() method
  30. *
  31. * @return void
  32. * @triggers fake.event
  33. */
  34. public function testName()
  35. {
  36. $event = new Event('fake.event');
  37. $this->assertEquals('fake.event', $event->getName());
  38. }
  39. /**
  40. * Tests the subject() method
  41. *
  42. * @return void
  43. * @triggers fake.event $this
  44. * @triggers fake.event
  45. */
  46. public function testSubject()
  47. {
  48. $event = new Event('fake.event', $this);
  49. $this->assertSame($this, $event->getSubject());
  50. $event = new Event('fake.event');
  51. $this->assertNull($event->getSubject());
  52. }
  53. /**
  54. * Tests the event propagation stopping property
  55. *
  56. * @return void
  57. * @triggers fake.event
  58. */
  59. public function testPropagation()
  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. * @return void
  70. * @triggers fake.event $this, array('some' => 'data')
  71. */
  72. public function testEventData()
  73. {
  74. $event = new Event('fake.event', $this, ['some' => 'data']);
  75. $this->assertEquals(['some' => 'data'], $event->getData());
  76. $this->assertEquals('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. * @return void
  83. * @triggers fake.event $this, array('some' => 'data')
  84. */
  85. public function testEventDataObject()
  86. {
  87. $data = new ArrayObject(['some' => 'data']);
  88. $event = new Event('fake.event', $this, $data);
  89. $this->assertEquals(['some' => 'data'], $event->getData());
  90. $this->assertEquals('data', $event->getData('some'));
  91. $this->assertNull($event->getData('undef'));
  92. }
  93. /**
  94. * Tests that it is possible to get the name and subject directly
  95. *
  96. * @return void
  97. * @triggers fake.event $this
  98. */
  99. public function testEventDirectPropertyAccess()
  100. {
  101. $event = new Event('fake.event', $this);
  102. $this->assertEquals($this, $event->getSubject());
  103. $this->assertEquals('fake.event', $event->getName());
  104. }
  105. }