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

tests/Doctrine/Tests/DBAL/StatementTest.php (3 issues)

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')
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')
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');
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())
1 ignored issue
show
The method expects() does not exist on Doctrine\DBAL\Configuration. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        $this->configuration->/** @scrutinizer ignore-call */ 
79
                              expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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())
1 ignored issue
show
The method expects() does not exist on Doctrine\DBAL\Connection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

142
        $this->conn->/** @scrutinizer ignore-call */ 
143
                     expects($this->any())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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())
1 ignored issue
show
The method expects() does not exist on PDOStatement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
        $this->pdoStatement->/** @scrutinizer ignore-call */ 
153
                             expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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