1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AbacaphiliacTest\Extractor; |
4
|
|
|
|
5
|
|
|
use Abacaphiliac\Extractor\ExtractionInterface; |
6
|
|
|
use Abacaphiliac\Extractor\ExtractorChain; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers \Abacaphiliac\Extractor\ExtractorChain |
10
|
|
|
*/ |
11
|
|
|
class ExtractorChainTest extends \PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
public function testExtractFromEmptyChain() |
14
|
|
|
{ |
15
|
|
|
$sut = new ExtractorChain(); |
16
|
|
|
|
17
|
|
|
$actual = $sut->extract(); |
18
|
|
|
|
19
|
|
|
self::assertCount(0, $actual); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testDefaultExtraction() |
23
|
|
|
{ |
24
|
|
|
$sut = new ExtractorChain([ |
25
|
|
|
$firstExtractor = $this->getMockBuilder(ExtractionInterface::class)->getMock(), |
26
|
|
|
$secondExtractor = $this->getMockBuilder(ExtractionInterface::class)->getMock(), |
27
|
|
|
]); |
28
|
|
|
|
29
|
|
|
$firstExtractor->expects(self::any())->method('extract')->will(self::returnValue([ |
30
|
|
|
'Foo' => 'Bar', |
31
|
|
|
'Bar' => 'Foo', |
32
|
|
|
])); |
33
|
|
|
|
34
|
|
|
$secondExtractor->expects(self::any())->method('extract')->will(self::returnValue([ |
35
|
|
|
'Foo' => 'FooBar', |
36
|
|
|
'Fizz' => 'Buzz', |
37
|
|
|
])); |
38
|
|
|
|
39
|
|
|
$actual = $sut->extract(); |
40
|
|
|
|
41
|
|
|
self::assertArraySubset( |
42
|
|
|
[ |
43
|
|
|
'Foo' => 'FooBar', |
44
|
|
|
'Bar' => 'Foo', |
45
|
|
|
'Fizz' => 'Buzz', |
46
|
|
|
], |
47
|
|
|
$actual |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testExtractNonRecursively() |
52
|
|
|
{ |
53
|
|
|
$sut = new ExtractorChain([ |
54
|
|
|
$firstExtractor = $this->getMockBuilder(ExtractionInterface::class)->getMock(), |
55
|
|
|
$secondExtractor = $this->getMockBuilder(ExtractionInterface::class)->getMock(), |
56
|
|
|
], false); |
57
|
|
|
|
58
|
|
|
$firstExtractor->expects(self::any())->method('extract')->will(self::returnValue([ |
59
|
|
|
'Foo' => [ |
60
|
|
|
'Fizz' => 'Buzz', |
61
|
|
|
], |
62
|
|
|
])); |
63
|
|
|
|
64
|
|
|
$secondExtractor->expects(self::any())->method('extract')->will(self::returnValue([ |
65
|
|
|
'Foo' => 'Bar', |
66
|
|
|
])); |
67
|
|
|
|
68
|
|
|
$actual = $sut->extract(); |
69
|
|
|
|
70
|
|
|
self::assertArraySubset( |
71
|
|
|
[ |
72
|
|
|
'Foo' => 'Bar', |
73
|
|
|
], |
74
|
|
|
$actual |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testExtractTraversable() |
79
|
|
|
{ |
80
|
|
|
$sut = new ExtractorChain([ |
81
|
|
|
$firstExtractor = $this->getMockBuilder(ExtractionInterface::class)->getMock(), |
82
|
|
|
$secondExtractor = $this->getMockBuilder(ExtractionInterface::class)->getMock(), |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
$firstExtractor->expects(self::any())->method('extract')->will(self::returnValue(new \ArrayIterator([ |
86
|
|
|
'Foo' => [ |
87
|
|
|
'Fizz' => 'Buzz', |
88
|
|
|
], |
89
|
|
|
]))); |
90
|
|
|
|
91
|
|
|
$secondExtractor->expects(self::any())->method('extract')->will(self::returnValue(new \ArrayIterator([ |
92
|
|
|
'Foo' => [ |
93
|
|
|
'Fizz' => 'Bar', |
94
|
|
|
], |
95
|
|
|
]))); |
96
|
|
|
|
97
|
|
|
$actual = $sut->extract(); |
98
|
|
|
|
99
|
|
|
self::assertArraySubset( |
100
|
|
|
[ |
101
|
|
|
'Foo' => [ |
102
|
|
|
'Fizz' => 'Bar', |
103
|
|
|
], |
104
|
|
|
], |
105
|
|
|
$actual |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @expectedException \Abacaphiliac\Extractor\Exception\UnexpectedValueException |
111
|
|
|
*/ |
112
|
|
|
public function testInvalidExtraction() |
113
|
|
|
{ |
114
|
|
|
$sut = new ExtractorChain([ |
115
|
|
|
$extractor = $this->getMockBuilder(ExtractionInterface::class)->getMock(), |
116
|
|
|
]); |
117
|
|
|
|
118
|
|
|
$extractor->expects(self::any())->method('extract')->will(self::returnValue(new \stdClass())); |
119
|
|
|
|
120
|
|
|
$sut->extract(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testExtractRecursively() |
124
|
|
|
{ |
125
|
|
|
$sut = new ExtractorChain([ |
126
|
|
|
$firstExtractor = $this->getMockBuilder(ExtractionInterface::class)->getMock(), |
127
|
|
|
$secondExtractor = $this->getMockBuilder(ExtractionInterface::class)->getMock(), |
128
|
|
|
$thirdExtractor = $this->getMockBuilder(ExtractionInterface::class)->getMock(), |
129
|
|
|
]); |
130
|
|
|
|
131
|
|
|
$firstExtractor->expects(self::any())->method('extract')->will(self::returnValue(new \ArrayIterator([ |
132
|
|
|
'Foo' => [ |
133
|
|
|
'Foo' => 'Bar', |
134
|
|
|
], |
135
|
|
|
]))); |
136
|
|
|
|
137
|
|
|
$secondExtractor->expects(self::any())->method('extract')->will(self::returnValue(new \ArrayIterator([ |
138
|
|
|
'Foo' => [ |
139
|
|
|
'Fizz' => 'Buzz', |
140
|
|
|
], |
141
|
|
|
]))); |
142
|
|
|
|
143
|
|
|
$thirdExtractor->expects(self::any())->method('extract')->will(self::returnValue(new \ArrayIterator([ |
144
|
|
|
'Foo' => [ |
145
|
|
|
'asdf' => 'qwer', |
146
|
|
|
], |
147
|
|
|
]))); |
148
|
|
|
|
149
|
|
|
$actual = $sut->extract(); |
150
|
|
|
|
151
|
|
|
self::assertArraySubset( |
152
|
|
|
[ |
153
|
|
|
'Foo' => [ |
154
|
|
|
'Foo' => 'Bar', |
155
|
|
|
'Fizz' => 'Buzz', |
156
|
|
|
'asdf' => 'qwer', |
157
|
|
|
], |
158
|
|
|
], |
159
|
|
|
$actual |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|