PageRenderTime 58ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/FluentDOM/CoreTest.php

http://github.com/ThomasWeinert/FluentDOM
PHP | 1784 lines | 1121 code | 156 blank | 507 comment | 6 complexity | 90c07e7e4a2b5f03457b1867196e784d MD5 | raw file
  1. <?php
  2. /**
  3. * Collection of tests for the FluentDOMCore class
  4. *
  5. * @version $Id$
  6. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  7. * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert
  8. *
  9. * @package FluentDOM
  10. * @subpackage unitTests
  11. */
  12. /**
  13. * load necessary files
  14. */
  15. require_once (dirname(__FILE__).'/../FluentDOMTestCase.php');
  16. /**
  17. * Test class for FluentDOM.
  18. *
  19. * @package FluentDOM
  20. * @subpackage unitTests
  21. */
  22. class FluentDOMCoreTest extends \PHPUnit_Framework_TestCase {
  23. const XML = '
  24. <items version="1.0">
  25. <group id="1st">
  26. <item index="0">text1</item>
  27. <item index="1">text2</item>
  28. <item index="2">text3</item>
  29. </group>
  30. <html>
  31. <div class="test1 test2">class testing</div>
  32. <div class="test2">class testing</div>
  33. </html>
  34. </items>
  35. ';
  36. /**
  37. * @group Load
  38. * @covers FluentDOMCore::__construct
  39. */
  40. public function testConstructor() {
  41. $fd = new FluentDOMCore();
  42. $this->assertAttributeInstanceOf(
  43. 'DOMDocument', '_document', $fd
  44. );
  45. }
  46. /**
  47. * @group Load
  48. * @covers FluentDOMCore::load
  49. */
  50. public function testLoadWithInvalidSource() {
  51. $fd = new FluentDOMCore();
  52. try {
  53. $fd->load(1);
  54. $this->fail('An expected exception has not been raised.');
  55. } catch (InvalidArgumentException $expected) {
  56. $this->assertEquals(
  57. 'Could not load invalid $source into FluentDOM object.',
  58. $expected->getMessage()
  59. );
  60. }
  61. }
  62. /**
  63. * @group Load
  64. * @covers FluentDOMCore::load
  65. */
  66. public function testLoadWithEmptySource() {
  67. $fd = new FluentDOMCore();
  68. try {
  69. $fd->load('');
  70. $this->fail('An expected exception has not been raised.');
  71. } catch (InvalidArgumentException $expected) {
  72. $this->assertEquals(
  73. 'Can not load empty $source into FluentDOM object.',
  74. $expected->getMessage()
  75. );
  76. }
  77. }
  78. /**
  79. * @group Load
  80. * @covers FluentDOMCore::load
  81. */
  82. public function testLoadWithFluentDOM() {
  83. $fdParent = new FluentDOMCore();
  84. $fdChild = new FluentDOMCore();
  85. $fdChild->load($fdParent);
  86. $this->assertAttributeEquals(
  87. $fdParent,
  88. '_parent',
  89. $fdChild
  90. );
  91. }
  92. /**
  93. * @group Load
  94. * @covers FluentDOMCore::load
  95. * @covers FluentDOMCore::setLoaders
  96. */
  97. public function testLoaderMechanism() {
  98. $firstLoaderMock = $this->getMock('FluentDOMLoader');
  99. $firstLoaderMock
  100. ->expects($this->once())
  101. ->method('load')
  102. ->with($this->equalTo('test load string'), $this->equalTo('text/xml'))
  103. ->will($this->returnValue(FALSE));
  104. $secondLoaderMock = $this->getMock('FluentDOMLoader');
  105. $secondLoaderMock
  106. ->expects($this->once())
  107. ->method('load')
  108. ->with($this->equalTo('test load string'), $this->equalTo('text/xml'))
  109. ->will($this->returnValue(new DOMDocument()));
  110. $fd = new FluentDOMCore();
  111. $fd->setLoaders(array($firstLoaderMock, $secondLoaderMock));
  112. $this->assertSame(
  113. $fd,
  114. $fd->load('test load string')
  115. );
  116. }
  117. /**
  118. * @group Load
  119. * @covers FluentDOMCore::load
  120. */
  121. public function testLoaderMechanismIncludingSelection() {
  122. $dom = new DOMDocument();
  123. $domNode = $dom->appendChild($dom->createElement('root'));
  124. $loaderMock = $this->getMock('FluentDOMLoader');
  125. $loaderMock
  126. ->expects($this->once())
  127. ->method('load')
  128. ->with($this->equalTo($domNode), $this->equalTo('text/xml'))
  129. ->will($this->returnValue($domNode));
  130. $fd = new FluentDOMCore();
  131. $fd->setLoaders(array($loaderMock));
  132. $fd->load($domNode);
  133. $this->assertAttributeSame(
  134. array($domNode), '_array', $fd
  135. );
  136. }
  137. /**
  138. * @group Load
  139. * @covers FluentDOMCore::load
  140. * @covers FluentDOMCore::_initLoaders
  141. */
  142. public function testLoadersMechanismDefaultLoaders() {
  143. $dom = new DOMDocument();
  144. $fd = new FluentDOMCoreProxy();
  145. $fd->load($dom);
  146. $this->assertAttributeNotEquals(
  147. array(),
  148. '_loaders',
  149. $fd
  150. );
  151. }
  152. /**
  153. * @group Load
  154. * @covers FluentDOMCore::setLoaders
  155. */
  156. public function testSetLoadersInvalid() {
  157. try {
  158. $fd = new FluentDOMCore();
  159. $fd->setLoaders(array(new stdClass));
  160. $this->fail('An expected exception has not been raised.');
  161. } catch (InvalidArgumentException $expected) {
  162. }
  163. }
  164. /**
  165. * @group Properties
  166. * @covers FluentDomCore::__isset
  167. */
  168. public function testIssetPropertyDocument() {
  169. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML);
  170. $this->assertTrue(isset($fd->document));
  171. }
  172. /**
  173. * @group Properties
  174. * @covers FluentDomCore::__get
  175. */
  176. public function testGetPropertyDocument() {
  177. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML);
  178. $this->assertSame(
  179. $this->readAttribute($fd, '_document'),
  180. $fd->document
  181. );
  182. }
  183. /**
  184. * @group Properties
  185. * @covers FluentDomCore::__set
  186. */
  187. public function testSetPropertyDocument() {
  188. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML);
  189. try {
  190. $fd->document = NULL;
  191. $this->fail('An expected exception has not been raised.');
  192. } catch (BadMethodCallException $expected) {
  193. }
  194. }
  195. /**
  196. * @group Properties
  197. * @covers FluentDOMCore::__isset
  198. */
  199. public function testIssetPropertyXpath() {
  200. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML);
  201. $this->assertTrue(isset($fd->xpath));
  202. }
  203. /**
  204. * @group Properties
  205. * @covers FluentDOMCore::__get
  206. * @covers FluentDOMCore::_xpath
  207. */
  208. public function testGetPropertyXpath() {
  209. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML);
  210. $this->assertTrue($fd->xpath instanceof DOMXPath);
  211. }
  212. /**
  213. * @group Properties
  214. * @covers FluentDOMCore::__get
  215. * @covers FluentDOMCore::_xpath
  216. */
  217. public function testGetPropertyXpathWithDefaultNamespaceInitialization() {
  218. $fd = $this->getFluentDOMCoreFixtureFromString('<sample xmlns="http://sample.tld/"/>');
  219. $this->assertEquals(
  220. 1,
  221. $fd->xpath->evaluate('count(//_:*)')
  222. );
  223. }
  224. /**
  225. * @group Properties
  226. * @covers FluentDOMCore::__get
  227. * @covers FluentDOMCore::_xpath
  228. */
  229. public function testGetPropertyXpathWithNamespaceInitialization() {
  230. $fd = new FluentDOM();
  231. $fd->namespaces(
  232. array(
  233. 'foo' => 'http://sample.tld/1',
  234. 'bar' => 'http://sample.tld/2'
  235. )
  236. );
  237. $fd->document->loadXML(
  238. '<sample xmlns="http://sample.tld/1"><foo:child xmlns:foo="http://sample.tld/2"/></sample>'
  239. );
  240. $this->assertEquals(
  241. 1,
  242. $fd->xpath->evaluate('count(//bar:child)')
  243. );
  244. }
  245. /**
  246. * @covers FluentDOMCore::__set
  247. */
  248. public function testSetPropertyXpath() {
  249. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML);
  250. try {
  251. $fd->xpath = NULL;
  252. $this->fail('An expected exception has not been raised.');
  253. } catch (BadMethodCallException $expected) {
  254. }
  255. }
  256. /**
  257. * @group Properties
  258. * @covers FluentDOMCore::__isset
  259. */
  260. public function testIssetPropertyLength() {
  261. $fd = new FluentDOMCore();
  262. $this->assertTrue(isset($fd->length));
  263. }
  264. /**
  265. * @group Properties
  266. * @covers FluentDOMCore::__get
  267. */
  268. public function testGetPropertyLength() {
  269. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '//item');
  270. $this->assertEquals(3, $fd->length);
  271. }
  272. /**
  273. * @group Properties
  274. * @covers FluentDOMCore::__set
  275. */
  276. public function testSetPropertyLength() {
  277. $fd = new FluentDOMCore;
  278. try {
  279. $fd->length = 50;
  280. $this->fail('An expected exception has not been raised.');
  281. } catch (BadMethodCallException $expected) {
  282. }
  283. }
  284. /**
  285. * @group Properties
  286. * @covers FluentDOMCore::__isset
  287. */
  288. public function testIssetPropertyContentType() {
  289. $fd = new FluentDOMCore();
  290. $this->assertTrue(isset($fd->contentType));
  291. }
  292. /**
  293. * @group Properties
  294. * @covers FluentDOMCore::__get
  295. */
  296. public function testGetPropertyContentType() {
  297. $fd = new FluentDOMCore();
  298. $this->assertEquals('text/xml', $fd->contentType);
  299. }
  300. /**
  301. * @group Properties
  302. * @covers FluentDOMCore::__set
  303. * @covers FluentDOMCore::_setContentType
  304. * @dataProvider getContentTypeSamples
  305. */
  306. public function testSetPropertyContentType($contentType, $expected) {
  307. $fd = new FluentDOMCore();
  308. $fd->contentType = $contentType;
  309. $this->assertAttributeEquals($expected, '_contentType', $fd);
  310. }
  311. public function getContentTypeSamples() {
  312. return array(
  313. array('text/xml', 'text/xml'),
  314. array('text/html', 'text/html'),
  315. array('xml', 'text/xml'),
  316. array('html', 'text/html'),
  317. array('TEXT/XML', 'text/xml'),
  318. array('TEXT/HTML', 'text/html'),
  319. array('XML', 'text/xml'),
  320. array('HTML', 'text/html')
  321. );
  322. }
  323. /**
  324. * @group Properties
  325. * @covers FluentDOMCore::__set
  326. * @covers FluentDOMCore::_setContentType
  327. */
  328. public function testSetPropertyContentTypeChaining() {
  329. $fdParent = new FluentDOMCore();
  330. $fdChild = $fdParent->spawn();
  331. $fdChild->contentType = 'text/html';
  332. $this->assertEquals(
  333. 'text/html',
  334. $fdParent->contentType
  335. );
  336. }
  337. /**
  338. * @group Properties
  339. * @covers FluentDOMCore::__set
  340. * @covers FluentDOMCore::_setContentType
  341. */
  342. public function testSetPropertyContentTypeInvalid() {
  343. $fd = new FluentDOMCore();
  344. try {
  345. $fd->contentType = 'INVALID/NOT_USEABLE';
  346. $this->fail('An expected exception has not been raised.');
  347. } catch (UnexpectedValueException $expected) {
  348. }
  349. }
  350. /**
  351. * @group Properties
  352. * @covers FluentDOMCore::__set
  353. */
  354. public function testSetInvalidPropertyContentType() {
  355. try {
  356. $fd = new FluentDOMCore();
  357. $fd->contentType = 'INVALID_TYPE';
  358. $this->fail('An expected exception has not been raised.');
  359. } catch (UnexpectedValueException $expected) {
  360. }
  361. }
  362. /**
  363. * @group Properties
  364. */
  365. public function testDynamicProperty() {
  366. $fd = new FluentDOMCore();
  367. $this->assertEquals(FALSE, isset($fd->dynamicProperty));
  368. $this->assertEquals(NULL, $fd->dynamicProperty);
  369. $fd->dynamicProperty = 'test';
  370. $this->assertEquals(TRUE, isset($fd->dynamicProperty));
  371. $this->assertEquals('test', $fd->dynamicProperty);
  372. }
  373. /*
  374. * __toString() method
  375. */
  376. /**
  377. * @group MagicFunctions
  378. */
  379. public function testMagicToString() {
  380. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML);
  381. $this->assertEquals($fd->document->saveXML(), (string)$fd);
  382. }
  383. /**
  384. * @group MagicFunctions
  385. */
  386. public function testMagicToStringHtml() {
  387. $dom = new DOMDocument();
  388. $dom->loadHTML('<html><body><br></body></html>');
  389. $loader = $this->getMock('FluentDOMLoader');
  390. $loader
  391. ->expects($this->once())
  392. ->method('load')
  393. ->with($this->equalTo('mocked'), $this->equalTo('text/html'))
  394. ->will($this->returnValue($dom));
  395. $fd = new FluentDOMCore();
  396. $fd->setLoaders(array($loader));
  397. $fd = $fd->load('mocked', 'text/html');
  398. $this->assertEquals($dom->saveHTML(), (string)$fd);
  399. }
  400. /*
  401. * Interfaces
  402. */
  403. /**
  404. * @group Interfaces
  405. * @covers FluentDOMCore::offsetExists
  406. */
  407. public function testInterfaceArrayAccessIssetExpectingTrue() {
  408. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '/*');
  409. $this->assertTrue(isset($fd[0]));
  410. }
  411. /**
  412. * @group Interfaces
  413. * @covers FluentDOMCore::offsetExists
  414. */
  415. public function testInterfaceArrayAccessIssetExpectingFalse() {
  416. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '/*');
  417. $this->assertFalse(isset($fd[99]));
  418. }
  419. /**
  420. * @group Interfaces
  421. * @covers FluentDOMCore::offsetGet
  422. */
  423. public function testInterfaceArrayAccessGet() {
  424. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '//item');
  425. $this->assertEquals(
  426. 'item',
  427. $fd[0]->nodeName
  428. );
  429. }
  430. /**
  431. * @group Interfaces
  432. * @covers FluentDOMCore::offsetSet
  433. */
  434. public function testInterfaceArrayAccessSet() {
  435. $fd = new FluentDOMCore();
  436. try {
  437. $fd[0] = NULL;
  438. $this->fail('An expected exception has not been raised.');
  439. } catch (BadMethodCallException $expected) {
  440. }
  441. }
  442. /**
  443. * @group Interfaces
  444. * @covers FluentDOMCore::offsetUnset
  445. */
  446. public function testInterfaceArrayAccessUnset() {
  447. $fd = new FluentDOMCore();
  448. try {
  449. unset($fd[0]);
  450. $this->fail('An expected exception has not been raised.');
  451. } catch (BadMethodCallException $expected) {
  452. }
  453. }
  454. /**
  455. * @group Interfaces
  456. * @covers FluentDOMCore::count
  457. */
  458. public function testInterfaceCountableExpectingZero() {
  459. $fd = new FluentDOMCore();
  460. $this->assertEquals(0, count($fd));
  461. }
  462. /**
  463. * @group Interfaces
  464. * @covers FluentDOMCore::count
  465. */
  466. public function testInterfaceCountable() {
  467. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '//item');
  468. $this->assertEquals(3, count($fd));
  469. }
  470. /**
  471. * @group Interfaces
  472. * @covers FluentDOMCore::getIterator
  473. */
  474. public function testInterfaceIteratorLoop() {
  475. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '//item');
  476. $counter = 0;
  477. foreach ($fd as $item) {
  478. $this->assertEquals('item', $item->nodeName);
  479. ++$counter;
  480. }
  481. $this->assertEquals(3, $counter);
  482. }
  483. /**
  484. * @group Interfaces
  485. * @covers FluentDOMCore::getIterator
  486. */
  487. public function testInterfaceRecursiveIterator() {
  488. $iterator = new RecursiveIteratorIterator(
  489. $this->getFluentDOMCoreFixtureFromString(self::XML, '/items'),
  490. RecursiveIteratorIterator::SELF_FIRST
  491. );
  492. $counter = 0;
  493. foreach ($iterator as $key => $value) {
  494. if ($value->nodeName == 'item') {
  495. ++$counter;
  496. }
  497. }
  498. $this->assertEquals(3, $counter);
  499. }
  500. /**
  501. * @group CoreFunctions
  502. * @covers FluentDOMCore::item
  503. */
  504. public function testItem() {
  505. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '/*');
  506. $this->assertEquals($fd->document->documentElement, $fd->item(0));
  507. }
  508. /**
  509. * @group CoreFunctions
  510. * @covers FluentDOMCore::item
  511. */
  512. public function testItemExpectingNull() {
  513. $fd = new FluentDOMCore();
  514. $this->assertNull($fd->item(0));
  515. }
  516. /**
  517. * @group CoreFunctions
  518. * @covers FluentDOMCore::formatOutput
  519. */
  520. public function testFormatOutput() {
  521. $fd = new FluentDOMCore();
  522. $fd->load('<html><body><br/></body></html>');
  523. $fd->formatOutput();
  524. $expected =
  525. "<?xml version=\"1.0\"?>\n".
  526. "<html>\n".
  527. " <body>\n".
  528. " <br/>\n".
  529. " </body>\n".
  530. "</html>\n";
  531. $this->assertSame('text/xml', $this->readAttribute($fd, '_contentType'));
  532. $this->assertSame($expected, (string)$fd);
  533. }
  534. /**
  535. * @group CoreFunctions
  536. * @covers FluentDOMCore::formatOutput
  537. */
  538. public function testFormatOutputWithContentTypeHtml() {
  539. $fd = new FluentDOMCore();
  540. $fd->load('<html><body><br/></body></html>');
  541. $fd->formatOutput('text/html');
  542. $expected = "<html><body><br></body></html>\n";
  543. $this->assertSame('text/html', $this->readAttribute($fd, '_contentType'));
  544. $this->assertSame($expected, (string)$fd);
  545. }
  546. /**
  547. * @group CoreFunctions
  548. * @covers FluentDOMCore::spawn
  549. */
  550. public function testSpawn() {
  551. $fdParent = new FluentDOMCore;
  552. $fdChild = $fdParent->spawn();
  553. $this->assertAttributeSame(
  554. $fdParent,
  555. '_parent',
  556. $fdChild
  557. );
  558. }
  559. /**
  560. * @group CoreFunctions
  561. * @covers FluentDOMCore::spawn
  562. */
  563. public function testSpawnWithElements() {
  564. $dom = new DOMDocument;
  565. $node = $dom->createElement('test');
  566. $dom->appendChild($node);
  567. $fdParent = new FluentDOMCore;
  568. $fdParent->load($dom);
  569. $fdChild = $fdParent->spawn($node);
  570. $this->assertAttributeSame(
  571. array($node),
  572. '_array',
  573. $fdChild
  574. );
  575. }
  576. /**
  577. * @group CoreFunctions
  578. * @covers FluentDOMCore::push
  579. */
  580. public function testPushWithDomnode() {
  581. $fd = new FluentDOMCore();
  582. $node = $fd->document->createElement('sample');
  583. $fd->push($node);
  584. $this->assertAttributeSame(
  585. array($node),
  586. '_array',
  587. $fd
  588. );
  589. }
  590. /**
  591. * @group CoreFunctions
  592. * @covers FluentDOMCore::push
  593. */
  594. public function testPushWithArrayContainingDomnode() {
  595. $fd = new FluentDOMCore();
  596. $node = $fd->document->createElement('sample');
  597. $fd->push(array($node));
  598. $this->assertAttributeSame(
  599. array($node),
  600. '_array',
  601. $fd
  602. );
  603. }
  604. /**
  605. * @group CoreFunctions
  606. * @covers FluentDOMCore::push
  607. */
  608. public function testPushWithForeignDomnodeExpectingException() {
  609. $fd = new FluentDOMCore();
  610. $dom = new DOMDocument();
  611. $node = $dom->createElement('sample');
  612. try {
  613. $fd->push($node);
  614. $this->fail('An expected exception has not been raised.');
  615. } catch (OutOfBoundsException $expected) {
  616. }
  617. }
  618. /**
  619. * @group CoreFunctions
  620. * @covers FluentDOMCore::push
  621. */
  622. public function testPushWithArrayContainingForeignDomnodeExpectingException() {
  623. $fd = new FluentDOMCore();
  624. $dom = new DOMDocument();
  625. $node = $dom->createElement('sample');
  626. try {
  627. $fd->push(array($node));
  628. $this->fail('An expected exception has not been raised.');
  629. } catch (OutOfBoundsException $expected) {
  630. }
  631. }
  632. /**
  633. * @group CoreFunctions
  634. * @covers FluentDOMCore::push
  635. */
  636. public function testPushWithInvalidArgumentExpectingException() {
  637. $fd = new FluentDOMCore();
  638. try {
  639. $fd->push(42);
  640. $this->fail('An expected exception has not been raised.');
  641. } catch (InvalidArgumentException $expected) {
  642. }
  643. }
  644. /**
  645. * @group CoreFunctions
  646. * @covers FluentDOMCore::unique
  647. */
  648. public function testUniqueWithArrayOfAppendedNodes() {
  649. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '//item');
  650. $nodes = array(
  651. $fd[1],
  652. $fd[2],
  653. $fd[0],
  654. $fd[2],
  655. $fd[1],
  656. $fd[2],
  657. $fd[0]
  658. );
  659. $unique = $fd->unique($nodes);
  660. $this->assertEquals(3, count($unique));
  661. $this->assertSame($fd[0], $unique[0]);
  662. }
  663. /**
  664. * @group CoreFunctions
  665. * @covers FluentDOMCore::unique
  666. */
  667. public function testUniqueWithArrayOfCreatedNodes() {
  668. $fd = new FluentDOM();
  669. $nodes = array(
  670. $fd->document->createElement('hello'),
  671. $fd->document->createElement('world'),
  672. $fd->document->createTextNode('!')
  673. );
  674. $nodes[] = $nodes[2];
  675. $nodes[] = $nodes[1];
  676. $nodes[] = $nodes[0];
  677. $unique = $fd->unique($nodes);
  678. $this->assertEquals(3, count($unique));
  679. $this->assertEquals('hello', $unique[0]->tagName);
  680. $this->assertEquals('world', $unique[1]->tagName);
  681. $this->assertEquals('!', $unique[2]->textContent);
  682. }
  683. /**
  684. * @group CoreFunctions
  685. * @covers FluentDOMCore::unique
  686. */
  687. public function testUniqueWithArrayOfAppenedAndCreatedNodes() {
  688. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '//item');
  689. $nodes = array(
  690. $fd->document->createElement('hello'),
  691. $fd[1],
  692. $fd->document->createTextNode('world'),
  693. $fd[0]
  694. );
  695. $unique = $fd->unique($nodes);
  696. $this->assertEquals(4, count($unique));
  697. $this->assertSame($fd[0], $unique[0]);
  698. $this->assertSame($fd[1], $unique[1]);
  699. $this->assertEquals('hello', $unique[2]->tagName);
  700. $this->assertEquals('world', $unique[3]->textContent);
  701. }
  702. /**
  703. * @group CoreFunctions
  704. * @covers FluentDOMCore::unique
  705. */
  706. public function testUniqueWithIntegerExpectingException() {
  707. $fd = new FluentDOM();
  708. try {
  709. $fd->unique(array(1));
  710. $this->fail('An expected exception has not been raised.');
  711. } catch (InvalidArgumentException $expected) {
  712. $this->assertEquals(
  713. 'Array must only contain dom nodes, found "integer".',
  714. $expected->getMessage()
  715. );
  716. }
  717. }
  718. /**
  719. * @group CoreFunctions
  720. * @covers FluentDOMCore::unique
  721. */
  722. public function testUniqueWithObjectExpectingException() {
  723. $fd = new FluentDOM();
  724. try {
  725. $fd->unique(array(new stdClass));
  726. $this->fail('An expected exception has not been raised.');
  727. } catch (InvalidArgumentException $expected) {
  728. $this->assertEquals(
  729. 'Array must only contain dom nodes, found "stdClass".',
  730. $expected->getMessage()
  731. );
  732. }
  733. }
  734. /**
  735. * @group CoreFunctions
  736. * @covers FluentDOMCore::_uniqueSort
  737. */
  738. public function testUniqueSort() {
  739. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '//item');
  740. $nodes = array(
  741. $fd->document->createElement('hello'),
  742. $fd[1],
  743. $fd->document->createTextNode('world'),
  744. $fd[0]
  745. );
  746. $fd->push($nodes);
  747. $fd->_uniqueSort();
  748. $this->assertEquals(5, count($fd));
  749. $this->assertSame($nodes[3], $fd[0]);
  750. $this->assertSame($nodes[1], $fd[1]);
  751. $this->assertSame($nodes[0], $fd[3]);
  752. $this->assertSame($nodes[2], $fd[4]);
  753. }
  754. /**
  755. * @group CoreFunctions
  756. * @covers FluentDOMCore::_registerNodeNamespaces
  757. */
  758. public function testRegisterNodeNamespaces() {
  759. $fd = new FluentDOMCoreProxy();
  760. $this->assertSame(
  761. version_compare(PHP_VERSION, '5.3.3', '<'), $fd->_registerNodeNamespaces()
  762. );
  763. }
  764. /**
  765. * @group CoreFunctions
  766. * @covers FluentDOMCore::_registerNodeNamespaces
  767. */
  768. public function testRegisterNodeNamespacesExpectingFalseAfterSet() {
  769. $fd = new FluentDOMCoreProxy();
  770. $this->assertFalse($fd->_registerNodeNamespaces(FALSE));
  771. }
  772. /**
  773. * @group CoreFunctions
  774. * @covers FluentDOMCore::_registerNodeNamespaces
  775. */
  776. public function testRegisterNodeNamespacesExpectingTrueAfterSet() {
  777. $fd = new FluentDOMCoreProxy();
  778. $this->assertTrue($fd->_registerNodeNamespaces(TRUE));
  779. }
  780. /**
  781. * @group CoreFunctions
  782. * @covers FluentDOMCore::evaluate
  783. * @covers FluentDOMCore::_evaluate
  784. */
  785. public function testEvaluate() {
  786. if (version_compare(PHP_VERSION, '5.3.3', '<')) {
  787. $this->markTestSkipped('$registerNodeNS parameter not availiable.');
  788. }
  789. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML);
  790. $fd->_registerNodeNamespaces(FALSE);
  791. $this->assertEquals(
  792. 3,
  793. $fd->evaluate('count(//item)')
  794. );
  795. }
  796. /**
  797. * @group CoreFunctions
  798. * @covers FluentDOMCore::evaluate
  799. * @covers FluentDOMCore::_evaluate
  800. */
  801. public function testEvaluateWithContext() {
  802. if (version_compare(PHP_VERSION, '5.3.3', '<')) {
  803. $this->markTestSkipped('$registerNodeNS parameter not availiable.');
  804. }
  805. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML);
  806. $this->assertEquals(
  807. 3,
  808. $fd->evaluate('count(group/item)', $fd->document->documentElement)
  809. );
  810. }
  811. /**
  812. * @group CoreFunctions
  813. * @covers FluentDOMCore::evaluate
  814. * @covers FluentDOMCore::_evaluate
  815. */
  816. public function testEvaluateWithNamespaceRegistration() {
  817. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML);
  818. $fd->_registerNodeNamespaces(TRUE);
  819. $this->assertEquals(
  820. 3,
  821. $fd->evaluate('count(//item)')
  822. );
  823. }
  824. /**
  825. * @group CoreFunctions
  826. * @covers FluentDOMCore::evaluate
  827. * @covers FluentDOMCore::_evaluate
  828. */
  829. public function testEvaluateWithContextWithNamespaceRegistration() {
  830. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML);
  831. $fd->_registerNodeNamespaces(TRUE);
  832. $this->assertEquals(
  833. 3,
  834. $fd->evaluate('count(group/item)', $fd->document->documentElement)
  835. );
  836. }
  837. /**
  838. * @group CoreFunctions
  839. * @covers FluentDOMCore::namespaces
  840. */
  841. public function testNamespacesRegisterNamespaces() {
  842. $fd = new FluentDOM();
  843. $fdResult = $fd->namespaces(array('test' => 'http://test.only/'));
  844. $this->assertAttributeEquals(
  845. array('test' => 'http://test.only/'),
  846. '_namespaces',
  847. $fdResult
  848. );
  849. $this->assertSame($fd, $fdResult);
  850. }
  851. /**
  852. * @group CoreFunctions
  853. * @covers FluentDOMCore::namespaces
  854. */
  855. public function testNamespacesGetNamespaces() {
  856. $fd = new FluentDOM();
  857. $fd->namespaces(array('test' => 'http://test.only/'));
  858. $this->assertEquals(
  859. array('test' => 'http://test.only/'),
  860. $fd->namespaces()
  861. );
  862. }
  863. /**
  864. * @group CoreFunctions
  865. * @covers FluentDOMCore::namespaces
  866. */
  867. public function testNamespacesWithChaining() {
  868. $fd = new FluentDOM();
  869. $fd->namespaces(array('test' => 'http://test.only/'));
  870. $fdChild = $fd->spawn();
  871. $this->assertAttributeEquals(
  872. array('test' => 'http://test.only/'),
  873. '_namespaces',
  874. $fdChild
  875. );
  876. }
  877. /**
  878. * @group CoreFunctions
  879. * @covers FluentDOMCore::_match
  880. */
  881. public function testMatch() {
  882. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML);
  883. $this->assertEquals(
  884. 3,
  885. $fd->_match('//item')->length
  886. );
  887. }
  888. /**
  889. * @group CoreFunctions
  890. * @covers FluentDOMCore::_match
  891. */
  892. public function testMatchWithContext() {
  893. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '/items/group');
  894. $this->assertEquals(
  895. 3,
  896. $fd->_match('item', $fd->item(0))->length
  897. );
  898. }
  899. /**
  900. * @group CoreFunctions
  901. * @covers FluentDOMCore::_match
  902. */
  903. public function testMatchWithExpressionThatDoesNotReturnAList() {
  904. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML);
  905. try {
  906. $fd->_match('count(/items/group)');
  907. $this->fail('An expected exception has not been thrown');
  908. } catch (InvalidArgumentException $e) {
  909. }
  910. }
  911. /**
  912. * @group CoreFunctions
  913. * @covers FluentDOMCore::_test
  914. */
  915. public function testTestMatchingNodelist() {
  916. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML);
  917. $this->assertTrue(
  918. $fd->_test('//item')
  919. );
  920. }
  921. /**
  922. * @group CoreFunctions
  923. * @covers FluentDOMCore::_test
  924. */
  925. public function testTestCountingNodesWithContextExpectingTrue() {
  926. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '/items/group');
  927. $this->assertTrue(
  928. $fd->_test('count(item)', $fd->item(0))
  929. );
  930. }
  931. /**
  932. * @group CoreFunctions
  933. * @covers FluentDOMCore::_inList
  934. */
  935. public function testInListExpectingTrue() {
  936. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '/items');
  937. $this->assertTrue(
  938. $fd->_inList($fd->document->documentElement)
  939. );
  940. }
  941. /**
  942. * @group CoreFunctions
  943. * @covers FluentDOMCore::_inList
  944. */
  945. public function testInListExpectingFalse() {
  946. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '//item');
  947. $this->assertFalse(
  948. $fd->_inList($fd->document->documentElement)
  949. );
  950. }
  951. /**
  952. * @group CoreFunctions
  953. * @covers FluentDOMCore::_isQName
  954. * @dataProvider dataProviderValidQualifiedNames
  955. */
  956. public function testIsQName($qualifiedName) {
  957. $fd = new FluentDOMCoreProxy();
  958. $this->assertTrue($fd->_isQName($qualifiedName));
  959. }
  960. public static function dataProviderValidQualifiedNames() {
  961. return array(
  962. array('tag'),
  963. array('namespace:tag'),
  964. array('_:_'),
  965. array('_-_'),
  966. array('_')
  967. );
  968. }
  969. /**
  970. * @group CoreFunctions
  971. * @covers FluentDOMCore::_isQName
  972. */
  973. public function testIsQnameWithEmptyNameExpectingException() {
  974. $fd = new FluentDOMCoreProxy();
  975. try {
  976. $fd->_isQName('');
  977. $this->fail('An expected exception has not been raised.');
  978. } catch (UnexpectedValueException $expected) {
  979. }
  980. }
  981. /**
  982. * @group CoreFunctions
  983. * @covers FluentDOMCore::_isNCName
  984. * @dataProvider dataProviderValidNCName
  985. */
  986. public function testIsNCName($tagName, $offset, $length) {
  987. $fd = new FluentDOMCoreProxy();
  988. $this->assertTrue($fd->_isNCName($tagName, $offset, $length));
  989. }
  990. public static function dataProviderValidNCName() {
  991. return array(
  992. array('html', 0, 0),
  993. array('tag23', 0, 0),
  994. array('sample-tag', 0, 0),
  995. array('sampleTag', 0, 0),
  996. array('ns:tag', 3, 0),
  997. array('ns:tag', 0, 2)
  998. );
  999. }
  1000. /**
  1001. * @group CoreFunctions
  1002. * @covers FluentDOMCore::_isNCName
  1003. */
  1004. public function testIsNCNameWithEmptyTagnameExpectingException() {
  1005. $fd = new FluentDOMCoreProxy();
  1006. try {
  1007. $fd->_isNCName('nc:', 3);
  1008. $this->fail('An expected exception has not been raised.');
  1009. } catch (UnexpectedValueException $expected) {
  1010. $this->assertEquals(
  1011. 'Invalid QName "nc:": Missing QName part.',
  1012. $expected->getMessage()
  1013. );
  1014. }
  1015. }
  1016. /**
  1017. * @group CoreFunctions
  1018. * @covers FluentDOMCore::_isNCName
  1019. */
  1020. public function testIsNCNameWithInvalidTagnameCharExpectingException() {
  1021. $fd = new FluentDOMCoreProxy();
  1022. try {
  1023. $fd->_isNCName('nc:ta<g>', 3);
  1024. $this->fail('An expected exception has not been raised.');
  1025. } catch (UnexpectedValueException $expected) {
  1026. $this->assertEquals(
  1027. 'Invalid QName "nc:ta<g>": Invalid character at index 5.',
  1028. $expected->getMessage()
  1029. );
  1030. }
  1031. }
  1032. /**
  1033. * @group CoreFunctions
  1034. * @covers FluentDOMCore::_isNCName
  1035. */
  1036. public function testIsNCNameWithInvalidTagnameStartingCharExpectingException() {
  1037. $fd = new FluentDOMCoreProxy();
  1038. try {
  1039. $fd->_isNCName('nc:1tag', 3);
  1040. $this->fail('An expected exception has not been raised.');
  1041. } catch (UnexpectedValueException $expected) {
  1042. $this->assertEquals(
  1043. 'Invalid QName "nc:1tag": Invalid character at index 3.',
  1044. $expected->getMessage()
  1045. );
  1046. }
  1047. }
  1048. /**
  1049. * @group CoreFunctions
  1050. * @covers FluentDOMCore::_isNode
  1051. */
  1052. public function testIsNodeWithDomnodeExpectingTrue() {
  1053. $dom = new DOMDocument();
  1054. $node = $dom->createElement('sample');
  1055. $fd = new FluentDOMCoreProxy();
  1056. $this->assertTrue($fd->_isNode($node));
  1057. }
  1058. /**
  1059. * @group CoreFunctions
  1060. * @covers FluentDOMCore::_isNode
  1061. */
  1062. public function testIsNodeWithDomtextExpectingTrue() {
  1063. $dom = new DOMDocument();
  1064. $node = $dom->createTextNode('sample');
  1065. $fd = new FluentDOMCoreProxy();
  1066. $this->assertTrue($fd->_isNode($node));
  1067. }
  1068. /**
  1069. * @group CoreFunctions
  1070. * @covers FluentDOMCore::_isNode
  1071. */
  1072. public function testIsNodeWithEmptyDomtextExpectingTrue() {
  1073. $dom = new DOMDocument();
  1074. $node = $dom->createTextNode(' ');
  1075. $fd = new FluentDOMCoreProxy();
  1076. $this->assertFalse($fd->_isNode($node));
  1077. }
  1078. /**
  1079. * @group CoreFunctions
  1080. * @covers FluentDOMCore::_isNodeList
  1081. */
  1082. public function testIsNodeListWithArrayExpectingTrue() {
  1083. $fd = new FluentDOMCoreProxy();
  1084. $this->assertTrue($fd->_isNodeList(array()));
  1085. }
  1086. /**
  1087. * @group CoreFunctions
  1088. * @covers FluentDOMCore::_isNodeList
  1089. */
  1090. public function testIsNodeListWithArrayExpectingFalse() {
  1091. $fd = new FluentDOMCoreProxy();
  1092. $this->assertFalse($fd->_isNodeList(42));
  1093. }
  1094. /**
  1095. * @group CoreFunctions
  1096. * @covers FluentDOMCore::_isCallback
  1097. */
  1098. public function testIsCallbackWithArrayCallbackExpectingTrue() {
  1099. $fd = new FluentDOMCoreProxy();
  1100. $this->assertTrue(
  1101. $fd->_isCallback(array($this, __METHOD__), FALSE, FALSE)
  1102. );
  1103. }
  1104. /**
  1105. * @group CoreFunctions
  1106. * @covers FluentDOMCore::_isCallback
  1107. */
  1108. public function testIsCallbackWithGlobalFunctionExpectingTrue() {
  1109. $fd = new FluentDOMCoreProxy();
  1110. $this->assertTrue(
  1111. $fd->_isCallback('strpos', TRUE, FALSE)
  1112. );
  1113. }
  1114. /**
  1115. * @group CoreFunctions
  1116. * @covers FluentDOMCore::_isCallback
  1117. */
  1118. public function testIsCallbackWithGlobalFunctionExpectingFalse() {
  1119. $fd = new FluentDOMCoreProxy();
  1120. $this->assertFalse(
  1121. $fd->_isCallback('strpos', FALSE, TRUE)
  1122. );
  1123. }
  1124. /**
  1125. * @group CoreFunctions
  1126. * @covers FluentDOMCore::_isCallback
  1127. */
  1128. public function testIsCallbackWithInvalidCallbackExpectingException() {
  1129. $fd = new FluentDOMCoreProxy();
  1130. try {
  1131. $fd->_isCallback('foo', FALSE, FALSE);
  1132. $this->fail('An expected exception has not been raised.');
  1133. } catch (InvalidArgumentException $expected) {
  1134. }
  1135. }
  1136. /**
  1137. * @group CoreFunctions
  1138. * @covers FluentDOMCore::_isCallback
  1139. */
  1140. public function testIsCallbackWithInvalidCallbackExpectingFalse() {
  1141. $fd = new FluentDOMCoreProxy();
  1142. $this->assertFalse(
  1143. $fd->_isCallback('foo', FALSE, TRUE)
  1144. );
  1145. }
  1146. /**
  1147. * @group CoreFunctions
  1148. * @covers FluentDOMCore::_getContentFragment
  1149. */
  1150. public function testGetContentFragmentIncludeTextNodes() {
  1151. $fragment = '<sample/>sample';
  1152. $fd = new FluentDOMCoreProxy();
  1153. $nodes = $fd->_getContentFragment($fragment);
  1154. $this->assertInstanceOf('DOMElement', $nodes[0]);
  1155. $this->assertInstanceOf('DOMText', $nodes[1]);
  1156. }
  1157. /**
  1158. * @group CoreFunctions
  1159. * @covers FluentDOMCore::_getContentFragment
  1160. */
  1161. public function testGetContentFragmentIncludeTextNodesLimit() {
  1162. $fragment = '<sample/>sample';
  1163. $fd = new FluentDOMCoreProxy();
  1164. $nodes = $fd->_getContentFragment($fragment, TRUE, 1);
  1165. $this->assertInstanceOf('DOMElement', $nodes[0]);
  1166. $this->assertEquals(1, count($nodes));
  1167. }
  1168. /**
  1169. * @group CoreFunctions
  1170. * @covers FluentDOMCore::_getContentFragment
  1171. */
  1172. public function testGetContentFragmentExcludeTextNodes() {
  1173. $fragment = '<sample/>sample';
  1174. $fd = new FluentDOMCoreProxy();
  1175. $nodes = $fd->_getContentFragment($fragment, FALSE);
  1176. $this->assertInstanceOf('DOMElement', $nodes[0]);
  1177. $this->assertEquals(1, count($nodes));
  1178. }
  1179. /**
  1180. * @group CoreFunctions
  1181. * @covers FluentDOMCore::_getContentFragment
  1182. */
  1183. public function testGetContentFragmentOnlyTextNodes() {
  1184. $fragment = 'sample';
  1185. $fd = new FluentDOMCoreProxy();
  1186. $nodes = $fd->_getContentFragment($fragment);
  1187. $this->assertInstanceOf('DOMText', $nodes[0]);
  1188. }
  1189. /**
  1190. * @group CoreFunctions
  1191. * @covers FluentDOMCore::_getContentFragment
  1192. */
  1193. public function testGetContentFragmentWithInvalidFragementExpectingException() {
  1194. $fragment = '<sample';
  1195. $fd = new FluentDOMCoreProxy();
  1196. try {
  1197. $nodes = $fd->_getContentFragment('', FALSE);
  1198. $this->fail('An expected exception has not been raised.');
  1199. } catch (UnexpectedValueException $expected) {
  1200. }
  1201. }
  1202. /**
  1203. * @group CoreFunctions
  1204. * @covers FluentDOMCore::_getContentNodes
  1205. */
  1206. public function testGetContentNodesWithDomelement() {
  1207. $fd = new FluentDOMCoreProxy();
  1208. $node = $fd->document->createElement('sample');
  1209. $this->assertEquals(
  1210. array($node),
  1211. $fd->_getContentNodes($node)
  1212. );
  1213. }
  1214. /**
  1215. * @group CoreFunctions
  1216. * @covers FluentDOMCore::_getContentNodes
  1217. */
  1218. public function testGetContentNodesWithDomtext() {
  1219. $fd = new FluentDOMCoreProxy();
  1220. $node = $fd->document->createTextNode('sample');
  1221. $this->assertEquals(
  1222. array($node),
  1223. $fd->_getContentNodes($node)
  1224. );
  1225. }
  1226. /**
  1227. * @group CoreFunctions
  1228. * @covers FluentDOMCore::_getContentNodes
  1229. */
  1230. public function testGetContentNodesWithDomtextIgnoringTextNodesExpectingException() {
  1231. $fd = new FluentDOMCoreProxy();
  1232. $node = $fd->document->createTextNode('sample');
  1233. try {
  1234. $fd->_getContentNodes($node, FALSE);
  1235. $this->fail('An expected exception has not been raised.');
  1236. } catch (InvalidArgumentException $expected) {
  1237. }
  1238. }
  1239. /**
  1240. * @group CoreFunctions
  1241. * @covers FluentDOMCore::_getContentNodes
  1242. */
  1243. public function testGetContentNodesWithString() {
  1244. $fd = new FluentDOMCoreProxy();
  1245. $nodes = $fd->_getContentNodes('sample');
  1246. $this->assertInstanceOf(
  1247. 'DOMText', $nodes[0]
  1248. );
  1249. }
  1250. /**
  1251. * @group CoreFunctions
  1252. * @covers FluentDOMCore::_getContentNodes
  1253. */
  1254. public function testGetContentNodesWithStringIgnoringTextNodesExpectingException() {
  1255. $fd = new FluentDOMCoreProxy();
  1256. try {
  1257. $fd->_getContentNodes('sample', FALSE);
  1258. $this->fail('An expected exception has not been raised.');
  1259. } catch (UnexpectedValueException $expected) {
  1260. }
  1261. }
  1262. /**
  1263. * @group CoreFunctions
  1264. * @covers FluentDOMCore::_getContentNodes
  1265. */
  1266. public function testGetContentNodesWithMixedArray() {
  1267. $fd = new FluentDOMCoreProxy();
  1268. $nodes = array(
  1269. 1,
  1270. 'foo',
  1271. $elementNode = $fd->document->createElement('sample'),
  1272. $textNode = $fd->document->createTextNode('sample')
  1273. );
  1274. $this->assertEquals(
  1275. array($elementNode, $textNode),
  1276. $fd->_getContentNodes($nodes)
  1277. );
  1278. }
  1279. /**
  1280. * @group CoreFunctions
  1281. * @covers FluentDOMCore::_getContentNodes
  1282. */
  1283. public function testGetContentNodesWithArrayIgnoringTextNodes() {
  1284. $fd = new FluentDOMCoreProxy();
  1285. $nodes = array(
  1286. $elementNode = $fd->document->createElement('sample'),
  1287. $textNode = $fd->document->createTextNode('sample')
  1288. );
  1289. $this->assertEquals(
  1290. array($elementNode),
  1291. $fd->_getContentNodes($nodes, FALSE)
  1292. );
  1293. }
  1294. /**
  1295. * @group CoreFunctions
  1296. * @covers FluentDOMCore::_getContentNodes
  1297. */
  1298. public function testGetContentNodesWithArrayAndLimit() {
  1299. $fd = new FluentDOMCoreProxy();
  1300. $nodes = array(
  1301. $elementNode = $fd->document->createElement('sample'),
  1302. $textNode = $fd->document->createTextNode('sample')
  1303. );
  1304. $this->assertEquals(
  1305. array($elementNode),
  1306. $fd->_getContentNodes($nodes, TRUE, 1)
  1307. );
  1308. }
  1309. /**
  1310. * @group CoreFunctions
  1311. * @covers FluentDOMCore::_getContentNodes
  1312. */
  1313. public function testGetContentNodesWithNodeFromAnotherDocument() {
  1314. $fd = new FluentDOMCoreProxy();
  1315. $dom = new DOMDocument();
  1316. $nodes = array(
  1317. $elementNode = $dom->createElement('sample')
  1318. );
  1319. $actual = $fd->_getContentNodes($nodes);
  1320. $this->assertThat(
  1321. $actual[0],
  1322. $this->logicalAnd(
  1323. $this->equalTo($elementNode),
  1324. $this->logicalNot(
  1325. $this->identicalTo($elementNode)
  1326. )
  1327. )
  1328. );
  1329. }
  1330. /**
  1331. * @group CoreFunctions
  1332. * @covers FluentDOMCore::_getContentElement
  1333. */
  1334. public function testGetContentElementWithDomelement() {
  1335. $fd = new FluentDOMCoreProxy();
  1336. $node = $fd->document->createElement('sample');
  1337. $this->assertSame(
  1338. $node,
  1339. $fd->_getContentElement($node)
  1340. );
  1341. }
  1342. /**
  1343. * @group CoreFunctions
  1344. * @covers FluentDOMCore::_getContentElement
  1345. */
  1346. public function testGetContentElementWithArray() {
  1347. $fd = new FluentDOMCoreProxy();
  1348. $textNode = $fd->document->createTextNode('sample');
  1349. $elementNode = $fd->document->createElement('sample');
  1350. $this->assertSame(
  1351. $elementNode,
  1352. $fd->_getContentElement(array($textNode, $elementNode))
  1353. );
  1354. }
  1355. /**
  1356. * @group CoreFunctions
  1357. * @covers FluentDOMCore::_getContextNodes
  1358. */
  1359. public function testGetContextNodesExpectingCurrentSelection() {
  1360. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '//item');
  1361. $this->assertSame(
  1362. $this->readAttribute($fd, '_array'),
  1363. $fd->_getContextNodes()
  1364. );
  1365. }
  1366. /**
  1367. * @group CoreFunctions
  1368. * @covers FluentDOMCore::_getContextNodes
  1369. */
  1370. public function testGetContextNodesWithExpressionExpectingArray() {
  1371. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML);
  1372. $this->assertSame(
  1373. $fd->document->documentElement,
  1374. $fd->_getContextNodes('/items')->item(0)
  1375. );
  1376. }
  1377. /**
  1378. * @group CoreFunctions
  1379. * @covers FluentDOMCore::_getTargetNodes
  1380. */
  1381. public function testGetTargetNodesWithSingleNodeExpectingArray() {
  1382. $fd = new FluentDOMCoreProxy();
  1383. $node = $fd->document->createElement('sample');
  1384. $this->assertSame(
  1385. array($node),
  1386. $fd->_getTargetNodes($node)
  1387. );
  1388. }
  1389. /**
  1390. * @group CoreFunctions
  1391. * @covers FluentDOMCore::_getTargetNodes
  1392. */
  1393. public function testGetTargetNodesWithStringExpectingDomnodelist() {
  1394. $fd = new FluentDOMCoreProxy();
  1395. $node = $fd->document->appendChild($fd->document->createElement('sample'));
  1396. $this->assertSame(
  1397. $node,
  1398. $fd->_getTargetNodes('/sample')->item(0)
  1399. );
  1400. }
  1401. /**
  1402. * @group CoreFunctions
  1403. * @covers FluentDOMCore::_getTargetNodes
  1404. */
  1405. public function testGetTargetNodesWithArrayExpectingArray() {
  1406. $fd = new FluentDOMCoreProxy();
  1407. $node = $fd->document->createElement('sample');
  1408. $this->assertSame(
  1409. array($node),
  1410. $fd->_getTargetNodes(array($node))
  1411. );
  1412. }
  1413. /**
  1414. * @group CoreFunctions
  1415. * @covers FluentDOMCore::_getTargetNodes
  1416. */
  1417. public function testGetTargetNodesWithInvalidSelectorExpectingException() {
  1418. $fd = new FluentDOMCoreProxy();
  1419. try {
  1420. $fd->_getTargetNodes(1);
  1421. $this->fail('An expected exception has not been raised.');
  1422. } catch (InvalidArgumentException $expected) {
  1423. }
  1424. }
  1425. /**
  1426. * @group CoreFunctions
  1427. * @covers FluentDOMCore::_getInnerXml
  1428. */
  1429. public function testGetInnerXml() {
  1430. $expect = '<item index="0">text1</item>'.
  1431. '<item index="1">text2</item>'.
  1432. '<item index="2">text3</item>';
  1433. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '//group');
  1434. $this->assertEquals($expect, $fd->_getInnerXml($fd->item(0)));
  1435. }
  1436. /**
  1437. * @group CoreFunctions
  1438. * @covers FluentDOMCore::_getInnerXml
  1439. */
  1440. public function testGetInnerXmlOnTextNode() {
  1441. $expect = 'text1';
  1442. $fd = $this->getFluentDOMCoreFixtureFromString(self::XML, '//group/item/text()');
  1443. $this->assertEquals($expect, $fd->_getInnerXml($fd->item(0)));
  1444. }
  1445. /**
  1446. * @group CoreFunctions
  1447. * @covers FluentDOMCore::_removeNodes
  1448. */
  1449. public function testRemoveNodes() {
  1450. $fd = new FluentDOMCoreProxy();
  1451. $node = $fd->document->appendChild($fd->document->createElement('sample'));
  1452. $actual = $fd->_removeNodes('/sample');
  1453. $this->assertSame(
  1454. array($node), $actual
  1455. );
  1456. $this->assertNull($actual[0]->parentNode);
  1457. $this->assertNull($fd->document->documentElement);
  1458. }
  1459. /**
  1460. * @group CoreFunctions
  1461. * @covers FluentDOMCore::_getHandler
  1462. */
  1463. public function testGetHandler() {
  1464. $fd = new FluentDOMCoreProxy();
  1465. $this->assertEquals(
  1466. 'FluentDOMHandler',
  1467. $fd->_getHandler()
  1468. );
  1469. }
  1470. /**
  1471. * @group CoreFunctions
  1472. * @covers FluentDOMCore::_applyContentToNodes
  1473. */
  1474. public function testApplyContentToNodes() {
  1475. $fd = new FluentDOMCoreProxy();
  1476. $fd->document->appendChild($fd->document->createElement('sample'));
  1477. $result = $fd->_applyContentToNodes(
  1478. array($fd->document->documentElement),
  1479. 'Hello World',
  1480. array($this, 'callbackHandlerForApplyContentToNodes')
  1481. );
  1482. $this->assertSame(
  1483. $fd->document->documentElement->childNodes->item(0), $result[0]
  1484. );
  1485. $this->assertEquals('Hello World', $result[0]->textContent);
  1486. }
  1487. /**
  1488. * @group CoreFunctions
  1489. * @covers FluentDOMCore::_applyContentToNodes
  1490. * @covers FluentDOMCore::_executeEasySetter
  1491. */
  1492. public function testApplyContentToNodesWithEasySetter() {
  1493. $fd = new FluentDOMCoreProxy();
  1494. $fd->document->appendChild(
  1495. $fd->document->createElement('sample')
  1496. );
  1497. $fd->document->documentElement->appendChild(
  1498. $fd->document->createTextNode('Hello World!')
  1499. );
  1500. $result = $fd->_applyContentToNodes(
  1501. array($fd->document->documentElement),
  1502. array($this, 'callbackEasySetterForApplyContentToNodes'),
  1503. array($this, 'callbackHandlerForApplyContentToNodes')
  1504. );
  1505. }
  1506. public function callbackHandlerForApplyContentToNodes($targetNode, $contentNodes) {
  1507. $targetNode->appendChild($contentNodes[0]);
  1508. return $contentNodes;
  1509. }
  1510. public function callbackEasySetterForApplyContentToNodes($node, $index, $value) {
  1511. $this->assertInstanceOf('DOMElement', $node);
  1512. $this->assertEquals(0, $index);
  1513. $this->assertEquals('Hello World!', $value);
  1514. return ' Hi Earth!';
  1515. }
  1516. /**
  1517. * @group CoreFunctions
  1518. * @covers FluentDOMCore::_executeEasySetter
  1519. */
  1520. public function testExecuteEasySetterExpectingEmptyArray() {
  1521. $fd = new FluentDOMCoreProxy();
  1522. $this->assertSame(
  1523. array(),
  1524. $fd->_executeEasySetter(
  1525. array($this, 'callbackEasySetterForEasySetterExpectingEmptyArray'),
  1526. NULL,
  1527. 0,
  1528. ''
  1529. )
  1530. );
  1531. }
  1532. public function callbackEasySetterForEasySetterExpectingEmptyArray($node, $index, $value) {
  1533. return NULL;
  1534. }
  1535. /******************************
  1536. * Fixtures
  1537. ******************************/
  1538. function getFluentDOMCoreFixtureFromString($string = NULL, $xpath = NULL) {
  1539. $fd = new FluentDOMCoreProxy();
  1540. if (!empty($string)) {
  1541. $dom = new DOMDocument();
  1542. $dom->loadXML($string);
  1543. $loader = $this->getMock('FluentDOMLoader');
  1544. $loader
  1545. ->expects($this->once())
  1546. ->method('load')
  1547. ->with($this->equalTo('mocked'))
  1548. ->will($this->returnValue($dom));
  1549. $fd->setLoaders(array($loader));
  1550. $fd->load('mocked');
  1551. if (!empty($xpath)) {
  1552. $query = new DOMXPath($dom);
  1553. $nodes = $query->evaluate($xpath);
  1554. $fd = $fd->spawn();
  1555. $fd->push($nodes);
  1556. }
  1557. }
  1558. return $fd;
  1559. }
  1560. }
  1561. /******************************
  1562. * Proxy
  1563. ******************************/
  1564. class FluentDOMCoreProxy extends FluentDOMCore {
  1565. public function _registerNodeNamespaces($registerNodeNS = NULL) {
  1566. return parent::_registerNodeNamespaces($registerNodeNS);
  1567. }
  1568. public function _evaluate($expr, DOMNode $context = NULL) {
  1569. return parent::_evaluate($expr, $context);
  1570. }
  1571. public function _match($expr, DOMNode $context = NULL) {
  1572. return parent::_match($expr, $context);
  1573. }
  1574. public function _test($expr, DOMNode $context = NULL) {
  1575. return parent::_test($expr, $context);
  1576. }
  1577. public function _uniqueSort() {
  1578. return parent::_uniqueSort();
  1579. }
  1580. public function _inList($node) {
  1581. return parent::_inList($node);
  1582. }
  1583. public function _isQName($name) {
  1584. return parent::_isQName($name);
  1585. }
  1586. public function _isNCName($name, $offset = 0, $length = 0) {
  1587. return parent::_isNCName($name, $offset, $length);
  1588. }
  1589. public function _isNode($node, $ignoreTextNodes = FALSE) {
  1590. return parent::_isNode($node, $ignoreTextNodes);
  1591. }
  1592. public function _isNodeList($elements) {
  1593. return parent::_isNodeList($elements);
  1594. }
  1595. public function _isCallback($callback, $allowGlobalFunctions, $silent) {
  1596. return parent::_isCallback($callback, $allowGlobalFunctions, $silent);
  1597. }
  1598. public function _getContentFragment($content, $includeTextNodes = TRUE, $limit = 0) {
  1599. return parent::_getContentFragment($content, $includeTextNodes, $limit);
  1600. }
  1601. public function _getContentNodes($content, $includeTextNodes = TRUE, $limit = 0) {
  1602. return parent:: _getContentNodes($content, $includeTextNodes, $limit);
  1603. }
  1604. public function _getContentElement($content) {
  1605. return parent::_getContentElement($content);
  1606. }
  1607. public function _getContextNodes($selector = NULL) {
  1608. return parent::_getContextNodes($selector);
  1609. }
  1610. public function _getTargetNodes($selector) {
  1611. return parent::_getTargetNodes($selector);
  1612. }
  1613. public function _getInnerXml($node) {
  1614. return parent::_getInnerXml($node);
  1615. }
  1616. public function _removeNodes($selector) {
  1617. return parent::_removeNodes($selector);
  1618. }
  1619. public function _getHandler() {
  1620. return parent::_getHandler();
  1621. }
  1622. public function _applyContentToNodes($targetNodes, $content, $handler) {
  1623. return parent::_applyContentToNodes($targetNodes, $content, $handler);
  1624. }
  1625. public function _executeEasySetter($easySetter, $node, $index, $value) {
  1626. return parent::_executeEasySetter($easySetter, $node, $index, $value);
  1627. }
  1628. }