StatementTest.php 4.81 KB
Newer Older
1 2
<?php

3
namespace Doctrine\DBAL\Tests\Portability;
4

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

15
class StatementTest extends TestCase
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
        $length   = 666;

46
        $this->wrappedStmt->expects(self::once())
47 48
            ->method('bindParam')
            ->with($column, $variable, $type, $length)
49
            ->will(self::returnValue(true));
50

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
        $this->wrappedStmt->expects(self::once())
61 62
            ->method('bindValue')
            ->with($param, $value, $type)
63
            ->will(self::returnValue(true));
64

65
        self::assertTrue($this->stmt->bindValue($param, $value, $type));
66 67
    }

68
    public function testCloseCursor() : void
69
    {
70
        $this->wrappedStmt->expects(self::once())
71
            ->method('closeCursor')
72
            ->will(self::returnValue(true));
73

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

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

81
        $this->wrappedStmt->expects(self::once())
82
            ->method('columnCount')
83
            ->will(self::returnValue($columnCount));
84

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

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

92
        $this->wrappedStmt->expects(self::once())
93
            ->method('errorCode')
94
            ->will(self::returnValue($errorCode));
95

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
        $this->wrappedStmt->expects(self::once())
104
            ->method('errorInfo')
105
            ->will(self::returnValue($errorInfo));
106

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
        $this->wrappedStmt->expects(self::once())
118 119
            ->method('execute')
            ->with($params)
120
            ->will(self::returnValue(true));
121

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
        $this->wrappedStmt->expects(self::once())
132 133
            ->method('setFetchMode')
            ->with($fetchMode, $arg1, $arg2)
134
            ->will(self::returnValue(true));
135

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
        $this->wrappedStmt->expects(self::exactly(3))
147 148
            ->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
    {
        $rowCount = 666;

157
        $this->wrappedStmt->expects(self::once())
158
            ->method('rowCount')
159
            ->will(self::returnValue($rowCount));
160

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);
    }
}