StatementTest.php 4.9 KB
Newer Older
1 2 3 4 5
<?php

namespace Doctrine\Tests\DBAL;

use Doctrine\DBAL\Statement;
6
use Doctrine\DBAL\Logging\SQLLogger;
7 8 9 10 11

class StatementTest extends \Doctrine\Tests\DbalTestCase
{
    /**
     *
12
     * @var \Doctrine\DBAL\Connection
13 14
     */
    private $conn;
15

16 17
    /**
     *
18
     * @var \Doctrine\DBAL\Configuration
19 20
     */
    private $configuration;
21 22 23 24 25 26

    /**
     * @var \PDOStatement
     */
    private $pdoStatement;

27
    protected function setUp()
28
    {
29 30 31
        $this->pdoStatement = $this->getMockBuilder('\PDOStatement')
            ->setMethods(array('execute', 'bindParam', 'bindValue'))
            ->getMock();
32
        $platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform();
33
        $driverConnection = $this->createMock('\Doctrine\DBAL\Driver\Connection');
34 35
        $driverConnection->expects($this->any())
                ->method('prepare')
36
                ->will($this->returnValue($this->pdoStatement));
37 38

        $driver = $this->createMock('\Doctrine\DBAL\Driver');
39 40 41 42 43 44
        $constructorArgs = array(
            array(
                'platform' => $platform
            ),
            $driver
        );
45 46 47
        $this->conn = $this->getMockBuilder('\Doctrine\DBAL\Connection')
            ->setConstructorArgs($constructorArgs)
            ->getMock();
48 49 50
        $this->conn->expects($this->atLeastOnce())
                ->method('getWrappedConnection')
                ->will($this->returnValue($driverConnection));
51 52

        $this->configuration = $this->createMock('\Doctrine\DBAL\Configuration');
53 54 55
        $this->conn->expects($this->any())
                ->method('getConfiguration')
                ->will($this->returnValue($this->configuration));
56 57 58 59 60

        $this->conn->expects($this->any())
            ->method('getDriver')
            ->will($this->returnValue($driver));

61
    }
62

63 64 65 66 67 68 69 70
    public function testExecuteCallsLoggerStartQueryWithParametersWhenValuesBound()
    {
        $name = 'foo';
        $var = 'bar';
        $type = \PDO::PARAM_STR;
        $values = array($name => $var);
        $types = array($name => $type);
        $sql = '';
71 72

        $logger = $this->createMock('\Doctrine\DBAL\Logging\SQLLogger');
73 74 75
        $logger->expects($this->once())
                ->method('startQuery')
                ->with($this->equalTo($sql), $this->equalTo($values), $this->equalTo($types));
76

77 78 79
        $this->configuration->expects($this->once())
                ->method('getSQLLogger')
                ->will($this->returnValue($logger));
80

81
        $statement = new Statement($sql, $this->conn);
82 83 84
        $statement->bindValue($name, $var, $type);
        $statement->execute();
    }
85

86 87 88 89 90 91 92
    public function testExecuteCallsLoggerStartQueryWithParametersWhenParamsPassedToExecute()
    {
        $name = 'foo';
        $var = 'bar';
        $values = array($name => $var);
        $types = array();
        $sql = '';
93 94

        $logger = $this->createMock('\Doctrine\DBAL\Logging\SQLLogger');
95 96 97
        $logger->expects($this->once())
                ->method('startQuery')
                ->with($this->equalTo($sql), $this->equalTo($values), $this->equalTo($types));
98

99 100 101
        $this->configuration->expects($this->once())
                ->method('getSQLLogger')
                ->will($this->returnValue($logger));
102

103
        $statement = new Statement($sql, $this->conn);
104 105
        $statement->execute($values);
    }
106

107 108 109 110
    public function testExecuteCallsStartQueryWithTheParametersBoundViaBindParam()
    {
        $name = 'foo';
        $var = 'bar';
111 112
        $values = [$name => $var];
        $types = [$name => \PDO::PARAM_STR];
113 114
        $sql = '';

115 116
        $logger = $this->createMock(SQLLogger::class);
        $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 125 126 127 128

        $statement = new Statement($sql, $this->conn);
        $statement->bindParam($name, $var);
        $statement->execute();
    }

129 130 131 132 133
    /**
     * @expectedException \Doctrine\DBAL\DBALException
     */
    public function testExecuteCallsLoggerStopQueryOnException()
    {
134
        $logger = $this->createMock('\Doctrine\DBAL\Logging\SQLLogger');
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

        $this->configuration->expects($this->once())
            ->method('getSQLLogger')
            ->will($this->returnValue($logger));

        // Needed to satisfy construction of DBALException
        $this->conn->expects($this->any())
            ->method('resolveParams')
            ->will($this->returnValue(array()));

        $logger->expects($this->once())
            ->method('startQuery');

        $logger->expects($this->once())
            ->method('stopQuery');

        $this->pdoStatement->expects($this->once())
            ->method('execute')
            ->will($this->throwException(new \Exception("Mock test exception")));

        $statement = new Statement("", $this->conn);
        $statement->execute();
    }
158
}