Completed
Push — master ( e51e73...fb0ce7 )
by Michael
17s queued 12s
created

StatementTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 26
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL;
4
5
use Doctrine\DBAL\ParameterType;
6
use Doctrine\DBAL\Statement;
7
use Doctrine\DBAL\Logging\SQLLogger;
8
9
class StatementTest extends \Doctrine\Tests\DbalTestCase
10
{
11
    /**
12
     *
13
     * @var \Doctrine\DBAL\Connection
14
     */
15
    private $conn;
16
17
    /**
18
     *
19
     * @var \Doctrine\DBAL\Configuration
20
     */
21
    private $configuration;
22
23
    /**
24
     * @var \PDOStatement
25
     */
26
    private $pdoStatement;
27
28
    protected function setUp()
29
    {
30
        $this->pdoStatement = $this->getMockBuilder('\PDOStatement')
1 ignored issue
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('\...bindValue'))->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type PDOStatement of property $pdoStatement.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31
            ->setMethods(array('execute', 'bindParam', 'bindValue'))
32
            ->getMock();
33
        $platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform();
34
        $driverConnection = $this->createMock('\Doctrine\DBAL\Driver\Connection');
35
        $driverConnection->expects($this->any())
36
                ->method('prepare')
37
                ->will($this->returnValue($this->pdoStatement));
38
39
        $driver = $this->createMock('\Doctrine\DBAL\Driver');
40
        $constructorArgs = array(
41
            array(
42
                'platform' => $platform
43
            ),
44
            $driver
45
        );
46
        $this->conn = $this->getMockBuilder('\Doctrine\DBAL\Connection')
1 ignored issue
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('\...tructorArgs)->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Doctrine\DBAL\Connection of property $conn.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
            ->setConstructorArgs($constructorArgs)
48
            ->getMock();
49
        $this->conn->expects($this->atLeastOnce())
50
                ->method('getWrappedConnection')
51
                ->will($this->returnValue($driverConnection));
52
53
        $this->configuration = $this->createMock('\Doctrine\DBAL\Configuration');
1 ignored issue
show
Documentation Bug introduced by
It seems like $this->createMock('\Doctrine\DBAL\Configuration') of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Doctrine\DBAL\Configuration of property $configuration.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
        $this->conn->expects($this->any())
55
                ->method('getConfiguration')
56
                ->will($this->returnValue($this->configuration));
57
58
        $this->conn->expects($this->any())
59
            ->method('getDriver')
60
            ->will($this->returnValue($driver));
61
62
    }
63
64
    public function testExecuteCallsLoggerStartQueryWithParametersWhenValuesBound()
65
    {
66
        $name   = 'foo';
67
        $var    = 'bar';
68
        $type   = ParameterType::STRING;
69
        $values = [$name => $var];
70
        $types  = [$name => $type];
71
        $sql    = '';
72
73
        $logger = $this->createMock('\Doctrine\DBAL\Logging\SQLLogger');
74
        $logger->expects($this->once())
75
                ->method('startQuery')
76
                ->with($this->equalTo($sql), $this->equalTo($values), $this->equalTo($types));
77
78
        $this->configuration->expects($this->once())
79
                ->method('getSQLLogger')
80
                ->will($this->returnValue($logger));
81
82
        $statement = new Statement($sql, $this->conn);
83
        $statement->bindValue($name, $var, $type);
84
        $statement->execute();
85
    }
86
87
    public function testExecuteCallsLoggerStartQueryWithParametersWhenParamsPassedToExecute()
88
    {
89
        $name = 'foo';
90
        $var = 'bar';
91
        $values = array($name => $var);
92
        $types = array();
93
        $sql = '';
94
95
        $logger = $this->createMock('\Doctrine\DBAL\Logging\SQLLogger');
96
        $logger->expects($this->once())
97
                ->method('startQuery')
98
                ->with($this->equalTo($sql), $this->equalTo($values), $this->equalTo($types));
99
100
        $this->configuration->expects($this->once())
101
                ->method('getSQLLogger')
102
                ->will($this->returnValue($logger));
103
104
        $statement = new Statement($sql, $this->conn);
105
        $statement->execute($values);
106
    }
107
108
    public function testExecuteCallsStartQueryWithTheParametersBoundViaBindParam()
109
    {
110
        $name   = 'foo';
111
        $var    = 'bar';
112
        $values = [$name => $var];
113
        $types  = [$name => ParameterType::STRING];
114
        $sql    = '';
115
116
        $logger = $this->createMock(SQLLogger::class);
117
        $logger->expects(self::once())
118
                ->method('startQuery')
119
                ->with(self::equalTo($sql), self::equalTo($values), self::equalTo($types));
120
121
        $this->configuration->expects(self::once())
122
                ->method('getSQLLogger')
123
                ->willReturn($logger);
124
125
        $statement = new Statement($sql, $this->conn);
126
        $statement->bindParam($name, $var);
127
        $statement->execute();
128
    }
129
130
    /**
131
     * @expectedException \Doctrine\DBAL\DBALException
132
     */
133
    public function testExecuteCallsLoggerStopQueryOnException()
134
    {
135
        $logger = $this->createMock('\Doctrine\DBAL\Logging\SQLLogger');
136
137
        $this->configuration->expects($this->once())
138
            ->method('getSQLLogger')
139
            ->will($this->returnValue($logger));
140
141
        // Needed to satisfy construction of DBALException
142
        $this->conn->expects($this->any())
143
            ->method('resolveParams')
144
            ->will($this->returnValue(array()));
145
146
        $logger->expects($this->once())
147
            ->method('startQuery');
148
149
        $logger->expects($this->once())
150
            ->method('stopQuery');
151
152
        $this->pdoStatement->expects($this->once())
153
            ->method('execute')
154
            ->will($this->throwException(new \Exception("Mock test exception")));
155
156
        $statement = new Statement("", $this->conn);
157
        $statement->execute();
158
    }
159
}
160