CrawlerTest.php 48 KB

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