EventListTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP Project
  13. * @since 3.3.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Event;
  17. use Cake\Event\Event;
  18. use Cake\Event\EventList;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Tests the Cake\Event\EvenList class functionality
  22. */
  23. class EventListTest extends TestCase
  24. {
  25. /**
  26. * testAddEventAndFlush
  27. */
  28. public function testAddEventAndFlush(): void
  29. {
  30. $eventList = new EventList();
  31. $event = new Event('my_event', $this);
  32. $event2 = new Event('my_second_event', $this);
  33. $eventList->add($event);
  34. $eventList->add($event2);
  35. $this->assertCount(2, $eventList);
  36. $this->assertEquals($eventList[0], $event);
  37. $this->assertEquals($eventList[1], $event2);
  38. $eventList->flush();
  39. $this->assertCount(0, $eventList);
  40. }
  41. /**
  42. * Testing implemented \ArrayAccess and \Count methods
  43. */
  44. public function testArrayAccess(): void
  45. {
  46. $eventList = new EventList();
  47. $event = new Event('my_event', $this);
  48. $event2 = new Event('my_second_event', $this);
  49. $eventList->add($event);
  50. $eventList->add($event2);
  51. $this->assertCount(2, $eventList);
  52. $this->assertTrue($eventList->hasEvent('my_event'));
  53. $this->assertFalse($eventList->hasEvent('does-not-exist'));
  54. $this->assertEquals($eventList->offsetGet(0), $event);
  55. $this->assertEquals($eventList->offsetGet(1), $event2);
  56. $this->assertTrue($eventList->offsetExists(0));
  57. $this->assertTrue($eventList->offsetExists(1));
  58. $this->assertFalse($eventList->offsetExists(2));
  59. $eventList->offsetUnset(1);
  60. $this->assertCount(1, $eventList);
  61. $eventList->flush();
  62. $this->assertCount(0, $eventList);
  63. }
  64. }