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.

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