QueryTableTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 UnitTests
  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_DataSetTestCase
  24. */
  25. require_once "Zend/Test/PHPUnit/Db/DataSet/DataSetTestCase.php";
  26. require_once "PHPUnit/Extensions/Database/DB/IDatabaseConnection.php";
  27. require_once "Zend/Db/Statement/Interface.php";
  28. require_once "Zend/Test/DbAdapter.php";
  29. require_once "Zend/Test/DbStatement.php";
  30. /**
  31. * @category Zend
  32. * @package Zend_Test
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Test
  37. */
  38. class Zend_Test_PHPUnit_Db_DataSet_QueryTableTest extends Zend_Test_PHPUnit_Db_DataSet_DataSetTestCase
  39. {
  40. public function testCreateQueryTableWithoutZendDbConnectionThrowsException()
  41. {
  42. $connectionMock = $this->getMock('PHPUnit_Extensions_Database_DB_IDatabaseConnection');
  43. $this->setExpectedException('Zend_Test_PHPUnit_Db_Exception');
  44. $queryTable = new Zend_Test_PHPUnit_Db_DataSet_QueryTable("foo", "SELECT * FROM foo", $connectionMock);
  45. }
  46. public function testCreateQueryTableWithZendDbConnection()
  47. {
  48. $this->decorateConnectionMockWithZendAdapter();
  49. $queryTable = new Zend_Test_PHPUnit_Db_DataSet_QueryTable("foo", "SELECT * FROM foo", $this->connectionMock);
  50. }
  51. public function testLoadDataExecutesQueryOnZendAdapter()
  52. {
  53. $statementMock = new Zend_Test_DbStatement();
  54. $statementMock->append(array('foo' => 'bar'));
  55. $adapterMock = new Zend_Test_DbAdapter();
  56. $adapterMock->appendStatementToStack($statementMock);
  57. $this->decorateConnectionGetConnectionWith($adapterMock);
  58. $queryTable = new Zend_Test_PHPUnit_Db_DataSet_QueryTable("foo", "SELECT * FROM foo", $this->connectionMock);
  59. $data = $queryTable->getRow(0);
  60. $this->assertEquals(
  61. array("foo" => "bar"), $data
  62. );
  63. }
  64. public function testGetRowCountLoadsData()
  65. {
  66. $statementMock = new Zend_Test_DbStatement();
  67. $statementMock->append(array('foo' => 'bar'));
  68. $adapterMock = new Zend_Test_DbAdapter();
  69. $adapterMock->appendStatementToStack($statementMock);
  70. $this->decorateConnectionGetConnectionWith($adapterMock);
  71. $queryTable = new Zend_Test_PHPUnit_Db_DataSet_QueryTable("foo", "SELECT * FROM foo", $this->connectionMock);
  72. $count = $queryTable->getRowCount();
  73. $this->assertEquals(1, $count);
  74. }
  75. public function testDataIsLoadedOnlyOnce()
  76. {
  77. $fixtureSql = "SELECT * FROM foo";
  78. $statementMock = new Zend_Test_DbStatement();
  79. $statementMock->append(array('foo' => 'bar'));
  80. $adapterMock = $this->getMock('Zend_Test_DbAdapter');
  81. $adapterMock->expects($this->once())
  82. ->method('query')
  83. ->with($fixtureSql)
  84. ->will($this->returnValue($statementMock));
  85. $this->decorateConnectionGetConnectionWith($adapterMock);
  86. $queryTable = new Zend_Test_PHPUnit_Db_DataSet_QueryTable("foo", $fixtureSql, $this->connectionMock);
  87. $this->assertEquals(1, $queryTable->getRowCount());
  88. $this->assertEquals(1, $queryTable->getRowCount());
  89. $row = $queryTable->getRow(0);
  90. $this->assertEquals(array('foo' => 'bar'), $row);
  91. }
  92. public function testQueryTableWithoutRows()
  93. {
  94. $statementMock = new Zend_Test_DbStatement();
  95. $adapterMock = new Zend_Test_DbAdapter();
  96. $adapterMock->appendStatementToStack($statementMock);
  97. $this->decorateConnectionGetConnectionWith($adapterMock);
  98. $queryTable = new Zend_Test_PHPUnit_Db_DataSet_QueryTable("foo", null, $this->connectionMock);
  99. $metadata = $queryTable->getTableMetaData();
  100. $this->assertTrue($metadata instanceof PHPUnit_Extensions_Database_DataSet_ITableMetaData);
  101. $this->assertEquals(array(), $metadata->getColumns());
  102. $this->assertEquals(array(), $metadata->getPrimaryKeys());
  103. $this->assertEquals("foo", $metadata->getTableName());
  104. }
  105. }