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