DbTableDataSet.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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-2012 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. require_once "PHPUnit/Extensions/Database/DataSet/QueryDataSet.php";
  23. require_once "PHPUnit/Extensions/Database/DB/IDatabaseConnection.php";
  24. /**
  25. * @see Zend_Test_PHPUnit_Db_DataSet_DbTable
  26. */
  27. require_once "Zend/Test/PHPUnit/Db/DataSet/DbTable.php";
  28. /**
  29. * Aggregate several Zend_Db_Table instances into a dataset.
  30. *
  31. * @uses Zend_Db_Table
  32. * @category Zend
  33. * @package Zend_Test
  34. * @subpackage PHPUnit
  35. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_Test_PHPUnit_Db_DataSet_DbTableDataSet extends PHPUnit_Extensions_Database_DataSet_AbstractDataSet
  39. {
  40. /**
  41. * @var array
  42. */
  43. protected $tables = array();
  44. /**
  45. * Add a Table dataset representation by specifiying an arbitrary select query.
  46. *
  47. * By default a select * will be done on the given tablename.
  48. *
  49. * @param Zend_Db_Table_Abstract $table
  50. * @param string|Zend_Db_Select $query
  51. * @param string $where
  52. * @param string $order
  53. * @param string $count
  54. * @param string $offset
  55. */
  56. public function addTable(Zend_Db_Table_Abstract $table, $where = null, $order = null, $count = null, $offset = null)
  57. {
  58. $tableName = $table->info('name');
  59. $this->tables[$tableName] = new Zend_Test_PHPUnit_Db_DataSet_DbTable($table, $where, $order, $count, $offset);
  60. }
  61. /**
  62. * Creates an iterator over the tables in the data set. If $reverse is
  63. * true a reverse iterator will be returned.
  64. *
  65. * @param bool $reverse
  66. * @return PHPUnit_Extensions_Database_DB_TableIterator
  67. */
  68. protected function createIterator($reverse = FALSE)
  69. {
  70. return new PHPUnit_Extensions_Database_DataSet_DefaultTableIterator($this->tables, $reverse);
  71. }
  72. /**
  73. * Returns a table object for the given table.
  74. *
  75. * @param string $tableName
  76. * @return PHPUnit_Extensions_Database_DB_Table
  77. */
  78. public function getTable($tableName)
  79. {
  80. if (!isset($this->tables[$tableName])) {
  81. throw new InvalidArgumentException("$tableName is not a table in the current database.");
  82. }
  83. return $this->tables[$tableName];
  84. }
  85. /**
  86. * Returns a list of table names for the database
  87. *
  88. * @return Array
  89. */
  90. public function getTableNames()
  91. {
  92. return array_keys($this->tables);
  93. }
  94. }