XPathQuery.test.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright (c) 2010-2017 Arne Blankerts <arne@blankerts.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of Arne Blankerts nor the names of contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT * NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER ORCONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. *
  33. * @category PHP
  34. * @package TheSeer\fDOM
  35. * @author Arne Blankerts <arne@blankerts.de>
  36. * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
  37. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  38. * @link http://github.com/theseer/fdomdocument
  39. *
  40. */
  41. namespace TheSeer\fDOM\Tests {
  42. use TheSeer\fDOM\XPathQuery;
  43. use TheSeer\fDOM\fDOMDocument;
  44. class XPathQueryTest extends \PHPUnit\Framework\TestCase {
  45. private $dom;
  46. protected function setUp() {
  47. $this->dom = new fDOMDocument();
  48. $this->dom->loadXML('<?xml version="1.0" ?><root attr="value" />');
  49. }
  50. public function testFindingKeysInQueryWorks() {
  51. $xp = new XPathQuery(':key');
  52. $this->assertEquals(array('key'), $xp->getKeys());
  53. }
  54. /**
  55. * @expectedException TheSeer\fDOM\XPathQueryException
  56. */
  57. public function testTryingToBindNonExistingKeyThrowsException() {
  58. $xp = new XPathQuery(':key');
  59. $xp->bind('other', 123);
  60. }
  61. public function testBoundValueForKeyGetsApplied() {
  62. $xp = new XPathQuery(':key');
  63. $xp->bind('key', 123);
  64. $this->assertEquals('"123"', $xp->generate($this->dom));
  65. }
  66. public function testAppliedValueForKeyIsUsedOnQueryAndReturnsNode() {
  67. $xp = new XPathQuery('//*[@attr = :key]');
  68. $xp->bind('key', 'value');
  69. $res = $xp->query($this->dom);
  70. $this->assertInstanceOf('\DOMNodelist', $res);
  71. $this->assertEquals(1, $res->length);
  72. $this->assertInstanceOf('\DOMNode', $res->item(0));
  73. }
  74. public function testOverwriteValueOnQuery() {
  75. $xp = new XPathQuery('//*[@attr = :key]');
  76. $xp->bind('key', 'first');
  77. $res = $xp->query($this->dom, array('key' => 'value'));
  78. $this->assertEquals(1, $res->length);
  79. $this->assertInstanceOf('\DOMNode', $res->item(0));
  80. }
  81. public function testAppliedValueForKeyIsUsedOnEvaluateAndReturnsNode() {
  82. $xp = new XPathQuery('//*[@attr = :key]');
  83. $xp->bind('key', 'value');
  84. $res = $xp->evaluate($this->dom);
  85. $this->assertInstanceOf('\DOMNodelist', $res);
  86. $this->assertEquals(1, $res->length);
  87. $this->assertInstanceOf('\DOMNode', $res->item(0));
  88. }
  89. public function testOverwriteValueOnEvaluate() {
  90. $xp = new XPathQuery('//*[@attr = :key]');
  91. $xp->bind('key', 'first');
  92. $res = $xp->evaluate($this->dom, array('key' => 'value'));
  93. $this->assertEquals(1, $res->length);
  94. $this->assertInstanceOf('\DOMNode', $res->item(0));
  95. }
  96. public function testCallToQueryOneReturnsOneNode() {
  97. $xp = new XPathQuery('//*[@attr]');
  98. $res = $xp->queryOne($this->dom);
  99. $this->assertInstanceOf('\DOMNode', $res);
  100. }
  101. public function testQueryCanBeRunWithStandardDomDocument() {
  102. $xp = new XPathQuery('/');
  103. $res = $xp->query(new \DomDocument());
  104. $this->assertInstanceOf('\DOMNodelist', $res);
  105. }
  106. }
  107. }