StatementTest.php 4.8 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
    protected $wrappedStmt;

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

    /**
     * @group DBAL-726
     */
36
    public function testBindParam() : void
37 38 39
    {
        $column   = 'mycolumn';
        $variable = 'myvalue';
40
        $type     = ParameterType::STRING;
41 42 43 44 45 46 47
        $length   = 666;

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

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

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

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

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

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

71
        self::assertTrue($this->stmt->closeCursor());
72 73
    }

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

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

82
        self::assertSame($columnCount, $this->stmt->columnCount());
83 84
    }

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

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

93
        self::assertSame($errorCode, $this->stmt->errorCode());
94 95
    }

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

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

104
        self::assertSame($errorInfo, $this->stmt->errorInfo());
105 106
    }

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

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

119
        self::assertTrue($this->stmt->execute($params));
120 121
    }

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

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

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

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

141
    public function testGetIterator() : void
142
    {
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
    public function testRowCount() : void
151 152 153 154 155 156 157
    {
        $rowCount = 666;

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

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

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

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