Completed
Pull Request — master (#3555)
by Sergei
22:57
created

StatementTest::createWrappedStatement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Portability;
4
5
use Doctrine\DBAL\Driver\Statement as DriverStatement;
6
use Doctrine\DBAL\FetchMode;
7
use Doctrine\DBAL\ParameterType;
8
use Doctrine\DBAL\Portability\Connection;
9
use Doctrine\DBAL\Portability\Statement;
10
use Doctrine\Tests\DbalTestCase;
11
use PHPUnit\Framework\MockObject\MockObject;
12
use ReflectionProperty;
13
use function iterator_to_array;
14
15
class StatementTest extends DbalTestCase
16
{
17
    /** @var Connection|MockObject */
18
    protected $conn;
19
20
    /** @var Statement */
21
    protected $stmt;
22
23
    /** @var DriverStatement|MockObject */
24
    protected $wrappedStmt;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function setUp() : void
30
    {
31
        $this->wrappedStmt = $this->createMock(DriverStatement::class);
32
        $this->conn        = $this->createConnection();
33
        $this->stmt        = $this->createStatement($this->wrappedStmt, $this->conn);
34
    }
35
36
    /**
37
     * @group DBAL-726
38
     */
39
    public function testBindParam()
40
    {
41
        $column   = 'mycolumn';
42
        $variable = 'myvalue';
43
        $type     = ParameterType::STRING;
44
        $length   = 666;
45
46
        $this->wrappedStmt->expects($this->once())
47
            ->method('bindParam')
48
            ->with($column, $variable, $type, $length)
49
            ->will($this->returnValue(true));
50
51
        self::assertTrue($this->stmt->bindParam($column, $variable, $type, $length));
52
    }
53
54
    public function testBindValue()
55
    {
56
        $param = 'myparam';
57
        $value = 'myvalue';
58
        $type  = ParameterType::STRING;
59
60
        $this->wrappedStmt->expects($this->once())
61
            ->method('bindValue')
62
            ->with($param, $value, $type)
63
            ->will($this->returnValue(true));
64
65
        self::assertTrue($this->stmt->bindValue($param, $value, $type));
66
    }
67
68
    public function testCloseCursor()
69
    {
70
        $this->wrappedStmt->expects($this->once())
71
            ->method('closeCursor')
72
            ->will($this->returnValue(true));
73
74
        self::assertTrue($this->stmt->closeCursor());
75
    }
76
77
    public function testColumnCount()
78
    {
79
        $columnCount = 666;
80
81
        $this->wrappedStmt->expects($this->once())
82
            ->method('columnCount')
83
            ->will($this->returnValue($columnCount));
84
85
        self::assertSame($columnCount, $this->stmt->columnCount());
86
    }
87
88
    public function testErrorCode()
89
    {
90
        $errorCode = '666';
91
92
        $this->wrappedStmt->expects($this->once())
93
            ->method('errorCode')
94
            ->will($this->returnValue($errorCode));
95
96
        self::assertSame($errorCode, $this->stmt->errorCode());
97
    }
98
99
    public function testErrorInfo()
100
    {
101
        $errorInfo = ['666', 'Evil error.'];
102
103
        $this->wrappedStmt->expects($this->once())
104
            ->method('errorInfo')
105
            ->will($this->returnValue($errorInfo));
106
107
        self::assertSame($errorInfo, $this->stmt->errorInfo());
108
    }
109
110
    public function testExecute()
111
    {
112
        $params = [
113
            'foo',
114
            'bar',
115
        ];
116
117
        $this->wrappedStmt->expects($this->once())
118
            ->method('execute')
119
            ->with($params)
120
            ->will($this->returnValue(true));
121
122
        self::assertTrue($this->stmt->execute($params));
123
    }
124
125
    public function testSetFetchMode()
126
    {
127
        $fetchMode = FetchMode::CUSTOM_OBJECT;
128
        $arg1      = 'MyClass';
129
        $arg2      = [1, 2];
130
131
        $this->wrappedStmt->expects($this->once())
132
            ->method('setFetchMode')
133
            ->with($fetchMode, $arg1, $arg2)
134
            ->will($this->returnValue(true));
135
136
        $re = new ReflectionProperty($this->stmt, 'defaultFetchMode');
137
        $re->setAccessible(true);
138
139
        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()
145
    {
146
        $this->wrappedStmt->expects($this->exactly(3))
147
            ->method('fetch')
148
            ->willReturnOnConsecutiveCalls('foo', 'bar', false);
149
150
        self::assertSame(['foo', 'bar'], iterator_to_array($this->stmt->getIterator()));
151
    }
152
153
    public function testRowCount()
154
    {
155
        $rowCount = 666;
156
157
        $this->wrappedStmt->expects($this->once())
158
            ->method('rowCount')
159
            ->will($this->returnValue($rowCount));
160
161
        self::assertSame($rowCount, $this->stmt->rowCount());
162
    }
163
164
    /**
165
     * @return Connection|MockObject
166
     */
167
    protected function createConnection()
168
    {
169
        return $this->getMockBuilder(Connection::class)
170
            ->disableOriginalConstructor()
171
            ->getMock();
172
    }
173
174
    /**
175
     * @return Statement
176
     */
177
    protected function createStatement(DriverStatement $wrappedStatement, Connection $connection)
178
    {
179
        return new Statement($wrappedStatement, $connection);
180
    }
181
}
182