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

namespace Doctrine\Tests\DBAL\Portability;

5
use Doctrine\DBAL\Driver\Statement as DriverStatement;
6 7
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
8 9
use Doctrine\DBAL\Portability\Connection;
use Doctrine\DBAL\Portability\Statement;
Sergei Morozov's avatar
Sergei Morozov committed
10
use Doctrine\Tests\DbalTestCase;
Sergei Morozov's avatar
Sergei Morozov committed
11
use Doctrine\Tests\Mocks\DriverStatementMock;
Sergei Morozov's avatar
Sergei Morozov committed
12
use PHPUnit_Framework_MockObject_MockObject;
13
use function iterator_to_array;
14

Sergei Morozov's avatar
Sergei Morozov committed
15
class StatementTest extends DbalTestCase
16
{
Sergei Morozov's avatar
Sergei Morozov committed
17
    /** @var Connection|PHPUnit_Framework_MockObject_MockObject */
18 19
    protected $conn;

Sergei Morozov's avatar
Sergei Morozov committed
20
    /** @var Statement */
21 22
    protected $stmt;

23
    /** @var DriverStatement|PHPUnit_Framework_MockObject_MockObject */
24 25 26 27 28
    protected $wrappedStmt;

    /**
     * {@inheritdoc}
     */
29
    protected function setUp()
30 31 32 33 34 35 36 37 38 39 40 41 42
    {
        $this->wrappedStmt = $this->createWrappedStatement();
        $this->conn        = $this->createConnection();
        $this->stmt        = $this->createStatement($this->wrappedStmt, $this->conn);
    }

    /**
     * @group DBAL-726
     */
    public function testBindParam()
    {
        $column   = 'mycolumn';
        $variable = 'myvalue';
43
        $type     = ParameterType::STRING;
44 45 46 47 48 49 50
        $length   = 666;

        $this->wrappedStmt->expects($this->once())
            ->method('bindParam')
            ->with($column, $variable, $type, $length)
            ->will($this->returnValue(true));

51
        self::assertTrue($this->stmt->bindParam($column, $variable, $type, $length));
52 53 54 55 56 57
    }

    public function testBindValue()
    {
        $param = 'myparam';
        $value = 'myvalue';
58
        $type  = ParameterType::STRING;
59 60 61 62 63 64

        $this->wrappedStmt->expects($this->once())
            ->method('bindValue')
            ->with($param, $value, $type)
            ->will($this->returnValue(true));

65
        self::assertTrue($this->stmt->bindValue($param, $value, $type));
66 67 68 69 70 71 72 73
    }

    public function testCloseCursor()
    {
        $this->wrappedStmt->expects($this->once())
            ->method('closeCursor')
            ->will($this->returnValue(true));

74
        self::assertTrue($this->stmt->closeCursor());
75 76 77 78 79 80 81 82 83 84
    }

    public function testColumnCount()
    {
        $columnCount = 666;

        $this->wrappedStmt->expects($this->once())
            ->method('columnCount')
            ->will($this->returnValue($columnCount));

85
        self::assertSame($columnCount, $this->stmt->columnCount());
86 87 88 89 90 91 92 93 94 95
    }

    public function testErrorCode()
    {
        $errorCode = '666';

        $this->wrappedStmt->expects($this->once())
            ->method('errorCode')
            ->will($this->returnValue($errorCode));

96
        self::assertSame($errorCode, $this->stmt->errorCode());
97 98 99 100
    }

    public function testErrorInfo()
    {
Sergei Morozov's avatar
Sergei Morozov committed
101
        $errorInfo = ['666', 'Evil error.'];
102 103 104 105 106

        $this->wrappedStmt->expects($this->once())
            ->method('errorInfo')
            ->will($this->returnValue($errorInfo));

107
        self::assertSame($errorInfo, $this->stmt->errorInfo());
108 109 110 111
    }

    public function testExecute()
    {
Sergei Morozov's avatar
Sergei Morozov committed
112
        $params = [
113
            'foo',
Sergei Morozov's avatar
Sergei Morozov committed
114 115
            'bar',
        ];
116 117 118 119 120 121

        $this->wrappedStmt->expects($this->once())
            ->method('execute')
            ->with($params)
            ->will($this->returnValue(true));

122
        self::assertTrue($this->stmt->execute($params));
123 124 125 126
    }

    public function testSetFetchMode()
    {
127
        $fetchMode = FetchMode::CUSTOM_OBJECT;
128
        $arg1      = 'MyClass';
Sergei Morozov's avatar
Sergei Morozov committed
129
        $arg2      = [1, 2];
130 131 132 133 134 135

        $this->wrappedStmt->expects($this->once())
            ->method('setFetchMode')
            ->with($fetchMode, $arg1, $arg2)
            ->will($this->returnValue(true));

136
        self::assertAttributeSame(FetchMode::MIXED, 'defaultFetchMode', $this->stmt);
137 138
        self::assertTrue($this->stmt->setFetchMode($fetchMode, $arg1, $arg2));
        self::assertAttributeSame($fetchMode, 'defaultFetchMode', $this->stmt);
139 140 141 142
    }

    public function testGetIterator()
    {
143 144 145
        $this->wrappedStmt->expects($this->exactly(3))
            ->method('fetch')
            ->willReturnOnConsecutiveCalls('foo', 'bar', false);
146

147
        self::assertSame(['foo', 'bar'], iterator_to_array($this->stmt->getIterator()));
148 149 150 151 152 153 154 155 156 157
    }

    public function testRowCount()
    {
        $rowCount = 666;

        $this->wrappedStmt->expects($this->once())
            ->method('rowCount')
            ->will($this->returnValue($rowCount));

158
        self::assertSame($rowCount, $this->stmt->rowCount());
159 160 161
    }

    /**
Sergei Morozov's avatar
Sergei Morozov committed
162
     * @return Connection|PHPUnit_Framework_MockObject_MockObject
163 164 165
     */
    protected function createConnection()
    {
Sergei Morozov's avatar
Sergei Morozov committed
166
        return $this->getMockBuilder(Connection::class)
167 168 169 170 171
            ->disableOriginalConstructor()
            ->getMock();
    }

    /**
Sergei Morozov's avatar
Sergei Morozov committed
172
     * @return Statement
173
     */
174
    protected function createStatement(DriverStatement $wrappedStatement, Connection $connection)
175 176 177 178 179
    {
        return new Statement($wrappedStatement, $connection);
    }

    /**
180
     * @return DriverStatement|PHPUnit_Framework_MockObject_MockObject
181 182 183
     */
    protected function createWrappedStatement()
    {
Sergei Morozov's avatar
Sergei Morozov committed
184
        return $this->createMock(DriverStatementMock::class);
185 186
    }
}