From 1e77eae57378e85319084b42470a2e24620e184f Mon Sep 17 00:00:00 2001 From: Imangazaliev Date: Thu, 15 Oct 2015 06:48:45 +0400 Subject: [PATCH 1/2] Fixed bug --- src/DiDom/Query.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/DiDom/Query.php b/src/DiDom/Query.php index 6ceb7c6..8d73507 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); From 5c30b6de8c0f75924947051454735f6597d6d04b Mon Sep 17 00:00:00 2001 From: Imangazaliev Date: Thu, 15 Oct 2015 06:50:51 +0400 Subject: [PATCH 2/2] More tests --- tests/DiDom/QueryTest.php | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) 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; + } }