EventListTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP Project
  12. * @since 3.3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Event;
  16. use Cake\Event\Event;
  17. use Cake\Event\EventStack;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Tests the Cake\Event\EvenStack class functionality
  21. */
  22. class EvenStackTest extends TestCase
  23. {
  24. /**
  25. * testAddEventAndFlush
  26. *
  27. * @return void
  28. */
  29. public function testAddEventAndFlush()
  30. {
  31. $eventStack = new EventStack();
  32. $event = new Event('my_event', $this);
  33. $event2 = new Event('my_second_event', $this);
  34. $eventStack->add($event);
  35. $eventStack->add($event2);
  36. $this->assertCount(2, $eventStack);
  37. $this->assertEquals($eventStack[0], $event);
  38. $this->assertEquals($eventStack[1], $event2);
  39. $eventStack->flush();
  40. $this->assertCount(0, $eventStack);
  41. }
  42. /**
  43. * Testing implemented \ArrayAccess and \Count methods
  44. *
  45. * @return void
  46. */
  47. public function testArrayAccess()
  48. {
  49. $eventStack = new EventStack();
  50. $event = new Event('my_event', $this);
  51. $event2 = new Event('my_second_event', $this);
  52. $eventStack->add($event);
  53. $eventStack->add($event2);
  54. $this->assertCount(2, $eventStack);
  55. $this->assertTrue($eventStack->hasEvent('my_event'));
  56. $this->assertFalse($eventStack->hasEvent('does-not-exist'));
  57. $this->assertEquals($eventStack->offsetGet(0), $event);
  58. $this->assertEquals($eventStack->offsetGet(1), $event2);
  59. $this->assertTrue($eventStack->offsetExists(0));
  60. $this->assertTrue($eventStack->offsetExists(1));
  61. $this->assertFalse($eventStack->offsetExists(2));
  62. $eventStack->offsetUnset(1);
  63. $this->assertCount(1, $eventStack);
  64. $eventStack->flush();
  65. $this->assertCount(0, $eventStack);
  66. }
  67. }