PdoClientUnitTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 234
Duplicated Lines 17.09 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 40
loc 234
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpClient() 0 6 1
B executeQuery() 0 24 1
A executeQuery_ShouldThrowExceptionIfCantBind() 20 20 1
A executeQuery_ShouldThrowExceptionIfCantExecute() 20 20 1
B executeCommand() 0 40 1
A executeCommand_ShouldThrowExceptionIfCantBind() 0 19 1
A executeCommand_ShouldThrowExceptionIfCantExecute() 0 22 1
A executeCommand_ShouldThrowExceptionIfCantResolvePdoType() 0 15 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Hgraca\MicroDbal\Test\Raw;
4
5
use Hgraca\MicroDbal\Raw\PdoClient;
6
use Hgraca\MicroDbal\Test\Stub\Foo;
7
use Mockery;
8
use Mockery\MockInterface;
9
use PDO;
10
use PDOStatement;
11
use PHPUnit_Framework_TestCase;
12
13
final class PdoClientUnitTest extends PHPUnit_Framework_TestCase
14
{
15
    /** @var MockInterface|PDO */
16
    private $pdo;
17
18
    /** @var PdoClient */
19
    private $client;
20
21
    /**
22
     * @before
23
     */
24
    protected function setUpClient()
25
    {
26
        $this->pdo = Mockery::mock(PDO::class);
27
        $this->pdo->shouldReceive('setAttribute');
28
        $this->client = new PdoClient($this->pdo);
29
    }
30
31
    /**
32
     * @test
33
     *
34
     * @small
35
     */
36
    public function executeQuery()
37
    {
38
        $sql = 'some dummy sql';
39
        $parameterList = [
40
            'trueVal' => true,
41
        ];
42
43
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
44
        $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...
45
        $pdoStatementMock->shouldReceive('bindValue')
46
            ->once()
47
            ->with('trueVal', $parameterList['trueVal'], PDO::PARAM_BOOL)
48
            ->andReturn(true);
49
50
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(true);
51
        $pdoStatementMock->shouldReceive('fetchAll')->once()->with(PDO::FETCH_ASSOC)->andReturn(
52
            $expectedResult = [
53
                ['propA' => 1, 'propB' => 2],
54
                ['propA' => 3, 'propB' => 4],
55
            ]
56
        );
57
58
        self::assertEquals($expectedResult, $this->client->executeQuery($sql, $parameterList));
59
    }
60
61
    /**
62
     * @test
63
     *
64
     * @small
65
     *
66
     * @expectedException \Hgraca\MicroDbal\Raw\Exception\BindingException
67
     */
68 View Code Duplication
    public function executeQuery_ShouldThrowExceptionIfCantBind()
0 ignored issues
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...
69
    {
70
        $sql = 'some dummy sql';
71
        $parameterList = [
72
            'trueVal' => true,
73
        ];
74
75
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
76
        $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...
77
        $pdoStatementMock->shouldReceive('bindValue')
78
            ->once()
79
            ->with('trueVal', $parameterList['trueVal'], PDO::PARAM_BOOL)
80
            ->andReturn(false);
81
82
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(false);
83
        $pdoStatementMock->shouldReceive('errorCode')->once()->andReturn('123');
84
        $pdoStatementMock->shouldReceive('errorInfo')->once()->andReturn(['some error info']);
85
86
        $this->client->executeQuery($sql, $parameterList);
87
    }
88
89
    /**
90
     * @test
91
     *
92
     * @small
93
     *
94
     * @expectedException \Hgraca\MicroDbal\Raw\Exception\ExecutionException
95
     */
96 View Code Duplication
    public function executeQuery_ShouldThrowExceptionIfCantExecute()
0 ignored issues
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...
97
    {
98
        $sql = 'some dummy sql';
99
        $parameterList = [
100
            'trueVal' => true,
101
        ];
102
103
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
104
        $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...
105
        $pdoStatementMock->shouldReceive('bindValue')
106
            ->once()
107
            ->with('trueVal', $parameterList['trueVal'], PDO::PARAM_BOOL)
108
            ->andReturn(true);
109
110
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(false);
111
        $pdoStatementMock->shouldReceive('errorCode')->once()->andReturn('123');
112
        $pdoStatementMock->shouldReceive('errorInfo')->once()->andReturn(['some error info']);
113
114
        $this->client->executeQuery($sql, $parameterList);
115
    }
116
117
    /**
118
     * @test
119
     *
120
     * @small
121
     */
122
    public function executeCommand()
123
    {
124
        $sql = 'some dummy sql';
125
        $parameterList = [
126
            'trueVal' => true,
127
            'stringVal' => 'some string',
128
            'intVal' => 1,
129
            'floatVal' => 1.5,
130
            'nullVal' => null,
131
        ];
132
133
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
134
        $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...
135
        $this->pdo->shouldReceive('beginTransaction')->once();
136
        $this->pdo->shouldReceive('commit')->once();
137
138
        $pdoStatementMock->shouldReceive('bindValue')
139
            ->once()
140
            ->with('trueVal', $parameterList['trueVal'], PDO::PARAM_BOOL)
141
            ->andReturn(true);
142
        $pdoStatementMock->shouldReceive('bindValue')
143
            ->once()
144
            ->with('stringVal', $parameterList['stringVal'], PDO::PARAM_STR)
145
            ->andReturn(true);
146
        $pdoStatementMock->shouldReceive('bindValue')
147
            ->once()
148
            ->with('intVal', $parameterList['intVal'], PDO::PARAM_INT)
149
            ->andReturn(true);
150
        $pdoStatementMock->shouldReceive('bindValue')
151
            ->once()
152
            ->with('floatVal', strval($parameterList['floatVal']), PDO::PARAM_STR)
153
            ->andReturn(true);
154
        $pdoStatementMock->shouldReceive('bindValue')
155
            ->once()
156
            ->with('nullVal', $parameterList['nullVal'], PDO::PARAM_NULL)
157
            ->andReturn(true);
158
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(true);
159
160
        $this->client->executeCommand($sql, $parameterList);
161
    }
162
163
    /**
164
     * @test
165
     *
166
     * @small
167
     *
168
     * @expectedException \Hgraca\MicroDbal\Raw\Exception\BindingException
169
     */
170
    public function executeCommand_ShouldThrowExceptionIfCantBind()
171
    {
172
        $sql = 'some dummy sql';
173
        $filterList = [
174
            'trueVal' => true,
175
        ];
176
177
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
178
        $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...
179
        $this->pdo->shouldReceive('beginTransaction')->once();
180
        $this->pdo->shouldReceive('rollBack')->once();
181
182
        $pdoStatementMock->shouldReceive('bindValue')
183
            ->once()
184
            ->with('trueVal', $filterList['trueVal'], PDO::PARAM_BOOL)
185
            ->andReturn(false);
186
187
        $this->client->executeCommand($sql, $filterList);
188
    }
189
190
    /**
191
     * @test
192
     *
193
     * @small
194
     *
195
     * @expectedException \Hgraca\MicroDbal\Raw\Exception\ExecutionException
196
     *
197
     * @covers \Hgraca\MicroDbal\Raw\PdoClient::__construct
198
     * @covers \Hgraca\MicroDbal\Raw\PdoClient::executeCommand
199
     * @covers \Hgraca\MicroDbal\Raw\PdoClient::execute
200
     */
201
    public function executeCommand_ShouldThrowExceptionIfCantExecute()
202
    {
203
        $sql = 'some dummy sql';
204
        $parameterList = [
205
            'trueVal' => true,
206
        ];
207
208
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
209
        $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...
210
        $this->pdo->shouldReceive('beginTransaction')->once();
211
        $this->pdo->shouldReceive('rollBack')->once();
212
213
        $pdoStatementMock->shouldReceive('bindValue')
214
            ->once()
215
            ->with('trueVal', $parameterList['trueVal'], PDO::PARAM_BOOL)
216
            ->andReturn(true);
217
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(false);
218
        $pdoStatementMock->shouldReceive('errorCode')->once()->andReturn('123');
219
        $pdoStatementMock->shouldReceive('errorInfo')->once()->andReturn(['some error info']);
220
221
        $this->client->executeCommand($sql, $parameterList);
222
    }
223
224
    /**
225
     * @test
226
     *
227
     * @small
228
     *
229
     * @expectedException \Hgraca\MicroDbal\Raw\Exception\TypeResolutionException
230
     */
231
    public function executeCommand_ShouldThrowExceptionIfCantResolvePdoType()
232
    {
233
        $sql = 'some dummy sql';
234
        $parameterList = [
235
            'trueVal' => new Foo(),
236
        ];
237
238
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
239
        $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...
240
        $this->pdo->shouldReceive('beginTransaction')->once();
241
        $this->pdo->shouldReceive('rollBack')->once();
242
        $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...
243
244
        $this->client->executeCommand($sql, $parameterList);
245
    }
246
}
247