Failed Conditions
Pull Request — master (#3115)
by Sergei
13:41
created

StatementIteratorTest::statementProvider()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 8
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Driver;
4
5
use Doctrine\DBAL\Driver\IBMDB2\DB2Statement;
6
use Doctrine\DBAL\Driver\Mysqli\MysqliStatement;
7
use Doctrine\DBAL\Driver\OCI8\OCI8Statement;
8
use Doctrine\DBAL\Driver\SQLAnywhere\SQLAnywhereStatement;
9
use Doctrine\DBAL\Driver\SQLSrv\SQLSrvStatement;
10
use Doctrine\DBAL\Driver\Statement;
11
use Doctrine\DBAL\Driver\StatementIterator;
12
use Doctrine\DBAL\Portability\Statement as PortabilityStatement;
13
use PHPUnit\Framework\MockObject\MockObject;
14
use function extension_loaded;
15
16
class StatementIteratorTest extends \Doctrine\Tests\DbalTestCase
17
{
18
    /**
19
     * @dataProvider statementProvider()
20
     */
21
    public function testGettingIteratorDoesNotCallFetch(string $class)
22
    {
23
        $stmt = $this->createPartialMock($class, ['fetch', 'fetchAll', 'fetchColumn']);
24
        $stmt->expects($this->never())->method('fetch');
25
        $stmt->expects($this->never())->method('fetchAll');
26
        $stmt->expects($this->never())->method('fetchColumn');
27
28
        $stmt->getIterator();
1 ignored issue
show
Bug introduced by
The method getIterator() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        $stmt->/** @scrutinizer ignore-call */ 
29
               getIterator();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
    }
30
31
    public function testIteratorIterationCallsFetchOncePerStep()
32
    {
33
        $stmt = $this->createMock(Statement::class);
34
        $this->configureStatement($stmt, $calls);
35
36
        $stmtIterator = new StatementIterator($stmt);
37
38
        $this->assertIterationCallsFetchOncePerStep($stmtIterator, $calls);
39
    }
40
41
    /**
42
     * @dataProvider statementProvider()
43
     */
44
    public function testStatementIterationCallsFetchOncePerStep(string $class)
45
    {
46
        $stmt = $this->createPartialMock($class, ['fetch']);
47
        $this->configureStatement($stmt, $calls);
48
        $this->assertIterationCallsFetchOncePerStep($stmt, $calls);
49
    }
50
51
    private function configureStatement(MockObject $stmt, &$calls) : void
52
    {
53
        $values = ['foo', '', 'bar', '0', 'baz', 0, 'qux', null, 'quz', false, 'impossible'];
54
        $calls = 0;
55
56
        $stmt->expects($this->exactly(10))
57
            ->method('fetch')
58
            ->willReturnCallback(function() use ($values, &$calls) {
59
                $value = $values[$calls];
60
                $calls++;
61
62
                return $value;
63
            });
64
    }
65
66
    private function assertIterationCallsFetchOncePerStep($iterator, &$calls) : void
67
    {
68
        foreach ($iterator as $i => $_) {
69
            $this->assertEquals($i + 1, $calls);
70
        }
71
    }
72
73
    public static function statementProvider()
74
    {
75
        if (extension_loaded('ibm_db2')) {
76
            yield [DB2Statement::class];
77
        }
78
79
        yield [MysqliStatement::class];
80
81
        if (extension_loaded('oci8')) {
82
            yield [OCI8Statement::class];
83
        }
84
85
        yield [PortabilityStatement::class];
86
        yield [SQLAnywhereStatement::class];
87
88
        if (extension_loaded('sqlsrv')) {
89
            yield [SQLSrvStatement::class];
90
        }
91
    }
92
}
93