Completed
Push — 3.0.x ( 616291...8be6d1 )
by Sergei
24s queued 16s
created

StatementTest::testPDOCustomClassConstructorArgs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\DBAL\Tests;
4
5
use Doctrine\DBAL\Configuration;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\Driver;
9
use Doctrine\DBAL\Driver\Connection as DriverConnection;
10
use Doctrine\DBAL\Driver\Statement as DriverStatement;
11
use Doctrine\DBAL\FetchMode;
12
use Doctrine\DBAL\Logging\SQLLogger;
13
use Doctrine\DBAL\ParameterType;
14
use Doctrine\DBAL\Statement;
15
use Exception;
16
use PDOStatement;
17
use PHPUnit\Framework\TestCase;
18
19
class StatementTest extends TestCase
20
{
21
    /** @var Connection */
22
    private $conn;
23
24
    /** @var Configuration */
25
    private $configuration;
26
27
    /** @var PDOStatement */
28
    private $driverStatement;
29
30
    protected function setUp() : void
31
    {
32
        $this->driverStatement = $this->createMock(DriverStatement::class);
33
34
        $driverConnection = $this->createConfiguredMock(DriverConnection::class, [
35
            'prepare' => $this->driverStatement,
36
        ]);
37
38
        $driver = $this->createMock(Driver::class);
39
40
        $this->conn = $this->getMockBuilder(Connection::class)
41
            ->setConstructorArgs([[], $driver])
42
            ->getMock();
43
        $this->conn->expects($this->atLeastOnce())
44
                ->method('getWrappedConnection')
45
                ->will($this->returnValue($driverConnection));
46
47
        $this->configuration = $this->createMock(Configuration::class);
48
        $this->conn->expects($this->any())
49
                ->method('getConfiguration')
50
                ->will($this->returnValue($this->configuration));
51
52
        $this->conn->expects($this->any())
53
            ->method('getDriver')
54
            ->will($this->returnValue($driver));
55
    }
56
57
    public function testExecuteCallsLoggerStartQueryWithParametersWhenValuesBound() : void
58
    {
59
        $name   = 'foo';
60
        $var    = 'bar';
61
        $type   = ParameterType::STRING;
62
        $values = [$name => $var];
63
        $types  = [$name => $type];
64
        $sql    = '';
65
66
        $logger = $this->createMock(SQLLogger::class);
67
        $logger->expects($this->once())
68
                ->method('startQuery')
69
                ->with($this->equalTo($sql), $this->equalTo($values), $this->equalTo($types));
70
71
        $this->configuration->expects($this->once())
72
                ->method('getSQLLogger')
73
                ->will($this->returnValue($logger));
74
75
        $statement = new Statement($sql, $this->conn);
76
        $statement->bindValue($name, $var, $type);
77
        $statement->execute();
78
    }
79
80
    public function testExecuteCallsLoggerStartQueryWithParametersWhenParamsPassedToExecute() : void
81
    {
82
        $name   = 'foo';
83
        $var    = 'bar';
84
        $values = [$name => $var];
85
        $types  = [];
86
        $sql    = '';
87
88
        $logger = $this->createMock(SQLLogger::class);
89
        $logger->expects($this->once())
90
                ->method('startQuery')
91
                ->with($this->equalTo($sql), $this->equalTo($values), $this->equalTo($types));
92
93
        $this->configuration->expects($this->once())
94
                ->method('getSQLLogger')
95
                ->will($this->returnValue($logger));
96
97
        $statement = new Statement($sql, $this->conn);
98
        $statement->execute($values);
99
    }
100
101
    public function testExecuteCallsStartQueryWithTheParametersBoundViaBindParam() : void
102
    {
103
        $name   = 'foo';
104
        $var    = 'bar';
105
        $values = [$name => $var];
106
        $types  = [$name => ParameterType::STRING];
107
        $sql    = '';
108
109
        $logger = $this->createMock(SQLLogger::class);
110
        $logger->expects(self::once())
111
                ->method('startQuery')
112
                ->with(self::equalTo($sql), self::equalTo($values), self::equalTo($types));
113
114
        $this->configuration->expects(self::once())
115
                ->method('getSQLLogger')
116
                ->willReturn($logger);
117
118
        $statement = new Statement($sql, $this->conn);
119
        $statement->bindParam($name, $var);
120
        $statement->execute();
121
    }
122
123
    public function testExecuteCallsLoggerStopQueryOnException() : void
124
    {
125
        $logger = $this->createMock(SQLLogger::class);
126
127
        $this->configuration->expects($this->once())
128
            ->method('getSQLLogger')
129
            ->will($this->returnValue($logger));
130
131
        // Needed to satisfy construction of DBALException
132
        $this->conn->expects($this->any())
133
            ->method('resolveParams')
134
            ->will($this->returnValue([]));
135
136
        $logger->expects($this->once())
137
            ->method('startQuery');
138
139
        $logger->expects($this->once())
140
            ->method('stopQuery');
141
142
        $this->driverStatement->expects($this->once())
143
            ->method('execute')
144
            ->will($this->throwException(new Exception('Mock test exception')));
145
146
        $statement = new Statement('', $this->conn);
147
148
        $this->expectException(DBALException::class);
149
150
        $statement->execute();
151
    }
152
153
    public function testPDOCustomClassConstructorArgs() : void
154
    {
155
        $statement = new Statement('', $this->conn);
156
157
        $this->driverStatement->expects($this->once())
158
            ->method('fetchAll')
159
            ->with(self::equalTo(FetchMode::CUSTOM_OBJECT), self::equalTo('Example'), self::equalTo(['arg1']));
160
161
        $statement->fetchAll(FetchMode::CUSTOM_OBJECT, 'Example', ['arg1']);
162
    }
163
}
164