1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace eNTiDi\FeedReader\Tests; |
4
|
|
|
|
5
|
|
|
use eNTiDi\FeedReader\FeedReaderService; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use GuzzleHttp\Handler\MockHandler; |
8
|
|
|
use GuzzleHttp\HandlerStack; |
9
|
|
|
use GuzzleHttp\Psr7\Response; |
10
|
|
|
use GuzzleHttp\Psr7\Request; |
11
|
|
|
use GuzzleHttp\Exception\RequestException; |
12
|
|
|
use SilverStripe\Dev\SapphireTest; |
13
|
|
|
|
14
|
|
|
class FeedReaderServiceTest extends SapphireTest |
15
|
|
|
{ |
16
|
|
|
protected $usesDatabase = false; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Check the behavior of the ATOM 1.0 parser. |
20
|
|
|
*/ |
21
|
|
|
public function testAtomParser() |
22
|
|
|
{ |
23
|
|
|
// Use a dummy (but valid) URI for the requests |
24
|
|
|
$url = 'http://localhost/'; |
25
|
|
|
|
26
|
|
|
// Create a mock and queue two responses |
27
|
|
|
$mock = new MockHandler([ |
28
|
|
|
new Response(200, [], file_get_contents(__DIR__ . '/sample1.atom')), |
29
|
|
|
new Response(200, [], file_get_contents(__DIR__ . '/sample2.atom')), |
30
|
|
|
]); |
31
|
|
|
$handler = HandlerStack::create($mock); |
32
|
|
|
|
33
|
|
|
$service = new FeedReaderService($url, 3600, [ 'handler' => $handler ]); |
34
|
|
|
$service->clearCache(); |
35
|
|
|
|
36
|
|
|
// First read |
37
|
|
|
$items = $service->getItems(); |
38
|
|
|
|
39
|
|
|
$this->assertEquals(1, $items->count()); |
40
|
|
|
|
41
|
|
|
$item = $items[0]; |
42
|
|
|
$this->assertEquals('urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a', $item->Id); |
43
|
|
|
$this->assertEquals('http://example.org/2003/12/13/atom03', $item->Link); |
44
|
|
|
$this->assertEquals('2003-12-13 18:30:02', (string) $item->Date); |
45
|
|
|
$this->assertEquals('Atom-Powered Robots Run Amok', $item->Title); |
46
|
|
|
$this->assertEquals('Some text.', $item->Summary); |
47
|
|
|
$this->assertTrue(strlen($item->Content) > 20); |
48
|
|
|
|
49
|
|
|
// Second read: must still return the first one (cached) |
50
|
|
|
$items = $service->getItems(); |
51
|
|
|
$this->assertEquals(1, $items->count()); |
52
|
|
|
|
53
|
|
|
// Third read: must read the sample2.atom mocked response |
54
|
|
|
$service->clearCache(); |
55
|
|
|
$items = $service->getItems(); |
56
|
|
|
|
57
|
|
|
$this->assertEquals(3, $items->count()); |
58
|
|
|
|
59
|
|
|
$item = $items[0]; |
60
|
|
|
$this->assertEquals('http://example.org/1', $item->Id); |
61
|
|
|
$this->assertEquals('', $item->Link); |
62
|
|
|
$this->assertEquals('2017-12-19 11:22:33', (string) $item->Date); |
63
|
|
|
$this->assertEquals('Entry 1', $item->Title); |
64
|
|
|
$this->assertEquals('', $item->Summary); |
65
|
|
|
$this->assertEquals('', $item->Content); |
66
|
|
|
|
67
|
|
|
$item = $items[1]; |
68
|
|
|
$this->assertEquals('http://example.org/2', $item->Id); |
69
|
|
|
$this->assertEquals('http://example.org/link/2', $item->Link); |
70
|
|
|
$this->assertEquals('2017-12-19 22:33:44', (string) $item->Date); |
71
|
|
|
$this->assertEquals('Entry 2', $item->Title); |
72
|
|
|
$this->assertEquals('Summary 2', $item->Summary); |
73
|
|
|
$this->assertTrue(strlen($item->Content) > 5); |
74
|
|
|
|
75
|
|
|
$item = $items[2]; |
76
|
|
|
$this->assertEquals('http://example.org/3', $item->Id); |
77
|
|
|
$this->assertEquals('http://example.org/link/3', $item->Link); |
78
|
|
|
$this->assertEquals('2017-12-19 03:44:55', (string) $item->Date); |
79
|
|
|
$this->assertEquals('Entry 3', $item->Title); |
80
|
|
|
$this->assertEquals('Summary 3', $item->Summary); |
81
|
|
|
$this->assertEquals('Summary 3', $item->Content); |
82
|
|
|
|
83
|
|
|
// Forth read: must still return sample2.atom (cached) |
84
|
|
|
$items = $service->getItems(); |
85
|
|
|
$this->assertEquals(3, $items->count()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check the behavior of the RSS 2.0 parser. |
90
|
|
|
*/ |
91
|
|
|
public function testRSSParser() |
92
|
|
|
{ |
93
|
|
|
// Use a dummy (but valid) URI for the requests |
94
|
|
|
$url = 'http://localhost/'; |
95
|
|
|
|
96
|
|
|
// Create a mock and queue two responses |
97
|
|
|
$mock = new MockHandler([ |
98
|
|
|
new Response(200, [], file_get_contents(__DIR__ . '/sample1.rss')), |
99
|
|
|
new Response(200, [], file_get_contents(__DIR__ . '/sample2.rss')), |
100
|
|
|
]); |
101
|
|
|
$handler = HandlerStack::create($mock); |
102
|
|
|
|
103
|
|
|
$service = new FeedReaderService($url, 3600, [ 'handler' => $handler ]); |
104
|
|
|
$service->clearCache(); |
105
|
|
|
|
106
|
|
|
// First read |
107
|
|
|
$items = $service->getItems(); |
108
|
|
|
|
109
|
|
|
$this->assertEquals(1, $items->count()); |
110
|
|
|
|
111
|
|
|
$item = $items[0]; |
112
|
|
|
$this->assertEquals('7bd204c6-1655-4c27-aeee-53f933c5395f', $item->Id); |
113
|
|
|
$this->assertEquals('http://www.example.com/blog/post/1', $item->Link); |
114
|
|
|
$this->assertEquals('2009-09-06 16:20:00', (string) $item->Date); |
115
|
|
|
$this->assertEquals('Example entry', $item->Title); |
116
|
|
|
$this->assertEquals('Here is some text containing an interesting description.', $item->Summary); |
117
|
|
|
$this->assertEquals('Here is some text containing an interesting description.', $item->Content); |
118
|
|
|
|
119
|
|
|
// Second read: must still return the first one (cached) |
120
|
|
|
$items = $service->getItems(); |
121
|
|
|
$this->assertEquals(1, $items->count()); |
122
|
|
|
|
123
|
|
|
// Third read: must read the sample2.rss mocked response |
124
|
|
|
$service->clearCache(); |
125
|
|
|
$items = $service->getItems(); |
126
|
|
|
|
127
|
|
|
$this->assertEquals(3, $items->count()); |
128
|
|
|
|
129
|
|
|
$item = $items[0]; |
130
|
|
|
$this->assertEquals('http://example.org/1', $item->Id); |
131
|
|
|
$this->assertEquals('', $item->Link); |
132
|
|
|
$this->assertEquals('2017-12-19 11:22:33', (string) $item->Date); |
133
|
|
|
$this->assertEquals('Entry 1', $item->Title); |
134
|
|
|
$this->assertEquals('', $item->Summary); |
135
|
|
|
$this->assertEquals('', $item->Content); |
136
|
|
|
|
137
|
|
|
$item = $items[1]; |
138
|
|
|
$this->assertEquals('http://example.org/2', $item->Id); |
139
|
|
|
$this->assertEquals('http://example.org/link/2', $item->Link); |
140
|
|
|
$this->assertEquals('2017-12-19 22:33:44', (string) $item->Date); |
141
|
|
|
$this->assertEquals('Entry 2', $item->Title); |
142
|
|
|
$this->assertEquals('Content 2', $item->Summary); |
143
|
|
|
$this->assertEquals('Content 2', $item->Content); |
144
|
|
|
|
145
|
|
|
$item = $items[2]; |
146
|
|
|
$this->assertEquals('http://example.org/3', $item->Id); |
147
|
|
|
$this->assertEquals('http://example.org/link/3', $item->Link); |
148
|
|
|
$this->assertEquals('2017-12-19 03:44:55', (string) $item->Date); |
149
|
|
|
$this->assertEquals('Entry 3', $item->Title); |
150
|
|
|
$this->assertEquals('Summary 3', $item->Summary); |
151
|
|
|
$this->assertEquals('Summary 3', $item->Content); |
152
|
|
|
|
153
|
|
|
// Forth read: must still return sample2.rss (cached) |
154
|
|
|
$items = $service->getItems(); |
155
|
|
|
$this->assertEquals(3, $items->count()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Check the summary length feature for RSS2 feeds. |
160
|
|
|
*/ |
161
|
|
|
public function testSummaryLen() |
162
|
|
|
{ |
163
|
|
|
// Create a mock and queue the responses |
164
|
|
|
$mock = new MockHandler([ |
165
|
|
|
new Response(200, [], file_get_contents(__DIR__ . '/sample1.rss')), |
166
|
|
|
new Response(200, [], file_get_contents(__DIR__ . '/sample1.rss')), |
167
|
|
|
new Response(200, [], file_get_contents(__DIR__ . '/sample1.rss')), |
168
|
|
|
new Response(200, [], file_get_contents(__DIR__ . '/sample1.rss')), |
169
|
|
|
]); |
170
|
|
|
$handler = HandlerStack::create($mock); |
171
|
|
|
$service = new FeedReaderService('http://localhost/', 3600, [ 'handler' => $handler ]); |
172
|
|
|
$service->clearCache(); |
173
|
|
|
|
174
|
|
|
// Check the default summary length |
175
|
|
|
$this->assertEquals(155, $service->getSummaryLen()); |
176
|
|
|
$item = $service->getItems()[0]; |
177
|
|
|
$this->assertEquals('Here is some text containing an interesting description.', $item->Summary); |
178
|
|
|
|
179
|
|
|
// Try the ellipsisation algorithm |
180
|
|
|
$service->setSummaryLen(50); |
181
|
|
|
$this->assertEquals(50, $service->getSummaryLen()); |
182
|
|
|
$item = $service->getItems()[0]; |
183
|
|
|
$this->assertEquals('Here is some text containing an interesting description.', $item->Summary); |
184
|
|
|
|
185
|
|
|
// Without clearing the cache, the old item values are retained |
186
|
|
|
$service->clearCache(); |
187
|
|
|
$item = $service->getItems()[0]; |
188
|
|
|
$this->assertEquals('Here is some text containing an interesting...', $item->Summary); |
189
|
|
|
|
190
|
|
|
$service->setSummaryLen(30); |
191
|
|
|
$this->assertEquals(30, $service->getSummaryLen()); |
192
|
|
|
$service->clearCache(); |
193
|
|
|
$item = $service->getItems()[0]; |
194
|
|
|
$this->assertEquals('Here is some text...', $item->Summary); |
195
|
|
|
|
196
|
|
|
$service->setSummaryLen(31); |
197
|
|
|
$this->assertEquals(31, $service->getSummaryLen()); |
198
|
|
|
$service->clearCache(); |
199
|
|
|
$item = $service->getItems()[0]; |
200
|
|
|
$this->assertEquals('Here is some text containing...', $item->Summary); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|