DbStatementTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. require_once "Zend/Test/DbAdapter.php";
  23. require_once "Zend/Test/DbStatement.php";
  24. /**
  25. * @category Zend
  26. * @package Zend_Test
  27. * @subpackage UnitTests
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @group Zend_Test
  31. */
  32. class Zend_Test_DbStatementTest extends PHPUnit_Framework_TestCase
  33. {
  34. public function testRowCountDefault()
  35. {
  36. $stmt = new Zend_Test_DbStatement();
  37. $this->assertEquals(0, $stmt->rowCount());
  38. }
  39. public function testSetRowCount()
  40. {
  41. $stmt = new Zend_Test_DbStatement();
  42. $stmt->setRowCount(10);
  43. $this->assertEquals(10, $stmt->rowCount());
  44. }
  45. public function testCreateSelectStatementWithRows()
  46. {
  47. $rows = array("foo", "bar");
  48. $stmt = Zend_Test_DbStatement::createSelectStatement($rows);
  49. $this->assertTrue($stmt instanceof Zend_Test_DbStatement);
  50. $this->assertEquals($rows, $stmt->fetchAll());
  51. }
  52. public function testCreateInsertStatementWithRowCount()
  53. {
  54. $stmt = Zend_Test_DbStatement::createInsertStatement(1234);
  55. $this->assertTrue($stmt instanceof Zend_Test_DbStatement);
  56. $this->assertEquals(1234, $stmt->rowCount());
  57. }
  58. public function testCreateUpdateStatementWithRowCount()
  59. {
  60. $stmt = Zend_Test_DbStatement::createUpdateStatement(1234);
  61. $this->assertTrue($stmt instanceof Zend_Test_DbStatement);
  62. $this->assertEquals(1234, $stmt->rowCount());
  63. }
  64. public function testCreateDeleteStatementWithRowCount()
  65. {
  66. $stmt = Zend_Test_DbStatement::createDeleteStatement(1234);
  67. $this->assertTrue($stmt instanceof Zend_Test_DbStatement);
  68. $this->assertEquals(1234, $stmt->rowCount());
  69. }
  70. public function testSetFetchRow()
  71. {
  72. $row = array("foo");
  73. $stmt = new Zend_Test_DbStatement();
  74. $stmt->append($row);
  75. $this->assertEquals($row, $stmt->fetch());
  76. }
  77. public function testFetchDefault()
  78. {
  79. $stmt = new Zend_Test_DbStatement();
  80. $this->assertFalse($stmt->fetch());
  81. }
  82. public function testFetchResult_FromEmptyResultStack()
  83. {
  84. $row = array("foo");
  85. $stmt = new Zend_Test_DbStatement();
  86. $stmt->append($row);
  87. $stmt->append($row);
  88. $this->assertTrue($stmt->fetch() !== false);
  89. $this->assertTrue($stmt->fetch() !== false);
  90. $this->assertFalse($stmt->fetch());
  91. }
  92. public function testFetchColumnDefault()
  93. {
  94. $stmt = new Zend_Test_DbStatement();
  95. $this->assertFalse($stmt->fetchColumn());
  96. }
  97. public function testFetchColumn()
  98. {
  99. $row = array("foo" => "bar", "bar" => "baz");
  100. $stmt = new Zend_Test_DbStatement();
  101. $stmt->append($row);
  102. $this->assertEquals("baz", $stmt->fetchColumn(1));
  103. }
  104. public function testFetchColumn_OutOfBounds()
  105. {
  106. $this->setExpectedException("Zend_Db_Statement_Exception");
  107. $row = array("foo" => "bar", "bar" => "baz");
  108. $stmt = new Zend_Test_DbStatement();
  109. $stmt->append($row);
  110. $stmt->fetchColumn(1234);
  111. }
  112. public function testFetchObject()
  113. {
  114. $row = array("foo" => "bar", "bar" => "baz");
  115. $stmt = new Zend_Test_DbStatement();
  116. $stmt->append($row);
  117. $object = $stmt->fetchObject();
  118. $this->assertTrue($object instanceof stdClass);
  119. $this->assertEquals('bar', $object->foo);
  120. $this->assertEquals('baz', $object->bar);
  121. }
  122. public function testFetchObject_ClassNotExists_ThrowsException()
  123. {
  124. $this->setExpectedException("Zend_Db_Statement_Exception");
  125. $row = array("foo" => "bar", "bar" => "baz");
  126. $stmt = new Zend_Test_DbStatement();
  127. $stmt->append($row);
  128. $object = $stmt->fetchObject("anInvalidClassName");
  129. }
  130. }