Completed
Push — master ( c7757e...39cb21 )
by Luís
16s
created

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

1
<?php
2
3
namespace Doctrine\Tests\DBAL;
4
5
use Doctrine\DBAL\Statement;
6
use Doctrine\DBAL\Logging\SQLLogger;
7
8
class StatementTest extends \Doctrine\Tests\DbalTestCase
9
{
10
    /**
11
     *
12
     * @var \Doctrine\DBAL\Connection
13
     */
14
    private $conn;
15
16
    /**
17
     *
18
     * @var \Doctrine\DBAL\Configuration
19
     */
20
    private $configuration;
21
22
    /**
23
     * @var \PDOStatement
24
     */
25
    private $pdoStatement;
26
27
    protected function setUp()
28
    {
29
        $this->pdoStatement = $this->getMockBuilder('\PDOStatement')
30
            ->setMethods(array('execute', 'bindParam', 'bindValue'))
31
            ->getMock();
32
        $platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform();
33
        $driverConnection = $this->createMock('\Doctrine\DBAL\Driver\Connection');
34
        $driverConnection->expects($this->any())
35
                ->method('prepare')
36
                ->will($this->returnValue($this->pdoStatement));
37
38
        $driver = $this->createMock('\Doctrine\DBAL\Driver');
39
        $constructorArgs = array(
40
            array(
41
                'platform' => $platform
42
            ),
43
            $driver
44
        );
45
        $this->conn = $this->getMockBuilder('\Doctrine\DBAL\Connection')
46
            ->setConstructorArgs($constructorArgs)
47
            ->getMock();
48
        $this->conn->expects($this->atLeastOnce())
49
                ->method('getWrappedConnection')
50
                ->will($this->returnValue($driverConnection));
51
52
        $this->configuration = $this->createMock('\Doctrine\DBAL\Configuration');
53
        $this->conn->expects($this->any())
54
                ->method('getConfiguration')
55
                ->will($this->returnValue($this->configuration));
56
57
        $this->conn->expects($this->any())
58
            ->method('getDriver')
59
            ->will($this->returnValue($driver));
60
61
    }
62
63
    public function testExecuteCallsLoggerStartQueryWithParametersWhenValuesBound()
64
    {
65
        $name = 'foo';
66
        $var = 'bar';
67
        $type = \PDO::PARAM_STR;
68
        $values = array($name => $var);
69
        $types = array($name => $type);
70
        $sql = '';
71
72
        $logger = $this->createMock('\Doctrine\DBAL\Logging\SQLLogger');
73
        $logger->expects($this->once())
74
                ->method('startQuery')
75
                ->with($this->equalTo($sql), $this->equalTo($values), $this->equalTo($types));
0 ignored issues
show
$this->equalTo($sql) of type PHPUnit\Framework\Constraint\IsEqual is incompatible with the type array expected by parameter $arguments of PHPUnit_Framework_MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

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

75
                ->with(/** @scrutinizer ignore-type */ $this->equalTo($sql), $this->equalTo($values), $this->equalTo($types));
Loading history...
76
77
        $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

77
        $this->configuration->/** @scrutinizer ignore-call */ 
78
                              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...
78
                ->method('getSQLLogger')
79
                ->will($this->returnValue($logger));
80
81
        $statement = new Statement($sql, $this->conn);
82
        $statement->bindValue($name, $var, $type);
83
        $statement->execute();
84
    }
85
86
    public function testExecuteCallsLoggerStartQueryWithParametersWhenParamsPassedToExecute()
87
    {
88
        $name = 'foo';
89
        $var = 'bar';
90
        $values = array($name => $var);
91
        $types = array();
92
        $sql = '';
93
94
        $logger = $this->createMock('\Doctrine\DBAL\Logging\SQLLogger');
95
        $logger->expects($this->once())
96
                ->method('startQuery')
97
                ->with($this->equalTo($sql), $this->equalTo($values), $this->equalTo($types));
0 ignored issues
show
$this->equalTo($sql) of type PHPUnit\Framework\Constraint\IsEqual is incompatible with the type array expected by parameter $arguments of PHPUnit_Framework_MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

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

97
                ->with(/** @scrutinizer ignore-type */ $this->equalTo($sql), $this->equalTo($values), $this->equalTo($types));
Loading history...
98
99
        $this->configuration->expects($this->once())
100
                ->method('getSQLLogger')
101
                ->will($this->returnValue($logger));
102
103
        $statement = new Statement($sql, $this->conn);
104
        $statement->execute($values);
105
    }
106
107
    public function testExecuteCallsStartQueryWithTheParametersBoundViaBindParam()
108
    {
109
        $name = 'foo';
110
        $var = 'bar';
111
        $values = [$name => $var];
112
        $types = [$name => \PDO::PARAM_STR];
113
        $sql = '';
114
115
        $logger = $this->createMock(SQLLogger::class);
116
        $logger->expects(self::once())
117
                ->method('startQuery')
118
                ->with(self::equalTo($sql), self::equalTo($values), self::equalTo($types));
119
120
        $this->configuration->expects(self::once())
121
                ->method('getSQLLogger')
122
                ->willReturn($logger);
123
124
        $statement = new Statement($sql, $this->conn);
125
        $statement->bindParam($name, $var);
126
        $statement->execute();
127
    }
128
129
    /**
130
     * @expectedException \Doctrine\DBAL\DBALException
131
     */
132
    public function testExecuteCallsLoggerStopQueryOnException()
133
    {
134
        $logger = $this->createMock('\Doctrine\DBAL\Logging\SQLLogger');
135
136
        $this->configuration->expects($this->once())
137
            ->method('getSQLLogger')
138
            ->will($this->returnValue($logger));
139
140
        // Needed to satisfy construction of DBALException
141
        $this->conn->expects($this->any())
142
            ->method('resolveParams')
143
            ->will($this->returnValue(array()));
144
145
        $logger->expects($this->once())
146
            ->method('startQuery');
147
148
        $logger->expects($this->once())
149
            ->method('stopQuery');
150
151
        $this->pdoStatement->expects($this->once())
152
            ->method('execute')
153
            ->will($this->throwException(new \Exception("Mock test exception")));
154
155
        $statement = new Statement("", $this->conn);
156
        $statement->execute();
157
    }
158
}
159