a8dc525e55d2f7770d47d007a3df981725b1329e
[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->assertGreaterThan(1, libxml_get_errors());
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 1 !== $i;
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(preg_replace('~>\s+<~', '><', $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         $this->assertEquals(array(array(), array(), array()), $crawler->extract(array()), '->extract() returns empty arrays if the attribute list is empty');
417
418         $this->assertEquals(array(), $this->createTestCrawler()->filterXPath('//ol')->extract('_text'), '->extract() returns an empty array if the node list is empty');
419     }
420
421     public function testFilterXpathComplexQueries()
422     {
423         $crawler = $this->createTestCrawler()->filterXPath('//body');
424
425         $this->assertCount(0, $crawler->filterXPath('/input'));
426         $this->assertCount(0, $crawler->filterXPath('/body'));
427         $this->assertCount(1, $crawler->filterXPath('./body'));
428         $this->assertCount(1, $crawler->filterXPath('.//body'));
429         $this->assertCount(5, $crawler->filterXPath('.//input'));
430         $this->assertCount(4, $crawler->filterXPath('//form')->filterXPath('//button | //input'));
431         $this->assertCount(1, $crawler->filterXPath('body'));
432         $this->assertCount(6, $crawler->filterXPath('//button | //input'));
433         $this->assertCount(1, $crawler->filterXPath('//body'));
434         $this->assertCount(1, $crawler->filterXPath('descendant-or-self::body'));
435         $this->assertCount(1, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('./div'), 'A child selection finds only the current div');
436         $this->assertCount(3, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('descendant::div'), 'A descendant selector matches the current div and its child');
437         $this->assertCount(3, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('//div'), 'A descendant selector matches the current div and its child');
438         $this->assertCount(5, $crawler->filterXPath('(//a | //div)//img'));
439         $this->assertCount(7, $crawler->filterXPath('((//a | //div)//img | //ul)'));
440         $this->assertCount(7, $crawler->filterXPath('( ( //a | //div )//img | //ul )'));
441         $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'])]"));
442     }
443
444     public function testFilterXPath()
445     {
446         $crawler = $this->createTestCrawler();
447         $this->assertNotSame($crawler, $crawler->filterXPath('//li'), '->filterXPath() returns a new instance of a crawler');
448         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->filterXPath() returns a new instance of a crawler');
449
450         $crawler = $this->createTestCrawler()->filterXPath('//ul');
451         $this->assertCount(6, $crawler->filterXPath('//li'), '->filterXPath() filters the node list with the XPath expression');
452
453         $crawler = $this->createTestCrawler();
454         $this->assertCount(3, $crawler->filterXPath('//body')->filterXPath('//button')->parents(), '->filterXpath() preserves parents when chained');
455     }
456
457     public function testFilterRemovesDuplicates()
458     {
459         $crawler = $this->createTestCrawler()->filter('html, body')->filter('li');
460         $this->assertCount(6, $crawler, 'The crawler removes duplicates when filtering.');
461     }
462
463     public function testFilterXPathWithDefaultNamespace()
464     {
465         $crawler = $this->createTestXmlCrawler()->filterXPath('//default:entry/default:id');
466         $this->assertCount(1, $crawler, '->filterXPath() automatically registers a namespace');
467         $this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text());
468     }
469
470     public function testFilterXPathWithCustomDefaultNamespace()
471     {
472         $crawler = $this->createTestXmlCrawler();
473         $crawler->setDefaultNamespacePrefix('x');
474         $crawler = $crawler->filterXPath('//x:entry/x:id');
475
476         $this->assertCount(1, $crawler, '->filterXPath() lets to override the default namespace prefix');
477         $this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text());
478     }
479
480     public function testFilterXPathWithNamespace()
481     {
482         $crawler = $this->createTestXmlCrawler()->filterXPath('//yt:accessControl');
483         $this->assertCount(2, $crawler, '->filterXPath() automatically registers a namespace');
484     }
485
486     public function testFilterXPathWithMultipleNamespaces()
487     {
488         $crawler = $this->createTestXmlCrawler()->filterXPath('//media:group/yt:aspectRatio');
489         $this->assertCount(1, $crawler, '->filterXPath() automatically registers multiple namespaces');
490         $this->assertSame('widescreen', $crawler->text());
491     }
492
493     public function testFilterXPathWithManuallyRegisteredNamespace()
494     {
495         $crawler = $this->createTestXmlCrawler();
496         $crawler->registerNamespace('m', 'http://search.yahoo.com/mrss/');
497
498         $crawler = $crawler->filterXPath('//m:group/yt:aspectRatio');
499         $this->assertCount(1, $crawler, '->filterXPath() uses manually registered namespace');
500         $this->assertSame('widescreen', $crawler->text());
501     }
502
503     public function testFilterXPathWithAnUrl()
504     {
505         $crawler = $this->createTestXmlCrawler();
506
507         $crawler = $crawler->filterXPath('//media:category[@scheme="http://gdata.youtube.com/schemas/2007/categories.cat"]');
508         $this->assertCount(1, $crawler);
509         $this->assertSame('Music', $crawler->text());
510     }
511
512     public function testFilterXPathWithFakeRoot()
513     {
514         $crawler = $this->createTestCrawler();
515         $this->assertCount(0, $crawler->filterXPath('.'), '->filterXPath() returns an empty result if the XPath references the fake root node');
516         $this->assertCount(0, $crawler->filterXPath('self::*'), '->filterXPath() returns an empty result if the XPath references the fake root node');
517         $this->assertCount(0, $crawler->filterXPath('self::_root'), '->filterXPath() returns an empty result if the XPath references the fake root node');
518     }
519
520     public function testFilterXPathWithAncestorAxis()
521     {
522         $crawler = $this->createTestCrawler()->filterXPath('//form');
523
524         $this->assertCount(0, $crawler->filterXPath('ancestor::*'), 'The fake root node has no ancestor nodes');
525     }
526
527     public function testFilterXPathWithAncestorOrSelfAxis()
528     {
529         $crawler = $this->createTestCrawler()->filterXPath('//form');
530
531         $this->assertCount(0, $crawler->filterXPath('ancestor-or-self::*'), 'The fake root node has no ancestor nodes');
532     }
533
534     public function testFilterXPathWithAttributeAxis()
535     {
536         $crawler = $this->createTestCrawler()->filterXPath('//form');
537
538         $this->assertCount(0, $crawler->filterXPath('attribute::*'), 'The fake root node has no attribute nodes');
539     }
540
541     public function testFilterXPathWithAttributeAxisAfterElementAxis()
542     {
543         $this->assertCount(3, $this->createTestCrawler()->filterXPath('//form/button/attribute::*'), '->filterXPath() handles attribute axes properly when they are preceded by an element filtering axis');
544     }
545
546     public function testFilterXPathWithChildAxis()
547     {
548         $crawler = $this->createTestCrawler()->filterXPath('//div[@id="parent"]');
549
550         $this->assertCount(1, $crawler->filterXPath('child::div'), 'A child selection finds only the current div');
551     }
552
553     public function testFilterXPathWithFollowingAxis()
554     {
555         $crawler = $this->createTestCrawler()->filterXPath('//a');
556
557         $this->assertCount(0, $crawler->filterXPath('following::div'), 'The fake root node has no following nodes');
558     }
559
560     public function testFilterXPathWithFollowingSiblingAxis()
561     {
562         $crawler = $this->createTestCrawler()->filterXPath('//a');
563
564         $this->assertCount(0, $crawler->filterXPath('following-sibling::div'), 'The fake root node has no following nodes');
565     }
566
567     public function testFilterXPathWithNamespaceAxis()
568     {
569         $crawler = $this->createTestCrawler()->filterXPath('//button');
570
571         $this->assertCount(0, $crawler->filterXPath('namespace::*'), 'The fake root node has no namespace nodes');
572     }
573
574     public function testFilterXPathWithNamespaceAxisAfterElementAxis()
575     {
576         $crawler = $this->createTestCrawler()->filterXPath('//div[@id="parent"]/namespace::*');
577
578         $this->assertCount(0, $crawler->filterXPath('namespace::*'), 'Namespace axes cannot be requested');
579     }
580
581     public function testFilterXPathWithParentAxis()
582     {
583         $crawler = $this->createTestCrawler()->filterXPath('//button');
584
585         $this->assertCount(0, $crawler->filterXPath('parent::*'), 'The fake root node has no parent nodes');
586     }
587
588     public function testFilterXPathWithPrecedingAxis()
589     {
590         $crawler = $this->createTestCrawler()->filterXPath('//form');
591
592         $this->assertCount(0, $crawler->filterXPath('preceding::*'), 'The fake root node has no preceding nodes');
593     }
594
595     public function testFilterXPathWithPrecedingSiblingAxis()
596     {
597         $crawler = $this->createTestCrawler()->filterXPath('//form');
598
599         $this->assertCount(0, $crawler->filterXPath('preceding-sibling::*'), 'The fake root node has no preceding nodes');
600     }
601
602     public function testFilterXPathWithSelfAxes()
603     {
604         $crawler = $this->createTestCrawler()->filterXPath('//a');
605
606         $this->assertCount(0, $crawler->filterXPath('self::a'), 'The fake root node has no "real" element name');
607         $this->assertCount(0, $crawler->filterXPath('self::a/img'), 'The fake root node has no "real" element name');
608         $this->assertCount(10, $crawler->filterXPath('self::*/a'));
609     }
610
611     public function testFilter()
612     {
613         $crawler = $this->createTestCrawler();
614         $this->assertNotSame($crawler, $crawler->filter('li'), '->filter() returns a new instance of a crawler');
615         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->filter() returns a new instance of a crawler');
616
617         $crawler = $this->createTestCrawler()->filter('ul');
618
619         $this->assertCount(6, $crawler->filter('li'), '->filter() filters the node list with the CSS selector');
620     }
621
622     public function testFilterWithDefaultNamespace()
623     {
624         $crawler = $this->createTestXmlCrawler()->filter('default|entry default|id');
625         $this->assertCount(1, $crawler, '->filter() automatically registers namespaces');
626         $this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text());
627     }
628
629     public function testFilterWithNamespace()
630     {
631         $crawler = $this->createTestXmlCrawler()->filter('yt|accessControl');
632         $this->assertCount(2, $crawler, '->filter() automatically registers namespaces');
633     }
634
635     public function testFilterWithMultipleNamespaces()
636     {
637         $crawler = $this->createTestXmlCrawler()->filter('media|group yt|aspectRatio');
638         $this->assertCount(1, $crawler, '->filter() automatically registers namespaces');
639         $this->assertSame('widescreen', $crawler->text());
640     }
641
642     public function testFilterWithDefaultNamespaceOnly()
643     {
644         $crawler = new Crawler('<?xml version="1.0" encoding="UTF-8"?>
645             <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
646                 <url>
647                     <loc>http://localhost/foo</loc>
648                     <changefreq>weekly</changefreq>
649                     <priority>0.5</priority>
650                     <lastmod>2012-11-16</lastmod>
651                </url>
652                <url>
653                     <loc>http://localhost/bar</loc>
654                     <changefreq>weekly</changefreq>
655                     <priority>0.5</priority>
656                     <lastmod>2012-11-16</lastmod>
657                 </url>
658             </urlset>
659         ');
660
661         $this->assertEquals(2, $crawler->filter('url')->count());
662     }
663
664     public function testSelectLink()
665     {
666         $crawler = $this->createTestCrawler();
667         $this->assertNotSame($crawler, $crawler->selectLink('Foo'), '->selectLink() returns a new instance of a crawler');
668         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->selectLink() returns a new instance of a crawler');
669
670         $this->assertCount(1, $crawler->selectLink('Fabien\'s Foo'), '->selectLink() selects links by the node values');
671         $this->assertCount(1, $crawler->selectLink('Fabien\'s Bar'), '->selectLink() selects links by the alt attribute of a clickable image');
672
673         $this->assertCount(2, $crawler->selectLink('Fabien"s Foo'), '->selectLink() selects links by the node values');
674         $this->assertCount(2, $crawler->selectLink('Fabien"s Bar'), '->selectLink() selects links by the alt attribute of a clickable image');
675
676         $this->assertCount(1, $crawler->selectLink('\' Fabien"s Foo'), '->selectLink() selects links by the node values');
677         $this->assertCount(1, $crawler->selectLink('\' Fabien"s Bar'), '->selectLink() selects links by the alt attribute of a clickable image');
678
679         $this->assertCount(4, $crawler->selectLink('Foo'), '->selectLink() selects links by the node values');
680         $this->assertCount(4, $crawler->selectLink('Bar'), '->selectLink() selects links by the node values');
681     }
682
683     public function testSelectImage()
684     {
685         $crawler = $this->createTestCrawler();
686         $this->assertNotSame($crawler, $crawler->selectImage('Bar'), '->selectImage() returns a new instance of a crawler');
687         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->selectImage() returns a new instance of a crawler');
688
689         $this->assertCount(1, $crawler->selectImage('Fabien\'s Bar'), '->selectImage() selects images by alt attribute');
690         $this->assertCount(2, $crawler->selectImage('Fabien"s Bar'), '->selectImage() selects images by alt attribute');
691         $this->assertCount(1, $crawler->selectImage('\' Fabien"s Bar'), '->selectImage() selects images by alt attribute');
692     }
693
694     public function testSelectButton()
695     {
696         $crawler = $this->createTestCrawler();
697         $this->assertNotSame($crawler, $crawler->selectButton('FooValue'), '->selectButton() returns a new instance of a crawler');
698         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->selectButton() returns a new instance of a crawler');
699
700         $this->assertEquals(1, $crawler->selectButton('FooValue')->count(), '->selectButton() selects buttons');
701         $this->assertEquals(1, $crawler->selectButton('FooName')->count(), '->selectButton() selects buttons');
702         $this->assertEquals(1, $crawler->selectButton('FooId')->count(), '->selectButton() selects buttons');
703
704         $this->assertEquals(1, $crawler->selectButton('BarValue')->count(), '->selectButton() selects buttons');
705         $this->assertEquals(1, $crawler->selectButton('BarName')->count(), '->selectButton() selects buttons');
706         $this->assertEquals(1, $crawler->selectButton('BarId')->count(), '->selectButton() selects buttons');
707
708         $this->assertEquals(1, $crawler->selectButton('FooBarValue')->count(), '->selectButton() selects buttons with form attribute too');
709         $this->assertEquals(1, $crawler->selectButton('FooBarName')->count(), '->selectButton() selects buttons with form attribute too');
710     }
711
712     public function testSelectButtonWithSingleQuotesInNameAttribute()
713     {
714         $html = <<<'HTML'
715 <!DOCTYPE html>
716 <html lang="en">
717 <body>
718     <div id="action">
719         <a href="/index.php?r=site/login">Login</a>
720     </div>
721     <form id="login-form" action="/index.php?r=site/login" method="post">
722         <button type="submit" name="Click 'Here'">Submit</button>
723     </form>
724 </body>
725 </html>
726 HTML;
727
728         $crawler = new Crawler($html);
729
730         $this->assertCount(1, $crawler->selectButton('Click \'Here\''));
731     }
732
733     public function testSelectButtonWithDoubleQuotesInNameAttribute()
734     {
735         $html = <<<'HTML'
736 <!DOCTYPE html>
737 <html lang="en">
738 <body>
739     <div id="action">
740         <a href="/index.php?r=site/login">Login</a>
741     </div>
742     <form id="login-form" action="/index.php?r=site/login" method="post">
743         <button type="submit" name='Click "Here"'>Submit</button>
744     </form>
745 </body>
746 </html>
747 HTML;
748
749         $crawler = new Crawler($html);
750
751         $this->assertCount(1, $crawler->selectButton('Click "Here"'));
752     }
753
754     public function testLink()
755     {
756         $crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo');
757         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Link', $crawler->link(), '->link() returns a Link instance');
758
759         $this->assertEquals('POST', $crawler->link('post')->getMethod(), '->link() takes a method as its argument');
760
761         $crawler = $this->createTestCrawler('http://example.com/bar')->selectLink('GetLink');
762         $this->assertEquals('http://example.com/bar?get=param', $crawler->link()->getUri(), '->link() returns a Link instance');
763
764         try {
765             $this->createTestCrawler()->filterXPath('//ol')->link();
766             $this->fail('->link() throws an \InvalidArgumentException if the node list is empty');
767         } catch (\InvalidArgumentException $e) {
768             $this->assertTrue(true, '->link() throws an \InvalidArgumentException if the node list is empty');
769         }
770     }
771
772     /**
773      * @expectedException \InvalidArgumentException
774      * @expectedExceptionMessage The selected node should be instance of DOMElement
775      */
776     public function testInvalidLink()
777     {
778         $crawler = $this->createTestCrawler('http://example.com/bar/');
779         $crawler->filterXPath('//li/text()')->link();
780     }
781
782     /**
783      * @expectedException \InvalidArgumentException
784      * @expectedExceptionMessage The selected node should be instance of DOMElement
785      */
786     public function testInvalidLinks()
787     {
788         $crawler = $this->createTestCrawler('http://example.com/bar/');
789         $crawler->filterXPath('//li/text()')->link();
790     }
791
792     public function testImage()
793     {
794         $crawler = $this->createTestCrawler('http://example.com/bar/')->selectImage('Bar');
795         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Image', $crawler->image(), '->image() returns an Image instance');
796
797         try {
798             $this->createTestCrawler()->filterXPath('//ol')->image();
799             $this->fail('->image() throws an \InvalidArgumentException if the node list is empty');
800         } catch (\InvalidArgumentException $e) {
801             $this->assertTrue(true, '->image() throws an \InvalidArgumentException if the node list is empty');
802         }
803     }
804
805     public function testSelectLinkAndLinkFiltered()
806     {
807         $html = <<<'HTML'
808 <!DOCTYPE html>
809 <html lang="en">
810 <body>
811     <div id="action">
812         <a href="/index.php?r=site/login">Login</a>
813     </div>
814     <form id="login-form" action="/index.php?r=site/login" method="post">
815         <button type="submit">Submit</button>
816     </form>
817 </body>
818 </html>
819 HTML;
820
821         $crawler = new Crawler($html);
822         $filtered = $crawler->filterXPath("descendant-or-self::*[@id = 'login-form']");
823
824         $this->assertCount(0, $filtered->selectLink('Login'));
825         $this->assertCount(1, $filtered->selectButton('Submit'));
826
827         $filtered = $crawler->filterXPath("descendant-or-self::*[@id = 'action']");
828
829         $this->assertCount(1, $filtered->selectLink('Login'));
830         $this->assertCount(0, $filtered->selectButton('Submit'));
831
832         $this->assertCount(1, $crawler->selectLink('Login')->selectLink('Login'));
833         $this->assertCount(1, $crawler->selectButton('Submit')->selectButton('Submit'));
834     }
835
836     public function testChaining()
837     {
838         $crawler = new Crawler('<div name="a"><div name="b"><div name="c"></div></div></div>');
839
840         $this->assertEquals('a', $crawler->filterXPath('//div')->filterXPath('div')->filterXPath('div')->attr('name'));
841     }
842
843     public function testLinks()
844     {
845         $crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo');
846         $this->assertInternalType('array', $crawler->links(), '->links() returns an array');
847
848         $this->assertCount(4, $crawler->links(), '->links() returns an array');
849         $links = $crawler->links();
850         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Link', $links[0], '->links() returns an array of Link instances');
851
852         $this->assertEquals(array(), $this->createTestCrawler()->filterXPath('//ol')->links(), '->links() returns an empty array if the node selection is empty');
853     }
854
855     public function testImages()
856     {
857         $crawler = $this->createTestCrawler('http://example.com/bar/')->selectImage('Bar');
858         $this->assertInternalType('array', $crawler->images(), '->images() returns an array');
859
860         $this->assertCount(4, $crawler->images(), '->images() returns an array');
861         $images = $crawler->images();
862         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Image', $images[0], '->images() returns an array of Image instances');
863
864         $this->assertEquals(array(), $this->createTestCrawler()->filterXPath('//ol')->links(), '->links() returns an empty array if the node selection is empty');
865     }
866
867     public function testForm()
868     {
869         $testCrawler = $this->createTestCrawler('http://example.com/bar/');
870         $crawler = $testCrawler->selectButton('FooValue');
871         $crawler2 = $testCrawler->selectButton('FooBarValue');
872         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Form', $crawler->form(), '->form() returns a Form instance');
873         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Form', $crawler2->form(), '->form() returns a Form instance');
874
875         $this->assertEquals($crawler->form()->getFormNode()->getAttribute('id'), $crawler2->form()->getFormNode()->getAttribute('id'), '->form() works on elements with form attribute');
876
877         $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');
878         $this->assertEquals(array('FooName' => 'FooValue', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler->form()->getValues(), '->getValues() returns correct form values');
879         $this->assertEquals(array('FooBarName' => 'FooBarValue', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler2->form()->getValues(), '->getValues() returns correct form values');
880
881         try {
882             $this->createTestCrawler()->filterXPath('//ol')->form();
883             $this->fail('->form() throws an \InvalidArgumentException if the node list is empty');
884         } catch (\InvalidArgumentException $e) {
885             $this->assertTrue(true, '->form() throws an \InvalidArgumentException if the node list is empty');
886         }
887     }
888
889     /**
890      * @expectedException \InvalidArgumentException
891      * @expectedExceptionMessage The selected node should be instance of DOMElement
892      */
893     public function testInvalidForm()
894     {
895         $crawler = $this->createTestCrawler('http://example.com/bar/');
896         $crawler->filterXPath('//li/text()')->form();
897     }
898
899     public function testLast()
900     {
901         $crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li');
902         $this->assertNotSame($crawler, $crawler->last(), '->last() returns a new instance of a crawler');
903         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->last() returns a new instance of a crawler');
904
905         $this->assertEquals('Three', $crawler->last()->text());
906     }
907
908     public function testFirst()
909     {
910         $crawler = $this->createTestCrawler()->filterXPath('//li');
911         $this->assertNotSame($crawler, $crawler->first(), '->first() returns a new instance of a crawler');
912         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->first() returns a new instance of a crawler');
913
914         $this->assertEquals('One', $crawler->first()->text());
915     }
916
917     public function testSiblings()
918     {
919         $crawler = $this->createTestCrawler()->filterXPath('//li')->eq(1);
920         $this->assertNotSame($crawler, $crawler->siblings(), '->siblings() returns a new instance of a crawler');
921         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->siblings() returns a new instance of a crawler');
922
923         $nodes = $crawler->siblings();
924         $this->assertEquals(2, $nodes->count());
925         $this->assertEquals('One', $nodes->eq(0)->text());
926         $this->assertEquals('Three', $nodes->eq(1)->text());
927
928         $nodes = $this->createTestCrawler()->filterXPath('//li')->eq(0)->siblings();
929         $this->assertEquals(2, $nodes->count());
930         $this->assertEquals('Two', $nodes->eq(0)->text());
931         $this->assertEquals('Three', $nodes->eq(1)->text());
932
933         try {
934             $this->createTestCrawler()->filterXPath('//ol')->siblings();
935             $this->fail('->siblings() throws an \InvalidArgumentException if the node list is empty');
936         } catch (\InvalidArgumentException $e) {
937             $this->assertTrue(true, '->siblings() throws an \InvalidArgumentException if the node list is empty');
938         }
939     }
940
941     public function testNextAll()
942     {
943         $crawler = $this->createTestCrawler()->filterXPath('//li')->eq(1);
944         $this->assertNotSame($crawler, $crawler->nextAll(), '->nextAll() returns a new instance of a crawler');
945         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->nextAll() returns a new instance of a crawler');
946
947         $nodes = $crawler->nextAll();
948         $this->assertEquals(1, $nodes->count());
949         $this->assertEquals('Three', $nodes->eq(0)->text());
950
951         try {
952             $this->createTestCrawler()->filterXPath('//ol')->nextAll();
953             $this->fail('->nextAll() throws an \InvalidArgumentException if the node list is empty');
954         } catch (\InvalidArgumentException $e) {
955             $this->assertTrue(true, '->nextAll() throws an \InvalidArgumentException if the node list is empty');
956         }
957     }
958
959     public function testPreviousAll()
960     {
961         $crawler = $this->createTestCrawler()->filterXPath('//li')->eq(2);
962         $this->assertNotSame($crawler, $crawler->previousAll(), '->previousAll() returns a new instance of a crawler');
963         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->previousAll() returns a new instance of a crawler');
964
965         $nodes = $crawler->previousAll();
966         $this->assertEquals(2, $nodes->count());
967         $this->assertEquals('Two', $nodes->eq(0)->text());
968
969         try {
970             $this->createTestCrawler()->filterXPath('//ol')->previousAll();
971             $this->fail('->previousAll() throws an \InvalidArgumentException if the node list is empty');
972         } catch (\InvalidArgumentException $e) {
973             $this->assertTrue(true, '->previousAll() throws an \InvalidArgumentException if the node list is empty');
974         }
975     }
976
977     public function testChildren()
978     {
979         $crawler = $this->createTestCrawler()->filterXPath('//ul');
980         $this->assertNotSame($crawler, $crawler->children(), '->children() returns a new instance of a crawler');
981         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->children() returns a new instance of a crawler');
982
983         $nodes = $crawler->children();
984         $this->assertEquals(3, $nodes->count());
985         $this->assertEquals('One', $nodes->eq(0)->text());
986         $this->assertEquals('Two', $nodes->eq(1)->text());
987         $this->assertEquals('Three', $nodes->eq(2)->text());
988
989         try {
990             $this->createTestCrawler()->filterXPath('//ol')->children();
991             $this->fail('->children() throws an \InvalidArgumentException if the node list is empty');
992         } catch (\InvalidArgumentException $e) {
993             $this->assertTrue(true, '->children() throws an \InvalidArgumentException if the node list is empty');
994         }
995
996         try {
997             $crawler = new Crawler('<p></p>');
998             $crawler->filter('p')->children();
999             $this->assertTrue(true, '->children() does not trigger a notice if the node has no children');
1000         } catch (\PHPUnit\Framework\Error\Notice $e) {
1001             $this->fail('->children() does not trigger a notice if the node has no children');
1002         } catch (\PHPUnit_Framework_Error_Notice $e) {
1003             $this->fail('->children() does not trigger a notice if the node has no children');
1004         }
1005     }
1006
1007     public function testParents()
1008     {
1009         $crawler = $this->createTestCrawler()->filterXPath('//li[1]');
1010         $this->assertNotSame($crawler, $crawler->parents(), '->parents() returns a new instance of a crawler');
1011         $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->parents() returns a new instance of a crawler');
1012
1013         $nodes = $crawler->parents();
1014         $this->assertEquals(3, $nodes->count());
1015
1016         $nodes = $this->createTestCrawler()->filterXPath('//html')->parents();
1017         $this->assertEquals(0, $nodes->count());
1018
1019         try {
1020             $this->createTestCrawler()->filterXPath('//ol')->parents();
1021             $this->fail('->parents() throws an \InvalidArgumentException if the node list is empty');
1022         } catch (\InvalidArgumentException $e) {
1023             $this->assertTrue(true, '->parents() throws an \InvalidArgumentException if the node list is empty');
1024         }
1025     }
1026
1027     /**
1028      * @dataProvider getBaseTagData
1029      */
1030     public function testBaseTag($baseValue, $linkValue, $expectedUri, $currentUri = null, $description = null)
1031     {
1032         $crawler = new Crawler('<html><base href="'.$baseValue.'"><a href="'.$linkValue.'"></a></html>', $currentUri);
1033         $this->assertEquals($expectedUri, $crawler->filterXPath('//a')->link()->getUri(), $description);
1034     }
1035
1036     public function getBaseTagData()
1037     {
1038         return array(
1039             array('http://base.com', 'link', 'http://base.com/link'),
1040             array('//base.com', 'link', 'https://base.com/link', 'https://domain.com', '<base> tag can use a schema-less URL'),
1041             array('path/', 'link', 'https://domain.com/path/link', 'https://domain.com', '<base> tag can set a path'),
1042             array('http://base.com', '#', 'http://base.com#', 'http://domain.com/path/link', '<base> tag does work with links to an anchor'),
1043             array('http://base.com', '', 'http://base.com', 'http://domain.com/path/link', '<base> tag does work with empty links'),
1044         );
1045     }
1046
1047     /**
1048      * @dataProvider getBaseTagWithFormData
1049      */
1050     public function testBaseTagWithForm($baseValue, $actionValue, $expectedUri, $currentUri = null, $description = null)
1051     {
1052         $crawler = new Crawler('<html><base href="'.$baseValue.'"><form method="post" action="'.$actionValue.'"><button type="submit" name="submit"/></form></html>', $currentUri);
1053         $this->assertEquals($expectedUri, $crawler->filterXPath('//button')->form()->getUri(), $description);
1054     }
1055
1056     public function getBaseTagWithFormData()
1057     {
1058         return array(
1059             array('https://base.com/', 'link/', 'https://base.com/link/', 'https://base.com/link/', '<base> tag does work with a path and relative form action'),
1060             array('/basepath', '/registration', 'http://domain.com/registration', 'http://domain.com/registration', '<base> tag does work with a path and form action'),
1061             array('/basepath', '', 'http://domain.com/registration', 'http://domain.com/registration', '<base> tag does work with a path and empty form action'),
1062             array('http://base.com/', '/registration', 'http://base.com/registration', 'http://domain.com/registration', '<base> tag does work with a URL and form action'),
1063             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'),
1064             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'),
1065         );
1066     }
1067
1068     public function testCountOfNestedElements()
1069     {
1070         $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>');
1071
1072         $this->assertCount(1, $crawler->filter('li:contains("List item 1")'));
1073     }
1074
1075     public function testEvaluateReturnsTypedResultOfXPathExpressionOnADocumentSubset()
1076     {
1077         $crawler = $this->createTestCrawler();
1078
1079         $result = $crawler->filterXPath('//form/input')->evaluate('substring-before(@name, "Name")');
1080
1081         $this->assertSame(array('Text', 'Foo', 'Bar'), $result);
1082     }
1083
1084     public function testEvaluateReturnsTypedResultOfNamespacedXPathExpressionOnADocumentSubset()
1085     {
1086         $crawler = $this->createTestXmlCrawler();
1087
1088         $result = $crawler->filterXPath('//yt:accessControl/@action')->evaluate('string(.)');
1089
1090         $this->assertSame(array('comment', 'videoRespond'), $result);
1091     }
1092
1093     public function testEvaluateReturnsTypedResultOfNamespacedXPathExpression()
1094     {
1095         $crawler = $this->createTestXmlCrawler();
1096         $crawler->registerNamespace('youtube', 'http://gdata.youtube.com/schemas/2007');
1097
1098         $result = $crawler->evaluate('string(//youtube:accessControl/@action)');
1099
1100         $this->assertSame(array('comment'), $result);
1101     }
1102
1103     public function testEvaluateReturnsACrawlerIfXPathExpressionEvaluatesToANode()
1104     {
1105         $crawler = $this->createTestCrawler()->evaluate('//form/input[1]');
1106
1107         $this->assertInstanceOf(Crawler::class, $crawler);
1108         $this->assertCount(1, $crawler);
1109         $this->assertSame('input', $crawler->first()->nodeName());
1110     }
1111
1112     /**
1113      * @expectedException \LogicException
1114      */
1115     public function testEvaluateThrowsAnExceptionIfDocumentIsEmpty()
1116     {
1117         (new Crawler())->evaluate('//form/input[1]');
1118     }
1119
1120     public function createTestCrawler($uri = null)
1121     {
1122         $dom = new \DOMDocument();
1123         $dom->loadHTML('
1124             <html>
1125                 <body>
1126                     <a href="foo">Foo</a>
1127                     <a href="/foo">   Fabien\'s Foo   </a>
1128                     <a href="/foo">Fabien"s Foo</a>
1129                     <a href="/foo">\' Fabien"s Foo</a>
1130
1131                     <a href="/bar"><img alt="Bar"/></a>
1132                     <a href="/bar"><img alt="   Fabien\'s Bar   "/></a>
1133                     <a href="/bar"><img alt="Fabien&quot;s Bar"/></a>
1134                     <a href="/bar"><img alt="\' Fabien&quot;s Bar"/></a>
1135
1136                     <a href="?get=param">GetLink</a>
1137
1138                     <a href="/example">Klausi|Claudiu</a>
1139
1140                     <form action="foo" id="FooFormId">
1141                         <input type="text" value="TextValue" name="TextName" />
1142                         <input type="submit" value="FooValue" name="FooName" id="FooId" />
1143                         <input type="button" value="BarValue" name="BarName" id="BarId" />
1144                         <button value="ButtonValue" name="ButtonName" id="ButtonId" />
1145                     </form>
1146
1147                     <input type="submit" value="FooBarValue" name="FooBarName" form="FooFormId" />
1148                     <input type="text" value="FooTextValue" name="FooTextName" form="FooFormId" />
1149
1150                     <ul class="first">
1151                         <li class="first">One</li>
1152                         <li>Two</li>
1153                         <li>Three</li>
1154                     </ul>
1155                     <ul>
1156                         <li>One Bis</li>
1157                         <li>Two Bis</li>
1158                         <li>Three Bis</li>
1159                     </ul>
1160                     <div id="parent">
1161                         <div id="child"></div>
1162                         <div id="child2" xmlns:foo="http://example.com"></div>
1163                     </div>
1164                     <div id="sibling"><img /></div>
1165                 </body>
1166             </html>
1167         ');
1168
1169         return new Crawler($dom, $uri);
1170     }
1171
1172     protected function createTestXmlCrawler($uri = null)
1173     {
1174         $xml = '<?xml version="1.0" encoding="UTF-8"?>
1175             <entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">
1176                 <id>tag:youtube.com,2008:video:kgZRZmEc9j4</id>
1177                 <yt:accessControl action="comment" permission="allowed"/>
1178                 <yt:accessControl action="videoRespond" permission="moderated"/>
1179                 <media:group>
1180                     <media:title type="plain">Chordates - CrashCourse Biology #24</media:title>
1181                     <yt:aspectRatio>widescreen</yt:aspectRatio>
1182                 </media:group>
1183                 <media:category label="Music" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Music</media:category>
1184             </entry>';
1185
1186         return new Crawler($xml, $uri);
1187     }
1188
1189     protected function createDomDocument()
1190     {
1191         $dom = new \DOMDocument();
1192         $dom->loadXML('<html><div class="foo"></div></html>');
1193
1194         return $dom;
1195     }
1196
1197     protected function createNodeList()
1198     {
1199         $dom = new \DOMDocument();
1200         $dom->loadXML('<html><div class="foo"></div></html>');
1201         $domxpath = new \DOMXPath($dom);
1202
1203         return $domxpath->query('//div');
1204     }
1205 }