QueryTable.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/QueryTable.php";
  23. require_once "PHPUnit/Extensions/Database/DB/IDatabaseConnection.php";
  24. /**
  25. * Represent a PHPUnit Database Extension table with Queries using a Zend_Db adapter for assertion against other tables.
  26. *
  27. * @uses PHPUnit_Extensions_Database_DataSet_QueryTable
  28. * @category Zend
  29. * @package Zend_Test
  30. * @subpackage PHPUnit
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Test_PHPUnit_Db_DataSet_QueryTable extends PHPUnit_Extensions_Database_DataSet_QueryTable
  35. {
  36. /**
  37. * Creates a new database query table object.
  38. *
  39. * @param string $table_name
  40. * @param string $query
  41. * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $databaseConnection
  42. */
  43. public function __construct($tableName, $query, PHPUnit_Extensions_Database_DB_IDatabaseConnection $databaseConnection)
  44. {
  45. if( !($databaseConnection instanceof Zend_Test_PHPUnit_Db_Connection) ) {
  46. require_once "Zend/Test/PHPUnit/Db/Exception.php";
  47. throw new Zend_Test_PHPUnit_Db_Exception("Zend_Test_PHPUnit_Db_DataSet_QueryTable only works with Zend_Test_PHPUnit_Db_Connection connections-");
  48. }
  49. parent::__construct($tableName, $query, $databaseConnection);
  50. }
  51. /**
  52. * Load data from the database.
  53. *
  54. * @return void
  55. */
  56. protected function loadData()
  57. {
  58. if($this->data === null) {
  59. $stmt = $this->databaseConnection->getConnection()->query($this->query);
  60. $this->data = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
  61. }
  62. }
  63. /**
  64. * Create Table Metadata
  65. */
  66. protected function createTableMetaData()
  67. {
  68. if ($this->tableMetaData === NULL)
  69. {
  70. $this->loadData();
  71. $keys = array();
  72. if(count($this->data) > 0) {
  73. $keys = array_keys($this->data[0]);
  74. }
  75. $this->tableMetaData = new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData(
  76. $this->tableName, $keys
  77. );
  78. }
  79. }
  80. }