1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jasny\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use function Jasny\iterable_expect_type; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers \Jasny\iterable_expect_type |
10
|
|
|
*/ |
11
|
|
|
class IterableExpectTypeTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
use LazyExecutionIteratorTrait; |
14
|
|
|
|
15
|
|
|
public function validProvider() |
16
|
|
|
{ |
17
|
|
|
return [ |
18
|
|
|
[['hello', 'world'], 'string'], |
19
|
|
|
[[1, 2, 3], 'int'], |
20
|
|
|
[['hello', 2, 'you'], ['string', 'int']], |
21
|
|
|
[[new \stdClass()], \stdClass::class], |
22
|
|
|
[[new \DateTime()], \DateTimeInterface::class] |
23
|
|
|
]; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @dataProvider validProvider |
28
|
|
|
*/ |
29
|
|
|
public function test(array $values, $type) |
30
|
|
|
{ |
31
|
|
|
$iterator = iterable_expect_type($values, $type); |
32
|
|
|
$result = iterator_to_array($iterator); |
33
|
|
|
|
34
|
|
|
$this->assertSame($values, $result); // Untouched |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @dataProvider validProvider |
39
|
|
|
*/ |
40
|
|
|
public function testIteratorValid(array $values, $type) |
41
|
|
|
{ |
42
|
|
|
$inner = new \ArrayIterator($values); |
43
|
|
|
|
44
|
|
|
$iterator = iterable_expect_type($inner, $type); |
45
|
|
|
$result = iterator_to_array($iterator); |
46
|
|
|
|
47
|
|
|
$this->assertSame($values, $result); // Untouched |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @expectedException \UnexpectedValueException |
52
|
|
|
* @expectedExceptionMessage Expected all elements to be of type string, integer given |
53
|
|
|
*/ |
54
|
|
|
public function testFirstInvalid() |
55
|
|
|
{ |
56
|
|
|
$values = [1, 'hello']; |
57
|
|
|
|
58
|
|
|
$iterator = iterable_expect_type($values, 'string'); |
59
|
|
|
|
60
|
|
|
iterator_to_array($iterator); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @expectedException \UnexpectedValueException |
65
|
|
|
* @expectedExceptionMessage Expected all elements to be of type integer, string given |
66
|
|
|
*/ |
67
|
|
|
public function testSecondInvalid() |
68
|
|
|
{ |
69
|
|
|
$values = [1, 'hello']; |
70
|
|
|
|
71
|
|
|
$iterator = iterable_expect_type($values, 'integer'); |
72
|
|
|
|
73
|
|
|
iterator_to_array($iterator); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @expectedException \TypeError |
78
|
|
|
* @expectedExceptionMessage FOO BOO stdClass object WOO |
79
|
|
|
*/ |
80
|
|
|
public function testTypeError() |
81
|
|
|
{ |
82
|
|
|
$values = [new \stdClass()]; |
83
|
|
|
|
84
|
|
|
$message = "FOO BOO %s WOO"; |
85
|
|
|
$iterator = iterable_expect_type($values, 'string', \TypeError::class, $message); |
86
|
|
|
|
87
|
|
|
iterator_to_array($iterator); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testEmpty() |
91
|
|
|
{ |
92
|
|
|
$iterator = iterable_expect_type(new \EmptyIterator(), 'int'); |
93
|
|
|
|
94
|
|
|
$result = iterator_to_array($iterator); |
95
|
|
|
|
96
|
|
|
$this->assertEquals([], $result); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Test that nothing happens when not iterating |
101
|
|
|
*/ |
102
|
|
|
public function testLazyExecution() |
103
|
|
|
{ |
104
|
|
|
$iterator = $this->createLazyExecutionIterator(); |
105
|
|
|
|
106
|
|
|
iterable_expect_type($iterator, 'int'); |
107
|
|
|
|
108
|
|
|
$this->assertTrue(true, "No warning"); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|