CrawlerTest.php 49 KB

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