QueryDataSet.php 2.8 KB

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