From df19f699d43797e5cb1918149c8cb115304a4386 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 6 Feb 2025 08:56:58 +0100 Subject: [PATCH] Fix tests. --- app/Support/Search/QueryParser/Node.php | 32 +++++++++++++++++++ .../Search/QueryParser/QueryParser.php | 15 ++++++--- ...ractQueryParserInterfaceParseQueryTest.php | 2 +- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/app/Support/Search/QueryParser/Node.php b/app/Support/Search/QueryParser/Node.php index 8472938653..805ea3f325 100644 --- a/app/Support/Search/QueryParser/Node.php +++ b/app/Support/Search/QueryParser/Node.php @@ -55,4 +55,36 @@ abstract class Node return $this->prohibited; } + + public function equals(Node $compare): bool + { + if ($compare->isProhibited(false) !== $this->isProhibited(false)) { + Log::debug('Return false because prohibited status is different'); + return false; + } + if ($compare instanceof NodeGroup) { + if (count($compare->getNodes()) !== count($this->getNodes())) { + Log::debug(sprintf('Return false because node count is different. Original is %d, compare is %d', count($this->getNodes()), count($compare->getNodes()))); +// var_dump($this); +// var_dump($compare); +// exit; + return false; + } + /** + * @var int $index + * @var Node $node + */ + foreach ($this->getNodes() as $index => $node) { + if (false === $node->equals($compare->getNodes()[$index])) { + Log::debug('Return false because nodes are different!'); + var_dump($this); + var_dump($compare); + exit; + return false; + } + } + return true; + } + return true; + } } diff --git a/app/Support/Search/QueryParser/QueryParser.php b/app/Support/Search/QueryParser/QueryParser.php index 6e9c1ea1e2..feca4d718b 100644 --- a/app/Support/Search/QueryParser/QueryParser.php +++ b/app/Support/Search/QueryParser/QueryParser.php @@ -71,6 +71,7 @@ class QueryParser implements QueryParserInterface while ($this->position < strlen($this->query)) { $char = $this->query[$this->position]; + //Log::debug(sprintf('Char #%d: %s', $this->position, $char)); // If we're in a quoted string, we treat all characters except another quote as ordinary characters if ($inQuotes) { @@ -150,15 +151,19 @@ class QueryParser implements QueryParserInterface case ':': - if ('' !== $tokenUnderConstruction) { + $skipNext = false; + if ('' === $tokenUnderConstruction) { // @phpstan-ignore-line + // In any other location, it's just a normal character + $tokenUnderConstruction .= $char; + $skipNext = true; + } + if ('' !== $tokenUnderConstruction && !$skipNext) { + Log::debug(sprintf('Turns out that "%s" is a field name. Reset the token.', $tokenUnderConstruction)); // If we meet a colon with a left-hand side string, we know we're in a field and are about to set up the value $fieldName = $tokenUnderConstruction; $tokenUnderConstruction = ''; } - if ('' === $tokenUnderConstruction) { // @phpstan-ignore-line - // In any other location, it's just a normal character - $tokenUnderConstruction .= $char; - } + break; diff --git a/tests/unit/Support/Search/QueryParser/AbstractQueryParserInterfaceParseQueryTest.php b/tests/unit/Support/Search/QueryParser/AbstractQueryParserInterfaceParseQueryTest.php index 5d99d11aac..f68cb80969 100644 --- a/tests/unit/Support/Search/QueryParser/AbstractQueryParserInterfaceParseQueryTest.php +++ b/tests/unit/Support/Search/QueryParser/AbstractQueryParserInterfaceParseQueryTest.php @@ -192,7 +192,7 @@ abstract class AbstractQueryParserInterfaceParseQueryTest extends TestCase { $actual = $this->createParser()->parse($query); - self::assertSame($expected, $actual); + self::assertObjectEquals($expected, $actual); } }