scratch – Blame information for rev 87
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 1 | <?php |
2 | |||
3 | /* |
||
4 | * This file is part of the Symfony package. |
||
5 | * |
||
6 | * (c) Fabien Potencier <fabien@symfony.com> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Symfony\Component\DomCrawler\Tests; |
||
13 | |||
14 | use PHPUnit\Framework\TestCase; |
||
15 | use Symfony\Component\DomCrawler\Crawler; |
||
16 | |||
17 | class CrawlerTest extends TestCase |
||
18 | { |
||
19 | public function testConstructor() |
||
20 | { |
||
21 | $crawler = new Crawler(); |
||
22 | $this->assertCount(0, $crawler, '__construct() returns an empty crawler'); |
||
23 | |||
24 | $doc = new \DOMDocument(); |
||
25 | $node = $doc->createElement('test'); |
||
26 | |||
27 | $crawler = new Crawler($node); |
||
28 | $this->assertCount(1, $crawler, '__construct() takes a node as a first argument'); |
||
29 | } |
||
30 | |||
31 | public function testAdd() |
||
32 | { |
||
33 | $crawler = new Crawler(); |
||
34 | $crawler->add($this->createDomDocument()); |
||
35 | $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMDocument'); |
||
36 | |||
37 | $crawler = new Crawler(); |
||
38 | $crawler->add($this->createNodeList()); |
||
39 | $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMNodeList'); |
||
40 | |||
41 | $list = array(); |
||
42 | foreach ($this->createNodeList() as $node) { |
||
43 | $list[] = $node; |
||
44 | } |
||
45 | $crawler = new Crawler(); |
||
46 | $crawler->add($list); |
||
47 | $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from an array of nodes'); |
||
48 | |||
49 | $crawler = new Crawler(); |
||
50 | $crawler->add($this->createNodeList()->item(0)); |
||
51 | $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMNode'); |
||
52 | |||
53 | $crawler = new Crawler(); |
||
54 | $crawler->add('<html><body>Foo</body></html>'); |
||
55 | $this->assertEquals('Foo', $crawler->filterXPath('//body')->text(), '->add() adds nodes from a string'); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @expectedException \InvalidArgumentException |
||
60 | */ |
||
61 | public function testAddInvalidType() |
||
62 | { |
||
63 | $crawler = new Crawler(); |
||
64 | $crawler->add(1); |
||
65 | } |
||
66 | |||
67 | public function testAddHtmlContent() |
||
68 | { |
||
69 | $crawler = new Crawler(); |
||
70 | $crawler->addHtmlContent('<html><div class="foo"></html>', 'UTF-8'); |
||
71 | |||
72 | $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addHtmlContent() adds nodes from an HTML string'); |
||
73 | } |
||
74 | |||
75 | public function testAddHtmlContentWithBaseTag() |
||
76 | { |
||
77 | $crawler = new Crawler(); |
||
78 | |||
79 | $crawler->addHtmlContent('<html><head><base href="http://symfony.com"></head><a href="/contact"></a></html>', 'UTF-8'); |
||
80 | |||
81 | $this->assertEquals('http://symfony.com', $crawler->filterXPath('//base')->attr('href'), '->addHtmlContent() adds nodes from an HTML string'); |
||
82 | $this->assertEquals('http://symfony.com/contact', $crawler->filterXPath('//a')->link()->getUri(), '->addHtmlContent() adds nodes from an HTML string'); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @requires extension mbstring |
||
87 | */ |
||
88 | public function testAddHtmlContentCharset() |
||
89 | { |
||
90 | $crawler = new Crawler(); |
||
91 | $crawler->addHtmlContent('<html><div class="foo">Tiếng Việt</html>', 'UTF-8'); |
||
92 | |||
93 | $this->assertEquals('Tiếng Việt', $crawler->filterXPath('//div')->text()); |
||
94 | } |
||
95 | |||
96 | public function testAddHtmlContentInvalidBaseTag() |
||
97 | { |
||
98 | $crawler = new Crawler(null, 'http://symfony.com'); |
||
99 | |||
100 | $crawler->addHtmlContent('<html><head><base target="_top"></head><a href="/contact"></a></html>', 'UTF-8'); |
||
101 | |||
102 | $this->assertEquals('http://symfony.com/contact', current($crawler->filterXPath('//a')->links())->getUri(), '->addHtmlContent() correctly handles a non-existent base tag href attribute'); |
||
103 | } |
||
104 | |||
105 | public function testAddHtmlContentUnsupportedCharset() |
||
106 | { |
||
107 | $crawler = new Crawler(); |
||
108 | $crawler->addHtmlContent(file_get_contents(__DIR__.'/Fixtures/windows-1250.html'), 'Windows-1250'); |
||
109 | |||
110 | $this->assertEquals('Žťčýů', $crawler->filterXPath('//p')->text()); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @requires extension mbstring |
||
115 | */ |
||
116 | public function testAddHtmlContentCharsetGbk() |
||
117 | { |
||
118 | $crawler = new Crawler(); |
||
119 | //gbk encode of <html><p>中文</p></html> |
||
120 | $crawler->addHtmlContent(base64_decode('PGh0bWw+PHA+1tDOxDwvcD48L2h0bWw+'), 'gbk'); |
||
121 | |||
122 | $this->assertEquals('中文', $crawler->filterXPath('//p')->text()); |
||
123 | } |
||
124 | |||
125 | public function testAddHtmlContentWithErrors() |
||
126 | { |
||
127 | $internalErrors = libxml_use_internal_errors(true); |
||
128 | |||
129 | $crawler = new Crawler(); |
||
130 | $crawler->addHtmlContent(<<<'EOF' |
||
131 | <!DOCTYPE html> |
||
132 | <html> |
||
133 | <head> |
||
134 | </head> |
||
135 | <body> |
||
136 | <nav><a href="#"><a href="#"></nav> |
||
137 | </body> |
||
138 | </html> |
||
139 | EOF |
||
140 | , 'UTF-8'); |
||
141 | |||
142 | $errors = libxml_get_errors(); |
||
143 | $this->assertCount(1, $errors); |
||
144 | $this->assertEquals("Tag nav invalid\n", $errors[0]->message); |
||
145 | |||
146 | libxml_clear_errors(); |
||
147 | libxml_use_internal_errors($internalErrors); |
||
148 | } |
||
149 | |||
150 | public function testAddXmlContent() |
||
151 | { |
||
152 | $crawler = new Crawler(); |
||
153 | $crawler->addXmlContent('<html><div class="foo"></div></html>', 'UTF-8'); |
||
154 | |||
155 | $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addXmlContent() adds nodes from an XML string'); |
||
156 | } |
||
157 | |||
158 | public function testAddXmlContentCharset() |
||
159 | { |
||
160 | $crawler = new Crawler(); |
||
161 | $crawler->addXmlContent('<html><div class="foo">Tiếng Việt</div></html>', 'UTF-8'); |
||
162 | |||
163 | $this->assertEquals('Tiếng Việt', $crawler->filterXPath('//div')->text()); |
||
164 | } |
||
165 | |||
166 | public function testAddXmlContentWithErrors() |
||
167 | { |
||
168 | $internalErrors = libxml_use_internal_errors(true); |
||
169 | |||
170 | $crawler = new Crawler(); |
||
171 | $crawler->addXmlContent(<<<'EOF' |
||
172 | <!DOCTYPE html> |
||
173 | <html> |
||
174 | <head> |
||
175 | </head> |
||
176 | <body> |
||
177 | <nav><a href="#"><a href="#"></nav> |
||
178 | </body> |
||
179 | </html> |
||
180 | EOF |
||
181 | , 'UTF-8'); |
||
182 | |||
183 | $this->assertTrue(count(libxml_get_errors()) > 1); |
||
184 | |||
185 | libxml_clear_errors(); |
||
186 | libxml_use_internal_errors($internalErrors); |
||
187 | } |
||
188 | |||
189 | public function testAddContent() |
||
190 | { |
||
191 | $crawler = new Crawler(); |
||
192 | $crawler->addContent('<html><div class="foo"></html>', 'text/html; charset=UTF-8'); |
||
193 | $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an HTML string'); |
||
194 | |||
195 | $crawler = new Crawler(); |
||
196 | $crawler->addContent('<html><div class="foo"></html>', 'text/html; charset=UTF-8; dir=RTL'); |
||
197 | $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an HTML string with extended content type'); |
||
198 | |||
199 | $crawler = new Crawler(); |
||
200 | $crawler->addContent('<html><div class="foo"></html>'); |
||
201 | $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() uses text/html as the default type'); |
||
202 | |||
203 | $crawler = new Crawler(); |
||
204 | $crawler->addContent('<html><div class="foo"></div></html>', 'text/xml; charset=UTF-8'); |
||
205 | $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an XML string'); |
||
206 | |||
207 | $crawler = new Crawler(); |
||
208 | $crawler->addContent('<html><div class="foo"></div></html>', 'text/xml'); |
||
209 | $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an XML string'); |
||
210 | |||
211 | $crawler = new Crawler(); |
||
212 | $crawler->addContent('foo bar', 'text/plain'); |
||
213 | $this->assertCount(0, $crawler, '->addContent() does nothing if the type is not (x|ht)ml'); |
||
214 | |||
215 | $crawler = new Crawler(); |
||
216 | $crawler->addContent('<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><span>中文</span></html>'); |
||
217 | $this->assertEquals('中文', $crawler->filterXPath('//span')->text(), '->addContent() guess wrong charset'); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @requires extension iconv |
||
222 | */ |
||
223 | public function testAddContentNonUtf8() |
||
224 | { |
||
225 | $crawler = new Crawler(); |
||
226 | $crawler->addContent(iconv('UTF-8', 'SJIS', '<html><head><meta charset="Shift_JIS"></head><body>日本語</body></html>')); |
||
227 | $this->assertEquals('日本語', $crawler->filterXPath('//body')->text(), '->addContent() can recognize "Shift_JIS" in html5 meta charset tag'); |
||
228 | } |
||
229 | |||
230 | public function testAddDocument() |
||
231 | { |
||
232 | $crawler = new Crawler(); |
||
233 | $crawler->addDocument($this->createDomDocument()); |
||
234 | |||
235 | $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addDocument() adds nodes from a \DOMDocument'); |
||
236 | } |
||
237 | |||
238 | public function testAddNodeList() |
||
239 | { |
||
240 | $crawler = new Crawler(); |
||
241 | $crawler->addNodeList($this->createNodeList()); |
||
242 | |||
243 | $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNodeList() adds nodes from a \DOMNodeList'); |
||
244 | } |
||
245 | |||
246 | public function testAddNodes() |
||
247 | { |
||
248 | $list = array(); |
||
249 | foreach ($this->createNodeList() as $node) { |
||
250 | $list[] = $node; |
||
251 | } |
||
252 | |||
253 | $crawler = new Crawler(); |
||
254 | $crawler->addNodes($list); |
||
255 | |||
256 | $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNodes() adds nodes from an array of nodes'); |
||
257 | } |
||
258 | |||
259 | public function testAddNode() |
||
260 | { |
||
261 | $crawler = new Crawler(); |
||
262 | $crawler->addNode($this->createNodeList()->item(0)); |
||
263 | |||
264 | $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNode() adds nodes from a \DOMNode'); |
||
265 | } |
||
266 | |||
267 | public function testClear() |
||
268 | { |
||
269 | $doc = new \DOMDocument(); |
||
270 | $node = $doc->createElement('test'); |
||
271 | |||
272 | $crawler = new Crawler($node); |
||
273 | $crawler->clear(); |
||
274 | $this->assertCount(0, $crawler, '->clear() removes all the nodes from the crawler'); |
||
275 | } |
||
276 | |||
277 | public function testEq() |
||
278 | { |
||
279 | $crawler = $this->createTestCrawler()->filterXPath('//li'); |
||
280 | $this->assertNotSame($crawler, $crawler->eq(0), '->eq() returns a new instance of a crawler'); |
||
281 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->eq() returns a new instance of a crawler'); |
||
282 | |||
283 | $this->assertEquals('Two', $crawler->eq(1)->text(), '->eq() returns the nth node of the list'); |
||
284 | $this->assertCount(0, $crawler->eq(100), '->eq() returns an empty crawler if the nth node does not exist'); |
||
285 | } |
||
286 | |||
287 | public function testEach() |
||
288 | { |
||
289 | $data = $this->createTestCrawler()->filterXPath('//ul[1]/li')->each(function ($node, $i) { |
||
290 | return $i.'-'.$node->text(); |
||
291 | }); |
||
292 | |||
293 | $this->assertEquals(array('0-One', '1-Two', '2-Three'), $data, '->each() executes an anonymous function on each node of the list'); |
||
294 | } |
||
295 | |||
296 | public function testIteration() |
||
297 | { |
||
298 | $crawler = $this->createTestCrawler()->filterXPath('//li'); |
||
299 | |||
300 | $this->assertInstanceOf('Traversable', $crawler); |
||
301 | $this->assertContainsOnlyInstancesOf('DOMElement', iterator_to_array($crawler), 'Iterating a Crawler gives DOMElement instances'); |
||
302 | } |
||
303 | |||
304 | public function testSlice() |
||
305 | { |
||
306 | $crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li'); |
||
307 | $this->assertNotSame($crawler->slice(), $crawler, '->slice() returns a new instance of a crawler'); |
||
308 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler->slice(), '->slice() returns a new instance of a crawler'); |
||
309 | |||
310 | $this->assertCount(3, $crawler->slice(), '->slice() does not slice the nodes in the list if any param is entered'); |
||
311 | $this->assertCount(1, $crawler->slice(1, 1), '->slice() slices the nodes in the list'); |
||
312 | } |
||
313 | |||
314 | public function testReduce() |
||
315 | { |
||
316 | $crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li'); |
||
317 | $nodes = $crawler->reduce(function ($node, $i) { |
||
318 | return $i !== 1; |
||
319 | }); |
||
320 | $this->assertNotSame($nodes, $crawler, '->reduce() returns a new instance of a crawler'); |
||
321 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $nodes, '->reduce() returns a new instance of a crawler'); |
||
322 | |||
323 | $this->assertCount(2, $nodes, '->reduce() filters the nodes in the list'); |
||
324 | } |
||
325 | |||
326 | public function testAttr() |
||
327 | { |
||
328 | $this->assertEquals('first', $this->createTestCrawler()->filterXPath('//li')->attr('class'), '->attr() returns the attribute of the first element of the node list'); |
||
329 | |||
330 | try { |
||
331 | $this->createTestCrawler()->filterXPath('//ol')->attr('class'); |
||
332 | $this->fail('->attr() throws an \InvalidArgumentException if the node list is empty'); |
||
333 | } catch (\InvalidArgumentException $e) { |
||
334 | $this->assertTrue(true, '->attr() throws an \InvalidArgumentException if the node list is empty'); |
||
335 | } |
||
336 | } |
||
337 | |||
338 | public function testMissingAttrValueIsNull() |
||
339 | { |
||
340 | $crawler = new Crawler(); |
||
341 | $crawler->addContent('<html><div non-empty-attr="sample value" empty-attr=""></div></html>', 'text/html; charset=UTF-8'); |
||
342 | $div = $crawler->filterXPath('//div'); |
||
343 | |||
344 | $this->assertEquals('sample value', $div->attr('non-empty-attr'), '->attr() reads non-empty attributes correctly'); |
||
345 | $this->assertEquals('', $div->attr('empty-attr'), '->attr() reads empty attributes correctly'); |
||
346 | $this->assertNull($div->attr('missing-attr'), '->attr() reads missing attributes correctly'); |
||
347 | } |
||
348 | |||
349 | public function testNodeName() |
||
350 | { |
||
351 | $this->assertEquals('li', $this->createTestCrawler()->filterXPath('//li')->nodeName(), '->nodeName() returns the node name of the first element of the node list'); |
||
352 | |||
353 | try { |
||
354 | $this->createTestCrawler()->filterXPath('//ol')->nodeName(); |
||
355 | $this->fail('->nodeName() throws an \InvalidArgumentException if the node list is empty'); |
||
356 | } catch (\InvalidArgumentException $e) { |
||
357 | $this->assertTrue(true, '->nodeName() throws an \InvalidArgumentException if the node list is empty'); |
||
358 | } |
||
359 | } |
||
360 | |||
361 | public function testText() |
||
362 | { |
||
363 | $this->assertEquals('One', $this->createTestCrawler()->filterXPath('//li')->text(), '->text() returns the node value of the first element of the node list'); |
||
364 | |||
365 | try { |
||
366 | $this->createTestCrawler()->filterXPath('//ol')->text(); |
||
367 | $this->fail('->text() throws an \InvalidArgumentException if the node list is empty'); |
||
368 | } catch (\InvalidArgumentException $e) { |
||
369 | $this->assertTrue(true, '->text() throws an \InvalidArgumentException if the node list is empty'); |
||
370 | } |
||
371 | } |
||
372 | |||
373 | public function testHtml() |
||
374 | { |
||
375 | $this->assertEquals('<img alt="Bar">', $this->createTestCrawler()->filterXPath('//a[5]')->html()); |
||
376 | $this->assertEquals('<input type="text" value="TextValue" name="TextName"><input type="submit" value="FooValue" name="FooName" id="FooId"><input type="button" value="BarValue" name="BarName" id="BarId"><button value="ButtonValue" name="ButtonName" id="ButtonId"></button>', trim($this->createTestCrawler()->filterXPath('//form[@id="FooFormId"]')->html())); |
||
377 | |||
378 | try { |
||
379 | $this->createTestCrawler()->filterXPath('//ol')->html(); |
||
380 | $this->fail('->html() throws an \InvalidArgumentException if the node list is empty'); |
||
381 | } catch (\InvalidArgumentException $e) { |
||
382 | $this->assertTrue(true, '->html() throws an \InvalidArgumentException if the node list is empty'); |
||
383 | } |
||
384 | } |
||
385 | |||
386 | public function testExtract() |
||
387 | { |
||
388 | $crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li'); |
||
389 | |||
390 | $this->assertEquals(array('One', 'Two', 'Three'), $crawler->extract('_text'), '->extract() returns an array of extracted data from the node list'); |
||
391 | $this->assertEquals(array(array('One', 'first'), array('Two', ''), array('Three', '')), $crawler->extract(array('_text', 'class')), '->extract() returns an array of extracted data from the node list'); |
||
392 | |||
393 | $this->assertEquals(array(), $this->createTestCrawler()->filterXPath('//ol')->extract('_text'), '->extract() returns an empty array if the node list is empty'); |
||
394 | } |
||
395 | |||
396 | public function testFilterXpathComplexQueries() |
||
397 | { |
||
398 | $crawler = $this->createTestCrawler()->filterXPath('//body'); |
||
399 | |||
400 | $this->assertCount(0, $crawler->filterXPath('/input')); |
||
401 | $this->assertCount(0, $crawler->filterXPath('/body')); |
||
402 | $this->assertCount(1, $crawler->filterXPath('./body')); |
||
403 | $this->assertCount(1, $crawler->filterXPath('.//body')); |
||
404 | $this->assertCount(5, $crawler->filterXPath('.//input')); |
||
405 | $this->assertCount(4, $crawler->filterXPath('//form')->filterXPath('//button | //input')); |
||
406 | $this->assertCount(1, $crawler->filterXPath('body')); |
||
407 | $this->assertCount(6, $crawler->filterXPath('//button | //input')); |
||
408 | $this->assertCount(1, $crawler->filterXPath('//body')); |
||
409 | $this->assertCount(1, $crawler->filterXPath('descendant-or-self::body')); |
||
410 | $this->assertCount(1, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('./div'), 'A child selection finds only the current div'); |
||
411 | $this->assertCount(3, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('descendant::div'), 'A descendant selector matches the current div and its child'); |
||
412 | $this->assertCount(3, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('//div'), 'A descendant selector matches the current div and its child'); |
||
413 | $this->assertCount(5, $crawler->filterXPath('(//a | //div)//img')); |
||
414 | $this->assertCount(7, $crawler->filterXPath('((//a | //div)//img | //ul)')); |
||
415 | $this->assertCount(7, $crawler->filterXPath('( ( //a | //div )//img | //ul )')); |
||
416 | $this->assertCount(1, $crawler->filterXPath("//a[./@href][((./@id = 'Klausi|Claudiu' or normalize-space(string(.)) = 'Klausi|Claudiu' or ./@title = 'Klausi|Claudiu' or ./@rel = 'Klausi|Claudiu') or .//img[./@alt = 'Klausi|Claudiu'])]")); |
||
417 | } |
||
418 | |||
419 | public function testFilterXPath() |
||
420 | { |
||
421 | $crawler = $this->createTestCrawler(); |
||
422 | $this->assertNotSame($crawler, $crawler->filterXPath('//li'), '->filterXPath() returns a new instance of a crawler'); |
||
423 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->filterXPath() returns a new instance of a crawler'); |
||
424 | |||
425 | $crawler = $this->createTestCrawler()->filterXPath('//ul'); |
||
426 | $this->assertCount(6, $crawler->filterXPath('//li'), '->filterXPath() filters the node list with the XPath expression'); |
||
427 | |||
428 | $crawler = $this->createTestCrawler(); |
||
429 | $this->assertCount(3, $crawler->filterXPath('//body')->filterXPath('//button')->parents(), '->filterXpath() preserves parents when chained'); |
||
430 | } |
||
431 | |||
432 | public function testFilterRemovesDuplicates() |
||
433 | { |
||
434 | $crawler = $this->createTestCrawler()->filter('html, body')->filter('li'); |
||
435 | $this->assertCount(6, $crawler, 'The crawler removes duplicates when filtering.'); |
||
436 | } |
||
437 | |||
438 | public function testFilterXPathWithDefaultNamespace() |
||
439 | { |
||
440 | $crawler = $this->createTestXmlCrawler()->filterXPath('//default:entry/default:id'); |
||
441 | $this->assertCount(1, $crawler, '->filterXPath() automatically registers a namespace'); |
||
442 | $this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text()); |
||
443 | } |
||
444 | |||
445 | public function testFilterXPathWithCustomDefaultNamespace() |
||
446 | { |
||
447 | $crawler = $this->createTestXmlCrawler(); |
||
448 | $crawler->setDefaultNamespacePrefix('x'); |
||
449 | $crawler = $crawler->filterXPath('//x:entry/x:id'); |
||
450 | |||
451 | $this->assertCount(1, $crawler, '->filterXPath() lets to override the default namespace prefix'); |
||
452 | $this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text()); |
||
453 | } |
||
454 | |||
455 | public function testFilterXPathWithNamespace() |
||
456 | { |
||
457 | $crawler = $this->createTestXmlCrawler()->filterXPath('//yt:accessControl'); |
||
458 | $this->assertCount(2, $crawler, '->filterXPath() automatically registers a namespace'); |
||
459 | } |
||
460 | |||
461 | public function testFilterXPathWithMultipleNamespaces() |
||
462 | { |
||
463 | $crawler = $this->createTestXmlCrawler()->filterXPath('//media:group/yt:aspectRatio'); |
||
464 | $this->assertCount(1, $crawler, '->filterXPath() automatically registers multiple namespaces'); |
||
465 | $this->assertSame('widescreen', $crawler->text()); |
||
466 | } |
||
467 | |||
468 | public function testFilterXPathWithManuallyRegisteredNamespace() |
||
469 | { |
||
470 | $crawler = $this->createTestXmlCrawler(); |
||
471 | $crawler->registerNamespace('m', 'http://search.yahoo.com/mrss/'); |
||
472 | |||
473 | $crawler = $crawler->filterXPath('//m:group/yt:aspectRatio'); |
||
474 | $this->assertCount(1, $crawler, '->filterXPath() uses manually registered namespace'); |
||
475 | $this->assertSame('widescreen', $crawler->text()); |
||
476 | } |
||
477 | |||
478 | public function testFilterXPathWithAnUrl() |
||
479 | { |
||
480 | $crawler = $this->createTestXmlCrawler(); |
||
481 | |||
482 | $crawler = $crawler->filterXPath('//media:category[@scheme="http://gdata.youtube.com/schemas/2007/categories.cat"]'); |
||
483 | $this->assertCount(1, $crawler); |
||
484 | $this->assertSame('Music', $crawler->text()); |
||
485 | } |
||
486 | |||
487 | public function testFilterXPathWithFakeRoot() |
||
488 | { |
||
489 | $crawler = $this->createTestCrawler(); |
||
490 | $this->assertCount(0, $crawler->filterXPath('.'), '->filterXPath() returns an empty result if the XPath references the fake root node'); |
||
491 | $this->assertCount(0, $crawler->filterXPath('self::*'), '->filterXPath() returns an empty result if the XPath references the fake root node'); |
||
492 | $this->assertCount(0, $crawler->filterXPath('self::_root'), '->filterXPath() returns an empty result if the XPath references the fake root node'); |
||
493 | } |
||
494 | |||
495 | /** @group legacy */ |
||
496 | public function testLegacyFilterXPathWithFakeRoot() |
||
497 | { |
||
498 | $crawler = $this->createTestCrawler(); |
||
499 | $this->assertCount(0, $crawler->filterXPath('/_root'), '->filterXPath() returns an empty result if the XPath references the fake root node'); |
||
500 | |||
501 | $crawler = $this->createTestCrawler()->filterXPath('//body'); |
||
502 | $this->assertCount(1, $crawler->filterXPath('/_root/body')); |
||
503 | } |
||
504 | |||
505 | public function testFilterXPathWithAncestorAxis() |
||
506 | { |
||
507 | $crawler = $this->createTestCrawler()->filterXPath('//form'); |
||
508 | |||
509 | $this->assertCount(0, $crawler->filterXPath('ancestor::*'), 'The fake root node has no ancestor nodes'); |
||
510 | } |
||
511 | |||
512 | public function testFilterXPathWithAncestorOrSelfAxis() |
||
513 | { |
||
514 | $crawler = $this->createTestCrawler()->filterXPath('//form'); |
||
515 | |||
516 | $this->assertCount(0, $crawler->filterXPath('ancestor-or-self::*'), 'The fake root node has no ancestor nodes'); |
||
517 | } |
||
518 | |||
519 | public function testFilterXPathWithAttributeAxis() |
||
520 | { |
||
521 | $crawler = $this->createTestCrawler()->filterXPath('//form'); |
||
522 | |||
523 | $this->assertCount(0, $crawler->filterXPath('attribute::*'), 'The fake root node has no attribute nodes'); |
||
524 | } |
||
525 | |||
526 | public function testFilterXPathWithAttributeAxisAfterElementAxis() |
||
527 | { |
||
528 | $this->assertCount(3, $this->createTestCrawler()->filterXPath('//form/button/attribute::*'), '->filterXPath() handles attribute axes properly when they are preceded by an element filtering axis'); |
||
529 | } |
||
530 | |||
531 | public function testFilterXPathWithChildAxis() |
||
532 | { |
||
533 | $crawler = $this->createTestCrawler()->filterXPath('//div[@id="parent"]'); |
||
534 | |||
535 | $this->assertCount(1, $crawler->filterXPath('child::div'), 'A child selection finds only the current div'); |
||
536 | } |
||
537 | |||
538 | public function testFilterXPathWithFollowingAxis() |
||
539 | { |
||
540 | $crawler = $this->createTestCrawler()->filterXPath('//a'); |
||
541 | |||
542 | $this->assertCount(0, $crawler->filterXPath('following::div'), 'The fake root node has no following nodes'); |
||
543 | } |
||
544 | |||
545 | public function testFilterXPathWithFollowingSiblingAxis() |
||
546 | { |
||
547 | $crawler = $this->createTestCrawler()->filterXPath('//a'); |
||
548 | |||
549 | $this->assertCount(0, $crawler->filterXPath('following-sibling::div'), 'The fake root node has no following nodes'); |
||
550 | } |
||
551 | |||
552 | public function testFilterXPathWithNamespaceAxis() |
||
553 | { |
||
554 | $crawler = $this->createTestCrawler()->filterXPath('//button'); |
||
555 | |||
556 | $this->assertCount(0, $crawler->filterXPath('namespace::*'), 'The fake root node has no namespace nodes'); |
||
557 | } |
||
558 | |||
559 | public function testFilterXPathWithNamespaceAxisAfterElementAxis() |
||
560 | { |
||
561 | $crawler = $this->createTestCrawler()->filterXPath('//div[@id="parent"]/namespace::*'); |
||
562 | |||
563 | $this->assertCount(0, $crawler->filterXPath('namespace::*'), 'Namespace axes cannot be requested'); |
||
564 | } |
||
565 | |||
566 | public function testFilterXPathWithParentAxis() |
||
567 | { |
||
568 | $crawler = $this->createTestCrawler()->filterXPath('//button'); |
||
569 | |||
570 | $this->assertCount(0, $crawler->filterXPath('parent::*'), 'The fake root node has no parent nodes'); |
||
571 | } |
||
572 | |||
573 | public function testFilterXPathWithPrecedingAxis() |
||
574 | { |
||
575 | $crawler = $this->createTestCrawler()->filterXPath('//form'); |
||
576 | |||
577 | $this->assertCount(0, $crawler->filterXPath('preceding::*'), 'The fake root node has no preceding nodes'); |
||
578 | } |
||
579 | |||
580 | public function testFilterXPathWithPrecedingSiblingAxis() |
||
581 | { |
||
582 | $crawler = $this->createTestCrawler()->filterXPath('//form'); |
||
583 | |||
584 | $this->assertCount(0, $crawler->filterXPath('preceding-sibling::*'), 'The fake root node has no preceding nodes'); |
||
585 | } |
||
586 | |||
587 | public function testFilterXPathWithSelfAxes() |
||
588 | { |
||
589 | $crawler = $this->createTestCrawler()->filterXPath('//a'); |
||
590 | |||
591 | $this->assertCount(0, $crawler->filterXPath('self::a'), 'The fake root node has no "real" element name'); |
||
592 | $this->assertCount(0, $crawler->filterXPath('self::a/img'), 'The fake root node has no "real" element name'); |
||
593 | $this->assertCount(10, $crawler->filterXPath('self::*/a')); |
||
594 | } |
||
595 | |||
596 | public function testFilter() |
||
597 | { |
||
598 | $crawler = $this->createTestCrawler(); |
||
599 | $this->assertNotSame($crawler, $crawler->filter('li'), '->filter() returns a new instance of a crawler'); |
||
600 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->filter() returns a new instance of a crawler'); |
||
601 | |||
602 | $crawler = $this->createTestCrawler()->filter('ul'); |
||
603 | |||
604 | $this->assertCount(6, $crawler->filter('li'), '->filter() filters the node list with the CSS selector'); |
||
605 | } |
||
606 | |||
607 | public function testFilterWithDefaultNamespace() |
||
608 | { |
||
609 | $crawler = $this->createTestXmlCrawler()->filter('default|entry default|id'); |
||
610 | $this->assertCount(1, $crawler, '->filter() automatically registers namespaces'); |
||
611 | $this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text()); |
||
612 | } |
||
613 | |||
614 | public function testFilterWithNamespace() |
||
615 | { |
||
616 | $crawler = $this->createTestXmlCrawler()->filter('yt|accessControl'); |
||
617 | $this->assertCount(2, $crawler, '->filter() automatically registers namespaces'); |
||
618 | } |
||
619 | |||
620 | public function testFilterWithMultipleNamespaces() |
||
621 | { |
||
622 | $crawler = $this->createTestXmlCrawler()->filter('media|group yt|aspectRatio'); |
||
623 | $this->assertCount(1, $crawler, '->filter() automatically registers namespaces'); |
||
624 | $this->assertSame('widescreen', $crawler->text()); |
||
625 | } |
||
626 | |||
627 | public function testFilterWithDefaultNamespaceOnly() |
||
628 | { |
||
629 | $crawler = new Crawler('<?xml version="1.0" encoding="UTF-8"?> |
||
630 | <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
||
631 | <url> |
||
632 | <loc>http://localhost/foo</loc> |
||
633 | <changefreq>weekly</changefreq> |
||
634 | <priority>0.5</priority> |
||
635 | <lastmod>2012-11-16</lastmod> |
||
636 | </url> |
||
637 | <url> |
||
638 | <loc>http://localhost/bar</loc> |
||
639 | <changefreq>weekly</changefreq> |
||
640 | <priority>0.5</priority> |
||
641 | <lastmod>2012-11-16</lastmod> |
||
642 | </url> |
||
643 | </urlset> |
||
644 | '); |
||
645 | |||
646 | $this->assertEquals(2, $crawler->filter('url')->count()); |
||
647 | } |
||
648 | |||
649 | public function testSelectLink() |
||
650 | { |
||
651 | $crawler = $this->createTestCrawler(); |
||
652 | $this->assertNotSame($crawler, $crawler->selectLink('Foo'), '->selectLink() returns a new instance of a crawler'); |
||
653 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->selectLink() returns a new instance of a crawler'); |
||
654 | |||
655 | $this->assertCount(1, $crawler->selectLink('Fabien\'s Foo'), '->selectLink() selects links by the node values'); |
||
656 | $this->assertCount(1, $crawler->selectLink('Fabien\'s Bar'), '->selectLink() selects links by the alt attribute of a clickable image'); |
||
657 | |||
658 | $this->assertCount(2, $crawler->selectLink('Fabien"s Foo'), '->selectLink() selects links by the node values'); |
||
659 | $this->assertCount(2, $crawler->selectLink('Fabien"s Bar'), '->selectLink() selects links by the alt attribute of a clickable image'); |
||
660 | |||
661 | $this->assertCount(1, $crawler->selectLink('\' Fabien"s Foo'), '->selectLink() selects links by the node values'); |
||
662 | $this->assertCount(1, $crawler->selectLink('\' Fabien"s Bar'), '->selectLink() selects links by the alt attribute of a clickable image'); |
||
663 | |||
664 | $this->assertCount(4, $crawler->selectLink('Foo'), '->selectLink() selects links by the node values'); |
||
665 | $this->assertCount(4, $crawler->selectLink('Bar'), '->selectLink() selects links by the node values'); |
||
666 | } |
||
667 | |||
668 | public function testSelectButton() |
||
669 | { |
||
670 | $crawler = $this->createTestCrawler(); |
||
671 | $this->assertNotSame($crawler, $crawler->selectButton('FooValue'), '->selectButton() returns a new instance of a crawler'); |
||
672 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->selectButton() returns a new instance of a crawler'); |
||
673 | |||
674 | $this->assertEquals(1, $crawler->selectButton('FooValue')->count(), '->selectButton() selects buttons'); |
||
675 | $this->assertEquals(1, $crawler->selectButton('FooName')->count(), '->selectButton() selects buttons'); |
||
676 | $this->assertEquals(1, $crawler->selectButton('FooId')->count(), '->selectButton() selects buttons'); |
||
677 | |||
678 | $this->assertEquals(1, $crawler->selectButton('BarValue')->count(), '->selectButton() selects buttons'); |
||
679 | $this->assertEquals(1, $crawler->selectButton('BarName')->count(), '->selectButton() selects buttons'); |
||
680 | $this->assertEquals(1, $crawler->selectButton('BarId')->count(), '->selectButton() selects buttons'); |
||
681 | |||
682 | $this->assertEquals(1, $crawler->selectButton('FooBarValue')->count(), '->selectButton() selects buttons with form attribute too'); |
||
683 | $this->assertEquals(1, $crawler->selectButton('FooBarName')->count(), '->selectButton() selects buttons with form attribute too'); |
||
684 | } |
||
685 | |||
686 | public function testSelectButtonWithSingleQuotesInNameAttribute() |
||
687 | { |
||
688 | $html = <<<'HTML' |
||
689 | <!DOCTYPE html> |
||
690 | <html lang="en"> |
||
691 | <body> |
||
692 | <div id="action"> |
||
693 | <a href="/index.php?r=site/login">Login</a> |
||
694 | </div> |
||
695 | <form id="login-form" action="/index.php?r=site/login" method="post"> |
||
696 | <button type="submit" name="Click 'Here'">Submit</button> |
||
697 | </form> |
||
698 | </body> |
||
699 | </html> |
||
700 | HTML; |
||
701 | |||
702 | $crawler = new Crawler($html); |
||
703 | |||
704 | $this->assertCount(1, $crawler->selectButton('Click \'Here\'')); |
||
705 | } |
||
706 | |||
707 | public function testSelectButtonWithDoubleQuotesInNameAttribute() |
||
708 | { |
||
709 | $html = <<<'HTML' |
||
710 | <!DOCTYPE html> |
||
711 | <html lang="en"> |
||
712 | <body> |
||
713 | <div id="action"> |
||
714 | <a href="/index.php?r=site/login">Login</a> |
||
715 | </div> |
||
716 | <form id="login-form" action="/index.php?r=site/login" method="post"> |
||
717 | <button type="submit" name='Click "Here"'>Submit</button> |
||
718 | </form> |
||
719 | </body> |
||
720 | </html> |
||
721 | HTML; |
||
722 | |||
723 | $crawler = new Crawler($html); |
||
724 | |||
725 | $this->assertCount(1, $crawler->selectButton('Click "Here"')); |
||
726 | } |
||
727 | |||
728 | public function testLink() |
||
729 | { |
||
730 | $crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo'); |
||
731 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Link', $crawler->link(), '->link() returns a Link instance'); |
||
732 | |||
733 | $this->assertEquals('POST', $crawler->link('post')->getMethod(), '->link() takes a method as its argument'); |
||
734 | |||
735 | $crawler = $this->createTestCrawler('http://example.com/bar')->selectLink('GetLink'); |
||
736 | $this->assertEquals('http://example.com/bar?get=param', $crawler->link()->getUri(), '->link() returns a Link instance'); |
||
737 | |||
738 | try { |
||
739 | $this->createTestCrawler()->filterXPath('//ol')->link(); |
||
740 | $this->fail('->link() throws an \InvalidArgumentException if the node list is empty'); |
||
741 | } catch (\InvalidArgumentException $e) { |
||
742 | $this->assertTrue(true, '->link() throws an \InvalidArgumentException if the node list is empty'); |
||
743 | } |
||
744 | } |
||
745 | |||
746 | /** |
||
747 | * @expectedException \InvalidArgumentException |
||
748 | * @expectedExceptionMessage The selected node should be instance of DOMElement |
||
749 | */ |
||
750 | public function testInvalidLink() |
||
751 | { |
||
752 | $crawler = $this->createTestCrawler('http://example.com/bar/'); |
||
753 | $crawler->filterXPath('//li/text()')->link(); |
||
754 | } |
||
755 | |||
756 | /** |
||
757 | * @expectedException \InvalidArgumentException |
||
758 | * @expectedExceptionMessage The selected node should be instance of DOMElement |
||
759 | */ |
||
760 | public function testInvalidLinks() |
||
761 | { |
||
762 | $crawler = $this->createTestCrawler('http://example.com/bar/'); |
||
763 | $crawler->filterXPath('//li/text()')->link(); |
||
764 | } |
||
765 | |||
766 | public function testSelectLinkAndLinkFiltered() |
||
767 | { |
||
768 | $html = <<<'HTML' |
||
769 | <!DOCTYPE html> |
||
770 | <html lang="en"> |
||
771 | <body> |
||
772 | <div id="action"> |
||
773 | <a href="/index.php?r=site/login">Login</a> |
||
774 | </div> |
||
775 | <form id="login-form" action="/index.php?r=site/login" method="post"> |
||
776 | <button type="submit">Submit</button> |
||
777 | </form> |
||
778 | </body> |
||
779 | </html> |
||
780 | HTML; |
||
781 | |||
782 | $crawler = new Crawler($html); |
||
783 | $filtered = $crawler->filterXPath("descendant-or-self::*[@id = 'login-form']"); |
||
784 | |||
785 | $this->assertCount(0, $filtered->selectLink('Login')); |
||
786 | $this->assertCount(1, $filtered->selectButton('Submit')); |
||
787 | |||
788 | $filtered = $crawler->filterXPath("descendant-or-self::*[@id = 'action']"); |
||
789 | |||
790 | $this->assertCount(1, $filtered->selectLink('Login')); |
||
791 | $this->assertCount(0, $filtered->selectButton('Submit')); |
||
792 | |||
793 | $this->assertCount(1, $crawler->selectLink('Login')->selectLink('Login')); |
||
794 | $this->assertCount(1, $crawler->selectButton('Submit')->selectButton('Submit')); |
||
795 | } |
||
796 | |||
797 | public function testChaining() |
||
798 | { |
||
799 | $crawler = new Crawler('<div name="a"><div name="b"><div name="c"></div></div></div>'); |
||
800 | |||
801 | $this->assertEquals('a', $crawler->filterXPath('//div')->filterXPath('div')->filterXPath('div')->attr('name')); |
||
802 | } |
||
803 | |||
804 | public function testLinks() |
||
805 | { |
||
806 | $crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo'); |
||
807 | $this->assertInternalType('array', $crawler->links(), '->links() returns an array'); |
||
808 | |||
809 | $this->assertCount(4, $crawler->links(), '->links() returns an array'); |
||
810 | $links = $crawler->links(); |
||
811 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Link', $links[0], '->links() returns an array of Link instances'); |
||
812 | |||
813 | $this->assertEquals(array(), $this->createTestCrawler()->filterXPath('//ol')->links(), '->links() returns an empty array if the node selection is empty'); |
||
814 | } |
||
815 | |||
816 | public function testForm() |
||
817 | { |
||
818 | $testCrawler = $this->createTestCrawler('http://example.com/bar/'); |
||
819 | $crawler = $testCrawler->selectButton('FooValue'); |
||
820 | $crawler2 = $testCrawler->selectButton('FooBarValue'); |
||
821 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Form', $crawler->form(), '->form() returns a Form instance'); |
||
822 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Form', $crawler2->form(), '->form() returns a Form instance'); |
||
823 | |||
824 | $this->assertEquals($crawler->form()->getFormNode()->getAttribute('id'), $crawler2->form()->getFormNode()->getAttribute('id'), '->form() works on elements with form attribute'); |
||
825 | |||
826 | $this->assertEquals(array('FooName' => 'FooBar', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler->form(array('FooName' => 'FooBar'))->getValues(), '->form() takes an array of values to submit as its first argument'); |
||
827 | $this->assertEquals(array('FooName' => 'FooValue', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler->form()->getValues(), '->getValues() returns correct form values'); |
||
828 | $this->assertEquals(array('FooBarName' => 'FooBarValue', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler2->form()->getValues(), '->getValues() returns correct form values'); |
||
829 | |||
830 | try { |
||
831 | $this->createTestCrawler()->filterXPath('//ol')->form(); |
||
832 | $this->fail('->form() throws an \InvalidArgumentException if the node list is empty'); |
||
833 | } catch (\InvalidArgumentException $e) { |
||
834 | $this->assertTrue(true, '->form() throws an \InvalidArgumentException if the node list is empty'); |
||
835 | } |
||
836 | } |
||
837 | |||
838 | /** |
||
839 | * @expectedException \InvalidArgumentException |
||
840 | * @expectedExceptionMessage The selected node should be instance of DOMElement |
||
841 | */ |
||
842 | public function testInvalidForm() |
||
843 | { |
||
844 | $crawler = $this->createTestCrawler('http://example.com/bar/'); |
||
845 | $crawler->filterXPath('//li/text()')->form(); |
||
846 | } |
||
847 | |||
848 | public function testLast() |
||
849 | { |
||
850 | $crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li'); |
||
851 | $this->assertNotSame($crawler, $crawler->last(), '->last() returns a new instance of a crawler'); |
||
852 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->last() returns a new instance of a crawler'); |
||
853 | |||
854 | $this->assertEquals('Three', $crawler->last()->text()); |
||
855 | } |
||
856 | |||
857 | public function testFirst() |
||
858 | { |
||
859 | $crawler = $this->createTestCrawler()->filterXPath('//li'); |
||
860 | $this->assertNotSame($crawler, $crawler->first(), '->first() returns a new instance of a crawler'); |
||
861 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->first() returns a new instance of a crawler'); |
||
862 | |||
863 | $this->assertEquals('One', $crawler->first()->text()); |
||
864 | } |
||
865 | |||
866 | public function testSiblings() |
||
867 | { |
||
868 | $crawler = $this->createTestCrawler()->filterXPath('//li')->eq(1); |
||
869 | $this->assertNotSame($crawler, $crawler->siblings(), '->siblings() returns a new instance of a crawler'); |
||
870 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->siblings() returns a new instance of a crawler'); |
||
871 | |||
872 | $nodes = $crawler->siblings(); |
||
873 | $this->assertEquals(2, $nodes->count()); |
||
874 | $this->assertEquals('One', $nodes->eq(0)->text()); |
||
875 | $this->assertEquals('Three', $nodes->eq(1)->text()); |
||
876 | |||
877 | $nodes = $this->createTestCrawler()->filterXPath('//li')->eq(0)->siblings(); |
||
878 | $this->assertEquals(2, $nodes->count()); |
||
879 | $this->assertEquals('Two', $nodes->eq(0)->text()); |
||
880 | $this->assertEquals('Three', $nodes->eq(1)->text()); |
||
881 | |||
882 | try { |
||
883 | $this->createTestCrawler()->filterXPath('//ol')->siblings(); |
||
884 | $this->fail('->siblings() throws an \InvalidArgumentException if the node list is empty'); |
||
885 | } catch (\InvalidArgumentException $e) { |
||
886 | $this->assertTrue(true, '->siblings() throws an \InvalidArgumentException if the node list is empty'); |
||
887 | } |
||
888 | } |
||
889 | |||
890 | public function testNextAll() |
||
891 | { |
||
892 | $crawler = $this->createTestCrawler()->filterXPath('//li')->eq(1); |
||
893 | $this->assertNotSame($crawler, $crawler->nextAll(), '->nextAll() returns a new instance of a crawler'); |
||
894 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->nextAll() returns a new instance of a crawler'); |
||
895 | |||
896 | $nodes = $crawler->nextAll(); |
||
897 | $this->assertEquals(1, $nodes->count()); |
||
898 | $this->assertEquals('Three', $nodes->eq(0)->text()); |
||
899 | |||
900 | try { |
||
901 | $this->createTestCrawler()->filterXPath('//ol')->nextAll(); |
||
902 | $this->fail('->nextAll() throws an \InvalidArgumentException if the node list is empty'); |
||
903 | } catch (\InvalidArgumentException $e) { |
||
904 | $this->assertTrue(true, '->nextAll() throws an \InvalidArgumentException if the node list is empty'); |
||
905 | } |
||
906 | } |
||
907 | |||
908 | public function testPreviousAll() |
||
909 | { |
||
910 | $crawler = $this->createTestCrawler()->filterXPath('//li')->eq(2); |
||
911 | $this->assertNotSame($crawler, $crawler->previousAll(), '->previousAll() returns a new instance of a crawler'); |
||
912 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->previousAll() returns a new instance of a crawler'); |
||
913 | |||
914 | $nodes = $crawler->previousAll(); |
||
915 | $this->assertEquals(2, $nodes->count()); |
||
916 | $this->assertEquals('Two', $nodes->eq(0)->text()); |
||
917 | |||
918 | try { |
||
919 | $this->createTestCrawler()->filterXPath('//ol')->previousAll(); |
||
920 | $this->fail('->previousAll() throws an \InvalidArgumentException if the node list is empty'); |
||
921 | } catch (\InvalidArgumentException $e) { |
||
922 | $this->assertTrue(true, '->previousAll() throws an \InvalidArgumentException if the node list is empty'); |
||
923 | } |
||
924 | } |
||
925 | |||
926 | public function testChildren() |
||
927 | { |
||
928 | $crawler = $this->createTestCrawler()->filterXPath('//ul'); |
||
929 | $this->assertNotSame($crawler, $crawler->children(), '->children() returns a new instance of a crawler'); |
||
930 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->children() returns a new instance of a crawler'); |
||
931 | |||
932 | $nodes = $crawler->children(); |
||
933 | $this->assertEquals(3, $nodes->count()); |
||
934 | $this->assertEquals('One', $nodes->eq(0)->text()); |
||
935 | $this->assertEquals('Two', $nodes->eq(1)->text()); |
||
936 | $this->assertEquals('Three', $nodes->eq(2)->text()); |
||
937 | |||
938 | try { |
||
939 | $this->createTestCrawler()->filterXPath('//ol')->children(); |
||
940 | $this->fail('->children() throws an \InvalidArgumentException if the node list is empty'); |
||
941 | } catch (\InvalidArgumentException $e) { |
||
942 | $this->assertTrue(true, '->children() throws an \InvalidArgumentException if the node list is empty'); |
||
943 | } |
||
944 | |||
945 | try { |
||
946 | $crawler = new Crawler('<p></p>'); |
||
947 | $crawler->filter('p')->children(); |
||
948 | $this->assertTrue(true, '->children() does not trigger a notice if the node has no children'); |
||
949 | } catch (\PHPUnit\Framework\Error\Notice $e) { |
||
950 | $this->fail('->children() does not trigger a notice if the node has no children'); |
||
951 | } catch (\PHPUnit_Framework_Error_Notice $e) { |
||
952 | $this->fail('->children() does not trigger a notice if the node has no children'); |
||
953 | } |
||
954 | } |
||
955 | |||
956 | public function testParents() |
||
957 | { |
||
958 | $crawler = $this->createTestCrawler()->filterXPath('//li[1]'); |
||
959 | $this->assertNotSame($crawler, $crawler->parents(), '->parents() returns a new instance of a crawler'); |
||
960 | $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->parents() returns a new instance of a crawler'); |
||
961 | |||
962 | $nodes = $crawler->parents(); |
||
963 | $this->assertEquals(3, $nodes->count()); |
||
964 | |||
965 | $nodes = $this->createTestCrawler()->filterXPath('//html')->parents(); |
||
966 | $this->assertEquals(0, $nodes->count()); |
||
967 | |||
968 | try { |
||
969 | $this->createTestCrawler()->filterXPath('//ol')->parents(); |
||
970 | $this->fail('->parents() throws an \InvalidArgumentException if the node list is empty'); |
||
971 | } catch (\InvalidArgumentException $e) { |
||
972 | $this->assertTrue(true, '->parents() throws an \InvalidArgumentException if the node list is empty'); |
||
973 | } |
||
974 | } |
||
975 | |||
976 | /** |
||
977 | * @dataProvider getBaseTagData |
||
978 | */ |
||
979 | public function testBaseTag($baseValue, $linkValue, $expectedUri, $currentUri = null, $description = null) |
||
980 | { |
||
981 | $crawler = new Crawler('<html><base href="'.$baseValue.'"><a href="'.$linkValue.'"></a></html>', $currentUri); |
||
982 | $this->assertEquals($expectedUri, $crawler->filterXPath('//a')->link()->getUri(), $description); |
||
983 | } |
||
984 | |||
985 | public function getBaseTagData() |
||
986 | { |
||
987 | return array( |
||
988 | array('http://base.com', 'link', 'http://base.com/link'), |
||
989 | array('//base.com', 'link', 'https://base.com/link', 'https://domain.com', '<base> tag can use a schema-less URL'), |
||
990 | array('path/', 'link', 'https://domain.com/path/link', 'https://domain.com', '<base> tag can set a path'), |
||
991 | array('http://base.com', '#', 'http://base.com#', 'http://domain.com/path/link', '<base> tag does work with links to an anchor'), |
||
992 | array('http://base.com', '', 'http://base.com', 'http://domain.com/path/link', '<base> tag does work with empty links'), |
||
993 | ); |
||
994 | } |
||
995 | |||
996 | /** |
||
997 | * @dataProvider getBaseTagWithFormData |
||
998 | */ |
||
999 | public function testBaseTagWithForm($baseValue, $actionValue, $expectedUri, $currentUri = null, $description = null) |
||
1000 | { |
||
1001 | $crawler = new Crawler('<html><base href="'.$baseValue.'"><form method="post" action="'.$actionValue.'"><button type="submit" name="submit"/></form></html>', $currentUri); |
||
1002 | $this->assertEquals($expectedUri, $crawler->filterXPath('//button')->form()->getUri(), $description); |
||
1003 | } |
||
1004 | |||
1005 | public function getBaseTagWithFormData() |
||
1006 | { |
||
1007 | return array( |
||
1008 | array('https://base.com/', 'link/', 'https://base.com/link/', 'https://base.com/link/', '<base> tag does work with a path and relative form action'), |
||
1009 | array('/basepath', '/registration', 'http://domain.com/registration', 'http://domain.com/registration', '<base> tag does work with a path and form action'), |
||
1010 | array('/basepath', '', 'http://domain.com/registration', 'http://domain.com/registration', '<base> tag does work with a path and empty form action'), |
||
1011 | array('http://base.com/', '/registration', 'http://base.com/registration', 'http://domain.com/registration', '<base> tag does work with a URL and form action'), |
||
1012 | array('http://base.com', '', 'http://domain.com/path/form', 'http://domain.com/path/form', '<base> tag does work with a URL and an empty form action'), |
||
1013 | array('http://base.com/path', '/registration', 'http://base.com/registration', 'http://domain.com/path/form', '<base> tag does work with a URL and form action'), |
||
1014 | ); |
||
1015 | } |
||
1016 | |||
1017 | public function testCountOfNestedElements() |
||
1018 | { |
||
1019 | $crawler = new Crawler('<html><body><ul><li>List item 1<ul><li>Sublist item 1</li><li>Sublist item 2</ul></li></ul></body></html>'); |
||
1020 | |||
1021 | $this->assertCount(1, $crawler->filter('li:contains("List item 1")')); |
||
1022 | } |
||
1023 | |||
1024 | public function createTestCrawler($uri = null) |
||
1025 | { |
||
1026 | $dom = new \DOMDocument(); |
||
1027 | $dom->loadHTML(' |
||
1028 | <html> |
||
1029 | <body> |
||
1030 | <a href="foo">Foo</a> |
||
1031 | <a href="/foo"> Fabien\'s Foo </a> |
||
1032 | <a href="/foo">Fabien"s Foo</a> |
||
1033 | <a href="/foo">\' Fabien"s Foo</a> |
||
1034 | |||
1035 | <a href="/bar"><img alt="Bar"/></a> |
||
1036 | <a href="/bar"><img alt=" Fabien\'s Bar "/></a> |
||
1037 | <a href="/bar"><img alt="Fabien"s Bar"/></a> |
||
1038 | <a href="/bar"><img alt="\' Fabien"s Bar"/></a> |
||
1039 | |||
1040 | <a href="?get=param">GetLink</a> |
||
1041 | |||
1042 | <a href="/example">Klausi|Claudiu</a> |
||
1043 | |||
1044 | <form action="foo" id="FooFormId"> |
||
1045 | <input type="text" value="TextValue" name="TextName" /> |
||
1046 | <input type="submit" value="FooValue" name="FooName" id="FooId" /> |
||
1047 | <input type="button" value="BarValue" name="BarName" id="BarId" /> |
||
1048 | <button value="ButtonValue" name="ButtonName" id="ButtonId" /> |
||
1049 | </form> |
||
1050 | |||
1051 | <input type="submit" value="FooBarValue" name="FooBarName" form="FooFormId" /> |
||
1052 | <input type="text" value="FooTextValue" name="FooTextName" form="FooFormId" /> |
||
1053 | |||
1054 | <ul class="first"> |
||
1055 | <li class="first">One</li> |
||
1056 | <li>Two</li> |
||
1057 | <li>Three</li> |
||
1058 | </ul> |
||
1059 | <ul> |
||
1060 | <li>One Bis</li> |
||
1061 | <li>Two Bis</li> |
||
1062 | <li>Three Bis</li> |
||
1063 | </ul> |
||
1064 | <div id="parent"> |
||
1065 | <div id="child"></div> |
||
1066 | <div id="child2" xmlns:foo="http://example.com"></div> |
||
1067 | </div> |
||
1068 | <div id="sibling"><img /></div> |
||
1069 | </body> |
||
1070 | </html> |
||
1071 | '); |
||
1072 | |||
1073 | return new Crawler($dom, $uri); |
||
1074 | } |
||
1075 | |||
1076 | protected function createTestXmlCrawler($uri = null) |
||
1077 | { |
||
1078 | $xml = '<?xml version="1.0" encoding="UTF-8"?> |
||
1079 | <entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007"> |
||
1080 | <id>tag:youtube.com,2008:video:kgZRZmEc9j4</id> |
||
1081 | <yt:accessControl action="comment" permission="allowed"/> |
||
1082 | <yt:accessControl action="videoRespond" permission="moderated"/> |
||
1083 | <media:group> |
||
1084 | <media:title type="plain">Chordates - CrashCourse Biology #24</media:title> |
||
1085 | <yt:aspectRatio>widescreen</yt:aspectRatio> |
||
1086 | </media:group> |
||
1087 | <media:category label="Music" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Music</media:category> |
||
1088 | </entry>'; |
||
1089 | |||
1090 | return new Crawler($xml, $uri); |
||
1091 | } |
||
1092 | |||
1093 | protected function createDomDocument() |
||
1094 | { |
||
1095 | $dom = new \DOMDocument(); |
||
1096 | $dom->loadXML('<html><div class="foo"></div></html>'); |
||
1097 | |||
1098 | return $dom; |
||
1099 | } |
||
1100 | |||
1101 | protected function createNodeList() |
||
1102 | { |
||
1103 | $dom = new \DOMDocument(); |
||
1104 | $dom->loadXML('<html><div class="foo"></div></html>'); |
||
1105 | $domxpath = new \DOMXPath($dom); |
||
1106 | |||
1107 | return $domxpath->query('//div'); |
||
1108 | } |
||
1109 | } |