CrawlerTest.php 49 KB

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