From 5434cf4ba5ba91c970b4a6d769eb220d20c72f9b Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Tue, 17 Oct 2017 16:03:10 -0700 Subject: [PATCH] Refactor where clause parsing --- phpunit.xml | 2 +- src/CacheKey.php | 59 ++++++++++++++++++++++++-------- tests/Unit/CachedBuilderTest.php | 3 -- 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index c3ae6e6..730c6c8 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -6,7 +6,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false" + stopOnFailure="true" syntaxCheck="false" bootstrap="vendor/autoload.php" > diff --git a/src/CacheKey.php b/src/CacheKey.php index 00fd605..bf2e65a 100644 --- a/src/CacheKey.php +++ b/src/CacheKey.php @@ -96,25 +96,54 @@ protected function getWhereClauses(array $wheres = []) : string { return $this->getWheres($wheres) ->reduce(function ($carry, $where) { - if (in_array($where['type'], ['Exists', 'Nested', 'NotExists'])) { - return '_' . strtolower($where['type']) . $this->getWhereClauses($where['query']->wheres); - } + $value = $this->getNestedClauses($where); + $value .= $this->getColumnClauses($where); + $value .= $this->getRawClauses($where); + $value .= $this->getOtherClauses($where, $carry); - if ($where['type'] === 'Column') { - return "_{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}"; - } + return $value; + }) + . ''; + } - if ($where['type'] === 'raw') { - return "_{$where['boolean']}_" . str_slug($where['sql']); - } + protected function getNestedClauses(array $where) : string + { + if (! in_array($where['type'], ['Exists', 'Nested', 'NotExists'])) { + return ''; + } - $value = array_get($where, 'value'); - $value .= $this->getTypeClause($where); - $value .= $this->getValuesClause($where); + return '_' . strtolower($where['type']) . $this->getWhereClauses($where['query']->wheres); + } - return "{$carry}-{$where['column']}_{$value}"; - }) - . ''; + protected function getColumnClauses(array $where) : string + { + if ($where['type'] !== 'Column') { + return ''; + } + + return "_{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}"; + } + + protected function getRawClauses(array $where) : string + { + if ($where['type'] !== 'raw') { + return ''; + } + + return "_{$where['boolean']}_" . str_slug($where['sql']); + } + + protected function getOtherClauses(array $where, string $carry = null) : string + { + if (in_array($where['type'], ['Exists', 'Nested', 'NotExists', 'raw', 'Column'])) { + return ''; + } + + $value = array_get($where, 'value'); + $value .= $this->getTypeClause($where); + $value .= $this->getValuesClause($where); + + return "{$carry}-{$where['column']}_{$value}"; } protected function getWheres(array $wheres) : Collection diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index 161e853..db1a00f 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -269,9 +269,6 @@ public function testCountModelResultsCreatesCache() $this->assertEquals($liveResults, $cachedResults); } - /** - * @group test - */ public function testCursorModelResultsCreatesCache() { $authors = (new Author)