Passed
Branch master (754829)
by Herberto
03:42 queued 01:55
created

PdoClientTest::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 17

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
dl 23
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace Hgraca\MicroOrm\Test\Repository\Client;
4
5
use Hgraca\Helper\InstanceHelper;
6
use Hgraca\MicroOrm\DataSource\Pdo\PdoClient;
7
use Hgraca\MicroOrm\Test\Stub\Bar;
8
use Hgraca\MicroOrm\Test\Stub\Foo;
9
use Mockery;
10
use Mockery\MockInterface;
11
use PDO;
12
use PDOStatement;
13
use PHPUnit_Framework_TestCase;
14
15
final class PdoClientTest extends PHPUnit_Framework_TestCase
16
{
17
    /** @var MockInterface|PDO */
18
    private $pdo;
19
20
    /** @var PdoClient */
21
    private $client;
22
23
    /**
24
     * @before
25
     */
26
    protected function setUpClient()
27
    {
28
        $this->pdo    = Mockery::mock(PDO::class);
29
        $this->client = new PdoClient($this->pdo);
30
    }
31
32
    /**
33
     * @test
34
     *
35
     * @small
36
     *
37
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::__construct
38
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::select
39
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::createSqlFilter
40
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::createSqlOrderBy
41
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::bindParameterList
42
     */
43
    public function select()
44
    {
45
        $table = 'DummyTable';
46
        $filter = [
47
            'propA' => true,
48
            'propB' => null,
49
        ];
50
        $orderBy = [
51
            'propA' => 'ASC',
52
            'propB' => 'DESC',
53
        ];
54
55
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
56
        $this->pdo->shouldReceive('prepare')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in PDO.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
57
            ->once()
58
            ->with('SELECT * FROM `DummyTable` WHERE `propA`=:propA_filter AND `propB` IS :propB_filter ORDER BY propA ASC, propB DESC')
59
            ->andReturn($pdoStatementMock);
60
61
        $pdoStatementMock->shouldReceive('bindValue')
62
            ->once()->with(':propA_filter', $filter['propA'], PDO::PARAM_BOOL)->andReturn(true);
63
        $pdoStatementMock->shouldReceive('bindValue')
64
            ->once()->with(':propB_filter', $filter['propB'], PDO::PARAM_NULL)->andReturn(true);
65
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(true);
66
        $pdoStatementMock->shouldReceive('fetchAll')->once()->with(PDO::FETCH_ASSOC)->andReturn(
67
            $expectedResult = [
68
                ['propA' => 1, 'propB' => 2],
69
                ['propA' => 3, 'propB' => 4],
70
            ]
71
        );
72
73
        self::assertEquals($expectedResult, $this->client->select($table, $filter, $orderBy));
74
    }
75
76
    /**
77
     * @test
78
     *
79
     * @small
80
     *
81
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::__construct
82
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::select
83
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::createSqlFilter
84
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::createSqlOrderBy
85
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::bindParameterList
86
     */
87
    public function select_WithNoFilter()
88
    {
89
        $table   = 'DummyTable';
90
        $filter  = [];
91
        $orderBy = [
92
            'propA' => 'ASC',
93
            'propB' => 'DESC',
94
        ];
95
96
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
97
        $this->pdo->shouldReceive('prepare')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in PDO.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
98
            ->once()
99
            ->with('SELECT * FROM `DummyTable` ORDER BY propA ASC, propB DESC')
100
            ->andReturn($pdoStatementMock);
101
102
        $pdoStatementMock->shouldNotReceive('bindValue');
0 ignored issues
show
Unused Code introduced by
The call to MockInterface::shouldNotReceive() has too many arguments starting with 'bindValue'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
103
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(true);
104
        $pdoStatementMock->shouldReceive('fetchAll')->once()->with(PDO::FETCH_ASSOC)->andReturn(
105
            $expectedResult = [
106
                ['propA' => 1, 'propB' => 2],
107
                ['propA' => 3, 'propB' => 4],
108
            ]
109
        );
110
111
        self::assertEquals($expectedResult, $this->client->select($table, $filter, $orderBy));
112
    }
113
114
    /**
115
     * @test
116
     *
117
     * @small
118
     *
119
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::__construct
120
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::select
121
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::createSqlFilter
122
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::createSqlOrderBy
123
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::bindParameterList
124
     */
125
    public function select_WithNoOrderBy()
126
    {
127
        $table   = 'DummyTable';
128
        $filter  = [
129
            'propA' => true,
130
            'propB' => null,
131
        ];
132
        $orderBy = [];
133
134
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
135
        $this->pdo->shouldReceive('prepare')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in PDO.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
136
            ->once()
137
            ->with('SELECT * FROM `DummyTable` WHERE `propA`=:propA_filter AND `propB` IS :propB_filter')
138
            ->andReturn($pdoStatementMock);
139
140
        $pdoStatementMock->shouldReceive('bindValue')
141
            ->once()->with(':propA_filter', $filter['propA'], PDO::PARAM_BOOL)->andReturn(true);
142
        $pdoStatementMock->shouldReceive('bindValue')
143
            ->once()->with(':propB_filter', $filter['propB'], PDO::PARAM_NULL)->andReturn(true);
144
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(true);
145
        $pdoStatementMock->shouldReceive('fetchAll')->once()->with(PDO::FETCH_ASSOC)->andReturn(
146
            $expectedResult = [
147
                ['propA' => 1, 'propB' => 2],
148
                ['propA' => 3, 'propB' => 4],
149
            ]
150
        );
151
152
        self::assertEquals($expectedResult, $this->client->select($table, $filter, $orderBy));
153
    }
154
155
    /**
156
     * @test
157
     *
158
     * @small
159
     *
160
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::__construct
161
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::insert
162
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::createInsertSql
163
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::createSqlFilter
164
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::bindParameterList
165
     */
166 View Code Duplication
    public function insert()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
    {
168
        $table  = 'DummyTable';
169
        $data = [
170
            'propA' => true,
171
            'propB' => null,
172
        ];
173
174
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
175
        $this->pdo->shouldReceive('prepare')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in PDO.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
176
            ->once()
177
            ->with('INSERT INTO `DummyTable` (`propA`, `propB`) VALUES (:propA, :propB)')
178
            ->andReturn($pdoStatementMock);
179
180
        $pdoStatementMock->shouldReceive('bindValue')
181
            ->once()->with(':propA', $data['propA'], PDO::PARAM_BOOL)->andReturn(true);
182
        $pdoStatementMock->shouldReceive('bindValue')
183
            ->once()->with(':propB', $data['propB'], PDO::PARAM_NULL)->andReturn(true);
184
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(true);
185
        $pdoStatementMock->shouldReceive('rowCount')->once()->andReturn($rowCount = 1);
186
187
        self::assertEquals($rowCount, $this->client->insert($table, $data));
188
    }
189
190
    /**
191
     * @test
192
     *
193
     * @small
194
     *
195
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::__construct
196
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::update
197
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::createUpdateSql
198
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::createSqlFilter
199
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::bindParameterList
200
     */
201
    public function update()
202
    {
203
        $table  = 'DummyTable';
204
        $data = [
205
            'propC' => true,
206
        ];
207
        $filter = [
208
            'propA' => true,
209
            'propB' => null,
210
        ];
211
212
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
213
        $this->pdo->shouldReceive('prepare')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in PDO.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
214
            ->once()
215
            ->with('UPDATE `DummyTable` SET `propC`=:propC WHERE `propA`=:propA_filter AND `propB` IS :propB_filter')
216
            ->andReturn($pdoStatementMock);
217
218
        $pdoStatementMock->shouldReceive('bindValue')
219
            ->once()->with(':propA_filter', $filter['propA'], PDO::PARAM_BOOL)->andReturn(true);
220
        $pdoStatementMock->shouldReceive('bindValue')
221
            ->once()->with(':propB_filter', $filter['propB'], PDO::PARAM_NULL)->andReturn(true);
222
        $pdoStatementMock->shouldReceive('bindValue')
223
            ->once()->with(':propC', $data['propC'], PDO::PARAM_BOOL)->andReturn(true);
224
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(true);
225
        $pdoStatementMock->shouldReceive('rowCount')->once()->andReturn($rowCount = 1);
226
227
        self::assertEquals($rowCount, $this->client->update($table, $filter, $data));
228
    }
229
230
    /**
231
     * @test
232
     *
233
     * @small
234
     *
235
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::__construct
236
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::delete
237
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::createSqlFilter
238
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::bindParameterList
239
     */
240 View Code Duplication
    public function delete()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
241
    {
242
        $table           = 'DummyTable';
243
        $filter = [
244
            'propA' => true,
245
            'propB' => 3,
246
        ];
247
248
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
249
        $this->pdo->shouldReceive('prepare')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in PDO.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
250
            ->once()
251
            ->with('DELETE FROM `DummyTable` WHERE `propA`=:propA_filter AND `propB`=:propB_filter')
252
            ->andReturn($pdoStatementMock);
253
254
        $pdoStatementMock->shouldReceive('bindValue')
255
            ->once()->with(':propA_filter', $filter['propA'], PDO::PARAM_BOOL)->andReturn(true);
256
        $pdoStatementMock->shouldReceive('bindValue')
257
            ->once()->with(':propB_filter', $filter['propB'], PDO::PARAM_INT)->andReturn(true);
258
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(true);
259
        $pdoStatementMock->shouldReceive('rowCount')->once()->andReturn($rowCount = 1);
260
261
        self::assertEquals($rowCount, $this->client->delete($table, $filter));
262
    }
263
264
    /**
265
     * @test
266
     *
267
     * @small
268
     *
269
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::__construct
270
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::executeQuery
271
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::fetchData
272
     */
273
    public function executeQuery_ReturnsArray()
274
    {
275
        $sql           = 'some dummy sql';
276
        $parameterList = [
277
            'trueVal' => true,
278
        ];
279
280
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
281
        $this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in PDO.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
282
283
        $pdoStatementMock->shouldReceive('bindValue')
284
            ->once()
285
            ->with(':trueVal_filter', $parameterList['trueVal'], PDO::PARAM_BOOL)
286
            ->andReturn(true);
287
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(true);
288
        $pdoStatementMock->shouldReceive('fetchAll')->once()->with(PDO::FETCH_ASSOC)->andReturn(
289
            $expectedResult = [
290
                ['propA' => 1, 'propB' => 2],
291
                ['propA' => 3, 'propB' => 4],
292
            ]
293
        );
294
295
        self::assertEquals($expectedResult, $this->client->executeQuery($sql, $parameterList));
296
    }
297
298
    /**
299
     * @test
300
     *
301
     * @small
302
     *
303
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::__construct
304
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::executeQuery
305
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::fetchData
306
     */
307
    public function executeQuery_ReturnsObjects()
308
    {
309
        $sql           = 'some dummy sql';
310
        $parameterList = [
311
            'trueVal' => true,
312
        ];
313
314
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
315
        $this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in PDO.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
316
317
        $pdoStatementMock->shouldReceive('bindValue')
318
            ->once()
319
            ->with(':trueVal_filter', $parameterList['trueVal'], PDO::PARAM_BOOL)
320
            ->andReturn(true);
321
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(true);
322
        $classFqcn = Bar::class;
323
        $pdoStatementMock->shouldReceive('fetchAll')->once()->with(PDO::FETCH_CLASS, $classFqcn)->andReturn(
324
            $expectedResult = [
325
                $obj1 = new Bar(),
326
                $obj2 = new Bar(),
327
            ]
328
        );
329
        InstanceHelper::setProtectedProperty($obj1, 'propA', 1);
330
        InstanceHelper::setProtectedProperty($obj1, 'propB', 2);
331
        InstanceHelper::setProtectedProperty($obj2, 'propA', 3);
332
        InstanceHelper::setProtectedProperty($obj2, 'propB', 4);
333
334
        self::assertEquals($expectedResult, $this->client->executeQuery($sql, $parameterList, $classFqcn));
335
    }
336
337
    /**
338
     * @test
339
     *
340
     * @small
341
     *
342
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::__construct
343
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::executeCommand
344
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::execute
345
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::resolvePdoType
346
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::bindParameterList
347
     */
348
    public function executeCommand()
349
    {
350
        $sql           = 'some dummy sql';
351
        $parameterList = [
352
            'trueVal'   => true,
353
            'stringVal' => 'some string',
354
            'intVal'    => 1,
355
            'floatVal'  => 1.5,
356
            'nullVal'   => null,
357
        ];
358
359
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
360
        $this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in PDO.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
361
362
        $pdoStatementMock->shouldReceive('bindValue')
363
            ->once()
364
            ->with(':trueVal_filter', $parameterList['trueVal'], PDO::PARAM_BOOL)
365
            ->andReturn(true);
366
        $pdoStatementMock->shouldReceive('bindValue')
367
            ->once()
368
            ->with(':stringVal_filter', $parameterList['stringVal'], PDO::PARAM_STR)
369
            ->andReturn(true);
370
        $pdoStatementMock->shouldReceive('bindValue')
371
            ->once()
372
            ->with(':intVal_filter', $parameterList['intVal'], PDO::PARAM_INT)
373
            ->andReturn(true);
374
        $pdoStatementMock->shouldReceive('bindValue')
375
            ->once()
376
            ->with(':floatVal_filter', strval($parameterList['floatVal']), PDO::PARAM_STR)
377
            ->andReturn(true);
378
        $pdoStatementMock->shouldReceive('bindValue')
379
            ->once()
380
            ->with(':nullVal_filter', $parameterList['nullVal'], PDO::PARAM_NULL)
381
            ->andReturn(true);
382
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(true);
383
        $pdoStatementMock->shouldReceive('rowCount')->once()->andReturn($rowCount = 1);
384
385
        self::assertEquals($rowCount, $this->client->executeCommand($sql, $parameterList));
386
    }
387
388
    /**
389
     * @test
390
     *
391
     * @small
392
     *
393
     * @expectedException \Hgraca\MicroOrm\DataSource\Exception\BindingMicroOrmException
394
     *
395
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::__construct
396
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::executeCommand
397
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::execute
398
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::bindParameterList
399
     */
400
    public function executeCommand_ShouldThrowExceptionIfCantBind()
401
    {
402
        $sql           = 'some dummy sql';
403
        $filterList = [
404
            'trueVal' => true,
405
        ];
406
407
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
408
        $this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in PDO.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
409
410
        $pdoStatementMock->shouldReceive('bindValue')
411
            ->once()
412
            ->with(':trueVal_filter', $filterList['trueVal'], PDO::PARAM_BOOL)
413
            ->andReturn(false);
414
415
        $this->client->executeCommand($sql, $filterList);
416
    }
417
418
    /**
419
     * @test
420
     *
421
     * @small
422
     *
423
     * @expectedException \Hgraca\MicroOrm\DataSource\Exception\ExecutionMicroOrmException
424
     *
425
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::__construct
426
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::executeCommand
427
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::execute
428
     */
429
    public function executeCommand_ShouldThrowExceptionIfCantExecute()
430
    {
431
        $sql           = 'some dummy sql';
432
        $parameterList = [
433
            'trueVal' => true,
434
        ];
435
436
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
437
        $this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in PDO.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
438
439
        $pdoStatementMock->shouldReceive('bindValue')
440
            ->once()
441
            ->with(':trueVal_filter', $parameterList['trueVal'], PDO::PARAM_BOOL)
442
            ->andReturn(true);
443
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(false);
444
        $pdoStatementMock->shouldReceive('errorCode')->once()->andReturn('123');
445
        $pdoStatementMock->shouldReceive('errorInfo')->once()->andReturn(['some error info']);
446
447
        $this->client->executeCommand($sql, $parameterList);
448
    }
449
450
    /**
451
     * @test
452
     *
453
     * @small
454
     *
455
     * @expectedException \Hgraca\MicroOrm\DataSource\Exception\TypeResolutionMicroOrmException
456
     *
457
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::__construct
458
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::executeCommand
459
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::execute
460
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::resolvePdoType
461
     */
462
    public function executeCommand_ShouldThrowExceptionIfCantResolvePdoType()
463
    {
464
        $sql           = 'some dummy sql';
465
        $parameterList = [
466
            'trueVal' => new Foo(),
467
        ];
468
469
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
470
        $this->pdo->shouldReceive('prepare')->once()->with($sql)->andReturn($pdoStatementMock);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in PDO.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
471
        $pdoStatementMock->shouldNotReceive('bindValue');
0 ignored issues
show
Unused Code introduced by
The call to MockInterface::shouldNotReceive() has too many arguments starting with 'bindValue'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
472
473
        $this->client->executeCommand($sql, $parameterList);
474
    }
475
476
    /**
477
     * @test
478
     *
479
     * @small
480
     *
481
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::__construct
482
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::getLastInsertId
483
     */
484
    public function getLastInsertId()
485
    {
486
        $id = '123';
487
        $this->pdo->shouldReceive('lastInsertId')->once()->andReturn($id);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in PDO.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
488
489
        self::assertEquals($id, $this->client->getLastInsertId());
490
    }
491
}
492