PageRenderTime 25ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/standard/tags/release-1.0.4/tests/Zend/Controller/Plugin/BrokerTest.php

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