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.

172 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. * @expectedException GeneralException
  51. * @expectedExceptionMessage Placeholder must be an array or string
  52. * @TODO: change Exception Message 'Placeholder must be an array or string' - no arrays available
  53. */
  54. public function testBindParamExceptionParam()
  55. {
  56. $val = 1;
  57. $this->stmt->bindParam(array(), $val);
  58. }
  59. /**
  60. * @expectedException GeneralException
  61. * @expectedExceptionMessage Objects excepts DbExpr not allowed.
  62. */
  63. public function testBindParamExceptionWrongObject()
  64. {
  65. $val = $this->getMock('NotDbExpr');
  66. $this->stmt->bindParam('paa', $val);
  67. }
  68. public function testExecute()
  69. {
  70. $this->stmt
  71. ->expects($this->once())
  72. ->method('driverExecute')
  73. ->with($this->anything())
  74. ->will($this->returnCallback(array($this, 'dbStatementAssemble')));
  75. $this->driver
  76. ->expects($this->any())
  77. ->method('quote')
  78. ->with($this->anything())
  79. ->will($this->returnCallback(array($this, 'driverQuote')));
  80. $result = $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val'));
  81. $this->assertEquals('SELECT * place_val FROM place_val AND new_val', $result);
  82. }
  83. public function testExecuteNoPlaceholders()
  84. {
  85. $this->sql = 'PLAIN SQL';
  86. $this->stmt = $this->getMockForAbstractClass('DbStatement', array($this->driver, $this->sql));
  87. $this->stmt
  88. ->expects($this->once())
  89. ->method('driverExecute')
  90. ->with($this->anything())
  91. ->will($this->returnCallback(array($this, 'dbStatementAssemble')));
  92. $result = $this->stmt->execute(array());
  93. $this->assertEquals('PLAIN SQL', $result);
  94. }
  95. public function testFetch()
  96. {
  97. $this->stmt
  98. ->expects($this->any())
  99. ->method('fetch')
  100. ->with($this->anything())
  101. ->will($this->returnCallback(array($this, 'dbStatementFetch')));
  102. $this->assertEquals(11, $this->stmt->fetchField('one'));
  103. $this->assertFalse($this->stmt->fetchField('zero'));
  104. reset($this->testData);
  105. $this->assertEquals(array(11, 21, 31), $this->stmt->fetchColumn('one'));
  106. reset($this->testData);
  107. $result = $this->stmt->fetchAll();
  108. $this->assertEquals(11, $result[0]->one);
  109. $this->assertEquals(32, $result[2]->two);
  110. reset($this->testData);
  111. $result = $this->stmt->fetchPairs();
  112. $this->assertEquals(31, $result['one']);
  113. }
  114. public function dbStatementAssemble($val)
  115. {
  116. return $val;
  117. }
  118. public function driverQuote($val)
  119. {
  120. return $val;
  121. }
  122. public function dbStatementFetch($style)
  123. {
  124. $result = null;
  125. switch ($style) {
  126. case Db::FETCH_OBJ:
  127. $data = each($this->testData);
  128. if ($data !== false) {
  129. $result = new ArrayObject($data['value'], ArrayObject::ARRAY_AS_PROPS);
  130. } else {
  131. $result = false;
  132. }
  133. break;
  134. case Db::FETCH_ASSOC:
  135. $data = each($this->testData);
  136. $result = $data['value'];
  137. break;
  138. case Db::FETCH_NUM:
  139. $data = each($this->testData);
  140. if ($data !== false) {
  141. $data = $data['value'];
  142. $result[0] = key($data);
  143. $result[1] = current($data);
  144. } else {
  145. $result = false;
  146. }
  147. }
  148. return $result;
  149. }
  150. }