StatementTest.php 4.75 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;
11 12
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionProperty;
13

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
    protected $wrappedStmt;

27
    protected function setUp(): void
28
    {
29
        $this->wrappedStmt = $this->createMock(DriverStatement::class);
30 31 32 33
        $this->conn        = $this->createConnection();
        $this->stmt        = $this->createStatement($this->wrappedStmt, $this->conn);
    }

34
    public function testBindParam(): void
35 36 37
    {
        $column   = 'mycolumn';
        $variable = 'myvalue';
38
        $type     = ParameterType::STRING;
39 40 41 42 43 44 45
        $length   = 666;

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

46
        self::assertTrue($this->stmt->bindParam($column, $variable, $type, $length));
47 48
    }

49
    public function testBindValue(): void
50 51 52
    {
        $param = 'myparam';
        $value = 'myvalue';
53
        $type  = ParameterType::STRING;
54 55 56 57 58 59

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

60
        self::assertTrue($this->stmt->bindValue($param, $value, $type));
61 62
    }

63
    public function testCloseCursor(): void
64 65 66 67 68
    {
        $this->wrappedStmt->expects($this->once())
            ->method('closeCursor')
            ->will($this->returnValue(true));

69
        self::assertTrue($this->stmt->closeCursor());
70 71
    }

72
    public function testColumnCount(): void
73 74 75 76 77 78 79
    {
        $columnCount = 666;

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

80
        self::assertSame($columnCount, $this->stmt->columnCount());
81 82
    }

83
    public function testErrorCode(): void
84 85 86 87 88 89 90
    {
        $errorCode = '666';

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

91
        self::assertSame($errorCode, $this->stmt->errorCode());
92 93
    }

94
    public function testErrorInfo(): void
95
    {
Sergei Morozov's avatar
Sergei Morozov committed
96
        $errorInfo = ['666', 'Evil error.'];
97 98 99 100 101

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

102
        self::assertSame($errorInfo, $this->stmt->errorInfo());
103 104
    }

105
    public function testExecute(): void
106
    {
Sergei Morozov's avatar
Sergei Morozov committed
107
        $params = [
108
            'foo',
Sergei Morozov's avatar
Sergei Morozov committed
109 110
            'bar',
        ];
111 112 113 114 115 116

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

117
        self::assertTrue($this->stmt->execute($params));
118 119
    }

120
    public function testSetFetchMode(): void
121
    {
122
        $fetchMode = FetchMode::CUSTOM_OBJECT;
123
        $arg1      = 'MyClass';
Sergei Morozov's avatar
Sergei Morozov committed
124
        $arg2      = [1, 2];
125 126 127 128 129 130

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

131 132 133 134
        $re = new ReflectionProperty($this->stmt, 'defaultFetchMode');
        $re->setAccessible(true);

        self::assertSame(FetchMode::MIXED, $re->getValue($this->stmt));
135
        self::assertTrue($this->stmt->setFetchMode($fetchMode, $arg1, $arg2));
136
        self::assertSame($fetchMode, $re->getValue($this->stmt));
137 138
    }

139
    public function testGetIterator(): void
140
    {
141 142 143
        $this->wrappedStmt->expects($this->exactly(3))
            ->method('fetch')
            ->willReturnOnConsecutiveCalls('foo', 'bar', false);
144

145
        self::assertSame(['foo', 'bar'], iterator_to_array($this->stmt->getIterator()));
146 147
    }

148
    public function testRowCount(): void
149 150 151 152 153 154 155
    {
        $rowCount = 666;

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

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

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

169
    protected function createStatement(DriverStatement $wrappedStatement, Connection $connection): Statement
170 171 172 173
    {
        return new Statement($wrappedStatement, $connection);
    }
}