DatabaseTestCase.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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-2015 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 Zend_Test_PHPUnit_Db_Operation_Truncate
  24. */
  25. require_once "Zend/Test/PHPUnit/Db/Operation/Truncate.php";
  26. /**
  27. * @see Zend_Test_PHPUnit_Db_Operation_Insert
  28. */
  29. require_once "Zend/Test/PHPUnit/Db/Operation/Insert.php";
  30. /**
  31. * @see Zend_Test_PHPUnit_Db_DataSet_DbTableDataSet
  32. */
  33. require_once "Zend/Test/PHPUnit/Db/DataSet/DbTableDataSet.php";
  34. /**
  35. * @see Zend_Test_PHPUnit_Db_DataSet_DbTable
  36. */
  37. require_once "Zend/Test/PHPUnit/Db/DataSet/DbTable.php";
  38. /**
  39. * @see Zend_Test_PHPUnit_Db_DataSet_DbRowset
  40. */
  41. require_once "Zend/Test/PHPUnit/Db/DataSet/DbRowset.php";
  42. /**
  43. * Generic Testcase for Zend Framework related DbUnit Testing with PHPUnit
  44. *
  45. * @uses PHPUnit_Extensions_Database_TestCase
  46. * @category Zend
  47. * @package Zend_Test
  48. * @subpackage PHPUnit
  49. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  50. * @license http://framework.zend.com/license/new-bsd New BSD License
  51. */
  52. abstract class Zend_Test_PHPUnit_DatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
  53. {
  54. /**
  55. * Creates a new Zend Database Connection using the given Adapter and database schema name.
  56. *
  57. * @param Zend_Db_Adapter_Abstract $connection
  58. * @param string $schema
  59. * @return Zend_Test_PHPUnit_Db_Connection
  60. */
  61. protected function createZendDbConnection(Zend_Db_Adapter_Abstract $connection, $schema)
  62. {
  63. return new Zend_Test_PHPUnit_Db_Connection($connection, $schema);
  64. }
  65. /**
  66. * Convenience function to get access to the database connection.
  67. *
  68. * @return Zend_Db_Adapter_Abstract
  69. */
  70. protected function getAdapter()
  71. {
  72. return $this->getConnection()->getConnection();
  73. }
  74. /**
  75. * Returns the database operation executed in test setup.
  76. *
  77. * @return PHPUnit_Extensions_Database_Operation_DatabaseOperation
  78. */
  79. protected function getSetUpOperation()
  80. {
  81. return new PHPUnit_Extensions_Database_Operation_Composite(array(
  82. new Zend_Test_PHPUnit_Db_Operation_Truncate(),
  83. new Zend_Test_PHPUnit_Db_Operation_Insert(),
  84. ));
  85. }
  86. /**
  87. * Returns the database operation executed in test cleanup.
  88. *
  89. * @return PHPUnit_Extensions_Database_Operation_DatabaseOperation
  90. */
  91. protected function getTearDownOperation()
  92. {
  93. return PHPUnit_Extensions_Database_Operation_Factory::NONE();
  94. }
  95. /**
  96. * Create a dataset based on multiple Zend_Db_Table instances
  97. *
  98. * @param array $tables
  99. * @return Zend_Test_PHPUnit_Db_DataSet_DbTableDataSet
  100. */
  101. protected function createDbTableDataSet(array $tables=array())
  102. {
  103. $dataSet = new Zend_Test_PHPUnit_Db_DataSet_DbTableDataSet();
  104. foreach($tables AS $table) {
  105. $dataSet->addTable($table);
  106. }
  107. return $dataSet;
  108. }
  109. /**
  110. * Create a table based on one Zend_Db_Table instance
  111. *
  112. * @param Zend_Db_Table_Abstract $table
  113. * @param string $where
  114. * @param string $order
  115. * @param string $count
  116. * @param string $offset
  117. * @return Zend_Test_PHPUnit_Db_DataSet_DbTable
  118. */
  119. protected function createDbTable(Zend_Db_Table_Abstract $table, $where=null, $order=null, $count=null, $offset=null)
  120. {
  121. return new Zend_Test_PHPUnit_Db_DataSet_DbTable($table, $where, $order, $count, $offset);
  122. }
  123. /**
  124. * Create a data table based on a Zend_Db_Table_Rowset instance
  125. *
  126. * @param Zend_Db_Table_Rowset_Abstract $rowset
  127. * @param string
  128. * @return Zend_Test_PHPUnit_Db_DataSet_DbRowset
  129. */
  130. protected function createDbRowset(Zend_Db_Table_Rowset_Abstract $rowset, $tableName = null)
  131. {
  132. return new Zend_Test_PHPUnit_Db_DataSet_DbRowset($rowset, $tableName);
  133. }
  134. }