Completed
Push — master ( 3b7d13...36c690 )
by David
28s queued 12s
created

MapIteratorTest.php$0 ➔ getIterator()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 Copyright (C) 2006-2018 David Négrier - THE CODING MACHINE
6
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
*/
21
22
namespace TheCodingMachine\TDBM;
23
24
use ArrayIterator;
25
use IteratorAggregate;
26
use PHPUnit\Framework\TestCase;
27
28
class MapIteratorTest extends TestCase
29
{
30
    public function testIteratorAggregate(): void
31
    {
32
        $mapIterator = new MapIterator(new class implements IteratorAggregate {
33
            public $property1 = "Public property one";
34
            public $property2 = "Public property two";
35
            public $property3 = "Public property three";
36
37
            public function getIterator()
38
            {
39
                return new ArrayIterator($this);
0 ignored issues
show
Bug introduced by
$this of type anonymous//tests/MapIteratorTest.php$0 is incompatible with the type array expected by parameter $array of ArrayIterator::__construct(). ( Ignorable by Annotation )

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

39
                return new ArrayIterator(/** @scrutinizer ignore-type */ $this);
Loading history...
40
            }
41
        }, function ($item) {
42
            return $item;
43
        });
44
45
        self::assertCount(3, $mapIterator);
46
    }
47
48
    public function testConstructorException1(): void
49
    {
50
        $this->expectException('TheCodingMachine\TDBM\TDBMException');
51
        $mapIterator = new MapIterator(new \DateTime(), function ($item) {
0 ignored issues
show
Bug introduced by
new DateTime() of type DateTime is incompatible with the type Traversable|array expected by parameter $iterator of TheCodingMachine\TDBM\MapIterator::__construct(). ( Ignorable by Annotation )

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

51
        $mapIterator = new MapIterator(/** @scrutinizer ignore-type */ new \DateTime(), function ($item) {
Loading history...
Unused Code introduced by
The assignment to $mapIterator is dead and can be removed.
Loading history...
52
            return $item;
53
        });
54
    }
55
56
    public function testConstructorException2(): void
57
    {
58
        $this->expectException('TheCodingMachine\TDBM\TDBMException');
59
        $mapIterator = new MapIterator(array(1, 2, 3), function () {
0 ignored issues
show
Unused Code introduced by
The assignment to $mapIterator is dead and can be removed.
Loading history...
60
            return $item;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $item seems to be never defined.
Loading history...
61
        });
62
    }
63
64
    public function testJsonSerialize(): void
65
    {
66
        $value = array(1, 2, 3);
67
        $mapIterator = new MapIterator($value, function ($item) {
68
            return $item;
69
        });
70
71
        $this->assertEquals($value, json_decode(json_encode($mapIterator), true));
72
    }
73
}
74