2
0

QueryTable.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 $table_name
  38. * @param string $query
  39. * @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $databaseConnection
  40. */
  41. public function __construct($tableName, $query, PHPUnit_Extensions_Database_DB_IDatabaseConnection $databaseConnection)
  42. {
  43. if( !($databaseConnection instanceof Zend_Test_PHPUnit_Db_Connection) ) {
  44. require_once "Zend/Test/PHPUnit/Db/Exception.php";
  45. throw new Zend_Test_PHPUnit_Db_Exception("Zend_Test_PHPUnit_Db_DataSet_QueryTable only works with Zend_Test_PHPUnit_Db_Connection connections-");
  46. }
  47. parent::__construct($tableName, $query, $databaseConnection);
  48. }
  49. /**
  50. * Load data from the database.
  51. *
  52. * @return void
  53. */
  54. protected function loadData()
  55. {
  56. if($this->data === null) {
  57. $stmt = $this->databaseConnection->getConnection()->query($this->query);
  58. $this->data = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
  59. }
  60. }
  61. /**
  62. * Create Table Metadata
  63. */
  64. protected function createTableMetaData()
  65. {
  66. if ($this->tableMetaData === NULL)
  67. {
  68. $this->loadData();
  69. $keys = array();
  70. if(count($this->data) > 0) {
  71. $keys = array_keys($this->data[0]);
  72. }
  73. $this->tableMetaData = new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData(
  74. $this->tableName, $keys
  75. );
  76. }
  77. }
  78. }