BrokerTest.php 13 KB

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