QueryTable.php 2.9 KB

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