Completed
Push — master ( c7757e...39cb21 )
by Luís
16s
created

Tests/DBAL/Functional/NamedParametersTest.php (1 issue)

1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional\Ticket;
4
5
use Doctrine\DBAL\Connection;
6
use PDO;
7
8
/**
9
 * @group DDC-1372
10
 */
11
class NamedParametersTest extends \Doctrine\Tests\DbalFunctionalTestCase
12
{
13
14
    public function ticketProvider()
15
    {
16
        return array(
17
            array(
18
                'SELECT * FROM ddc1372_foobar f WHERE f.foo = :foo AND f.bar IN (:bar)',
19
                array('foo'=>1,'bar'=> array(1, 2, 3)),
20
                array('foo'=>PDO::PARAM_INT,'bar'=> Connection::PARAM_INT_ARRAY,),
21
                array(
22
                    array('id'=>1,'foo'=>1,'bar'=>1),
23
                    array('id'=>2,'foo'=>1,'bar'=>2),
24
                    array('id'=>3,'foo'=>1,'bar'=>3),
25
                )
26
            ),
27
28
            array(
29
                'SELECT * FROM ddc1372_foobar f WHERE f.foo = :foo AND f.bar IN (:bar)',
30
                array('foo'=>1,'bar'=> array(1, 2, 3)),
31
                array('bar'=> Connection::PARAM_INT_ARRAY,'foo'=>PDO::PARAM_INT),
32
                array(
33
                    array('id'=>1,'foo'=>1,'bar'=>1),
34
                    array('id'=>2,'foo'=>1,'bar'=>2),
35
                    array('id'=>3,'foo'=>1,'bar'=>3),
36
                )
37
            ),
38
39
            array(
40
                'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo = :foo',
41
                array('foo'=>1,'bar'=> array(1, 2, 3)),
42
                array('bar'=> Connection::PARAM_INT_ARRAY,'foo'=>PDO::PARAM_INT),
43
                array(
44
                    array('id'=>1,'foo'=>1,'bar'=>1),
45
                    array('id'=>2,'foo'=>1,'bar'=>2),
46
                    array('id'=>3,'foo'=>1,'bar'=>3),
47
                )
48
            ),
49
50
            array(
51
                'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo = :foo',
52
                array('foo'=>1,'bar'=> array('1', '2', '3')),
53
                array('bar'=> Connection::PARAM_STR_ARRAY,'foo'=>PDO::PARAM_INT),
54
                array(
55
                    array('id'=>1,'foo'=>1,'bar'=>1),
56
                    array('id'=>2,'foo'=>1,'bar'=>2),
57
                    array('id'=>3,'foo'=>1,'bar'=>3),
58
                )
59
            ),
60
61
            array(
62
                'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo IN (:foo)',
63
                array('foo'=>array('1'),'bar'=> array(1, 2, 3,4)),
64
                array('bar'=> Connection::PARAM_STR_ARRAY,'foo'=>Connection::PARAM_INT_ARRAY),
65
                array(
66
                    array('id'=>1,'foo'=>1,'bar'=>1),
67
                    array('id'=>2,'foo'=>1,'bar'=>2),
68
                    array('id'=>3,'foo'=>1,'bar'=>3),
69
                    array('id'=>4,'foo'=>1,'bar'=>4),
70
                )
71
            ),
72
73
            array(
74
                'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo IN (:foo)',
75
                array('foo'=>1,'bar'=> 2),
76
                array('bar'=>PDO::PARAM_INT,'foo'=>PDO::PARAM_INT),
77
                array(
78
                    array('id'=>2,'foo'=>1,'bar'=>2),
79
                )
80
            ),
81
82
            array(
83
                'SELECT * FROM ddc1372_foobar f WHERE f.bar = :arg AND f.foo <> :arg',
84
                array('arg'=>'1'),
85
                array('arg'=>PDO::PARAM_STR),
86
                array(
87
                    array('id'=>5,'foo'=>2,'bar'=>1),
88
                )
89
            ),
90
91
            array(
92
                'SELECT * FROM ddc1372_foobar f WHERE f.bar NOT IN (:arg) AND f.foo IN (:arg)',
93
                array('arg'=>array(1, 2)),
94
                array('arg'=>Connection::PARAM_INT_ARRAY),
95
                array(
96
                    array('id'=>3,'foo'=>1,'bar'=>3),
97
                    array('id'=>4,'foo'=>1,'bar'=>4),
98
                )
99
            ),
100
101
        );
102
    }
103
104
    protected function setUp()
105
    {
106
        parent::setUp();
107
108
        if (!$this->_conn->getSchemaManager()->tablesExist("ddc1372_foobar")) {
0 ignored issues
show
'ddc1372_foobar' of type string is incompatible with the type array expected by parameter $tableNames of Doctrine\DBAL\Schema\Abs...aManager::tablesExist(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
        if (!$this->_conn->getSchemaManager()->tablesExist(/** @scrutinizer ignore-type */ "ddc1372_foobar")) {
Loading history...
109
            try {
110
                $table = new \Doctrine\DBAL\Schema\Table("ddc1372_foobar");
111
                $table->addColumn('id', 'integer');
112
                $table->addColumn('foo','string');
113
                $table->addColumn('bar','string');
114
                $table->setPrimaryKey(array('id'));
115
116
117
                $sm = $this->_conn->getSchemaManager();
118
                $sm->createTable($table);
119
120
                $this->_conn->insert('ddc1372_foobar', array(
121
                        'id'    => 1, 'foo'   => 1,  'bar'   => 1
122
                ));
123
                $this->_conn->insert('ddc1372_foobar', array(
124
                        'id'    => 2, 'foo'   => 1,  'bar'   => 2
125
                ));
126
                $this->_conn->insert('ddc1372_foobar', array(
127
                        'id'    => 3, 'foo'   => 1,  'bar'   => 3
128
                ));
129
                $this->_conn->insert('ddc1372_foobar', array(
130
                        'id'    => 4, 'foo'   => 1,  'bar'   => 4
131
                ));
132
                $this->_conn->insert('ddc1372_foobar', array(
133
                        'id'    => 5, 'foo'   => 2,  'bar'   => 1
134
                ));
135
                $this->_conn->insert('ddc1372_foobar', array(
136
                        'id'    => 6, 'foo'   => 2,  'bar'   => 2
137
                ));
138
            } catch(\Exception $e) {
139
                $this->fail($e->getMessage());
140
            }
141
        }
142
    }
143
144
    /**
145
     * @dataProvider ticketProvider
146
     * @param string $query
147
     * @param array $params
148
     * @param array $types
149
     * @param array $expected
150
     */
151
    public function testTicket($query,$params,$types,$expected)
152
    {
153
        $stmt   = $this->_conn->executeQuery($query, $params, $types);
154
        $result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
155
156
        foreach ($result as $k => $v) {
157
            $result[$k] = array_change_key_case($v, CASE_LOWER);
158
        }
159
160
        self::assertEquals($result, $expected);
161
    }
162
163
}
164