Issues (291)

tests/MapIteratorTest.php (5 issues)

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

40
                return new ArrayIterator(/** @scrutinizer ignore-type */ $this);
Loading history...
41
            }
42
        }, function ($item) {
43
            return $item;
44
        });
45
46
        self::assertCount(3, $mapIterator);
47
    }
48
49
    public function testConstructorException1(): void
50
    {
51
        $this->expectException('TheCodingMachine\TDBM\TDBMException');
52
        $mapIterator = new MapIterator(new \DateTime(), function ($item) {
0 ignored issues
show
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

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