2
0
Просмотр исходного кода

ZF-4010
Zend_Test_PHPUnit_Constraint_DomQuery allows matching of attribute values.
Although the original issue seems to be related to the PHP version used, the present fix is PHP version independent.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18224 44c647ce-9c0f-0410-b52a-842ac1e357ba

sgehrig 16 лет назад
Родитель
Сommit
e4f497ea46

+ 9 - 5
library/Zend/Test/PHPUnit/Constraint/DomQuery.php

@@ -392,10 +392,14 @@ class Zend_Test_PHPUnit_Constraint_DomQuery extends PHPUnit_Framework_Constraint
      */
     protected function _getNodeContent(DOMNode $node)
     {
-        $doc     = $node->ownerDocument;
-        $content = $doc->saveXML($node);
-        $tag     = $node->nodeName;
-        $regex   = '|</?' . $tag . '[^>]*>|';
-        return preg_replace($regex, '', $content);
+        if ($node instanceof DOMAttr) {
+            return $node->value;
+        } else {
+            $doc     = $node->ownerDocument;
+            $content = $doc->saveXML($node);
+            $tag     = $node->nodeName;
+            $regex   = '|</?' . $tag . '[^>]*>|';
+            return preg_replace($regex, '', $content);
+        }
     }
 }

+ 10 - 1
tests/Zend/Dom/Query/Css2XpathTest.php

@@ -40,7 +40,7 @@ require_once 'Zend/Dom/Query/Css2Xpath.php';
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  * @group      Zend_Dom
  */
-class Zend_Dom_Query_Css2XpathTest extends PHPUnit_Framework_TestCase 
+class Zend_Dom_Query_Css2XpathTest extends PHPUnit_Framework_TestCase
 {
     /**
      * Runs the test methods of this class.
@@ -174,6 +174,15 @@ class Zend_Dom_Query_Css2XpathTest extends PHPUnit_Framework_TestCase
         $test = Zend_Dom_Query_Css2Xpath::transform('div[foo*="bar"]');
         $this->assertEquals("//div[contains(@foo, 'bar')]", $test);
     }
+
+    /**
+     * @group ZF-4010
+     */
+    public function testShouldAllowMatchingOfAttributeValues()
+    {
+        $test = Zend_Dom_Query_Css2Xpath::transform('tag#id @attribute');
+        $this->assertEquals("//tag[@id='id']//@attribute", $test);
+    }
 }
 
 // Call Zend_Dom_Query_Css2XpathTest::main() if this source file is executed directly.

+ 2 - 0
tests/Zend/Test/PHPUnit/AllTests.php

@@ -31,6 +31,7 @@ if (!defined('PHPUnit_MAIN_METHOD')) {
 
 require_once 'Zend/Test/PHPUnit/ControllerTestCaseTest.php';
 require_once 'Zend/Test/PHPUnit/Db/AllTests.php';
+require_once 'Zend/Test/PHPUnit/Constraint/AllTests.php';
 
 /**
  * @category   Zend
@@ -54,6 +55,7 @@ class Zend_Test_PHPUnit_AllTests
 
         $suite->addTestSuite('Zend_Test_PHPUnit_ControllerTestCaseTest');
         $suite->addTest(Zend_Test_PHPUnit_Db_AllTests::suite());
+        $suite->addTest(Zend_Test_PHPUnit_Constraint_AllTests::suite());
 
         return $suite;
     }

+ 64 - 0
tests/Zend/Test/PHPUnit/Constraint/AllTests.php

@@ -0,0 +1,64 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Test
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../../../TestHelper.php';
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Zend_Test_PHPUnit_Constraint_AllTests::main');
+}
+
+/**
+ * @category   Zend
+ * @package    Zend_Test
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @group      Zend_Test
+ * @group      Zend_Test_PHPUnit
+ */
+class Zend_Test_PHPUnit_Constraint_AllTests
+{
+    public static function main()
+    {
+        PHPUnit_TextUI_TestRunner::run(self::suite());
+    }
+
+    public static function suite()
+    {
+        $suite = new PHPUnit_Framework_TestSuite('Zend Framework - Zend_Test_PHPUnit_Constraint');
+
+        /**
+         * @see Zend_Test_PHPUnit_Constraint_DomQueryTest
+         */
+        require_once 'Zend/Test/PHPUnit/Constraint/DomQueryTest.php';
+        $suite->addTestSuite('Zend_Test_PHPUnit_Constraint_DomQueryTest');
+
+        return $suite;
+    }
+}
+
+if (PHPUnit_MAIN_METHOD == 'Zend_Test_PHPUnit_Constraint_AllTests::main') {
+    Zend_Test_PHPUnit_Constraint_AllTests::main();
+}

+ 68 - 0
tests/Zend/Test/PHPUnit/Constraint/DomQueryTest.php

@@ -0,0 +1,68 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Test
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+require_once dirname(__FILE__) . '/../../../../TestHelper.php';
+
+/**
+ * @see Zend_Test_PHPUnit_Constraint_DomQuery
+ */
+require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Test
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @group      Zend_Test
+ * @group      Zend_Test_PHPUnit
+ */
+class Zend_Test_PHPUnit_Constraint_DomQueryTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * @group ZF-4010
+     */
+    public function testShouldAllowMatchingOfAttributeValues()
+    {
+        $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html>
+    <head>
+        <title>ZF Issue ZF-4010</title>
+    </head>
+    <body>
+        <form>
+            <fieldset id="fieldset-input"><legend>Inputs</legend>
+                <ol>
+                    <li><input type="text" name="input[0]" id="input-0" value="value1" /></li>
+                    <li><input type="text" name="input[1]" id="input-1" value="value2" /></li>
+                    <li><input type="text" name="input[2]" id="input-2" value="" /></li>
+                </ol>
+            </fieldset>
+        </form>
+    </body>
+</html>';
+        $assertion = new Zend_Test_PHPUnit_Constraint_DomQuery('input#input-0 @value');
+        $result = $assertion->evaluate($html,
+            Zend_Test_PHPUnit_Constraint_DomQuery::ASSERT_CONTENT_CONTAINS, 'value1');
+        $this->assertTrue($result);
+    }
+}