StaticIntegrationTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_EventManager
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. if (!defined('PHPUnit_MAIN_METHOD')) {
  22. define('PHPUnit_MAIN_METHOD', 'Zend_EventManager_StaticIntegrationTest::main');
  23. }
  24. require_once 'Zend/EventManager/EventManager.php';
  25. require_once 'Zend/EventManager/StaticEventManager.php';
  26. require_once 'Zend/EventManager/TestAsset/ClassWithEvents.php';
  27. require_once 'Zend/EventManager/TestAsset/StaticEventsMock.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_EventManager
  31. * @subpackage UnitTests
  32. * @group Zend_EventManager
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_EventManager_StaticIntegrationTest extends PHPUnit_Framework_TestCase
  37. {
  38. public static function main()
  39. {
  40. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  41. $result = PHPUnit_TextUI_TestRunner::run($suite);
  42. }
  43. public function setUp()
  44. {
  45. Zend_EventManager_StaticEventManager::resetInstance();
  46. }
  47. public function testCanConnectStaticallyToClassWithEvents()
  48. {
  49. $this->counter = (object) array('count' => 0);
  50. Zend_EventManager_StaticEventManager::getInstance()->attach(
  51. 'Zend_EventManager_TestAsset_ClassWithEvents',
  52. 'foo',
  53. array($this, 'advanceCounter')
  54. );
  55. $class = new Zend_EventManager_TestAsset_ClassWithEvents();
  56. $class->foo();
  57. $this->assertEquals(1, $this->counter->count);
  58. }
  59. public function testLocalHandlersAreExecutedPriorToStaticHandlersWhenSetWithSamePriority()
  60. {
  61. $this->test = (object) array('results' => array());
  62. Zend_EventManager_StaticEventManager::getInstance()->attach(
  63. 'Zend_EventManager_TestAsset_ClassWithEvents',
  64. 'foo',
  65. array($this, 'aggregateStatic')
  66. );
  67. $class = new Zend_EventManager_TestAsset_ClassWithEvents();
  68. $class->events()->attach('foo', array($this, 'aggregateLocal'));
  69. $class->foo();
  70. $this->assertEquals(array('local', 'static'), $this->test->results);
  71. }
  72. public function testLocalHandlersAreExecutedInPriorityOrderRegardlessOfStaticOrLocalRegistration()
  73. {
  74. $this->test = (object) array('results' => array());
  75. Zend_EventManager_StaticEventManager::getInstance()->attach(
  76. 'Zend_EventManager_TestAsset_ClassWithEvents',
  77. 'foo',
  78. array($this, 'aggregateStatic'),
  79. 10000 // high priority
  80. );
  81. $class = new Zend_EventManager_TestAsset_ClassWithEvents();
  82. $class->events()->attach('foo', array($this, 'aggregateLocal'), 1); // low priority
  83. $class->events()->attach('foo', array($this, 'aggregateLocal2'), 1000); // medium priority
  84. $class->events()->attach('foo', array($this, 'aggregateLocal3'), 15000); // highest priority
  85. $class->foo();
  86. $this->assertEquals(array('local3', 'static', 'local2', 'local'), $this->test->results);
  87. }
  88. public function testPassingNullValueToSetSharedCollectionsDisablesSharedCollections()
  89. {
  90. $this->counter = (object) array('count' => 0);
  91. Zend_EventManager_StaticEventManager::getInstance()->attach(
  92. 'Zend_EventManager_TestAsset_ClassWithEvents',
  93. 'foo',
  94. array($this, 'advanceCounter')
  95. );
  96. $class = new Zend_EventManager_TestAsset_ClassWithEvents();
  97. $class->events()->unsetSharedCollections();
  98. $class->foo();
  99. $this->assertEquals(0, $this->counter->count);
  100. }
  101. public function testCanPassAlternateSharedCollectionsHolder()
  102. {
  103. $this->counter = (object) array('count' => 0);
  104. Zend_EventManager_StaticEventManager::getInstance()->attach(
  105. 'Zend_EventManager_TestAsset_ClassWithEvents',
  106. 'foo',
  107. array($this, 'advanceCounter')
  108. );
  109. $mockStaticEvents = new Zend_EventManager_TestAsset_StaticEventsMock();
  110. $class = new Zend_EventManager_TestAsset_ClassWithEvents();
  111. $class->events()->setSharedCollections($mockStaticEvents);
  112. $this->assertSame($mockStaticEvents, $class->events()->getSharedCollections());
  113. $class->foo();
  114. $this->assertEquals(0, $this->counter->count);
  115. }
  116. public function testTriggerMergesPrioritiesOfStaticAndInstanceListeners()
  117. {
  118. $this->test = (object) array('results' => array());
  119. Zend_EventManager_StaticEventManager::getInstance()->attach(
  120. 'Zend_EventManager_TestAsset_ClassWithEvents',
  121. 'foo',
  122. array($this, 'aggregateStatic'),
  123. 100
  124. );
  125. $class = new Zend_EventManager_TestAsset_ClassWithEvents();
  126. $class->events()->attach('foo', array($this, 'aggregateLocal'), -100);
  127. $class->foo();
  128. $this->assertEquals(array('static', 'local'), $this->test->results);
  129. }
  130. /*
  131. * Listeners used in tests
  132. */
  133. public function advanceCounter($e)
  134. {
  135. $this->counter->count++;
  136. }
  137. public function aggregateStatic($e)
  138. {
  139. $this->test->results[] = 'static';
  140. }
  141. public function aggregateLocal($e)
  142. {
  143. $this->test->results[] = 'local';
  144. }
  145. public function aggregateLocal2($e)
  146. {
  147. $this->test->results[] = 'local2';
  148. }
  149. public function aggregateLocal3($e)
  150. {
  151. $this->test->results[] = 'local3';
  152. }
  153. }
  154. if (PHPUnit_MAIN_METHOD == 'Zend_EventManager_StaticIntegrationTest::main') {
  155. Zend_EventManager_StaticIntegrationTest::main();
  156. }