NamedParametersTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Functional\Ticket;
  3. use Doctrine\DBAL\Connection;
  4. use\PDO;
  5. require_once __DIR__ . '/../../TestInit.php';
  6. /**
  7. * @group DDC-1372
  8. */
  9. class NamedParametersTest extends \Doctrine\Tests\DbalFunctionalTestCase
  10. {
  11. public function ticketProvider()
  12. {
  13. return array(
  14. array(
  15. 'SELECT * FROM ddc1372_foobar f WHERE f.foo = :foo AND f.bar IN (:bar)',
  16. array('foo'=>1,'bar'=> array(1, 2, 3)),
  17. array('foo'=>PDO::PARAM_INT,'bar'=> Connection::PARAM_INT_ARRAY,),
  18. array(
  19. array('id'=>1,'foo'=>1,'bar'=>1),
  20. array('id'=>2,'foo'=>1,'bar'=>2),
  21. array('id'=>3,'foo'=>1,'bar'=>3),
  22. )
  23. ),
  24. array(
  25. 'SELECT * FROM ddc1372_foobar f WHERE f.foo = :foo AND f.bar IN (:bar)',
  26. array('foo'=>1,'bar'=> array(1, 2, 3)),
  27. array('bar'=> Connection::PARAM_INT_ARRAY,'foo'=>PDO::PARAM_INT),
  28. array(
  29. array('id'=>1,'foo'=>1,'bar'=>1),
  30. array('id'=>2,'foo'=>1,'bar'=>2),
  31. array('id'=>3,'foo'=>1,'bar'=>3),
  32. )
  33. ),
  34. array(
  35. 'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo = :foo',
  36. array('foo'=>1,'bar'=> array(1, 2, 3)),
  37. array('bar'=> Connection::PARAM_INT_ARRAY,'foo'=>PDO::PARAM_INT),
  38. array(
  39. array('id'=>1,'foo'=>1,'bar'=>1),
  40. array('id'=>2,'foo'=>1,'bar'=>2),
  41. array('id'=>3,'foo'=>1,'bar'=>3),
  42. )
  43. ),
  44. array(
  45. 'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo = :foo',
  46. array('foo'=>1,'bar'=> array('1', '2', '3')),
  47. array('bar'=> Connection::PARAM_STR_ARRAY,'foo'=>PDO::PARAM_INT),
  48. array(
  49. array('id'=>1,'foo'=>1,'bar'=>1),
  50. array('id'=>2,'foo'=>1,'bar'=>2),
  51. array('id'=>3,'foo'=>1,'bar'=>3),
  52. )
  53. ),
  54. array(
  55. 'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo IN (:foo)',
  56. array('foo'=>array('1'),'bar'=> array(1, 2, 3,4)),
  57. array('bar'=> Connection::PARAM_STR_ARRAY,'foo'=>Connection::PARAM_INT_ARRAY),
  58. array(
  59. array('id'=>1,'foo'=>1,'bar'=>1),
  60. array('id'=>2,'foo'=>1,'bar'=>2),
  61. array('id'=>3,'foo'=>1,'bar'=>3),
  62. array('id'=>4,'foo'=>1,'bar'=>4),
  63. )
  64. ),
  65. array(
  66. 'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo IN (:foo)',
  67. array('foo'=>1,'bar'=> 2),
  68. array('bar'=>PDO::PARAM_INT,'foo'=>PDO::PARAM_INT),
  69. array(
  70. array('id'=>2,'foo'=>1,'bar'=>2),
  71. )
  72. ),
  73. array(
  74. 'SELECT * FROM ddc1372_foobar f WHERE f.bar = :arg AND f.foo <> :arg',
  75. array('arg'=>'1'),
  76. array('arg'=>PDO::PARAM_STR),
  77. array(
  78. array('id'=>5,'foo'=>2,'bar'=>1),
  79. )
  80. ),
  81. array(
  82. 'SELECT * FROM ddc1372_foobar f WHERE f.bar NOT IN (:arg) AND f.foo IN (:arg)',
  83. array('arg'=>array(1, 2)),
  84. array('arg'=>Connection::PARAM_INT_ARRAY),
  85. array(
  86. array('id'=>3,'foo'=>1,'bar'=>3),
  87. array('id'=>4,'foo'=>1,'bar'=>4),
  88. )
  89. ),
  90. );
  91. }
  92. public function setUp()
  93. {
  94. parent::setUp();
  95. if (!$this->_conn->getSchemaManager()->tablesExist("ddc1372_foobar")) {
  96. try {
  97. $table = new \Doctrine\DBAL\Schema\Table("ddc1372_foobar");
  98. $table->addColumn('id', 'integer');
  99. $table->addColumn('foo','string');
  100. $table->addColumn('bar','string');
  101. $table->setPrimaryKey(array('id'));
  102. $sm = $this->_conn->getSchemaManager();
  103. $sm->createTable($table);
  104. $this->_conn->insert('ddc1372_foobar', array(
  105. 'id' => 1, 'foo' => 1, 'bar' => 1
  106. ));
  107. $this->_conn->insert('ddc1372_foobar', array(
  108. 'id' => 2, 'foo' => 1, 'bar' => 2
  109. ));
  110. $this->_conn->insert('ddc1372_foobar', array(
  111. 'id' => 3, 'foo' => 1, 'bar' => 3
  112. ));
  113. $this->_conn->insert('ddc1372_foobar', array(
  114. 'id' => 4, 'foo' => 1, 'bar' => 4
  115. ));
  116. $this->_conn->insert('ddc1372_foobar', array(
  117. 'id' => 5, 'foo' => 2, 'bar' => 1
  118. ));
  119. $this->_conn->insert('ddc1372_foobar', array(
  120. 'id' => 6, 'foo' => 2, 'bar' => 2
  121. ));
  122. } catch(\Exception $e) {
  123. $this->fail($e->getMessage());
  124. }
  125. }
  126. }
  127. /**
  128. * @dataProvider ticketProvider
  129. * @param string $query
  130. * @param array $params
  131. * @param array $types
  132. * @param array $expected
  133. */
  134. public function testTicket($query,$params,$types,$expected)
  135. {
  136. $stmt = $this->_conn->executeQuery($query, $params, $types);
  137. $result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  138. foreach ($result as $k => $v) {
  139. $result[$k] = array_change_key_case($v, CASE_LOWER);
  140. }
  141. $this->assertEquals($result, $expected);
  142. }
  143. }