You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.3 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-11-3
  8. *
  9. * Unit tests for DbStatement class
  10. */
  11. require_once dirname(__FILE__) . '/../../model/Db.php';
  12. require_once dirname(__FILE__) . '/../../model/DbDriver.php';
  13. require_once dirname(__FILE__) . '/../../model/DbStatement.php';
  14. require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
  15. class DbStatementTest extends PHPUnit_Framework_TestCase
  16. {
  17. private $driver;
  18. private $sql;
  19. private $stmt;
  20. private $testData = array(
  21. array('one' => 11, 'two' => 12),
  22. array('one' => 21, 'two' => 22),
  23. array('one' => 31, 'two' => 32),
  24. );
  25. public function setUp()
  26. {
  27. if (!isset($this->stmt)) {
  28. $this->driver = $this->getMockBuilder('DbDriverMock')
  29. ->disableOriginalConstructor()
  30. ->setMethods(array('quote'))
  31. ->getMock();
  32. $this->sql = 'SELECT * :place FROM :place AND :new';
  33. $this->stmt = $this->getMockForAbstractClass('DbStatement', array($this->driver, $this->sql));
  34. }
  35. }
  36. public function testConstruct()
  37. {
  38. $this->assertAttributeEquals($this->driver, 'driver', $this->stmt);
  39. $this->assertAttributeEquals($this->sql, 'sql', $this->stmt);
  40. }
  41. public function testBindParam()
  42. {
  43. $val = $this->getMockBuilder('DbExpr')
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $this->assertFalse($this->stmt->bindParam('var', $val));
  47. $this->assertTrue($this->stmt->bindParam('place', $val));
  48. }
  49. /**
  50. * @TODO: change Exception Message 'Placeholder must be an array or string' - no arrays available
  51. */
  52. public function testBindParamExceptionParam()
  53. {
  54. $val = 1;
  55. $this->setExpectedException('GeneralException', 'Placeholder must be an array or string');
  56. $this->stmt->bindParam(array(), $val);
  57. }
  58. public function testBindParamExceptionWrongObject()
  59. {
  60. $this->setExpectedException('GeneralException', 'Objects excepts DbExpr not allowed.');
  61. $val = $this->getMock('NotDbExpr');
  62. $this->stmt->bindParam('paa', $val);
  63. }
  64. public function testExecute()
  65. {
  66. $this->stmt
  67. ->expects($this->once())
  68. ->method('driverExecute')
  69. ->with($this->anything())
  70. ->will($this->returnCallback(array($this, 'dbStatementAssemble')));
  71. $this->driver
  72. ->expects($this->any())
  73. ->method('quote')
  74. ->with($this->anything())
  75. ->will($this->returnCallback(array($this, 'driverQuote')));
  76. $result = $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  77. $this->assertEquals('SELECT * place_val FROM place_val AND new_val', $result);
  78. }
  79. public function testExecuteNoPlaceholders()
  80. {
  81. $this->sql = 'PLAIN SQL';
  82. $this->stmt = $this->getMockForAbstractClass('DbStatement', array($this->driver, $this->sql));
  83. $this->stmt
  84. ->expects($this->once())
  85. ->method('driverExecute')
  86. ->with($this->anything())
  87. ->will($this->returnCallback(array($this, 'dbStatementAssemble')));
  88. $result = $this->stmt->execute(array());
  89. $this->assertEquals('PLAIN SQL', $result);
  90. }
  91. public function testFetch()
  92. {
  93. $this->stmt
  94. ->expects($this->any())
  95. ->method('fetch')
  96. ->with($this->anything())
  97. ->will($this->returnCallback(array($this, 'dbStatementFetch')));
  98. $this->assertEquals(11, $this->stmt->fetchField('one'));
  99. $this->assertFalse($this->stmt->fetchField('zero'));
  100. reset($this->testData);
  101. $this->assertEquals(array(11, 21, 31), $this->stmt->fetchColumn('one'));
  102. reset($this->testData);
  103. $result = $this->stmt->fetchAll();
  104. $this->assertEquals(11, $result[0]->one);
  105. $this->assertEquals(32, $result[2]->two);
  106. reset($this->testData);
  107. $result = $this->stmt->fetchPairs();
  108. $this->assertEquals(31, $result['one']);
  109. }
  110. public function dbStatementAssemble($val)
  111. {
  112. return $val;
  113. }
  114. public function driverQuote($val)
  115. {
  116. return $val;
  117. }
  118. public function dbStatementFetch($style)
  119. {
  120. $result = null;
  121. switch ($style) {
  122. case Db::FETCH_OBJ:
  123. $data = each($this->testData);
  124. if ($data !== false) {
  125. $result = new ArrayObject($data['value'], ArrayObject::ARRAY_AS_PROPS);
  126. } else {
  127. $result = false;
  128. }
  129. break;
  130. case Db::FETCH_ASSOC:
  131. $data = each($this->testData);
  132. $result = $data['value'];
  133. break;
  134. case Db::FETCH_NUM:
  135. $data = each($this->testData);
  136. if ($data !== false) {
  137. $data = $data['value'];
  138. $result[0] = key($data);
  139. $result[1] = current($data);
  140. } else {
  141. $result = false;
  142. }
  143. }
  144. return $result;
  145. }
  146. }