DbStatementTest.php 4.6 KB

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