PdoClientUnitTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 198
c 0
b 0
f 0
wmc 7
lcom 1
cbo 6
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpClient() 0 6 1
A executeQuery() 0 20 1
A executeQuery_ShouldThrowException() 0 16 1
B executeCommand() 0 40 1
A executeCommand_ShouldThrowExceptionIfCantBind() 0 19 1
A executeCommand_ShouldThrowExceptionIfCantExecute() 0 22 1
A executeCommand_ShouldThrowExceptionIfCantResolvePdoType() 0 15 1
1
<?php
2
3
namespace Hgraca\MicroOrm\Test\Repository\Client;
4
5
use Hgraca\MicroOrm\DataSource\Pdo\PdoClient;
6
use Hgraca\MicroOrm\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
46
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(true);
47
        $pdoStatementMock->shouldReceive('fetchAll')->once()->with(PDO::FETCH_ASSOC)->andReturn(
48
            $expectedResult = [
49
                ['propA' => 1, 'propB' => 2],
50
                ['propA' => 3, 'propB' => 4],
51
            ]
52
        );
53
54
        self::assertEquals($expectedResult, $this->client->executeQuery($sql, $parameterList));
0 ignored issues
show
Unused Code introduced by
The call to PdoClient::executeQuery() has too many arguments starting with $parameterList.

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...
55
    }
56
57
    /**
58
     * @test
59
     *
60
     * @small
61
     *
62
     * @expectedException \Hgraca\MicroOrm\DataSource\Exception\ExecutionException
63
     */
64
    public function executeQuery_ShouldThrowException()
65
    {
66
        $sql = 'some dummy sql';
67
        $parameterList = [
68
            'trueVal' => true,
69
        ];
70
71
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
72
        $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...
73
74
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(false);
75
        $pdoStatementMock->shouldReceive('errorCode')->once()->andReturn('123');
76
        $pdoStatementMock->shouldReceive('errorInfo')->once()->andReturn(['some error info']);
77
78
        $this->client->executeQuery($sql, $parameterList);
0 ignored issues
show
Unused Code introduced by
The call to PdoClient::executeQuery() has too many arguments starting with $parameterList.

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...
79
    }
80
81
    /**
82
     * @test
83
     *
84
     * @small
85
     */
86
    public function executeCommand()
87
    {
88
        $sql = 'some dummy sql';
89
        $parameterList = [
90
            'trueVal' => true,
91
            'stringVal' => 'some string',
92
            'intVal' => 1,
93
            'floatVal' => 1.5,
94
            'nullVal' => null,
95
        ];
96
97
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
98
        $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...
99
        $this->pdo->shouldReceive('beginTransaction')->once();
100
        $this->pdo->shouldReceive('commit')->once();
101
102
        $pdoStatementMock->shouldReceive('bindValue')
103
            ->once()
104
            ->with(':trueVal', $parameterList['trueVal'], PDO::PARAM_BOOL)
105
            ->andReturn(true);
106
        $pdoStatementMock->shouldReceive('bindValue')
107
            ->once()
108
            ->with(':stringVal', $parameterList['stringVal'], PDO::PARAM_STR)
109
            ->andReturn(true);
110
        $pdoStatementMock->shouldReceive('bindValue')
111
            ->once()
112
            ->with(':intVal', $parameterList['intVal'], PDO::PARAM_INT)
113
            ->andReturn(true);
114
        $pdoStatementMock->shouldReceive('bindValue')
115
            ->once()
116
            ->with(':floatVal', strval($parameterList['floatVal']), PDO::PARAM_STR)
117
            ->andReturn(true);
118
        $pdoStatementMock->shouldReceive('bindValue')
119
            ->once()
120
            ->with(':nullVal', $parameterList['nullVal'], PDO::PARAM_NULL)
121
            ->andReturn(true);
122
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(true);
123
124
        $this->client->executeCommand($sql, $parameterList);
125
    }
126
127
    /**
128
     * @test
129
     *
130
     * @small
131
     *
132
     * @expectedException \Hgraca\MicroOrm\DataSource\Exception\BindingException
133
     */
134
    public function executeCommand_ShouldThrowExceptionIfCantBind()
135
    {
136
        $sql = 'some dummy sql';
137
        $filterList = [
138
            'trueVal' => true,
139
        ];
140
141
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
142
        $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...
143
        $this->pdo->shouldReceive('beginTransaction')->once();
144
        $this->pdo->shouldReceive('rollBack')->once();
145
146
        $pdoStatementMock->shouldReceive('bindValue')
147
            ->once()
148
            ->with(':trueVal', $filterList['trueVal'], PDO::PARAM_BOOL)
149
            ->andReturn(false);
150
151
        $this->client->executeCommand($sql, $filterList);
152
    }
153
154
    /**
155
     * @test
156
     *
157
     * @small
158
     *
159
     * @expectedException \Hgraca\MicroOrm\DataSource\Exception\ExecutionException
160
     *
161
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::__construct
162
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::executeCommand
163
     * @covers \Hgraca\MicroOrm\DataSource\Pdo\PdoClient::execute
164
     */
165
    public function executeCommand_ShouldThrowExceptionIfCantExecute()
166
    {
167
        $sql = 'some dummy sql';
168
        $parameterList = [
169
            'trueVal' => true,
170
        ];
171
172
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
173
        $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...
174
        $this->pdo->shouldReceive('beginTransaction')->once();
175
        $this->pdo->shouldReceive('rollBack')->once();
176
177
        $pdoStatementMock->shouldReceive('bindValue')
178
            ->once()
179
            ->with(':trueVal', $parameterList['trueVal'], PDO::PARAM_BOOL)
180
            ->andReturn(true);
181
        $pdoStatementMock->shouldReceive('execute')->once()->andReturn(false);
182
        $pdoStatementMock->shouldReceive('errorCode')->once()->andReturn('123');
183
        $pdoStatementMock->shouldReceive('errorInfo')->once()->andReturn(['some error info']);
184
185
        $this->client->executeCommand($sql, $parameterList);
186
    }
187
188
    /**
189
     * @test
190
     *
191
     * @small
192
     *
193
     * @expectedException \Hgraca\MicroOrm\DataSource\Exception\TypeResolutionException
194
     */
195
    public function executeCommand_ShouldThrowExceptionIfCantResolvePdoType()
196
    {
197
        $sql = 'some dummy sql';
198
        $parameterList = [
199
            'trueVal' => new Foo(),
200
        ];
201
202
        $pdoStatementMock = Mockery::mock(PDOStatement::class);
203
        $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...
204
        $this->pdo->shouldReceive('beginTransaction')->once();
205
        $this->pdo->shouldReceive('rollBack')->once();
206
        $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...
207
208
        $this->client->executeCommand($sql, $parameterList);
209
    }
210
}
211