Failed Conditions
Push — master ( c37710...e51e73 )
by Sergei
16s queued 12s
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 IteratorAggregate;
14
use PHPUnit\Framework\MockObject\MockObject;
15
use Traversable;
16
use function extension_loaded;
17
18
class StatementIteratorTest extends \Doctrine\Tests\DbalTestCase
19
{
20
    /**
21
     * @dataProvider statementProvider()
22
     */
23
    public function testGettingIteratorDoesNotCallFetch(string $class) : void
24
    {
25
        /** @var IteratorAggregate|MockObject $stmt */
26
        $stmt = $this->createPartialMock($class, ['fetch', 'fetchAll', 'fetchColumn']);
27
        $stmt->expects($this->never())->method('fetch');
1 ignored issue
show
Bug introduced by
The method expects() does not exist on IteratorAggregate. ( Ignorable by Annotation )

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

27
        $stmt->/** @scrutinizer ignore-call */ 
28
               expects($this->never())->method('fetch');

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...
28
        $stmt->expects($this->never())->method('fetchAll');
29
        $stmt->expects($this->never())->method('fetchColumn');
30
31
        $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

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