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

https://github.com/dextercowley/joomla-cms · PHP · 288 lines · 125 code · 26 blank · 137 comment · 6 complexity · 8bcfb033d73e7510b6868f0d2c9ef2ff MD5 · raw file

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