PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/View/Helper/ActionTest.php

https://github.com/alab1001101/zf2
PHP | 313 lines | 174 code | 37 blank | 102 comment | 0 complexity | 7623ad7e7db18db6c60acdcc9380a7b2 MD5 | raw file
  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_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 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. /**
  23. * @namespace
  24. */
  25. namespace ZendTest\View\Helper;
  26. use Zend\Controller;
  27. use Zend\Controller\Request;
  28. use Zend\Controller\Response;
  29. use Zend\View\Helper;
  30. use Zend\Controller\Action\HelperBroker;
  31. /**
  32. * Test class for Zend_View_Helper_Action.
  33. *
  34. * @category Zend
  35. * @package Zend_View
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_View
  40. * @group Zend_View_Helper
  41. */
  42. class ActionTest extends \PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * Sets up the fixture, for example, open a network connection.
  46. * This method is called before a test is executed.
  47. *
  48. * @return void
  49. */
  50. public function setUp()
  51. {
  52. $this->_origServer = $_SERVER;
  53. $_SERVER = array(
  54. 'SCRIPT_FILENAME' => __FILE__,
  55. 'PHP_SELF' => __FILE__,
  56. );
  57. $front = Controller\Front::getInstance();
  58. $front->resetInstance();
  59. $this->request = new Request\HTTP('http://framework.zend.com/action-foo');
  60. $this->response = new Response\HTTP();
  61. $this->response->headersSentThrowsException = false;
  62. $front->setRequest($this->request)
  63. ->setResponse($this->response)
  64. ->addModuleDirectory(dirname(__FILE__) . '/_files/modules');
  65. $this->view = new \Zend\View\View();
  66. $this->helper = new Helper\Action();
  67. $this->helper->setView($this->view);
  68. }
  69. /**
  70. * Tears down the fixture, for example, close a network connection.
  71. * This method is called after a test is executed.
  72. *
  73. * @return void
  74. */
  75. public function tearDown()
  76. {
  77. unset($this->request, $this->response, $this->helper);
  78. $_SERVER = $this->_origServer;
  79. }
  80. /**
  81. * @return void
  82. */
  83. public function testInitialStateHasClonedObjects()
  84. {
  85. $this->assertNotSame($this->request, $this->helper->request);
  86. $this->assertNotSame($this->response, $this->helper->response);
  87. $dispatcher = Controller\Front::getInstance()->getDispatcher();
  88. $this->assertNotSame($dispatcher, $this->helper->dispatcher);
  89. }
  90. /**
  91. * @return void
  92. */
  93. public function testInitialStateHasDefaultModuleName()
  94. {
  95. $dispatcher = Controller\Front::getInstance()->getDispatcher();
  96. $module = $dispatcher->getDefaultModule();
  97. $this->assertEquals($module, $this->helper->defaultModule);
  98. $dispatcher->setDefaultModule('foo');
  99. $helper = new Helper\Action();
  100. $this->assertEquals('foo', $helper->defaultModule);
  101. }
  102. /**
  103. * @return void
  104. */
  105. public function testResetObjectsClearsRequestVars()
  106. {
  107. $this->helper->request->setParam('foo', 'action-bar');
  108. $this->helper->resetObjects();
  109. $this->assertNull($this->helper->request->getParam('foo'));
  110. }
  111. /**
  112. * @return void
  113. */
  114. public function testResetObjectsClearsResponseBody()
  115. {
  116. $this->helper->response->setBody('foobarbaz');
  117. $this->helper->resetObjects();
  118. $body = $this->helper->response->getBody();
  119. $this->assertTrue(empty($body));
  120. }
  121. /**
  122. * @return void
  123. */
  124. public function testResetObjectsClearsResponseHeaders()
  125. {
  126. $this->helper->response->setHeader('X-Foo', 'Bar')
  127. ->setRawHeader('HTTP/1.1');
  128. $this->helper->resetObjects();
  129. $headers = $this->helper->response->getHeaders();
  130. $rawHeaders = $this->helper->response->getRawHeaders();
  131. $this->assertTrue(empty($headers));
  132. $this->assertTrue(empty($rawHeaders));
  133. }
  134. /**
  135. * @return void
  136. */
  137. public function testActionReturnsContentFromDefaultModule()
  138. {
  139. $value = $this->helper->direct('bar', 'action-foo');
  140. $this->assertContains('In default module, FooController::barAction()', $value);
  141. }
  142. /**
  143. * @return void
  144. */
  145. public function testActionReturnsContentFromSpecifiedModule()
  146. {
  147. $value = $this->helper->direct('bar', 'foo', 'foo');
  148. $this->assertContains('In foo module, Foo_FooController::barAction()', $value);
  149. }
  150. /**
  151. * @return void
  152. */
  153. public function testActionReturnsContentReflectingPassedParams()
  154. {
  155. $value = $this->helper->direct('baz', 'action-foo', null, array('bat' => 'This is my message'));
  156. $this->assertNotContains('BOGUS', $value, var_export($this->helper->request->getUserParams(), 1));
  157. $this->assertContains('This is my message', $value);
  158. }
  159. /**
  160. * @return void
  161. */
  162. public function testActionReturnsEmptyStringWhenForwardDetected()
  163. {
  164. $value = $this->helper->direct('forward', 'action-foo');
  165. $this->assertEquals('', $value);
  166. }
  167. /**
  168. * @return void
  169. */
  170. public function testActionReturnsEmptyStringWhenRedirectDetected()
  171. {
  172. $value = $this->helper->direct('redirect', 'action-foo');
  173. $this->assertEquals('', $value);
  174. }
  175. /**
  176. * @return void
  177. */
  178. public function testConstructorThrowsExceptionWithNoControllerDirsInFrontController()
  179. {
  180. Controller\Front::getInstance()->resetInstance();
  181. try {
  182. $helper = new Helper\Action();
  183. $this->fail('Empty front controller should cause action helper to throw exception');
  184. } catch (\Exception $e) {
  185. }
  186. }
  187. /**
  188. * @return void
  189. */
  190. public function testConstructorThrowsExceptionWithNoRequestInFrontController()
  191. {
  192. $front = Controller\Front::getInstance();
  193. $front->resetInstance();
  194. $response = new Response\HTTP();
  195. $response->headersSentThrowsException = false;
  196. $front->setResponse($response)
  197. ->addModuleDirectory(dirname(__FILE__) . '/_files/modules');
  198. try {
  199. $helper = new Helper\Action();
  200. $this->fail('No request in front controller should cause action helper to throw exception');
  201. } catch (\Exception $e) {
  202. }
  203. }
  204. /**
  205. * @return void
  206. */
  207. public function testConstructorThrowsExceptionWithNoResponseInFrontController()
  208. {
  209. $front = Controller\Front::getInstance();
  210. $front->resetInstance();
  211. $request = new Request\HTTP('http://framework.zend.com/foo');
  212. $front->setRequest($this->request)
  213. ->addModuleDirectory(dirname(__FILE__) . '/_files/modules');
  214. try {
  215. $helper = new Helper\Action();
  216. $this->fail('No response in front controller should cause action helper to throw exception');
  217. } catch (\Exception $e) {
  218. }
  219. }
  220. public function testViewObjectRemainsUnchangedAfterAction()
  221. {
  222. $value = $this->helper->direct('bar', 'foo', 'foo');
  223. $this->assertContains('In foo module, Foo_FooController::barAction()', $value);
  224. $this->assertNull($this->view->bar);
  225. }
  226. public function testNestingActionsDoesNotBreakPlaceholderHelpers()
  227. {
  228. $html = $this->helper->direct('nest', 'foo', 'foo');
  229. $title = $this->view->headTitle()->toString();
  230. $this->assertContains(' - ', $title, $title);
  231. $this->assertContains('Foo Nest', $title);
  232. $this->assertContains('Nested Stuff', $title);
  233. }
  234. /**
  235. * @issue ZF-2716
  236. */
  237. public function testActionWithPartialsUseOfViewRendererReturnsToOriginatingViewState()
  238. {
  239. $partial = new \Zend\View\Helper\Partial();
  240. $this->view->setScriptPath(dirname(__FILE__) . '/_files/modules/default/views/scripts/');
  241. $partial->setView($this->view);
  242. HelperBroker\HelperBroker::getStaticHelper('viewRenderer')->view = $this->view;
  243. $partial->direct('partialActionCall.phtml');
  244. $this->assertSame($this->view, HelperBroker\HelperBroker::getStaticHelper('viewRenderer')->view);
  245. }
  246. /**
  247. * Future ViewRenderer State issues should be included in this test.
  248. *
  249. * @issue ZF-2846
  250. */
  251. public function testActionReturnsViewRendererToOriginalState()
  252. {
  253. /* Setup the VR as if we were inside an action controller */
  254. $viewRenderer = new \Zend\Controller\Action\Helper\ViewRenderer();
  255. $viewRenderer->init();
  256. HelperBroker\HelperBroker::addHelper($viewRenderer);
  257. // make sure noRender is false
  258. $this->assertFalse($viewRenderer->getNoRender());
  259. $value = $this->helper->direct('bar', 'action-foo');
  260. $viewRendererPostAction = HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
  261. // ViewRenderer noRender should still be false
  262. $this->assertFalse($viewRendererPostAction->getNoRender());
  263. $this->assertSame($viewRenderer, $viewRendererPostAction);
  264. }
  265. /**
  266. * Multiple call state issue
  267. *
  268. *
  269. * @group ZF-3456
  270. */
  271. public function testActionCalledWithinActionResetsResponseState()
  272. {
  273. $value = $this->helper->direct('bar-one', 'baz', 'foo');
  274. $this->assertRegexp('/Baz-Three-View-Script\s+Baz-Two-View-Script\s+Baz-One-View-Script/s', $value);
  275. }
  276. }