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

/libs/php/pear/PHPUnit/Extensions/Database/TestCase.php

https://github.com/KaRLsM/SIFO
PHP | 250 lines | 75 code | 24 blank | 151 comment | 1 complexity | b37202d6d9a26cba30d095fb44fcd106 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2011, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package DbUnit
  38. * @author Mike Lively <m@digitalsandwich.com>
  39. * @copyright 2002-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
  40. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  41. * @link http://www.phpunit.de/
  42. * @since File available since Release 1.0.0
  43. */
  44. /**
  45. * A TestCase extension that provides functionality for testing and asserting
  46. * against a real database.
  47. *
  48. * @package DbUnit
  49. * @author Mike Lively <m@digitalsandwich.com>
  50. * @copyright 2010 Mike Lively <m@digitalsandwich.com>
  51. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  52. * @version Release: 1.0.3
  53. * @link http://www.phpunit.de/
  54. * @since Class available since Release 1.0.0
  55. */
  56. abstract class PHPUnit_Extensions_Database_TestCase extends PHPUnit_Framework_TestCase
  57. {
  58. /**
  59. * @var PHPUnit_Extensions_Database_ITester
  60. */
  61. protected $databaseTester;
  62. /**
  63. * Closes the specified connection.
  64. *
  65. * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
  66. */
  67. protected function closeConnection(PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection)
  68. {
  69. $this->getDatabaseTester()->closeConnection($connection);
  70. }
  71. /**
  72. * Returns the test database connection.
  73. *
  74. * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
  75. */
  76. protected abstract function getConnection();
  77. /**
  78. * Gets the IDatabaseTester for this testCase. If the IDatabaseTester is
  79. * not set yet, this method calls newDatabaseTester() to obtain a new
  80. * instance.
  81. *
  82. * @return PHPUnit_Extensions_Database_ITester
  83. */
  84. protected function getDatabaseTester()
  85. {
  86. if (empty($this->databaseTester)) {
  87. $this->databaseTester = $this->newDatabaseTester();
  88. }
  89. return $this->databaseTester;
  90. }
  91. /**
  92. * Returns the test dataset.
  93. *
  94. * @return PHPUnit_Extensions_Database_DataSet_IDataSet
  95. */
  96. protected abstract function getDataSet();
  97. /**
  98. * Returns the database operation executed in test setup.
  99. *
  100. * @return PHPUnit_Extensions_Database_Operation_DatabaseOperation
  101. */
  102. protected function getSetUpOperation()
  103. {
  104. return PHPUnit_Extensions_Database_Operation_Factory::CLEAN_INSERT();
  105. }
  106. /**
  107. * Returns the database operation executed in test cleanup.
  108. *
  109. * @return PHPUnit_Extensions_Database_Operation_DatabaseOperation
  110. */
  111. protected function getTearDownOperation()
  112. {
  113. return PHPUnit_Extensions_Database_Operation_Factory::NONE();
  114. }
  115. /**
  116. * Creates a IDatabaseTester for this testCase.
  117. *
  118. * @return PHPUnit_Extensions_Database_ITester
  119. */
  120. protected function newDatabaseTester()
  121. {
  122. return new PHPUnit_Extensions_Database_DefaultTester($this->getConnection());
  123. }
  124. /**
  125. * Creates a new DefaultDatabaseConnection using the given PDO connection
  126. * and database schema name.
  127. *
  128. * @param PDO $connection
  129. * @param string $schema
  130. * @return PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection
  131. */
  132. protected function createDefaultDBConnection(PDO $connection, $schema = '')
  133. {
  134. return new PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection($connection, $schema);
  135. }
  136. /**
  137. * Creates a new FlatXmlDataSet with the given $xmlFile. (absolute path.)
  138. *
  139. * @param string $xmlFile
  140. * @return PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet
  141. */
  142. protected function createFlatXMLDataSet($xmlFile)
  143. {
  144. return new PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet($xmlFile);
  145. }
  146. /**
  147. * Creates a new XMLDataSet with the given $xmlFile. (absolute path.)
  148. *
  149. * @param string $xmlFile
  150. * @return PHPUnit_Extensions_Database_DataSet_XmlDataSet
  151. */
  152. protected function createXMLDataSet($xmlFile)
  153. {
  154. return new PHPUnit_Extensions_Database_DataSet_XmlDataSet($xmlFile);
  155. }
  156. /**
  157. * Create a a new MysqlXmlDataSet with the given $xmlFile. (absolute path.)
  158. *
  159. * @param string $xmlFile
  160. * @return PHPUnit_Extensions_Database_DataSet_MysqlXmlDataSet
  161. * @since Method available since Release 1.0.0
  162. */
  163. protected function createMySQLXMLDataSet($xmlFile)
  164. {
  165. return new PHPUnit_Extensions_Database_DataSet_MysqlXmlDataSet($xmlFile);
  166. }
  167. /**
  168. * Returns an operation factory instance that can be used to instantiate
  169. * new operations.
  170. *
  171. * @return PHPUnit_Extensions_Database_Operation_Factory
  172. */
  173. protected function getOperations()
  174. {
  175. return new PHPUnit_Extensions_Database_Operation_Factory();
  176. }
  177. /**
  178. * Performs operation returned by getSetUpOperation().
  179. */
  180. protected function setUp()
  181. {
  182. parent::setUp();
  183. $this->databaseTester = NULL;
  184. $this->getDatabaseTester()->setSetUpOperation($this->getSetUpOperation());
  185. $this->getDatabaseTester()->setDataSet($this->getDataSet());
  186. $this->getDatabaseTester()->onSetUp();
  187. }
  188. /**
  189. * Performs operation returned by getSetUpOperation().
  190. */
  191. protected function tearDown()
  192. {
  193. $this->getDatabaseTester()->setTearDownOperation($this->getTearDownOperation());
  194. $this->getDatabaseTester()->setDataSet($this->getDataSet());
  195. $this->getDatabaseTester()->onTearDown();
  196. /**
  197. * Destroy the tester after the test is run to keep DB connections
  198. * from piling up.
  199. */
  200. $this->databaseTester = NULL;
  201. }
  202. /**
  203. * Asserts that two given tables are equal.
  204. *
  205. * @param PHPUnit_Extensions_Database_DataSet_ITable $expected
  206. * @param PHPUnit_Extensions_Database_DataSet_ITable $actual
  207. * @param string $message
  208. */
  209. public static function assertTablesEqual(PHPUnit_Extensions_Database_DataSet_ITable $expected, PHPUnit_Extensions_Database_DataSet_ITable $actual, $message = '')
  210. {
  211. $constraint = new PHPUnit_Extensions_Database_Constraint_TableIsEqual($expected);
  212. self::assertThat($actual, $constraint, $message);
  213. }
  214. /**
  215. * Asserts that two given datasets are equal.
  216. *
  217. * @param PHPUnit_Extensions_Database_DataSet_ITable $expected
  218. * @param PHPUnit_Extensions_Database_DataSet_ITable $actual
  219. * @param string $message
  220. */
  221. public static function assertDataSetsEqual(PHPUnit_Extensions_Database_DataSet_IDataSet $expected, PHPUnit_Extensions_Database_DataSet_IDataSet $actual, $message = '')
  222. {
  223. $constraint = new PHPUnit_Extensions_Database_Constraint_DataSetIsEqual($expected);
  224. self::assertThat($actual, $constraint, $message);
  225. }
  226. }