AbstractCrawlerTest.php 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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. }