-
Notifications
You must be signed in to change notification settings - Fork 8
/
toArrayTest.php
141 lines (128 loc) · 3.26 KB
/
toArrayTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* @covers Dash\toArray
* @covers Dash\Curry\toArray
*/
class toArrayTest extends PHPUnit\Framework\TestCase
{
/**
* @dataProvider cases
*/
public function test($value, $expected)
{
$this->assertSame($expected, Dash\toArray($value));
}
/**
* @dataProvider cases
*/
public function testCurried($value, $expected)
{
$toArray = Dash\Curry\toArray();
$this->assertSame($expected, $toArray($value));
}
public function cases()
{
return [
'With null' => [
'value' => null,
'expected' => [],
],
'With a string' => [
'value' => 'hello',
'expected' => ['hello'],
],
'With a number' => [
'value' => 3.14,
'expected' => [3.14],
],
'With a DateTime' => [
'value' => new DateTime('@0'),
'expected' => [
'date' => '1970-01-01 00:00:00.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
],
],
/*
With array
*/
'With an empty array' => [
'value' => [],
'expected' => [],
],
'With an array with one element' => [
'value' => ['a' => 3],
'expected' => ['a' => 3],
],
'With an indexed array' => [
'value' => [3, 8, 2, 5],
'expected' => [3, 8, 2, 5],
],
'With an associative array' => [
'value' => ['a' => 3, 'b' => 8, 'c' => 2, 'd' => 5],
'expected' => ['a' => 3, 'b' => 8, 'c' => 2, 'd' => 5],
],
/*
With stdClass
*/
'With an empty stdClass' => [
'value' => (object) [],
'expected' => [],
],
'With an stdClass of an array with one element' => [
'value' => (object) ['a' => 3],
'expected' => ['a' => 3],
],
'With an stdClass of an indexed array' => [
'value' => (object) [3, 8, 2, 5],
'expected' => [3, 8, 2, 5],
],
'With an stdClass of an associative array' => [
'value' => (object) ['a' => 3, 'b' => 8, 'c' => 2, 'd' => 5],
'expected' => ['a' => 3, 'b' => 8, 'c' => 2, 'd' => 5],
],
/*
With ArrayObject
*/
'With an empty ArrayObject' => [
'value' => new ArrayObject([]),
'expected' => [],
],
'With an ArrayObject with one element' => [
'value' => new ArrayObject(['a' => 3]),
'expected' => ['a' => 3],
],
'With an ArrayObject of an indexed array' => [
'value' => new ArrayObject([3, 8, 2, 5]),
'expected' => [3, 8, 2, 5],
],
'With an ArrayObject of an associative array' => [
'value' => new ArrayObject(['a' => 3, 'b' => 8, 'c' => 2, 'd' => 5]),
'expected' => ['a' => 3, 'b' => 8, 'c' => 2, 'd' => 5],
],
];
}
public function testDirectoryIterator()
{
$input = new \FilesystemIterator(
__DIR__,
\FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO
);
$output = Dash\toArray($input);
$expectedCount = iterator_count(new \FilesystemIterator(__DIR__, \FilesystemIterator::SKIP_DOTS));
$actualCount = count($output);
$this->assertSame($expectedCount, $actualCount);
$prevValue = null;
foreach ($output as $key => $value) {
$this->assertStringEndsWith('.php', $key);
$this->assertInstanceOf('SplFileInfo', $value);
$this->assertNotSame($prevValue, $value);
$prevValue = $value;
}
}
public function testExamples()
{
$this->assertSame(['a' => 1, 'b' => 2], Dash\toArray((object) ['a' => 1, 'b' => 2]));
// 2nd example tested by testDirectoryIterator()
}
}