Failed Conditions
Pull Request — master (#3115)
by Sergei
11:12
created

StatementIteratorTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 75
rs 10
c 0
b 0
f 0

6 Methods

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