QueryDataSet.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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-2009 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_QueryTable
  26. */
  27. require_once "Zend/Test/PHPUnit/Db/DataSet/QueryTable.php";
  28. /**
  29. * @see Zend_Db_Select
  30. */
  31. require_once "Zend/Db/Select.php";
  32. /**
  33. * Uses several query strings or Zend_Db_Select objects to form a dataset of tables for assertion with other datasets.
  34. *
  35. * @uses PHPUnit_Extensions_Database_DataSet_QueryDataSet
  36. * @category Zend
  37. * @package Zend_Test
  38. * @subpackage PHPUnit
  39. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Zend_Test_PHPUnit_Db_DataSet_QueryDataSet extends PHPUnit_Extensions_Database_DataSet_QueryDataSet
  43. {
  44. /**
  45. * Creates a new dataset using the given database connection.
  46. *
  47. * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $databaseConnection
  48. */
  49. public function __construct(PHPUnit_Extensions_Database_DB_IDatabaseConnection $databaseConnection)
  50. {
  51. if( !($databaseConnection instanceof Zend_Test_PHPUnit_Db_Connection) ) {
  52. require_once "Zend/Test/PHPUnit/Db/Exception.php";
  53. throw new Zend_Test_PHPUnit_Db_Exception("Zend_Test_PHPUnit_Db_DataSet_QueryDataSet only works with Zend_Test_PHPUnit_Db_Connection connections-");
  54. }
  55. $this->databaseConnection = $databaseConnection;
  56. }
  57. /**
  58. * Add a Table dataset representation by specifiying an arbitrary select query.
  59. *
  60. * By default a select * will be done on the given tablename.
  61. *
  62. * @param string $tableName
  63. * @param string|Zend_Db_Select $query
  64. */
  65. public function addTable($tableName, $query = NULL)
  66. {
  67. if ($query === NULL) {
  68. $query = $this->databaseConnection->getConnection()->select();
  69. $query->from($tableName, Zend_Db_Select::SQL_WILDCARD);
  70. }
  71. if($query instanceof Zend_Db_Select) {
  72. $query = $query->__toString();
  73. }
  74. $this->tables[$tableName] = new Zend_Test_PHPUnit_Db_DataSet_QueryTable($tableName, $query, $this->databaseConnection);
  75. }
  76. }