CrawlerTest.php 52 KB

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