/tests/unit/core/mock/database/driver.php

https://github.com/pjwiseman/joomla-cms · PHP · 221 lines · 125 code · 13 blank · 83 comment · 2 complexity · 491d28d17900f539984510273ce12f65 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 JDatabaseDriver.
  10. *
  11. * @package Joomla.Test
  12. * @since 12.1
  13. */
  14. class TestMockDatabaseDriver
  15. {
  16. /**
  17. * A query string or object.
  18. *
  19. * @var mixed
  20. * @since 11.3
  21. */
  22. public static $lastQuery = null;
  23. /**
  24. * Creates and instance of the mock JDatabaseDriver object.
  25. *
  26. * @param PHPUnit_Framework_TestCase $test A test object.
  27. * @param string $driver Optional driver to create a sub-class of JDatabaseDriver.
  28. * @param array $extraMethods An array of additional methods to add to the mock.
  29. * @param string $nullDate A null date string for the driver.
  30. * @param string $dateFormat A date format for the driver.
  31. *
  32. * @return PHPUnit_Framework_MockObject_MockObject
  33. *
  34. * @since 11.3
  35. */
  36. public static function create($test, $driver = '', array $extraMethods = array(), $nullDate = '0000-00-00 00:00:00', $dateFormat = 'Y-m-d H:i:s')
  37. {
  38. // Collect all the relevant methods in JDatabaseDriver.
  39. $methods = array_merge($extraMethods, array(
  40. 'connect',
  41. 'connected',
  42. 'disconnect',
  43. 'dropTable',
  44. 'escape',
  45. 'execute',
  46. 'fetchArray',
  47. 'fetchAssoc',
  48. 'fetchObject',
  49. 'freeResult',
  50. 'getAffectedRows',
  51. 'getCollation',
  52. 'getConnectionCollation',
  53. 'getConnectors',
  54. 'getDateFormat',
  55. 'getErrorMsg',
  56. 'getErrorNum',
  57. 'getInstance',
  58. 'getLog',
  59. 'getNullDate',
  60. 'getNumRows',
  61. 'getPrefix',
  62. 'getQuery',
  63. 'getTableColumns',
  64. 'getTableCreate',
  65. 'getTableKeys',
  66. 'getTableList',
  67. 'getUtfSupport',
  68. 'getVersion',
  69. 'insertId',
  70. 'insertObject',
  71. 'loadAssoc',
  72. 'loadAssocList',
  73. 'loadColumn',
  74. 'loadObject',
  75. 'loadObjectList',
  76. 'loadResult',
  77. 'loadRow',
  78. 'loadRowList',
  79. 'lockTable',
  80. 'query',
  81. 'quote',
  82. 'quoteName',
  83. 'renameTable',
  84. 'replacePrefix',
  85. 'select',
  86. 'setQuery',
  87. 'setUTF',
  88. 'splitSql',
  89. 'test',
  90. 'isSupported',
  91. 'transactionCommit',
  92. 'transactionRollback',
  93. 'transactionStart',
  94. 'unlockTables',
  95. 'updateObject',
  96. ));
  97. // Create the mock.
  98. $mockObject = $test->getMock(
  99. 'JDatabaseDriver' . $driver,
  100. $methods,
  101. // Constructor arguments.
  102. array(),
  103. // Mock class name.
  104. '',
  105. // Call original constructor.
  106. false
  107. );
  108. // Mock selected methods.
  109. $test->assignMockReturns(
  110. $mockObject, array(
  111. 'getNullDate' => $nullDate,
  112. 'getDateFormat' => $dateFormat
  113. )
  114. );
  115. $test->assignMockCallbacks(
  116. $mockObject,
  117. array(
  118. 'escape' => array((is_callable(array($test, 'mockEscape')) ? $test : __CLASS__), 'mockEscape'),
  119. 'getQuery' => array((is_callable(array($test, 'mockGetQuery')) ? $test : __CLASS__), 'mockGetQuery'),
  120. 'quote' => array((is_callable(array($test, 'mockQuote')) ? $test : __CLASS__), 'mockQuote'),
  121. 'quoteName' => array((is_callable(array($test, 'mockQuoteName')) ? $test : __CLASS__), 'mockQuoteName'),
  122. 'setQuery' => array((is_callable(array($test, 'mockSetQuery')) ? $test : __CLASS__), 'mockSetQuery'),
  123. )
  124. );
  125. return $mockObject;
  126. }
  127. /**
  128. * Callback for the dbo escape method.
  129. *
  130. * @param string $text The input text.
  131. *
  132. * @return string
  133. *
  134. * @since 11.3
  135. */
  136. public static function mockEscape($text)
  137. {
  138. return "_{$text}_";
  139. }
  140. /**
  141. * Callback for the dbo setQuery method.
  142. *
  143. * @param boolean $new True to get a new query, false to get the last query.
  144. *
  145. * @return JDatabaseQuery
  146. *
  147. * @since 11.3
  148. */
  149. public static function mockGetQuery($new = false)
  150. {
  151. if ($new)
  152. {
  153. return new TestMockDatabaseQuery;
  154. }
  155. else
  156. {
  157. return self::$lastQuery;
  158. }
  159. }
  160. /**
  161. * Mocking the quote method.
  162. *
  163. * @param string $value The value to be quoted.
  164. * @param boolean $escape Optional parameter to provide extra escaping.
  165. *
  166. * @return string The value passed wrapped in MySQL quotes.
  167. *
  168. * @since 11.3
  169. */
  170. public static function mockQuote($value, $escape = true)
  171. {
  172. if (is_array($value))
  173. {
  174. foreach ($value as $k => $v)
  175. {
  176. $value[$k] = self::mockQuote($v, $escape);
  177. }
  178. return $value;
  179. }
  180. return '\'' . ($escape ? self::mockEscape($value) : $value) . '\'';
  181. }
  182. /**
  183. * Mock quoteName method.
  184. *
  185. * @param string $value The value to be quoted.
  186. *
  187. * @return string The value passed wrapped in MySQL quotes.
  188. *
  189. * @since 11.3
  190. */
  191. public static function mockQuoteName($value)
  192. {
  193. return "`$value`";
  194. }
  195. /**
  196. * Callback for the dbo setQuery method.
  197. *
  198. * @param string $query The query.
  199. *
  200. * @return void
  201. *
  202. * @since 11.3
  203. */
  204. public static function mockSetQuery($query)
  205. {
  206. self::$lastQuery = $query;
  207. }
  208. }