AbstractCrawlerTest.php 56 KB

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