EventListTest.php 2.3 KB

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