CrawlerTest.php 48 KB

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