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;
12 13
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionProperty;
14
use function iterator_to_array;
15

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

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

24
    /** @var DriverStatement|MockObject */
25 26 27 28 29
    protected $wrappedStmt;

    /**
     * {@inheritdoc}
     */
30
    protected function setUp() : void
31 32 33 34 35 36 37 38 39 40 41 42 43
    {
        $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';
44
        $type     = ParameterType::STRING;
45 46 47 48 49 50 51
        $length   = 666;

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

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

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

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

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

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

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

    public function testColumnCount()
    {
        $columnCount = 666;

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

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

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

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

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

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

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

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

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

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

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

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

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

137 138 139 140
        $re = new ReflectionProperty($this->stmt, 'defaultFetchMode');
        $re->setAccessible(true);

        self::assertSame(FetchMode::MIXED, $re->getValue($this->stmt));
141
        self::assertTrue($this->stmt->setFetchMode($fetchMode, $arg1, $arg2));
142
        self::assertSame($fetchMode, $re->getValue($this->stmt));
143 144 145 146
    }

    public function testGetIterator()
    {
147 148 149
        $this->wrappedStmt->expects($this->exactly(3))
            ->method('fetch')
            ->willReturnOnConsecutiveCalls('foo', 'bar', false);
150

151
        self::assertSame(['foo', 'bar'], iterator_to_array($this->stmt->getIterator()));
152 153 154 155 156 157 158 159 160 161
    }

    public function testRowCount()
    {
        $rowCount = 666;

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

162
        self::assertSame($rowCount, $this->stmt->rowCount());
163 164 165
    }

    /**
166
     * @return Connection|MockObject
167 168 169
     */
    protected function createConnection()
    {
Sergei Morozov's avatar
Sergei Morozov committed
170
        return $this->getMockBuilder(Connection::class)
171 172 173 174 175
            ->disableOriginalConstructor()
            ->getMock();
    }

    /**
Sergei Morozov's avatar
Sergei Morozov committed
176
     * @return Statement
177
     */
178
    protected function createStatement(DriverStatement $wrappedStatement, Connection $connection)
179 180 181 182 183
    {
        return new Statement($wrappedStatement, $connection);
    }

    /**
184
     * @return DriverStatement|MockObject
185 186 187
     */
    protected function createWrappedStatement()
    {
Sergei Morozov's avatar
Sergei Morozov committed
188
        return $this->createMock(DriverStatementMock::class);
189 190
    }
}