QueryTableTest.php 4.6 KB

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