/vendor_full/doctrine-dbal/tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php

https://github.com/l3l0/BehatExamples · PHP · 222 lines · 164 code · 48 blank · 10 comment · 0 complexity · 5f5328f25a412f3e541319b3d516c249 MD5 · raw file

  1. <?php
  2. namespace Doctrine\Tests\DBAL\Functional;
  3. use Doctrine\DBAL\Types\Type;
  4. use PDO;
  5. require_once __DIR__ . '/../../TestInit.php';
  6. class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
  7. {
  8. public function setUp()
  9. {
  10. parent::setUp();
  11. try {
  12. /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
  13. $table = new \Doctrine\DBAL\Schema\Table("fetch_table");
  14. $table->addColumn('test_int', 'integer');
  15. $table->addColumn('test_string', 'string');
  16. $table->addColumn('test_datetime', 'datetime', array('notnull' => false));
  17. $sm = $this->_conn->getSchemaManager();
  18. $sm->createTable($table);
  19. $this->_conn->insert('fetch_table', array('test_int' => 1, 'test_string' => 'foo', 'test_datetime' => '2010-01-01 10:10:10'));
  20. } catch(\Exception $e) {
  21. }
  22. }
  23. public function testPrepareWithBindValue()
  24. {
  25. $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
  26. $stmt = $this->_conn->prepare($sql);
  27. $this->assertInstanceOf('Doctrine\DBAL\Statement', $stmt);
  28. $stmt->bindValue(1, 1);
  29. $stmt->bindValue(2, 'foo');
  30. $stmt->execute();
  31. $row = $stmt->fetch(\PDO::FETCH_ASSOC);
  32. $row = array_change_key_case($row, \CASE_LOWER);
  33. $this->assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $row);
  34. }
  35. public function testPrepareWithBindParam()
  36. {
  37. $paramInt = 1;
  38. $paramStr = 'foo';
  39. $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
  40. $stmt = $this->_conn->prepare($sql);
  41. $this->assertInstanceOf('Doctrine\DBAL\Statement', $stmt);
  42. $stmt->bindParam(1, $paramInt);
  43. $stmt->bindParam(2, $paramStr);
  44. $stmt->execute();
  45. $row = $stmt->fetch(\PDO::FETCH_ASSOC);
  46. $row = array_change_key_case($row, \CASE_LOWER);
  47. $this->assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $row);
  48. }
  49. public function testPrepareWithFetchAll()
  50. {
  51. $paramInt = 1;
  52. $paramStr = 'foo';
  53. $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
  54. $stmt = $this->_conn->prepare($sql);
  55. $this->assertInstanceOf('Doctrine\DBAL\Statement', $stmt);
  56. $stmt->bindParam(1, $paramInt);
  57. $stmt->bindParam(2, $paramStr);
  58. $stmt->execute();
  59. $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  60. $rows[0] = array_change_key_case($rows[0], \CASE_LOWER);
  61. $this->assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $rows[0]);
  62. }
  63. public function testPrepareWithFetchColumn()
  64. {
  65. $paramInt = 1;
  66. $paramStr = 'foo';
  67. $sql = "SELECT test_int FROM fetch_table WHERE test_int = ? AND test_string = ?";
  68. $stmt = $this->_conn->prepare($sql);
  69. $this->assertInstanceOf('Doctrine\DBAL\Statement', $stmt);
  70. $stmt->bindParam(1, $paramInt);
  71. $stmt->bindParam(2, $paramStr);
  72. $stmt->execute();
  73. $column = $stmt->fetchColumn();
  74. $this->assertEquals(1, $column);
  75. }
  76. public function testPrepareWithQuoted()
  77. {
  78. $table = 'fetch_table';
  79. $paramInt = 1;
  80. $paramStr = 'foo';
  81. $sql = "SELECT test_int, test_string FROM " . $this->_conn->quoteIdentifier($table) . " ".
  82. "WHERE test_int = " . $this->_conn->quote($paramInt) . " AND test_string = " . $this->_conn->quote($paramStr);
  83. $stmt = $this->_conn->prepare($sql);
  84. $this->assertInstanceOf('Doctrine\DBAL\Statement', $stmt);
  85. }
  86. public function testPrepareWithExecuteParams()
  87. {
  88. $paramInt = 1;
  89. $paramStr = 'foo';
  90. $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
  91. $stmt = $this->_conn->prepare($sql);
  92. $this->assertInstanceOf('Doctrine\DBAL\Statement', $stmt);
  93. $stmt->execute(array($paramInt, $paramStr));
  94. $row = $stmt->fetch(\PDO::FETCH_ASSOC);
  95. $row = array_change_key_case($row, \CASE_LOWER);
  96. $this->assertEquals(array('test_int' => 1, 'test_string' => 'foo'), $row);
  97. }
  98. public function testFetchAll()
  99. {
  100. $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
  101. $data = $this->_conn->fetchAll($sql, array(1, 'foo'));
  102. $this->assertEquals(1, count($data));
  103. $row = $data[0];
  104. $this->assertEquals(2, count($row));
  105. $row = array_change_key_case($row, \CASE_LOWER);
  106. $this->assertEquals(1, $row['test_int']);
  107. $this->assertEquals('foo', $row['test_string']);
  108. }
  109. public function testFetchRow()
  110. {
  111. $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
  112. $row = $this->_conn->fetchAssoc($sql, array(1, 'foo'));
  113. $row = array_change_key_case($row, \CASE_LOWER);
  114. $this->assertEquals(1, $row['test_int']);
  115. $this->assertEquals('foo', $row['test_string']);
  116. }
  117. public function testFetchArray()
  118. {
  119. $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
  120. $row = $this->_conn->fetchArray($sql, array(1, 'foo'));
  121. $this->assertEquals(1, $row[0]);
  122. $this->assertEquals('foo', $row[1]);
  123. }
  124. public function testFetchColumn()
  125. {
  126. $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
  127. $testInt = $this->_conn->fetchColumn($sql, array(1, 'foo'), 0);
  128. $this->assertEquals(1, $testInt);
  129. $sql = "SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?";
  130. $testString = $this->_conn->fetchColumn($sql, array(1, 'foo'), 1);
  131. $this->assertEquals('foo', $testString);
  132. }
  133. /**
  134. * @group DDC-697
  135. */
  136. public function testExecuteQueryBindDateTimeType()
  137. {
  138. $sql = 'SELECT count(*) AS c FROM fetch_table WHERE test_datetime = ?';
  139. $stmt = $this->_conn->executeQuery($sql,
  140. array(1 => new \DateTime('2010-01-01 10:10:10')),
  141. array(1 => Type::DATETIME)
  142. );
  143. $this->assertEquals(1, $stmt->fetchColumn());
  144. }
  145. /**
  146. * @group DDC-697
  147. */
  148. public function testExecuteUpdateBindDateTimeType()
  149. {
  150. $datetime = new \DateTime('2010-02-02 20:20:20');
  151. $sql = 'INSERT INTO fetch_table (test_int, test_string, test_datetime) VALUES (?, ?, ?)';
  152. $affectedRows = $this->_conn->executeUpdate($sql,
  153. array(1 => 1, 2 => 'foo', 3 => $datetime),
  154. array(1 => PDO::PARAM_INT, 2 => PDO::PARAM_STR, 3 => Type::DATETIME)
  155. );
  156. $this->assertEquals(1, $affectedRows);
  157. $this->assertEquals(1, $this->_conn->executeQuery(
  158. 'SELECT count(*) AS c FROM fetch_table WHERE test_datetime = ?',
  159. array(1 => $datetime),
  160. array(1 => Type::DATETIME)
  161. )->fetchColumn());
  162. }
  163. /**
  164. * @group DDC-697
  165. */
  166. public function testPrepareQueryBindValueDateTimeType()
  167. {
  168. $sql = 'SELECT count(*) AS c FROM fetch_table WHERE test_datetime = ?';
  169. $stmt = $this->_conn->prepare($sql);
  170. $stmt->bindValue(1, new \DateTime('2010-01-01 10:10:10'), Type::DATETIME);
  171. $stmt->execute();
  172. $this->assertEquals(1, $stmt->fetchColumn());
  173. }
  174. }