diff --git a/src/DiDom/Query.php b/src/DiDom/Query.php index 7593e3a..8e0d55d 100644 --- a/src/DiDom/Query.php +++ b/src/DiDom/Query.php @@ -40,6 +40,7 @@ public static function compile($expression, $type = self::TYPE_CSS) if (array_key_exists($expression, static::$compiled)) { $paths[] = static::$compiled[$expression]; + continue; } static::$compiled[$expression] = static::cssToXpath($expression); diff --git a/tests/DiDom/QueryTest.php b/tests/DiDom/QueryTest.php index b1153f5..fd9ca04 100644 --- a/tests/DiDom/QueryTest.php +++ b/tests/DiDom/QueryTest.php @@ -7,7 +7,20 @@ class QueryTest extends TestCase { - public function testSetCompiledException() + /** + * @dataProvider compiledCssProvider + */ + public function testCompileCssSelector($selector, $xpath) + { + $this->assertEquals($xpath, Query::compile($selector)); + } + + public function testCompileXpath() + { + $this->assertEquals('xpath-expression', Query::compile('xpath-expression', Query::TYPE_XPATH)); + } + + public function testSetCompiledInvalidArgument() { $this->setExpectedException('InvalidArgumentException'); @@ -29,12 +42,23 @@ public function testGetCompiled() Query::setCompiled([]); $selector = '.post h2'; - $xpath = "//*[contains(concat(' ', normalize-space(@class), ' '), ' post ')]"; + $xpath = '//*[contains(concat(" ", normalize-space(@class), " "), " post ")]//h2'; $compiled = [$selector => $xpath]; - $xpath = Query::compile($selector); - $compiled = Query::getCompiled(); + Query::compile($selector); $this->assertEquals($compiled, Query::getCompiled()); } + + public function compiledCssProvider() + { + $compiled = [ + ['h2', '//h2'], + ['.post h2', '//*[contains(concat(" ", normalize-space(@class), " "), " post ")]//h2'], + ['.post, h2', '//*[contains(concat(" ", normalize-space(@class), " "), " post ")]|//h2'], + ['div#layout', "//div[@id='layout']"], + ]; + + return $compiled; + } }