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

namespace Doctrine\Tests\DBAL;

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

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

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

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

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

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

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

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

62
    }
63

64 65
    public function testExecuteCallsLoggerStartQueryWithParametersWhenValuesBound()
    {
66 67 68 69 70 71
        $name   = 'foo';
        $var    = 'bar';
        $type   = ParameterType::STRING;
        $values = [$name => $var];
        $types  = [$name => $type];
        $sql    = '';
72 73

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

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

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

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

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

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

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

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

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

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

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

        $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();
    }
159
}