PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/ZendTest/Test/PHPUnit/Controller/AbstractControllerTestCaseTest.php

https://bitbucket.org/gencer/zf2
PHP | 329 lines | 252 code | 57 blank | 20 comment | 1 complexity | aa3d7f7218f265ba59d577cce96df722 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Test\PHPUnit\Controller;
  10. use org\bovigo\vfs\vfsStream;
  11. use org\bovigo\vfs\vfsStreamWrapper;
  12. use Zend\Console\Console;
  13. use Zend\Mvc\Application;
  14. use Zend\Mvc\MvcEvent;
  15. use Zend\Stdlib\RequestInterface;
  16. use Zend\Stdlib\ResponseInterface;
  17. use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
  18. /**
  19. * @group Zend_Test
  20. */
  21. class AbstractControllerTestCaseTest extends AbstractHttpControllerTestCase
  22. {
  23. public function tearDownCacheDir()
  24. {
  25. vfsStreamWrapper::register();
  26. $cacheDir = vfsStream::url('zf2-module-test');
  27. if (is_dir($cacheDir)) {
  28. static::rmdir($cacheDir);
  29. }
  30. }
  31. public static function rmdir($dir)
  32. {
  33. $files = array_diff(scandir($dir), array('.','..'));
  34. foreach ($files as $file) {
  35. (is_dir("$dir/$file")) ? static::rmdir("$dir/$file") : unlink("$dir/$file");
  36. }
  37. return rmdir($dir);
  38. }
  39. protected function setUp()
  40. {
  41. $this->tearDownCacheDir();
  42. Console::overrideIsConsole(null);
  43. $this->setApplicationConfig(
  44. include __DIR__ . '/../../_files/application.config.php'
  45. );
  46. parent::setUp();
  47. }
  48. protected function tearDown()
  49. {
  50. $this->tearDownCacheDir();
  51. parent::tearDown();
  52. }
  53. public function testModuleCacheIsDisabled()
  54. {
  55. $config = $this->getApplicationConfig();
  56. $config = $config['module_listener_options']['cache_dir'];
  57. $this->assertEquals(0, count(glob($config . '/*.php')));
  58. }
  59. public function testCanNotDefineApplicationConfigWhenApplicationIsBuilt()
  60. {
  61. // cosntruct app
  62. $this->getApplication();
  63. $this->setExpectedException('Zend\Stdlib\Exception\LogicException');
  64. $this->setApplicationConfig(
  65. include __DIR__ . '/../../_files/application.config.php'
  66. );
  67. }
  68. public function testUseOfRouter()
  69. {
  70. // default value
  71. $this->assertEquals(false, $this->useConsoleRequest);
  72. }
  73. public function testApplicationClass()
  74. {
  75. $applicationClass = get_class($this->getApplication());
  76. $this->assertEquals($applicationClass, 'Zend\Mvc\Application');
  77. }
  78. public function testApplicationClassAndTestRestoredConsoleFlag()
  79. {
  80. $this->assertTrue(Console::isConsole(), '1. Console::isConsole returned false in initial test');
  81. $this->getApplication();
  82. $this->assertFalse(Console::isConsole(), '2. Console::isConsole returned true after retrieving application');
  83. $this->tearDown();
  84. $this->assertTrue(Console::isConsole(), '3. Console::isConsole returned false after tearDown');
  85. Console::overrideIsConsole(false);
  86. parent::setUp();
  87. $this->assertFalse(Console::isConsole(), '4. Console::isConsole returned true after parent::setUp');
  88. $this->getApplication();
  89. $this->assertFalse(Console::isConsole(), '5. Console::isConsole returned true after retrieving application');
  90. parent::tearDown();
  91. $this->assertFalse(Console::isConsole(), '6. Console.isConsole returned true after parent::tearDown');
  92. }
  93. public function testApplicationServiceLocatorClass()
  94. {
  95. $smClass = get_class($this->getApplicationServiceLocator());
  96. $this->assertEquals($smClass, 'Zend\ServiceManager\ServiceManager');
  97. }
  98. public function testAssertApplicationRequest()
  99. {
  100. $this->assertEquals(true, $this->getRequest() instanceof RequestInterface);
  101. }
  102. public function testAssertApplicationResponse()
  103. {
  104. $this->assertEquals(true, $this->getResponse() instanceof ResponseInterface);
  105. }
  106. public function testAssertModuleName()
  107. {
  108. $this->dispatch('/tests');
  109. // tests with case insensitive
  110. $this->assertModuleName('baz');
  111. $this->assertModuleName('Baz');
  112. $this->assertModuleName('BAz');
  113. $this->setExpectedException(
  114. 'PHPUnit_Framework_ExpectationFailedException',
  115. 'actual module name is "baz"' // check actual module is display
  116. );
  117. $this->assertModuleName('Application');
  118. }
  119. public function testAssertNotModuleName()
  120. {
  121. $this->dispatch('/tests');
  122. $this->assertNotModuleName('Application');
  123. $this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
  124. $this->assertNotModuleName('baz');
  125. }
  126. public function testAssertControllerClass()
  127. {
  128. $this->dispatch('/tests');
  129. // tests with case insensitive
  130. $this->assertControllerClass('IndexController');
  131. $this->assertControllerClass('Indexcontroller');
  132. $this->assertControllerClass('indexcontroller');
  133. $this->setExpectedException(
  134. 'PHPUnit_Framework_ExpectationFailedException',
  135. 'actual controller class is "indexcontroller"' // check actual controller class is display
  136. );
  137. $this->assertControllerClass('Index');
  138. }
  139. public function testAssertNotControllerClass()
  140. {
  141. $this->dispatch('/tests');
  142. $this->assertNotControllerClass('Index');
  143. $this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
  144. $this->assertNotControllerClass('IndexController');
  145. }
  146. public function testAssertControllerName()
  147. {
  148. $this->dispatch('/tests');
  149. // tests with case insensitive
  150. $this->assertControllerName('baz_index');
  151. $this->assertControllerName('Baz_index');
  152. $this->assertControllerName('BAz_index');
  153. $this->setExpectedException(
  154. 'PHPUnit_Framework_ExpectationFailedException',
  155. 'actual controller name is "baz_index"' // check actual controller name is display
  156. );
  157. $this->assertControllerName('baz');
  158. }
  159. public function testAssertNotControllerName()
  160. {
  161. $this->dispatch('/tests');
  162. $this->assertNotControllerName('baz');
  163. $this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
  164. $this->assertNotControllerName('baz_index');
  165. }
  166. public function testAssertActionName()
  167. {
  168. $this->dispatch('/tests');
  169. // tests with case insensitive
  170. $this->assertActionName('unittests');
  171. $this->assertActionName('unitTests');
  172. $this->assertActionName('UnitTests');
  173. $this->setExpectedException(
  174. 'PHPUnit_Framework_ExpectationFailedException',
  175. 'actual action name is "unittests"' // check actual action name is display
  176. );
  177. $this->assertActionName('unit');
  178. }
  179. public function testAssertNotActionName()
  180. {
  181. $this->dispatch('/tests');
  182. $this->assertNotActionName('unit');
  183. $this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
  184. $this->assertNotActionName('unittests');
  185. }
  186. public function testAssertMatchedRouteName()
  187. {
  188. $this->dispatch('/tests');
  189. // tests with case insensitive
  190. $this->assertMatchedRouteName('myroute');
  191. $this->assertMatchedRouteName('myRoute');
  192. $this->assertMatchedRouteName('MyRoute');
  193. $this->setExpectedException(
  194. 'PHPUnit_Framework_ExpectationFailedException',
  195. 'actual matched route name is "myroute"' // check actual matched route name is display
  196. );
  197. $this->assertMatchedRouteName('route');
  198. }
  199. public function testAssertNotMatchedRouteName()
  200. {
  201. $this->dispatch('/tests');
  202. $this->assertNotMatchedRouteName('route');
  203. $this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
  204. $this->assertNotMatchedRouteName('myroute');
  205. }
  206. /**
  207. * Sample tests on Application errors events
  208. */
  209. public function testAssertApplicationErrorsEvents()
  210. {
  211. $this->url('/bad-url');
  212. $result = $this->triggerApplicationEvent(MvcEvent::EVENT_ROUTE);
  213. $this->assertEquals(true, $result->stopped());
  214. $this->assertEquals(Application::ERROR_ROUTER_NO_MATCH, $this->getApplication()->getMvcEvent()->getError());
  215. }
  216. public function testDispatchRequestUri()
  217. {
  218. $this->dispatch('/tests');
  219. $this->assertEquals('/tests', $this->getApplication()->getRequest()->getRequestUri());
  220. }
  221. public function testDefaultDispatchMethod()
  222. {
  223. $this->dispatch('/tests');
  224. $this->assertEquals('GET', $this->getRequest()->getMethod());
  225. }
  226. public function testDispatchMethodSetOnRequest()
  227. {
  228. $this->getRequest()->setMethod('POST');
  229. $this->dispatch('/tests');
  230. $this->assertEquals('POST', $this->getRequest()->getMethod());
  231. }
  232. public function testExplicitDispatchMethodOverrideRequestMethod()
  233. {
  234. $this->getRequest()->setMethod('POST');
  235. $this->dispatch('/tests', 'GET');
  236. $this->assertEquals('GET', $this->getRequest()->getMethod());
  237. }
  238. public function testPutRequestParams()
  239. {
  240. $this->dispatch('/tests', 'PUT', array('a' => 1));
  241. $this->assertEquals('a=1', $this->getRequest()->getContent());
  242. }
  243. public function testPreserveContentOfPutRequest()
  244. {
  245. $this->getRequest()->setMethod('PUT');
  246. $this->getRequest()->setContent('my content');
  247. $this->dispatch('/tests');
  248. $this->assertEquals('my content', $this->getRequest()->getContent());
  249. }
  250. public function testExplicityPutParamsOverrideRequestContent()
  251. {
  252. $this->getRequest()->setContent('my content');
  253. $this->dispatch('/tests', 'PUT', array('a' => 1));
  254. $this->assertEquals('a=1', $this->getRequest()->getContent());
  255. }
  256. public function testAssertTemplateName()
  257. {
  258. $this->dispatch('/tests');
  259. $this->assertTemplateName('layout/layout');
  260. $this->assertTemplateName('baz/index/unittests');
  261. }
  262. public function testAssertNotTemplateName()
  263. {
  264. $this->dispatch('/tests');
  265. $this->assertNotTemplateName('template/does/not/exist');
  266. }
  267. public function testCustomResponseObject()
  268. {
  269. $this->dispatch('/custom-response');
  270. $this->assertResponseStatusCode(999);
  271. }
  272. }