BrokerTest.php 13 KB

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