/library/Zend/Test/PHPUnit/Db/DataSet/QueryTable.php

https://bitbucket.org/hamidrezas/melobit · PHP · 91 lines · 35 code · 5 blank · 51 comment · 4 complexity · 2d8cdac7ccbcf9cd688372ba23649f30 MD5 · raw file

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