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