DatabaseTestCase.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Test
  17. * @subpackage PHPUnit
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see PHPUnit_Extensions_Database_TestCase
  24. */
  25. require_once "PHPUnit/Extensions/Database/TestCase.php";
  26. /**
  27. * @see Zend_Test_PHPUnit_Db_Operation_Truncate
  28. */
  29. require_once "Zend/Test/PHPUnit/Db/Operation/Truncate.php";
  30. /**
  31. * @see Zend_Test_PHPUnit_Db_Operation_Insert
  32. */
  33. require_once "Zend/Test/PHPUnit/Db/Operation/Insert.php";
  34. /**
  35. * @see Zend_Test_PHPUnit_Db_DataSet_DbTableDataSet
  36. */
  37. require_once "Zend/Test/PHPUnit/Db/DataSet/DbTableDataSet.php";
  38. /**
  39. * @see Zend_Test_PHPUnit_Db_DataSet_DbTable
  40. */
  41. require_once "Zend/Test/PHPUnit/Db/DataSet/DbTable.php";
  42. /**
  43. * @see Zend_Test_PHPUnit_Db_DataSet_DbRowset
  44. */
  45. require_once "Zend/Test/PHPUnit/Db/DataSet/DbRowset.php";
  46. /**
  47. * Generic Testcase for Zend Framework related DbUnit Testing with PHPUnit
  48. *
  49. * @uses PHPUnit_Extensions_Database_TestCase
  50. * @category Zend
  51. * @package Zend_Test
  52. * @subpackage PHPUnit
  53. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  54. * @license http://framework.zend.com/license/new-bsd New BSD License
  55. */
  56. abstract class Zend_Test_PHPUnit_DatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
  57. {
  58. /**
  59. * Creates a new Zend Database Connection using the given Adapter and database schema name.
  60. *
  61. * @param Zend_Db_Adapter_Abstract $connection
  62. * @param string $schema
  63. * @return Zend_Test_PHPUnit_Db_Connection
  64. */
  65. protected function createZendDbConnection(Zend_Db_Adapter_Abstract $connection, $schema)
  66. {
  67. return new Zend_Test_PHPUnit_Db_Connection($connection, $schema);
  68. }
  69. /**
  70. * Convenience function to get access to the database connection.
  71. *
  72. * @return Zend_Db_Adapter_Abstract
  73. */
  74. protected function getAdapter()
  75. {
  76. return $this->getConnection()->getConnection();
  77. }
  78. /**
  79. * Returns the database operation executed in test setup.
  80. *
  81. * @return PHPUnit_Extensions_Database_Operation_DatabaseOperation
  82. */
  83. protected function getSetUpOperation()
  84. {
  85. return new PHPUnit_Extensions_Database_Operation_Composite(array(
  86. new Zend_Test_PHPUnit_Db_Operation_Truncate(),
  87. new Zend_Test_PHPUnit_Db_Operation_Insert(),
  88. ));
  89. }
  90. /**
  91. * Returns the database operation executed in test cleanup.
  92. *
  93. * @return PHPUnit_Extensions_Database_Operation_DatabaseOperation
  94. */
  95. protected function getTearDownOperation()
  96. {
  97. return PHPUnit_Extensions_Database_Operation_Factory::NONE();
  98. }
  99. /**
  100. * Create a dataset based on multiple Zend_Db_Table instances
  101. *
  102. * @param array $tables
  103. * @return Zend_Test_PHPUnit_Db_DataSet_DbTableDataSet
  104. */
  105. protected function createDbTableDataSet(array $tables=array())
  106. {
  107. $dataSet = new Zend_Test_PHPUnit_Db_DataSet_DbTableDataSet();
  108. foreach($tables AS $table) {
  109. $dataSet->addTable($table);
  110. }
  111. return $dataSet;
  112. }
  113. /**
  114. * Create a table based on one Zend_Db_Table instance
  115. *
  116. * @param Zend_Db_Table_Abstract $table
  117. * @param string $where
  118. * @param string $order
  119. * @param string $count
  120. * @param string $offset
  121. * @return Zend_Test_PHPUnit_Db_DataSet_DbTable
  122. */
  123. protected function createDbTable(Zend_Db_Table_Abstract $table, $where=null, $order=null, $count=null, $offset=null)
  124. {
  125. return new Zend_Test_PHPUnit_Db_DataSet_DbTable($table, $where, $order, $count, $offset);
  126. }
  127. /**
  128. * Create a data table based on a Zend_Db_Table_Rowset instance
  129. *
  130. * @param Zend_Db_Table_Rowset_Abstract $rowset
  131. * @param string
  132. * @return Zend_Test_PHPUnit_Db_DataSet_DbRowset
  133. */
  134. protected function createDbRowset(Zend_Db_Table_Rowset_Abstract $rowset, $tableName = null)
  135. {
  136. return new Zend_Test_PHPUnit_Db_DataSet_DbRowset($rowset, $tableName);
  137. }
  138. }