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.

101 lines
3.1 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, 'request', $this->stmt);
  40. }
  41. public function testFetch()
  42. {
  43. $this->stmt
  44. ->expects($this->any())
  45. ->method('fetch')
  46. ->with($this->anything())
  47. ->will($this->returnCallback(array($this, 'dbStatementFetch')));
  48. $this->assertSame(11, $this->stmt->fetchField('one'));
  49. $this->assertFalse($this->stmt->fetchField('zero'));
  50. reset($this->testData);
  51. $this->assertSame(array(11, 21, 31), $this->stmt->fetchColumn('one'));
  52. reset($this->testData);
  53. $result = $this->stmt->fetchAll();
  54. $this->assertSame(11, $result[0]->one);
  55. $this->assertSame(32, $result[2]->two);
  56. // reset($this->testData);
  57. // $result = $this->stmt->fetchPairs();
  58. // $this->assertEquals(31, $result['one']);
  59. }
  60. public function dbStatementFetch($style)
  61. {
  62. $result = null;
  63. switch ($style) {
  64. case Db::FETCH_OBJ:
  65. $data = each($this->testData);
  66. if ($data !== false) {
  67. $result = new ArrayObject($data['value'], ArrayObject::ARRAY_AS_PROPS);
  68. } else {
  69. $result = false;
  70. }
  71. break;
  72. case Db::FETCH_ASSOC:
  73. $data = each($this->testData);
  74. $result = $data['value'];
  75. break;
  76. case Db::FETCH_NUM:
  77. $data = each($this->testData);
  78. if ($data !== false) {
  79. $data = $data['value'];
  80. $result[0] = key($data);
  81. $result[1] = current($data);
  82. } else {
  83. $result = false;
  84. }
  85. }
  86. return $result;
  87. }
  88. }