Completed
Push — master ( 92913a...680cd6 )
by Jeroen De
03:27 queued 01:46
created

testGivenTraversable_iteratorIsReturned()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\IterableFunction\Tests\Unit;
6
7
use PHPUnit\Framework\TestCase;
8
9
/**
10
 * @license GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
class IterableToIteratorTest extends TestCase {
14
15
	/**
16
	 * @dataProvider arrayProvider
17
	 */
18
	public function testGivenArray_itIsReturnedAsIterator( array $array ) {
19
		$iterator = iterable_to_iterator( $array );
20
21
		$this->assertInstanceOf( \Iterator::class, $iterator );
22
		$this->assertSame( $array, iterator_to_array( $iterator ) );
23
	}
24
25
	public function arrayProvider() {
26
		return [
27
			[ [] ],
28
			[ [ 42 ] ],
29
			[ [ null, null, null ] ],
30
			[ [ [], (object)[], [], (object)[] ] ],
31
			[ [ 'foo' => 100, 'bar' => 200, 'baz' => 300 ] ],
32
			[ [ 10 => 'foo', 20 => 'bar', 30 => 'baz' ] ],
33
			[ [ 2 => 'foo', 'bar', 3 => 'baz' ] ],
34
		];
35
	}
36
37
	/**
38
	 * @dataProvider iteratorProvider
39
	 */
40
	public function testGivenIterable_itIsReturnedAsIs( \Traversable $traversable ) {
41
		$this->assertSame( $traversable, iterable_to_iterator( $traversable ) );
42
	}
43
44
	public function iteratorProvider() {
45
		return [
46
			'empty iterator' => [
47
				new \ArrayIterator()
48
			],
49
			'normal iterator' => [
50
				new \ArrayIterator( [ 'a', 'b', 'c' ] )
51
			],
52
			'iterator with keys' => [
53
				new \ArrayIterator( [ 'a' => 10, 'b' => 20, 'c' => 30 ] )
54
			],
55
			'iterator with some explicit keys' => [
56
				new \ArrayIterator( [ 3 => null, 'a' => 10, 20, 'c' => 30 ] )
57
			],
58
			'Generator instance' => [
59
				( function() {
60
					yield 'a' => 10;
61
					yield 'b' => 20;
62
					yield 'c' => 30;
63
				} )()
64
			]
65
		];
66
	}
67
68
	public function testGivenIteratorAggregate_iteratorIsReturned() {
69
		$traversable = new class() implements \IteratorAggregate {
70
			public function getIterator() {
71
				return new \ArrayIterator( [ 'a' => 10, 'b' => 20, 'c' => 30 ] );
72
			}
73
		};
74
75
		$iterator = iterable_to_iterator( $traversable );
76
77
		$this->assertInstanceOf( \Iterator::class, $iterator );
78
		$this->assertSame( [ 'a' => 10, 'b' => 20, 'c' => 30 ], iterator_to_array( $iterator ) );
79
	}
80
81
	public function testGivenIteratorAggregateWithGenerator_returnedIteratorIsRewindable() {
82
		$traversable = new class() implements \IteratorAggregate {
83
			public function getIterator() {
84
				yield 'a' => 10;
85
				yield 'b' => 20;
86
				yield 'c' => 30;
87
			}
88
		};
89
90
		$iterator = iterable_to_iterator( $traversable );
91
92
		$this->assertContainsOnly( 'int', $iterator );
93
		$this->assertContainsOnly( 'int', $iterator );
94
	}
95
96
	public function testGivenTraversable_iteratorIsReturned() {
97
		$traversable = new \DatePeriod(
98
			new \DateTime( '2012-08-01' ),
99
			new \DateInterval( 'P1D' ),
100
			new \DateTime( '2012-08-05' )
101
		);
102
103
		$iterator = iterable_to_iterator( $traversable );
104
105
		$this->assertInstanceOf( \Iterator::class, $iterator );
106
		$this->assertCount( 4, $iterator );
0 ignored issues
show
Documentation introduced by
$iterator is of type object<Iterator>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
	}
108
109
	public function testGivenTraversable_returnedIteratorIsRewindable() {
110
		$traversable = new \DatePeriod(
111
			new \DateTime( '2012-08-01' ),
112
			new \DateInterval( 'P1D' ),
113
			new \DateTime( '2012-08-05' )
114
		);
115
116
		$iterator = iterable_to_iterator( $traversable );
117
118
		$this->assertContainsOnlyInstancesOf( \DateTime::class, $iterator );
119
		$this->assertContainsOnlyInstancesOf( \DateTime::class, $iterator );
120
	}
121
122
}
123