Failed Conditions
Pull Request — develop (#3590)
by Jonathan
63:20
created

testGettingIteratorDoesNotCallFetch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Driver;
6
7
use Doctrine\DBAL\Driver\Statement;
8
use Doctrine\DBAL\Driver\StatementIterator;
9
use Doctrine\Tests\DbalTestCase;
10
use PHPUnit\Framework\MockObject\MockObject;
11
use Traversable;
12
13
class StatementIteratorTest extends DbalTestCase
14
{
15
    public function testIteratorIterationCallsFetchOncePerStep() : void
16
    {
17
        $stmt = $this->createMock(Statement::class);
18
19
        $calls = 0;
20
        $this->configureStatement($stmt, $calls);
21
22
        $stmtIterator = new StatementIterator($stmt);
23
24
        $this->assertIterationCallsFetchOncePerStep($stmtIterator, $calls);
25
    }
26
27
    private function configureStatement(MockObject $stmt, int &$calls) : void
28
    {
29
        $values = ['foo', '', 'bar', '0', 'baz', 0, 'qux', null, 'quz', false, 'impossible'];
30
        $calls  = 0;
31
32
        $stmt->expects($this->exactly(10))
33
            ->method('fetch')
34
            ->willReturnCallback(static function () use ($values, &$calls) {
35
                $value = $values[$calls];
36
                $calls++;
37
38
                return $value;
39
            });
40
    }
41
42
    private function assertIterationCallsFetchOncePerStep(Traversable $iterator, int &$calls) : void
43
    {
44
        foreach ($iterator as $i => $_) {
45
            $this->assertEquals($i + 1, $calls);
46
        }
47
    }
48
}
49