CrawlerTest.php 52 KB

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