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.

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