MapIteratorTest::testKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace FinalizerTest\Reflection;
4
5
use Finalizer\Iterator\MapIterator;
6
use PHPUnit\Framework\TestCase;
7
8
/**
9
 * @covers \Finalizer\Iterator\MapIterator
10
 */
11
class MapIteratorTest extends TestCase
12
{
13
    public function testCurrent()
14
    {
15
        /* @var $map callable|\PHPUnit_Framework_MockObject_MockObject */
16
        $map             = $this->createPartialMock(\stdClass::class, ['__invoke']);
17
        /* @var $wrappedIterator \Iterator|\PHPUnit_Framework_MockObject_MockObject */
18
        $wrappedIterator = $this->createMock(\Iterator::class);
19
20
        $wrappedIterator->expects($this->at(0))->method('current')->will($this->returnValue('foo'));
21
        $wrappedIterator->expects($this->at(1))->method('current')->will($this->returnValue('bar'));
22
23
        $map->expects($this->at(0))->method('__invoke')->with('foo')->will($this->returnValue('baz'));
24
        $map->expects($this->at(1))->method('__invoke')->with('bar')->will($this->returnValue('tab'));
25
26
        $iterator = new MapIterator($wrappedIterator, $map);
0 ignored issues
show
Bug introduced by
It seems like $wrappedIterator defined by $this->createMock(\Iterator::class) on line 18 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Finalizer\Iterator\MapIterator::__construct() does only seem to accept object<Iterator>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $map defined by $this->createPartialMock...ass, array('__invoke')) on line 16 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Finalizer\Iterator\MapIterator::__construct() does only seem to accept callable, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
27
28
        $this->assertSame('baz', $iterator->current());
29
        $this->assertSame('tab', $iterator->current());
30
    }
31
32
    public function testNext()
33
    {
34
        /* @var $map callable|\PHPUnit_Framework_MockObject_MockObject */
35
        $map             = $this->createPartialMock(\stdClass::class, ['__invoke']);
36
        /* @var $wrappedIterator \Iterator|\PHPUnit_Framework_MockObject_MockObject */
37
        $wrappedIterator = $this->createMock(\Iterator::class);
38
39
        $wrappedIterator->expects($this->exactly(2))->method('next');
40
41
        $map->expects($this->never())->method('__invoke');
42
43
        $iterator = new MapIterator($wrappedIterator, $map);
0 ignored issues
show
Bug introduced by
It seems like $wrappedIterator defined by $this->createMock(\Iterator::class) on line 37 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Finalizer\Iterator\MapIterator::__construct() does only seem to accept object<Iterator>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $map defined by $this->createPartialMock...ass, array('__invoke')) on line 35 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Finalizer\Iterator\MapIterator::__construct() does only seem to accept callable, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
44
45
        $iterator->next();
46
        $iterator->next();
47
    }
48
49
    public function testRewind()
50
    {
51
        /* @var $map callable|\PHPUnit_Framework_MockObject_MockObject */
52
        $map             = $this->createPartialMock(\stdClass::class, ['__invoke']);
53
        /* @var $wrappedIterator \Iterator|\PHPUnit_Framework_MockObject_MockObject */
54
        $wrappedIterator = $this->createMock(\Iterator::class);
55
56
        $wrappedIterator->expects($this->exactly(2))->method('rewind');
57
58
        $map->expects($this->never())->method('__invoke');
59
60
        $iterator = new MapIterator($wrappedIterator, $map);
0 ignored issues
show
Bug introduced by
It seems like $wrappedIterator defined by $this->createMock(\Iterator::class) on line 54 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Finalizer\Iterator\MapIterator::__construct() does only seem to accept object<Iterator>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $map defined by $this->createPartialMock...ass, array('__invoke')) on line 52 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Finalizer\Iterator\MapIterator::__construct() does only seem to accept callable, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
61
62
        $iterator->rewind();
63
        $iterator->rewind();
64
    }
65
66
    public function testKey()
67
    {
68
        /* @var $map callable|\PHPUnit_Framework_MockObject_MockObject */
69
        $map             = $this->createPartialMock(\stdClass::class, ['__invoke']);
70
        /* @var $wrappedIterator \Iterator|\PHPUnit_Framework_MockObject_MockObject */
71
        $wrappedIterator = $this->createMock(\Iterator::class);
72
73
        $wrappedIterator->expects($this->at(0))->method('key')->will($this->returnValue('foo'));
74
        $wrappedIterator->expects($this->at(1))->method('key')->will($this->returnValue('bar'));
75
76
        $map->expects($this->never())->method('__invoke');
77
78
        $iterator = new MapIterator($wrappedIterator, $map);
0 ignored issues
show
Bug introduced by
It seems like $wrappedIterator defined by $this->createMock(\Iterator::class) on line 71 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Finalizer\Iterator\MapIterator::__construct() does only seem to accept object<Iterator>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $map defined by $this->createPartialMock...ass, array('__invoke')) on line 69 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Finalizer\Iterator\MapIterator::__construct() does only seem to accept callable, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
79
80
        $this->assertSame('foo', $iterator->key());
81
        $this->assertSame('bar', $iterator->key());
82
    }
83
84
    public function testValid()
85
    {
86
        /* @var $map callable|\PHPUnit_Framework_MockObject_MockObject */
87
        $map             = $this->createPartialMock(\stdClass::class, ['__invoke']);
88
        /* @var $wrappedIterator \Iterator|\PHPUnit_Framework_MockObject_MockObject */
89
        $wrappedIterator = $this->createMock(\Iterator::class);
90
91
        $wrappedIterator->expects($this->at(0))->method('valid')->will($this->returnValue(true));
92
        $wrappedIterator->expects($this->at(1))->method('valid')->will($this->returnValue(false));
93
94
        $map->expects($this->never())->method('__invoke');
95
96
        $iterator = new MapIterator($wrappedIterator, $map);
0 ignored issues
show
Bug introduced by
It seems like $wrappedIterator defined by $this->createMock(\Iterator::class) on line 89 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Finalizer\Iterator\MapIterator::__construct() does only seem to accept object<Iterator>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $map defined by $this->createPartialMock...ass, array('__invoke')) on line 87 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Finalizer\Iterator\MapIterator::__construct() does only seem to accept callable, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
97
98
        $this->assertTrue($iterator->valid());
99
        $this->assertFalse($iterator->valid());
100
    }
101
102
    /**
103
     * @coversNothing
104
     */
105
    public function testIterationWithActualArrayIterator()
106
    {
107
        $this->assertEquals(
108
            [2, 4, 6],
109
            iterator_to_array(new MapIterator(
110
                new \ArrayIterator([1, 2, 3]),
111
                function ($value) {
112
                    return $value * 2;
113
                }
114
            ))
115
        );
116
    }
117
}
118