Failed Conditions
Pull Request — develop (#3582)
by Jonathan
69:13 queued 04:11
created

FetchOncePerStep()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Driver;
6
7
use Doctrine\DBAL\Driver\IBMDB2\DB2Statement;
8
use Doctrine\DBAL\Driver\Mysqli\MysqliStatement;
9
use Doctrine\DBAL\Driver\OCI8\OCI8Statement;
10
use Doctrine\DBAL\Driver\SQLAnywhere\SQLAnywhereStatement;
11
use Doctrine\DBAL\Driver\SQLSrv\SQLSrvStatement;
12
use Doctrine\DBAL\Driver\Statement;
13
use Doctrine\DBAL\Driver\StatementIterator;
14
use Doctrine\DBAL\Portability\Statement as PortabilityStatement;
15
use Doctrine\Tests\DbalTestCase;
16
use IteratorAggregate;
17
use PHPUnit\Framework\MockObject\MockObject;
18
use function assert;
19
use function extension_loaded;
20
use function is_iterable;
21
22
class StatementIteratorTest extends DbalTestCase
23
{
24
    /**
25
     * @dataProvider statementProvider()
26
     */
27
    public function testGettingIteratorDoesNotCallFetch(string $class) : void
28
    {
29
        /** @var IteratorAggregate|MockObject $stmt */
30
        $stmt = $this->createPartialMock($class, ['fetch', 'fetchAll', 'fetchColumn']);
31
        $stmt->expects($this->never())->method('fetch');
32
        $stmt->expects($this->never())->method('fetchAll');
33
        $stmt->expects($this->never())->method('fetchColumn');
34
35
        $stmt->getIterator();
36
    }
37
38
    public function testIteratorIterationCallsFetchOncePerStep() : void
39
    {
40
        $stmt = $this->createMock(Statement::class);
41
42
        $calls = 0;
43
        $this->configureStatement($stmt, $calls);
44
45
        $stmtIterator = new StatementIterator($stmt);
46
47
        foreach ($stmtIterator as $i => $_) {
48
            $this->assertEquals($i + 1, $calls);
49
        }
50
    }
51
52
    /**
53
     * @dataProvider statementProvider()
54
     */
55
    public function testStatementIterationCallsFetchOncePerStep(string $class) : void
56
    {
57
        $stmt = $this->createPartialMock($class, ['fetch']);
58
        assert(is_iterable($stmt));
59
60
        $calls = 0;
61
        $this->configureStatement($stmt, $calls);
62
63
        foreach ($stmt as $i => $_) {
64
            $this->assertEquals($i + 1, $calls);
65
        }
66
    }
67
68
    private function configureStatement(MockObject $stmt, int &$calls) : void
69
    {
70
        $values = ['foo', '', 'bar', '0', 'baz', 0, 'qux', null, 'quz', false, 'impossible'];
71
        $calls  = 0;
72
73
        $stmt->expects($this->exactly(10))
74
            ->method('fetch')
75
            ->willReturnCallback(static function () use ($values, &$calls) {
76
                $value = $values[$calls];
77
                $calls++;
78
79
                return $value;
80
            });
81
    }
82
83
    /**
84
     * @return string[][]
85
     */
86
    public static function statementProvider() : iterable
87
    {
88
        if (extension_loaded('ibm_db2')) {
89
            yield [DB2Statement::class];
0 ignored issues
show
Bug Best Practice introduced by
The expression yield array(Doctrine\DBA...B2\DB2Statement::class) returns the type Generator which is incompatible with the documented return type array<mixed,string[]>.
Loading history...
90
        }
91
92
        yield [MysqliStatement::class];
93
94
        if (extension_loaded('oci8')) {
95
            yield [OCI8Statement::class];
96
        }
97
98
        yield [PortabilityStatement::class];
99
        yield [SQLAnywhereStatement::class];
100
101
        if (extension_loaded('sqlsrv')) {
102
            yield [SQLSrvStatement::class];
103
        }
104
    }
105
}
106