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

/typo3/sysext/extbase/Tests/MVC/Controller/AbstractController_testcase.php

https://bitbucket.org/linxpinx/mercurial
PHP | 235 lines | 131 code | 41 blank | 63 comment | 0 complexity | e253f76f7c63be84682011d2fc7ea838 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Unlicense, LGPL-2.1, Apache-2.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. *
  5. * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
  6. * All rights reserved
  7. *
  8. * This class is a backport of the corresponding class of FLOW3.
  9. * All credits go to the v5 team.
  10. *
  11. * This script is part of the TYPO3 project. The TYPO3 project is
  12. * free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * The GNU General Public License can be found at
  18. * http://www.gnu.org/copyleft/gpl.html.
  19. *
  20. * This script is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * This copyright notice MUST APPEAR in all copies of the script!
  26. ***************************************************************/
  27. class Tx_Extbase_MVC_Controller_AbstractController_testcase extends Tx_Extbase_BaseTestCase {
  28. /**
  29. * @test
  30. */
  31. public function theExtensionNameIsInitialized() {
  32. $extensionName = uniqid('Test');
  33. $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('initializeObjects'), array(), 'Tx_' . $extensionName . '_Controller');
  34. $this->assertSame($extensionName, $controller->_get('extensionName'));
  35. }
  36. /**
  37. * @test
  38. * @expectedException Tx_Extbase_MVC_Exception_UnsupportedRequestType
  39. */
  40. public function processRequestWillThrowAnExceptionIfTheGivenRequestIsNotSupported() {
  41. $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
  42. $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
  43. $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('mapRequestArgumentsToControllerArguments'), array(), '', FALSE);
  44. $controller->_set('supportedRequestTypes', array('Tx_Something_Request'));
  45. $controller->processRequest($mockRequest, $mockResponse);
  46. }
  47. /**
  48. * @test
  49. */
  50. public function processRequestSetsTheDispatchedFlagOfTheRequest() {
  51. $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
  52. $mockRequest->expects($this->once())->method('setDispatched')->with(TRUE);
  53. $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
  54. $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('initializeArguments', 'initializeControllerArgumentsBaseValidators', 'mapRequestArgumentsToControllerArguments'), array(), '', FALSE);
  55. $controller->processRequest($mockRequest, $mockResponse);
  56. }
  57. /**
  58. * @test
  59. * @expectedException Tx_Extbase_MVC_Exception_StopAction
  60. */
  61. public function forwardThrowsAStopActionException() {
  62. $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
  63. $mockRequest->expects($this->once())->method('setDispatched')->with(FALSE);
  64. $mockRequest->expects($this->once())->method('setControllerActionName')->with('foo');
  65. $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
  66. $controller->_set('request', $mockRequest);
  67. $controller->_call('forward', 'foo');
  68. }
  69. /**
  70. * @test
  71. * @expectedException Tx_Extbase_MVC_Exception_StopAction
  72. */
  73. public function forwardSetsControllerAndArgumentsAtTheRequestObjectIfTheyAreSpecified() {
  74. $arguments = array('foo' => 'bar');
  75. $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
  76. $mockRequest->expects($this->once())->method('setControllerActionName')->with('foo');
  77. $mockRequest->expects($this->once())->method('setControllerName')->with('Bar');
  78. $mockRequest->expects($this->once())->method('setControllerExtensionName')->with('Baz');
  79. $mockRequest->expects($this->once())->method('setArguments')->with($arguments);
  80. $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
  81. $controller->_set('request', $mockRequest);
  82. $controller->_call('forward', 'foo', 'Bar', 'Baz', $arguments);
  83. }
  84. /**
  85. * @test
  86. */
  87. public function redirectRedirectsToTheSpecifiedAction() {
  88. $arguments = array('foo' => 'bar');
  89. $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
  90. $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
  91. $mockUriBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_UriBuilder');
  92. $mockUriBuilder->expects($this->once())->method('reset')->will($this->returnValue($mockUriBuilder));
  93. $mockUriBuilder->expects($this->once())->method('setTargetPageUid')->with(123)->will($this->returnValue($mockUriBuilder));
  94. $mockUriBuilder->expects($this->once())->method('uriFor')->with('theActionName', $arguments, 'TheControllerName', 'TheExtensionName')->will($this->returnValue('the uri'));
  95. $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('redirectToURI'), array(), '', FALSE);
  96. $controller->expects($this->once())->method('redirectToURI')->with('the uri');
  97. $controller->_set('request', $mockRequest);
  98. $controller->_set('response', $mockResponse);
  99. $controller->_set('uriBuilder', $mockUriBuilder);
  100. $controller->_call('redirect', 'theActionName', 'TheControllerName', 'TheExtensionName', $arguments, 123);
  101. }
  102. /**
  103. * @test
  104. */
  105. public function theBaseUriIsAddedIfNotAlreadyExists() {
  106. $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
  107. $mockRequest->expects($this->any())->method('getBaseURI')->will($this->returnValue('http://www.example.com/foo/'));
  108. $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
  109. $controller->_set('request', $mockRequest);
  110. $actualResult = $controller->_call('addBaseUriIfNecessary', 'bar/baz/boom.html');
  111. $expectedResult = 'http://www.example.com/foo/bar/baz/boom.html';
  112. $this->assertEquals($expectedResult, $actualResult);
  113. }
  114. /**
  115. * @test
  116. */
  117. public function theBaseUriIsNotAddedIfAlreadyExists() {
  118. $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
  119. $mockRequest->expects($this->any())->method('getBaseURI')->will($this->returnValue('http://www.example.com/foo/'));
  120. $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
  121. $controller->_set('request', $mockRequest);
  122. $actualResult = $controller->_call('addBaseUriIfNecessary', 'http://www.example.com/foo/bar/baz/boom.html');
  123. $expectedResult = 'http://www.example.com/foo/bar/baz/boom.html';
  124. $this->assertEquals($expectedResult, $actualResult);
  125. }
  126. /**
  127. * @test
  128. * @expectedException Tx_Extbase_MVC_Exception_StopAction
  129. */
  130. public function throwStatusSetsTheSpecifiedStatusHeaderAndStopsTheCurrentAction() {
  131. $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
  132. $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
  133. $mockResponse->expects($this->once())->method('setStatus')->with(404, 'File Really Not Found');
  134. $mockResponse->expects($this->once())->method('setContent')->with('<h1>All wrong!</h1><p>Sorry, the file does not exist.</p>');
  135. $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
  136. $controller->_set('request', $mockRequest);
  137. $controller->_set('response', $mockResponse);
  138. $controller->_call('throwStatus', 404, 'File Really Not Found', '<h1>All wrong!</h1><p>Sorry, the file does not exist.</p>');
  139. }
  140. /**
  141. * @test
  142. */
  143. public function initializeControllerArgumentsBaseValidatorsRegistersValidatorsDeclaredInTheArgumentModels() {
  144. $mockValidators = array(
  145. 'foo' => $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface'),
  146. );
  147. $mockValidatorResolver = $this->getMock('Tx_Extbase_Validation_ValidatorResolver', array(), array(), '', FALSE);
  148. $mockValidatorResolver->expects($this->at(0))->method('getBaseValidatorConjunction')->with('FooType')->will($this->returnValue($mockValidators['foo']));
  149. $mockValidatorResolver->expects($this->at(1))->method('getBaseValidatorConjunction')->with('BarType')->will($this->returnValue(NULL));
  150. $mockArgumentFoo = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array('foo'), '', FALSE);
  151. $mockArgumentFoo->expects($this->once())->method('getDataType')->will($this->returnValue('FooType'));
  152. $mockArgumentFoo->expects($this->once())->method('setValidator')->with($mockValidators['foo']);
  153. $mockArgumentBar = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array('bar'), '', FALSE);
  154. $mockArgumentBar->expects($this->once())->method('getDataType')->will($this->returnValue('BarType'));
  155. $mockArgumentBar->expects($this->never())->method('setValidator');
  156. $mockArguments = new Tx_Extbase_MVC_Controller_Arguments();
  157. $mockArguments->addArgument($mockArgumentFoo);
  158. $mockArguments->addArgument($mockArgumentBar);
  159. $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
  160. $controller->_set('arguments', $mockArguments);
  161. $controller->injectValidatorResolver($mockValidatorResolver);
  162. $controller->_call('initializeControllerArgumentsBaseValidators');
  163. }
  164. /**
  165. * @test
  166. */
  167. public function mapRequestArgumentsToControllerArgumentsPreparesInformationAndValidatorsAndMapsAndValidates() {
  168. $mockValidator = new Tx_Extbase_MVC_Controller_ArgumentsValidator(); // FIXME see original FLOW3 code
  169. $mockArgumentFoo = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array('foo'), '', FALSE);
  170. $mockArgumentFoo->expects($this->any())->method('getName')->will($this->returnValue('foo'));
  171. $mockArgumentBar = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array('bar'), '', FALSE);
  172. $mockArgumentBar->expects($this->any())->method('getName')->will($this->returnValue('bar'));
  173. $mockArguments = new Tx_Extbase_MVC_Controller_Arguments();
  174. $mockArguments->addArgument($mockArgumentFoo);
  175. $mockArguments->addArgument($mockArgumentBar);
  176. $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
  177. $mockRequest->expects($this->once())->method('getArguments')->will($this->returnValue(array('requestFoo', 'requestBar')));
  178. $mockMappingResults = $this->getMock('Tx_Extbase_Property_MappingResults');
  179. $mockPropertyMapper = $this->getMock('Tx_Extbase_Property_Mapper', array(), array(), '', FALSE);
  180. $mockPropertyMapper->expects($this->once())->method('mapAndValidate')
  181. ->with(array('foo', 'bar'), array('requestFoo', 'requestBar'), $mockArguments, array(), $mockValidator)
  182. ->will($this->returnValue(TRUE));
  183. $mockPropertyMapper->expects($this->once())->method('getMappingResults')->will($this->returnValue($mockMappingResults));
  184. $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
  185. $controller->_set('arguments', $mockArguments);
  186. $controller->_set('request', $mockRequest);
  187. $controller->_set('propertyMapper', $mockPropertyMapper);
  188. $controller->_set('objectManager', $mockObjectManager);
  189. $controller->_call('mapRequestArgumentsToControllerArguments');
  190. $this->assertSame($mockMappingResults, $controller->_get('argumentsMappingResults'));
  191. // $this->assertTrue(in_array('Tx_Extbase_Validation_Validator_ObjectValidatorInterface', class_implements($controller->_get('argumentsMappingResults'))));
  192. }
  193. }
  194. ?>