2
0

BrokerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. // Call Zend_Controller_Plugin_BrokerTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Plugin_BrokerTest::main");
  5. $basePath = realpath(dirname(__FILE__) . str_repeat(DIRECTORY_SEPARATOR . '..', 3));
  6. set_include_path(
  7. $basePath . DIRECTORY_SEPARATOR . 'tests'
  8. . PATH_SEPARATOR . $basePath . DIRECTORY_SEPARATOR . 'library'
  9. . PATH_SEPARATOR . get_include_path()
  10. );
  11. }
  12. require_once "PHPUnit/Framework/TestCase.php";
  13. require_once "PHPUnit/Framework/TestSuite.php";
  14. require_once 'Zend/Controller/Front.php';
  15. require_once 'Zend/Controller/Action/HelperBroker.php';
  16. require_once 'Zend/Controller/Request/Http.php';
  17. require_once 'Zend/Controller/Request/Simple.php';
  18. require_once 'Zend/Controller/Response/Cli.php';
  19. class Zend_Controller_Plugin_BrokerTest extends PHPUnit_Framework_TestCase
  20. {
  21. public $controller;
  22. /**
  23. * Runs the test methods of this class.
  24. *
  25. * @access public
  26. * @static
  27. */
  28. public static function main()
  29. {
  30. require_once "PHPUnit/TextUI/TestRunner.php";
  31. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Plugin_BrokerTest");
  32. $result = PHPUnit_TextUI_TestRunner::run($suite);
  33. }
  34. public function setUp()
  35. {
  36. $this->controller = Zend_Controller_Front::getInstance();
  37. $this->controller->resetInstance();
  38. $this->controller->setParam('noViewRenderer', true)
  39. ->setParam('noErrorHandler', true);
  40. }
  41. public function testDuplicatePlugin()
  42. {
  43. $broker = new Zend_Controller_Plugin_Broker();
  44. $plugin = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
  45. $broker->registerPlugin($plugin);
  46. try {
  47. $broker->registerPlugin($plugin);
  48. $this->fail('Duplicate registry of plugin object should be disallowed');
  49. } catch (Exception $expected) {
  50. $this->assertContains('already', $expected->getMessage());
  51. }
  52. }
  53. public function testUsingFrontController()
  54. {
  55. $this->controller->setControllerDirectory(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files');
  56. $request = new Zend_Controller_Request_Http('http://framework.zend.com/empty');
  57. $this->controller->setResponse(new Zend_Controller_Response_Cli());
  58. $plugin = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
  59. $this->controller->registerPlugin($plugin);
  60. $this->controller->returnResponse(true);
  61. $response = $this->controller->dispatch($request);
  62. $this->assertEquals('123456', $response->getBody());
  63. $this->assertEquals('123456', $plugin->getResponse()->getBody());
  64. }
  65. public function testUnregisterPluginWithObject()
  66. {
  67. $broker = new Zend_Controller_Plugin_Broker();
  68. $plugin = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
  69. $broker->registerPlugin($plugin);
  70. $plugins = $broker->getPlugins();
  71. $this->assertEquals(1, count($plugins));
  72. $broker->unregisterPlugin($plugin);
  73. $plugins = $broker->getPlugins();
  74. $this->assertEquals(0, count($plugins));
  75. }
  76. public function testUnregisterPluginByClassName()
  77. {
  78. $broker = new Zend_Controller_Plugin_Broker();
  79. $plugin = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
  80. $broker->registerPlugin($plugin);
  81. $plugins = $broker->getPlugins();
  82. $this->assertEquals(1, count($plugins));
  83. $broker->unregisterPlugin('Zend_Controller_Plugin_BrokerTest_TestPlugin');
  84. $plugins = $broker->getPlugins();
  85. $this->assertEquals(0, count($plugins));
  86. }
  87. public function testGetPlugins()
  88. {
  89. $broker = new Zend_Controller_Plugin_Broker();
  90. $plugin = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
  91. $broker->registerPlugin($plugin);
  92. $plugins = $broker->getPlugins();
  93. $this->assertEquals(1, count($plugins));
  94. $this->assertSame($plugin, $plugins[0]);
  95. }
  96. public function testGetPluginByName()
  97. {
  98. $broker = new Zend_Controller_Plugin_Broker();
  99. $plugin = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
  100. $broker->registerPlugin($plugin);
  101. $retrieved = $broker->getPlugin('Zend_Controller_Plugin_BrokerTest_TestPlugin');
  102. $this->assertTrue($retrieved instanceof Zend_Controller_Plugin_BrokerTest_TestPlugin);
  103. $this->assertSame($plugin, $retrieved);
  104. }
  105. public function testGetPluginByNameReturnsFalseWithBadClassName()
  106. {
  107. $broker = new Zend_Controller_Plugin_Broker();
  108. $plugin = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
  109. $broker->registerPlugin($plugin);
  110. $retrieved = $broker->getPlugin('TestPlugin');
  111. $this->assertFalse($retrieved);
  112. }
  113. public function testGetPluginByNameReturnsArray()
  114. {
  115. $broker = new Zend_Controller_Plugin_Broker();
  116. $plugin = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
  117. $broker->registerPlugin($plugin);
  118. $plugin2 = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
  119. $broker->registerPlugin($plugin2);
  120. $retrieved = $broker->getPlugin('Zend_Controller_Plugin_BrokerTest_TestPlugin');
  121. $this->assertTrue(is_array($retrieved));
  122. $this->assertEquals(2, count($retrieved));
  123. $this->assertSame($plugin, $retrieved[0]);
  124. $this->assertSame($plugin2, $retrieved[1]);
  125. }
  126. public function testHasPlugin()
  127. {
  128. $broker = new Zend_Controller_Plugin_Broker();
  129. $this->assertFalse($broker->hasPlugin('Zend_Controller_Plugin_BrokerTest_TestPlugin'));
  130. $plugin = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
  131. $broker->registerPlugin($plugin);
  132. $this->assertTrue($broker->hasPlugin('Zend_Controller_Plugin_BrokerTest_TestPlugin'));
  133. }
  134. public function testBrokerCatchesExceptions()
  135. {
  136. $request = new Zend_Controller_Request_Http('http://framework.zend.com/empty');
  137. $response = new Zend_Controller_Response_Cli();
  138. $broker = new Zend_Controller_Plugin_Broker();
  139. $broker->setResponse($response);
  140. $broker->registerPlugin(new Zend_Controller_Plugin_BrokerTest_ExceptionTestPlugin());
  141. try {
  142. $broker->routeStartup($request);
  143. $broker->routeShutdown($request);
  144. $broker->dispatchLoopStartup($request);
  145. $broker->preDispatch($request);
  146. $broker->postDispatch($request);
  147. $broker->dispatchLoopShutdown();
  148. } catch (Exception $e) {
  149. $this->fail('Broker should catch exceptions');
  150. }
  151. $this->assertTrue($response->hasExceptionOfMessage('routeStartup triggered exception'));
  152. $this->assertTrue($response->hasExceptionOfMessage('routeShutdown triggered exception'));
  153. $this->assertTrue($response->hasExceptionOfMessage('dispatchLoopStartup triggered exception'));
  154. $this->assertTrue($response->hasExceptionOfMessage('preDispatch triggered exception'));
  155. $this->assertTrue($response->hasExceptionOfMessage('postDispatch triggered exception'));
  156. $this->assertTrue($response->hasExceptionOfMessage('dispatchLoopShutdown triggered exception'));
  157. }
  158. public function testRegisterPluginStackOrderIsSane()
  159. {
  160. $broker = new Zend_Controller_Plugin_Broker();
  161. $plugin1 = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
  162. $plugin2 = new Zend_Controller_Plugin_BrokerTest_ExceptionTestPlugin();
  163. $plugin3 = new Zend_Controller_Plugin_BrokerTest_TestPlugin2();
  164. $broker->registerPlugin($plugin1, 5);
  165. $broker->registerPlugin($plugin2, -5);
  166. $broker->registerPlugin($plugin3, 2);
  167. $plugins = $broker->getPlugins();
  168. $expected = array(-5 => $plugin2, 2 => $plugin3, 5 => $plugin1);
  169. $this->assertSame($expected, $plugins);
  170. }
  171. public function testRegisterPluginThrowsExceptionOnDuplicateStackIndex()
  172. {
  173. $broker = new Zend_Controller_Plugin_Broker();
  174. $plugin1 = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
  175. $plugin2 = new Zend_Controller_Plugin_BrokerTest_ExceptionTestPlugin();
  176. $broker->registerPlugin($plugin1, 5);
  177. try {
  178. $broker->registerPlugin($plugin2, 5);
  179. $this->fail('Registering plugins with same stack index should raise exception');
  180. } catch (Exception $e) {
  181. }
  182. }
  183. public function testRegisterPluginStackOrderWithAutmaticNumbersIncrementsCorrectly()
  184. {
  185. $broker = new Zend_Controller_Plugin_Broker();
  186. $plugin1 = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
  187. $plugin2 = new Zend_Controller_Plugin_BrokerTest_ExceptionTestPlugin();
  188. $plugin3 = new Zend_Controller_Plugin_BrokerTest_TestPlugin2();
  189. $broker->registerPlugin($plugin1, 2);
  190. $broker->registerPlugin($plugin2, 3);
  191. $broker->registerPlugin($plugin3);
  192. $plugins = $broker->getPlugins();
  193. $expected = array(2 => $plugin1, 3 => $plugin2, 4 => $plugin3);
  194. $this->assertSame($expected, $plugins);
  195. }
  196. /**
  197. * Test for ZF-2305
  198. * @return void
  199. */
  200. public function testRegisterPluginSetsRequestAndResponse()
  201. {
  202. $broker = new Zend_Controller_Plugin_Broker();
  203. $request = new Zend_Controller_Request_Simple();
  204. $response = new Zend_Controller_Response_Cli();
  205. $broker->setRequest($request);
  206. $broker->setResponse($response);
  207. $plugin = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
  208. $broker->registerPlugin($plugin);
  209. $this->assertSame($request, $plugin->getRequest());
  210. $this->assertSame($response, $plugin->getResponse());
  211. }
  212. }
  213. class Zend_Controller_Plugin_BrokerTest_TestPlugin extends Zend_Controller_Plugin_Abstract
  214. {
  215. public function routeStartup(Zend_Controller_Request_Abstract $request)
  216. {
  217. $this->getResponse()->appendBody('1');
  218. }
  219. public function routeShutdown(Zend_Controller_Request_Abstract $request)
  220. {
  221. $this->getResponse()->appendBody('2');
  222. }
  223. public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
  224. {
  225. $this->getResponse()->appendBody('3');
  226. }
  227. public function preDispatch(Zend_Controller_Request_Abstract $request)
  228. {
  229. $this->getResponse()->appendBody('4');
  230. }
  231. public function postDispatch(Zend_Controller_Request_Abstract $request)
  232. {
  233. $this->getResponse()->appendBody('5');
  234. }
  235. public function dispatchLoopShutdown()
  236. {
  237. $this->getResponse()->appendBody('6');
  238. }
  239. }
  240. class Zend_Controller_Plugin_BrokerTest_TestPlugin2 extends Zend_Controller_Plugin_BrokerTest_TestPlugin
  241. {
  242. }
  243. class Zend_Controller_Plugin_BrokerTest_ExceptionTestPlugin extends Zend_Controller_Plugin_Abstract
  244. {
  245. public function routeStartup(Zend_Controller_Request_Abstract $request)
  246. {
  247. throw new Exception('routeStartup triggered exception');
  248. }
  249. public function routeShutdown(Zend_Controller_Request_Abstract $request)
  250. {
  251. throw new Exception('routeShutdown triggered exception');
  252. }
  253. public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
  254. {
  255. throw new Exception('dispatchLoopStartup triggered exception');
  256. }
  257. public function preDispatch(Zend_Controller_Request_Abstract $request)
  258. {
  259. throw new Exception('preDispatch triggered exception');
  260. }
  261. public function postDispatch(Zend_Controller_Request_Abstract $request)
  262. {
  263. throw new Exception('postDispatch triggered exception');
  264. }
  265. public function dispatchLoopShutdown()
  266. {
  267. throw new Exception('dispatchLoopShutdown triggered exception');
  268. }
  269. }
  270. // Call Zend_Controller_Plugin_BrokerTest::main() if this source file is executed directly.
  271. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Plugin_BrokerTest::main") {
  272. Zend_Controller_Plugin_BrokerTest::main();
  273. }