CrawlerTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Tests\Components\DomCrawler;
  11. use Symfony\Components\DomCrawler\Crawler;
  12. class CrawlerTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testConstructor()
  15. {
  16. $crawler = new Crawler();
  17. $this->assertEquals(0, count($crawler), '__construct() returns an empty crawler');
  18. $crawler = new Crawler(new \DOMNode());
  19. $this->assertEquals(1, count($crawler), '__construct() takes a node as a first argument');
  20. }
  21. /**
  22. * @covers Symfony\Components\DomCrawler\Crawler::add
  23. */
  24. public function testAdd()
  25. {
  26. $crawler = new Crawler();
  27. $crawler->add($this->createDomDocument());
  28. $this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->add() adds nodes from a \DOMDocument');
  29. $crawler = new Crawler();
  30. $crawler->add($this->createNodeList());
  31. $this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->add() adds nodes from a \DOMNodeList');
  32. foreach ($this->createNodeList() as $node)
  33. {
  34. $list[] = $node;
  35. }
  36. $crawler = new Crawler();
  37. $crawler->add($list);
  38. $this->assertEquals('foo', $crawler->filter('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->filter('div')->attr('class'), '->add() adds nodes from an \DOMNode');
  42. }
  43. /**
  44. * @covers Symfony\Components\DomCrawler\Crawler::addHtmlContent
  45. */
  46. public function testAddHtmlContent()
  47. {
  48. $crawler = new Crawler();
  49. $crawler->addHtmlContent('<html><div class="foo"></html>', 'UTF-8');
  50. $this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->addHtmlContent() adds nodes from an HTML string');
  51. }
  52. /**
  53. * @covers Symfony\Components\DomCrawler\Crawler::addXmlContent
  54. */
  55. public function testAddXmlContent()
  56. {
  57. $crawler = new Crawler();
  58. $crawler->addXmlContent('<html><div class="foo"></div></html>', 'UTF-8');
  59. $this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->addXmlContent() adds nodes from an XML string');
  60. }
  61. /**
  62. * @covers Symfony\Components\DomCrawler\Crawler::addDocument
  63. */
  64. public function testAddDocument()
  65. {
  66. $crawler = new Crawler();
  67. $crawler->addDocument($this->createDomDocument());
  68. $this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->addDocument() adds nodes from a \DOMDocument');
  69. }
  70. /**
  71. * @covers Symfony\Components\DomCrawler\Crawler::addNodeList
  72. */
  73. public function testAddNodeList()
  74. {
  75. $crawler = new Crawler();
  76. $crawler->addNodeList($this->createNodeList());
  77. $this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->addNodeList() adds nodes from a \DOMNodeList');
  78. }
  79. /**
  80. * @covers Symfony\Components\DomCrawler\Crawler::addNodes
  81. */
  82. public function testAddNodes()
  83. {
  84. foreach ($this->createNodeList() as $node)
  85. {
  86. $list[] = $node;
  87. }
  88. $crawler = new Crawler();
  89. $crawler->addNodes($list);
  90. $this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->addNodes() adds nodes from an array of nodes');
  91. }
  92. /**
  93. * @covers Symfony\Components\DomCrawler\Crawler::addNode
  94. */
  95. public function testAddNode()
  96. {
  97. $crawler = new Crawler();
  98. $crawler->addNode($this->createNodeList()->item(0));
  99. $this->assertEquals('foo', $crawler->filter('div')->attr('class'), '->addNode() adds nodes from an \DOMNode');
  100. }
  101. public function testClear()
  102. {
  103. $crawler = new Crawler(new \DOMNode());
  104. $crawler->clear();
  105. $this->assertEquals(0, count($crawler), '->clear() removes all the nodes from the crawler');
  106. }
  107. public function testIsEmpty()
  108. {
  109. $crawler = new Crawler(new \DOMNode());
  110. $this->assertFalse($crawler->isEmpty(), '->isEmpty() returns false if the crawler node list is not empty');
  111. $crawler->clear();
  112. $this->assertTrue($crawler->isEmpty(), '->isEmpty() returns true if the crawler node list is empty');
  113. }
  114. public function testEq()
  115. {
  116. $crawler = $this->createTestCrawler()->filter('li');
  117. $this->assertNotSame($crawler, $crawler->eq(0), '->eq() returns a new instance of a crawler');
  118. $this->assertInstanceOf('Symfony\\Components\\DomCrawler\\Crawler', $crawler, '->eq() returns a new instance of a crawler');
  119. $this->assertEquals('Two', $crawler->eq(1)->text(), '->eq() returns the nth node of the list');
  120. $this->assertTrue($crawler->eq(100)->isEmpty(), '->eq() returns an empty crawler if the nth node does not exist');
  121. }
  122. public function testEach()
  123. {
  124. $data = $this->createTestCrawler()->filter('ul.first li')->each(function ($node, $i)
  125. {
  126. return $i.'-'.$node->nodeValue;
  127. });
  128. $this->assertEquals(array('0-One', '1-Two', '2-Three'), $data, '->each() executes an anonymous function on each node of the list');
  129. }
  130. public function testReduce()
  131. {
  132. $crawler = $this->createTestCrawler()->filter('ul.first li');
  133. $nodes = $crawler->reduce(function ($node, $i)
  134. {
  135. return $i == 1 ? false : true;
  136. });
  137. $this->assertNotSame($nodes, $crawler, '->reduce() returns a new instance of a crawler');
  138. $this->assertInstanceOf('Symfony\\Components\\DomCrawler\\Crawler', $nodes, '->reduce() returns a new instance of a crawler');
  139. $this->assertEquals(2, count($nodes), '->reduce() filters the nodes in the list');
  140. }
  141. public function testAttr()
  142. {
  143. $this->assertEquals('first', $this->createTestCrawler()->filter('li')->attr('class'), '->attr() returns the attribute of the first element of the node list');
  144. try
  145. {
  146. $this->createTestCrawler()->filter('ol')->attr('class');
  147. $this->fail('->attr() throws an \InvalidArgumentException if the node list is empty');
  148. }
  149. catch (\InvalidArgumentException $e)
  150. {
  151. $this->assertTrue(true, '->attr() throws an \InvalidArgumentException if the node list is empty');
  152. }
  153. }
  154. public function testText()
  155. {
  156. $this->assertEquals('One', $this->createTestCrawler()->filter('li')->text(), '->text() returns the node value of the first element of the node list');
  157. try
  158. {
  159. $this->createTestCrawler()->filter('ol')->text();
  160. $this->fail('->text() throws an \InvalidArgumentException if the node list is empty');
  161. }
  162. catch (\InvalidArgumentException $e)
  163. {
  164. $this->assertTrue(true, '->text() throws an \InvalidArgumentException if the node list is empty');
  165. }
  166. }
  167. public function testExtract()
  168. {
  169. $crawler = $this->createTestCrawler()->filter('ul.first li');
  170. $this->assertEquals(array('One', 'Two', 'Three'), $crawler->extract('_text'), '->extract() returns an array of extracted data from the node list');
  171. $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');
  172. $this->assertEquals(array(), $this->createTestCrawler()->filter('lo')->extract('_text'), '->extract() returns an empty array if the node list is empty');
  173. }
  174. /**
  175. * @covers Symfony\Components\DomCrawler\Crawler::filterXPath
  176. */
  177. public function testFilterXPath()
  178. {
  179. $crawler = $this->createTestCrawler();
  180. $this->assertNotSame($crawler, $crawler->filterXPath('//li'), '->filterXPath() returns a new instance of a crawler');
  181. $this->assertInstanceOf('Symfony\\Components\\DomCrawler\\Crawler', $crawler, '->filterXPath() returns a new instance of a crawler');
  182. $crawler = $this->createTestCrawler()->filter('ul');
  183. $this->assertEquals(6, $crawler->filterXPath('//li')->count(), '->filterXPath() filters the node list with the XPath expression');
  184. }
  185. /**
  186. * @covers Symfony\Components\DomCrawler\Crawler::filter
  187. */
  188. public function testFilter()
  189. {
  190. $crawler = $this->createTestCrawler();
  191. $this->assertNotSame($crawler, $crawler->filter('li'), '->filter() returns a new instance of a crawler');
  192. $this->assertInstanceOf('Symfony\\Components\\DomCrawler\\Crawler', $crawler, '->filter() returns a new instance of a crawler');
  193. $crawler = $this->createTestCrawler()->filter('ul');
  194. $this->assertEquals(6, $crawler->filter('li')->count(), '->filter() filters the node list with the CSS selector');
  195. }
  196. public function testSelectLink()
  197. {
  198. $crawler = $this->createTestCrawler();
  199. $this->assertNotSame($crawler, $crawler->selectLink('Foo'), '->selectLink() returns a new instance of a crawler');
  200. $this->assertInstanceOf('Symfony\\Components\\DomCrawler\\Crawler', $crawler, '->selectLink() returns a new instance of a crawler');
  201. $this->assertEquals(1, $crawler->selectLink('Fabien\'s Foo')->count(), '->selectLink() selects links by the node values');
  202. $this->assertEquals(1, $crawler->selectLink('Fabien\'s Bar')->count(), '->selectLink() selects links by the alt attribute of a clickable image');
  203. $this->assertEquals(2, $crawler->selectLink('Fabien"s Foo')->count(), '->selectLink() selects links by the node values');
  204. $this->assertEquals(2, $crawler->selectLink('Fabien"s Bar')->count(), '->selectLink() selects links by the alt attribute of a clickable image');
  205. $this->assertEquals(1, $crawler->selectLink('\' Fabien"s Foo')->count(), '->selectLink() selects links by the node values');
  206. $this->assertEquals(1, $crawler->selectLink('\' Fabien"s Bar')->count(), '->selectLink() selects links by the alt attribute of a clickable image');
  207. $this->assertEquals(4, $crawler->selectLink('Foo')->count(), '->selectLink() selects links by the node values');
  208. $this->assertEquals(4, $crawler->selectLink('Bar')->count(), '->selectLink() selects links by the node values');
  209. }
  210. public function testSelectButton()
  211. {
  212. $crawler = $this->createTestCrawler();
  213. $this->assertNotSame($crawler, $crawler->selectButton('FooValue'), '->selectButton() returns a new instance of a crawler');
  214. $this->assertInstanceOf('Symfony\\Components\\DomCrawler\\Crawler', $crawler, '->selectButton() returns a new instance of a crawler');
  215. $this->assertEquals(1, $crawler->selectButton('FooValue')->count(), '->selectButton() selects buttons');
  216. $this->assertEquals(1, $crawler->selectButton('FooName')->count(), '->selectButton() selects buttons');
  217. $this->assertEquals(1, $crawler->selectButton('FooId')->count(), '->selectButton() selects buttons');
  218. $this->assertEquals(1, $crawler->selectButton('BarValue')->count(), '->selectButton() selects buttons');
  219. $this->assertEquals(1, $crawler->selectButton('BarName')->count(), '->selectButton() selects buttons');
  220. $this->assertEquals(1, $crawler->selectButton('BarId')->count(), '->selectButton() selects buttons');
  221. }
  222. public function testLink()
  223. {
  224. $crawler = $this->createTestCrawler()->selectLink('Foo');
  225. $this->assertInstanceOf('Symfony\\Components\\DomCrawler\\Link', $crawler->link(), '->link() returns a Link instance');
  226. $this->assertEquals('/foo', $crawler->link()->getUri(), '->link() returns a Link instance');
  227. $this->assertEquals('post', $crawler->link('post')->getMethod(), '->link() takes a method as its argument');
  228. $crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo');
  229. $this->assertEquals('http://example.com/bar/foo', $crawler->link()->getUri(), '->link() returns a Link instance');
  230. $crawler = $this->createTestCrawler('http://example.com/bar')->selectLink('Foo');
  231. $this->assertEquals('http://example.com/foo', $crawler->link()->getUri(), '->form() linketurns a Link instance');
  232. try
  233. {
  234. $this->createTestCrawler()->filter('ol')->link();
  235. $this->fail('->link() throws an \InvalidArgumentException if the node list is empty');
  236. }
  237. catch (\InvalidArgumentException $e)
  238. {
  239. $this->assertTrue(true, '->link() throws an \InvalidArgumentException if the node list is empty');
  240. }
  241. }
  242. public function testLinks()
  243. {
  244. $crawler = $this->createTestCrawler()->selectLink('Foo');
  245. $this->assertType('array', $crawler->links(), '->links() returns an array');
  246. $this->assertEquals(4, count($crawler->links()), '->links() returns an array');
  247. $links = $crawler->links();
  248. $this->assertInstanceOf('Symfony\\Components\\DomCrawler\\Link', $links[0], '->links() returns an array of Link instances');
  249. $this->assertEquals(array(), $this->createTestCrawler()->filter('ol')->links(), '->links() returns an empty array if the node selection is empty');
  250. }
  251. public function testForm()
  252. {
  253. $crawler = $this->createTestCrawler()->selectButton('FooValue');
  254. $this->assertInstanceOf('Symfony\\Components\\DomCrawler\\Form', $crawler->form(), '->form() returns a Form instance');
  255. $this->assertEquals('/foo?FooName=FooValue', $crawler->form()->getUri(), '->form() returns a Form instance');
  256. $this->assertEquals(array('FooName' => 'FooBar'), $crawler->form(array('FooName' => 'FooBar'))->getValues(), '->form() takes an array of values to submit as its first argument');
  257. $crawler = $this->createTestCrawler('http://example.com/bar/')->selectButton('FooValue');
  258. $this->assertEquals('http://example.com/bar/foo?FooName=FooValue', $crawler->form()->getUri(), '->form() returns a Form instance');
  259. $crawler = $this->createTestCrawler('http://example.com/bar')->selectButton('FooValue');
  260. $this->assertEquals('http://example.com/foo?FooName=FooValue', $crawler->form()->getUri(), '->form() returns a Form instance');
  261. try
  262. {
  263. $this->createTestCrawler()->filter('ol')->form();
  264. $this->fail('->form() throws an \InvalidArgumentException if the node list is empty');
  265. }
  266. catch (\InvalidArgumentException $e)
  267. {
  268. $this->assertTrue(true, '->form() throws an \InvalidArgumentException if the node list is empty');
  269. }
  270. }
  271. public function testLast()
  272. {
  273. $crawler = $this->createTestCrawler()->filter('ul.first li');
  274. $this->assertNotSame($crawler, $crawler->last(), '->last() returns a new instance of a crawler');
  275. $this->assertInstanceOf('Symfony\\Components\\DomCrawler\\Crawler', $crawler, '->last() returns a new instance of a crawler');
  276. $this->assertEquals('Three', $crawler->last()->text());
  277. }
  278. public function testFirst()
  279. {
  280. $crawler = $this->createTestCrawler()->filter('li');
  281. $this->assertNotSame($crawler, $crawler->first(), '->first() returns a new instance of a crawler');
  282. $this->assertInstanceOf('Symfony\\Components\\DomCrawler\\Crawler', $crawler, '->first() returns a new instance of a crawler');
  283. $this->assertEquals('One', $crawler->first()->text());
  284. }
  285. public function testSiblings()
  286. {
  287. $crawler = $this->createTestCrawler()->filter('li')->eq(1);
  288. $this->assertNotSame($crawler, $crawler->siblings(), '->siblings() returns a new instance of a crawler');
  289. $this->assertInstanceOf('Symfony\\Components\\DomCrawler\\Crawler', $crawler, '->siblings() returns a new instance of a crawler');
  290. $nodes = $crawler->siblings();
  291. $this->assertEquals(2, $nodes->count());
  292. $this->assertEquals('One', $nodes->eq(0)->text());
  293. $this->assertEquals('Three', $nodes->eq(1)->text());
  294. $nodes = $this->createTestCrawler()->filter('li')->eq(0)->siblings();
  295. $this->assertEquals(2, $nodes->count());
  296. $this->assertEquals('Two', $nodes->eq(0)->text());
  297. $this->assertEquals('Three', $nodes->eq(1)->text());
  298. try
  299. {
  300. $this->createTestCrawler()->filter('ol')->siblings();
  301. $this->fail('->siblings() throws an \InvalidArgumentException if the node list is empty');
  302. }
  303. catch (\InvalidArgumentException $e)
  304. {
  305. $this->assertTrue(true, '->siblings() throws an \InvalidArgumentException if the node list is empty');
  306. }
  307. }
  308. public function testNextAll()
  309. {
  310. $crawler = $this->createTestCrawler()->filter('li')->eq(1);
  311. $this->assertNotSame($crawler, $crawler->nextAll(), '->nextAll() returns a new instance of a crawler');
  312. $this->assertInstanceOf('Symfony\\Components\\DomCrawler\\Crawler', $crawler, '->nextAll() returns a new instance of a crawler');
  313. $nodes = $crawler->nextAll();
  314. $this->assertEquals(1, $nodes->count());
  315. $this->assertEquals('Three', $nodes->eq(0)->text());
  316. try
  317. {
  318. $this->createTestCrawler()->filter('ol')->nextAll();
  319. $this->fail('->nextAll() throws an \InvalidArgumentException if the node list is empty');
  320. }
  321. catch (\InvalidArgumentException $e)
  322. {
  323. $this->assertTrue(true, '->nextAll() throws an \InvalidArgumentException if the node list is empty');
  324. }
  325. }
  326. public function testPreviousAll()
  327. {
  328. $crawler = $this->createTestCrawler()->filter('li')->eq(2);
  329. $this->assertNotSame($crawler, $crawler->previousAll(), '->previousAll() returns a new instance of a crawler');
  330. $this->assertInstanceOf('Symfony\\Components\\DomCrawler\\Crawler', $crawler, '->previousAll() returns a new instance of a crawler');
  331. $nodes = $crawler->previousAll();
  332. $this->assertEquals(2, $nodes->count());
  333. $this->assertEquals('Two', $nodes->eq(0)->text());
  334. try
  335. {
  336. $this->createTestCrawler()->filter('ol')->previousAll();
  337. $this->fail('->previousAll() throws an \InvalidArgumentException if the node list is empty');
  338. }
  339. catch (\InvalidArgumentException $e)
  340. {
  341. $this->assertTrue(true, '->previousAll() throws an \InvalidArgumentException if the node list is empty');
  342. }
  343. }
  344. public function testChildren()
  345. {
  346. $crawler = $this->createTestCrawler()->filter('ul');
  347. $this->assertNotSame($crawler, $crawler->children(), '->children() returns a new instance of a crawler');
  348. $this->assertInstanceOf('Symfony\\Components\\DomCrawler\\Crawler', $crawler, '->children() returns a new instance of a crawler');
  349. $nodes = $crawler->children();
  350. $this->assertEquals(3, $nodes->count());
  351. $this->assertEquals('One', $nodes->eq(0)->text());
  352. $this->assertEquals('Two', $nodes->eq(1)->text());
  353. $this->assertEquals('Three', $nodes->eq(2)->text());
  354. try
  355. {
  356. $this->createTestCrawler()->filter('ol')->children();
  357. $this->fail('->children() throws an \InvalidArgumentException if the node list is empty');
  358. }
  359. catch (\InvalidArgumentException $e)
  360. {
  361. $this->assertTrue(true, '->children() throws an \InvalidArgumentException if the node list is empty');
  362. }
  363. }
  364. public function testParents()
  365. {
  366. $crawler = $this->createTestCrawler()->filter('li:first-child');
  367. $this->assertNotSame($crawler, $crawler->parents(), '->parents() returns a new instance of a crawler');
  368. $this->assertInstanceOf('Symfony\\Components\\DomCrawler\\Crawler', $crawler, '->parents() returns a new instance of a crawler');
  369. $nodes = $crawler->parents();
  370. $this->assertEquals(3, $nodes->count());
  371. $nodes = $this->createTestCrawler()->filter('html')->parents();
  372. $this->assertEquals(0, $nodes->count());
  373. try
  374. {
  375. $this->createTestCrawler()->filter('ol')->parents();
  376. $this->fail('->parents() throws an \InvalidArgumentException if the node list is empty');
  377. }
  378. catch (\InvalidArgumentException $e)
  379. {
  380. $this->assertTrue(true, '->parents() throws an \InvalidArgumentException if the node list is empty');
  381. }
  382. }
  383. public function createTestCrawler($uri = null)
  384. {
  385. $dom = new \DOMDocument();
  386. $dom->loadHTML('
  387. <html>
  388. <body>
  389. <a href="foo">Foo</a>
  390. <a href="/foo"> Fabien\'s Foo </a>
  391. <a href="/foo">Fabien"s Foo</a>
  392. <a href="/foo">\' Fabien"s Foo</a>
  393. <a href="/bar"><img alt="Bar"/></a>
  394. <a href="/bar"><img alt=" Fabien\'s Bar "/></a>
  395. <a href="/bar"><img alt="Fabien&quot;s Bar"/></a>
  396. <a href="/bar"><img alt="\' Fabien&quot;s Bar"/></a>
  397. <form action="foo">
  398. <input type="submit" value="FooValue" name="FooName" id="FooId" />
  399. <input type="button" value="BarValue" name="BarName" id="BarId" />
  400. <button value="ButtonValue" name="ButtonName" id="ButtonId" />
  401. </form>
  402. <ul class="first">
  403. <li class="first">One</li>
  404. <li>Two</li>
  405. <li>Three</li>
  406. </ul>
  407. <ul>
  408. <li>One Bis</li>
  409. <li>Two Bis</li>
  410. <li>Three Bis</li>
  411. </ul>
  412. </body>
  413. </html>
  414. ');
  415. return new Crawler($dom, $uri);
  416. }
  417. protected function createDomDocument()
  418. {
  419. $dom = new \DOMDocument();
  420. $dom->loadXML('<html><div class="foo"></div></html>');
  421. return $dom;
  422. }
  423. protected function createNodeList()
  424. {
  425. $dom = new \DOMDocument();
  426. $dom->loadXML('<html><div class="foo"></div></html>');
  427. $domxpath = new \DOMXPath($dom);
  428. return $domxpath->query('//div');
  429. }
  430. }