Passed
Push — master ( 61343d...c25359 )
by Herberto
04:28
created

PdoClientUnitTest   A

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