StatementTest.php 4.83 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
use function iterator_to_array;
14

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

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

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

    /**
     * {@inheritdoc}
     */
29
    protected function setUp() : void
30
    {
31
        $this->wrappedStmt = $this->createMock(DriverStatement::class);
32 33 34 35 36 37 38
        $this->conn        = $this->createConnection();
        $this->stmt        = $this->createStatement($this->wrappedStmt, $this->conn);
    }

    /**
     * @group DBAL-726
     */
39
    public function testBindParam() : void
40 41 42
    {
        $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
    public function testBindValue() : void
55 56 57
    {
        $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
    public function testCloseCursor() : void
69 70 71 72 73
    {
        $this->wrappedStmt->expects($this->once())
            ->method('closeCursor')
            ->will($this->returnValue(true));

74
        self::assertTrue($this->stmt->closeCursor());
75 76
    }

77
    public function testColumnCount() : void
78 79 80 81 82 83 84
    {
        $columnCount = 666;

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

85
        self::assertSame($columnCount, $this->stmt->columnCount());
86 87
    }

88
    public function testErrorCode() : void
89 90 91 92 93 94 95
    {
        $errorCode = '666';

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

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

99
    public function testErrorInfo() : void
100
    {
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
    public function testExecute() : void
111
    {
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
    public function testSetFetchMode() : void
126
    {
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 137 138 139
        $re = new ReflectionProperty($this->stmt, 'defaultFetchMode');
        $re->setAccessible(true);

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

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

150
        self::assertSame(['foo', 'bar'], iterator_to_array($this->stmt->getIterator()));
151 152
    }

153
    public function testRowCount() : void
154 155 156 157 158 159 160
    {
        $rowCount = 666;

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

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

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

174
    protected function createStatement(DriverStatement $wrappedStatement, Connection $connection) : Statement
175 176 177 178
    {
        return new Statement($wrappedStatement, $connection);
    }
}