/tests/unit/core/mock/application/web.php

https://github.com/andreatarr/joomla-cms · PHP · 315 lines · 129 code · 30 blank · 156 comment · 6 complexity · 7a1b92079135d4fbee8dd06b72b8a619 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Test
  4. *
  5. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE
  7. */
  8. /**
  9. * Class to mock JApplicationWeb.
  10. *
  11. * @package Joomla.Test
  12. * @since 12.1
  13. */
  14. class TestMockApplicationWeb extends TestMockApplicationBase
  15. {
  16. /**
  17. * Mock storage for the response body.
  18. *
  19. * @var array
  20. * @since 12.2
  21. */
  22. public static $body = array();
  23. /**
  24. * Mock storage for the response headers.
  25. *
  26. * @var array
  27. * @since 3.2
  28. */
  29. public static $headers = array();
  30. /**
  31. * Mock storage for the response cache status.
  32. *
  33. * @var boolean
  34. * @since 3.2
  35. */
  36. public static $cachable = false;
  37. /**
  38. * Gets the methods of the JApplicationWeb object.
  39. *
  40. * @return array
  41. *
  42. * @since 3.4
  43. */
  44. public static function getMethods()
  45. {
  46. // Collect all the relevant methods in JApplicationWeb (work in progress).
  47. $methods = array(
  48. 'allowCache',
  49. 'appendBody',
  50. 'clearHeaders',
  51. 'execute',
  52. 'get',
  53. 'getBody',
  54. 'getDocument',
  55. 'getHeaders',
  56. 'getLanguage',
  57. 'getSession',
  58. 'loadConfiguration',
  59. 'loadDocument',
  60. 'loadLanguage',
  61. 'loadSession',
  62. 'prependBody',
  63. 'redirect',
  64. 'sendHeaders',
  65. 'set',
  66. 'setBody',
  67. 'setHeader',
  68. );
  69. return array_merge($methods, parent::getMethods());
  70. }
  71. /**
  72. * Adds mock objects for some methods.
  73. *
  74. * @param TestCase $test A test object.
  75. * @param PHPUnit_Framework_MockObject_MockObject $mockObject The mock object.
  76. * @param array $options A set of options to configure the mock.
  77. *
  78. * @return PHPUnit_Framework_MockObject_MockObject The object with the behaviours added
  79. *
  80. * @since 3.4
  81. */
  82. public static function addBehaviours($test, $mockObject, $options)
  83. {
  84. // Mock calls to JApplicationWeb::getDocument().
  85. $mockObject->expects($test->any())->method('getDocument')->willReturn(TestMockDocument::create($test));
  86. // Mock calls to JApplicationWeb::getLanguage().
  87. $mockObject->expects($test->any())->method('getLanguage')->willReturn(TestMockLanguage::create($test));
  88. // Mock a call to JApplicationWeb::getSession().
  89. if (isset($options['session']))
  90. {
  91. $mockObject->expects($test->any())->method('getSession')->willReturn($options['session']);
  92. }
  93. else
  94. {
  95. $mockObject->expects($test->any())->method('getSession')->willReturn(TestMockSession::create($test));
  96. }
  97. $test->assignMockCallbacks(
  98. $mockObject,
  99. array(
  100. 'appendBody' => array((is_callable(array($test, 'mockAppendBody')) ? $test : get_called_class()), 'mockAppendBody'),
  101. 'getBody' => array((is_callable(array($test, 'mockGetBody')) ? $test : get_called_class()), 'mockGetBody'),
  102. 'prependBody' => array((is_callable(array($test, 'mockPrependBody')) ? $test : get_called_class()), 'mockPrependBody'),
  103. 'setBody' => array((is_callable(array($test, 'mockSetBody')) ? $test : get_called_class()), 'mockSetBody'),
  104. 'getHeaders' => array((is_callable(array($test, 'mockGetHeaders')) ? $test : get_called_class()), 'mockGetHeaders'),
  105. 'setHeader' => array((is_callable(array($test, 'mockSetHeader')) ? $test : get_called_class()), 'mockSetHeader'),
  106. 'clearHeaders' => array((is_callable(array($test, 'mockClearHeaders')) ? $test : get_called_class()), 'mockClearHeaders'),
  107. 'allowCache' => array((is_callable(array($test, 'mockAllowCache')) ? $test : get_called_class()), 'mockAllowCache'),
  108. )
  109. );
  110. // Reset the body storage.
  111. static::$body = array();
  112. // Reset the headers storage.
  113. static::$headers = array();
  114. // Reset the cache storage.
  115. static::$cachable = false;
  116. return parent::addBehaviours($test, $mockObject, $options);
  117. }
  118. /**
  119. * Creates and instance of the mock JApplicationWeb object.
  120. *
  121. * The test can implement the following overrides:
  122. * - mockAppendBody
  123. * - mockGetBody
  124. * - mockPrepentBody
  125. * - mockSetBody
  126. * - mockGetHeaders
  127. * - mockSetHeaders
  128. * - mockAllowCache
  129. *
  130. * If any *Body methods are implemented in the test class, all should be implemented otherwise behaviour will be unreliable.
  131. *
  132. * @param TestCase $test A test object.
  133. * @param array $options A set of options to configure the mock.
  134. *
  135. * @return PHPUnit_Framework_MockObject_MockObject
  136. *
  137. * @since 11.3
  138. */
  139. public static function create($test, $options = array())
  140. {
  141. // Set expected server variables.
  142. if (!isset($_SERVER['HTTP_HOST']))
  143. {
  144. $_SERVER['HTTP_HOST'] = 'localhost';
  145. }
  146. // Collect all the relevant methods in JApplicationWeb (work in progress).
  147. $methods = self::getMethods();
  148. // Create the mock.
  149. $mockObject = $test->getMock(
  150. 'JApplicationWeb',
  151. $methods,
  152. // Constructor arguments.
  153. array(),
  154. // Mock class name.
  155. '',
  156. // Call original constructor.
  157. true
  158. );
  159. $mockObject = self::addBehaviours($test, $mockObject, $options);
  160. return $mockObject;
  161. }
  162. /**
  163. * Mock JApplicationWeb->appendBody method.
  164. *
  165. * @param string $content The content to append to the response body.
  166. *
  167. * @return mixed
  168. *
  169. * @since 12.2
  170. */
  171. public static function mockAppendBody($content)
  172. {
  173. array_push(static::$body, (string) $content);
  174. }
  175. /**
  176. * Mock JApplicationWeb->getBody method.
  177. *
  178. * @param boolean $asArray True to return the body as an array of strings.
  179. *
  180. * @return mixed
  181. *
  182. * @since 12.2
  183. */
  184. public static function mockGetBody($asArray = false)
  185. {
  186. return $asArray ? static::$body : implode((array) static::$body);
  187. }
  188. /**
  189. * Mock JApplicationWeb->appendBody method.
  190. *
  191. * @param string $content The content to append to the response body.
  192. *
  193. * @return mixed
  194. *
  195. * @since 12.2
  196. */
  197. public static function mockPrependBody($content)
  198. {
  199. array_unshift(static::$body, (string) $content);
  200. }
  201. /**
  202. * Mock JApplicationWeb->setBody method.
  203. *
  204. * @param string $content The body of the response.
  205. *
  206. * @return void
  207. *
  208. * @since 12.2
  209. */
  210. public static function mockSetBody($content)
  211. {
  212. static::$body = array($content);
  213. }
  214. /**
  215. * Mock JApplicationWeb->getHeaders method.
  216. *
  217. * @return array
  218. *
  219. * @since 3.2
  220. */
  221. public static function mockGetHeaders()
  222. {
  223. return static::$headers;
  224. }
  225. /**
  226. * Mock JApplicationWeb->setHeader method.
  227. *
  228. * @param string $name The name of the header to set.
  229. * @param string $value The value of the header to set.
  230. * @param boolean $replace True to replace any headers with the same name.
  231. *
  232. * @return void
  233. *
  234. * @since 3.2
  235. */
  236. public function mockSetHeader($name, $value, $replace = false)
  237. {
  238. // Sanitize the input values.
  239. $name = (string) $name;
  240. $value = (string) $value;
  241. // If the replace flag is set, unset all known headers with the given name.
  242. if ($replace)
  243. {
  244. foreach (static::$headers as $key => $header)
  245. {
  246. if ($name == $header['name'])
  247. {
  248. unset(static::$headers[$key]);
  249. }
  250. }
  251. // Clean up the array as unsetting nested arrays leaves some junk.
  252. static::$headers = array_values(static::$headers);
  253. }
  254. // Add the header to the internal array.
  255. static::$headers[] = array('name' => $name, 'value' => $value);
  256. }
  257. /**
  258. * Mock JApplicationWeb->clearHeaders method.
  259. *
  260. * @return void
  261. *
  262. * @since 3.2
  263. */
  264. public static function mockClearHeaders()
  265. {
  266. static::$headers = array();
  267. }
  268. /**
  269. * Mock JApplicationWeb->allowCache method.
  270. *
  271. * @param boolean $allow True to allow browser caching.
  272. *
  273. * @return boolean
  274. *
  275. * @since 3.2
  276. */
  277. public static function mockAllowCache($allow = null)
  278. {
  279. if ($allow !== null)
  280. {
  281. static::$cachable = (bool) $allow;
  282. }
  283. return static::$cachable;
  284. }
  285. }