Skip to content

Commit 34fd2c2

Browse files
committed
Fix parsing of between clause, bring tests to green
1 parent a6b0006 commit 34fd2c2

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

src/CacheKey.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,30 +111,40 @@ protected function getValuesClause(array $where = null) : string
111111
}
112112

113113
$values = $this->getValuesFromWhere($where);
114-
$values = $this->getValuesFromBindings($values);
114+
$values = $this->getValuesFromBindings($where, $values);
115+
116+
115117

116118
return "_" . $values;
117119
}
118120

119121
protected function getValuesFromWhere(array $where) : string
120122
{
121-
return is_array(array_get($where, "values"))
122-
? implode("_", $where["values"])
123-
: "";
123+
if (is_array(array_get($where, "values"))) {
124+
return implode("_", $where["values"]);
125+
}
126+
127+
return array_get($where, "value", "");
124128
}
125129

126-
protected function getValuesFromBindings(string $values) : string
130+
protected function getValuesFromBindings(array $where, string $values) : string
127131
{
128-
if (! $values && $this->query->bindings["where"] ?? false) {
132+
if (! $values && ($this->query->bindings["where"] ?? false)) {
129133
$values = $this->query->bindings["where"][$this->currentBinding];
134+
$this->currentBinding++;
135+
136+
if ($where["type"] === "between") {
137+
$values .= "_" . $this->query->bindings["where"][$this->currentBinding];
138+
$this->currentBinding++;
139+
}
130140
}
131141

132-
return $values;
142+
return $values ?: "";
133143
}
134144

135145
protected function getWhereClauses(array $wheres = []) : string
136146
{
137-
$whereClause = $this->getWheres($wheres)
147+
return "" . $this->getWheres($wheres)
138148
->reduce(function ($carry, $where) {
139149
$value = $carry;
140150
$value .= $this->getNestedClauses($where);
@@ -143,10 +153,7 @@ protected function getWhereClauses(array $wheres = []) : string
143153
$value .= $this->getOtherClauses($where, $carry);
144154

145155
return $value;
146-
})
147-
. "";
148-
149-
return $whereClause;
156+
});
150157
}
151158

152159
protected function getNestedClauses(array $where) : string
@@ -157,7 +164,7 @@ protected function getNestedClauses(array $where) : string
157164

158165
$this->currentBinding++;
159166

160-
return "_" . strtolower($where["type"]) . $this->getWhereClauses($where["query"]->wheres);
167+
return "-" . strtolower($where["type"]) . $this->getWhereClauses($where["query"]->wheres);
161168
}
162169

163170
protected function getColumnClauses(array $where) : string
@@ -168,7 +175,7 @@ protected function getColumnClauses(array $where) : string
168175

169176
$this->currentBinding++;
170177

171-
return "_{$where["boolean"]}_{$where["first"]}_{$where["operator"]}_{$where["second"]}";
178+
return "-{$where["boolean"]}_{$where["first"]}_{$where["operator"]}_{$where["second"]}";
172179
}
173180

174181
protected function getRawClauses(array $where) : string
@@ -192,7 +199,7 @@ protected function getRawClauses(array $where) : string
192199
$clause .= "_" . $lastPart;
193200
}
194201

195-
return str_replace(" ", "_", $clause);
202+
return "-" . str_replace(" ", "_", $clause);
196203
}
197204

198205
protected function getOtherClauses(array $where, string $carry = null) : string
@@ -203,9 +210,8 @@ protected function getOtherClauses(array $where, string $carry = null) : string
203210

204211
$value = $this->getTypeClause($where);
205212
$value .= $this->getValuesClause($where);
206-
$this->currentBinding++;
207213

208-
return "{$where["column"]}_{$value}";
214+
return "-{$where["column"]}_{$value}";
209215
}
210216

211217
protected function getWheres(array $wheres) : Collection

tests/Integration/CachedBuilder/WhereRawTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public function testRawWhereClauseParsing()
3737
$this->assertTrue($liveResults->diffKeys($cachedResults)->isEmpty());
3838
}
3939

40-
/** @group test */
4140
public function testWhereRawWithQueryParameters()
4241
{
4342
$authorName = (new Author)->first()->name;
@@ -60,7 +59,6 @@ public function testWhereRawWithQueryParameters()
6059
$this->assertTrue($liveResults->diffKeys($cachedResults)->isEmpty());
6160
}
6261

63-
/** @group test */
6462
public function testMultipleWhereRawCacheUniquely()
6563
{
6664
$book1 = (new UncachedBook)->first();

tests/Integration/CachedBuilderRelationshipsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testHasRelationshipResults()
2525
->with("stores")
2626
->has("stores")
2727
->get();
28-
$key = "genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook_exists_and_books.id_=_book_store.book_id-stores";
28+
$key = "genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook-exists-and_books.id_=_book_store.book_id-stores";
2929
$tags = [
3030
"genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook",
3131
"genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesstore",

tests/Integration/CachedBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ public function testExistsRelationshipWhereClauseParsing()
523523
$authors = (new Author)->whereHas('books')
524524
->get();
525525

526-
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor_exists_and_authors.id_=_books.author_id');
526+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor-exists-and_authors.id_=_books.author_id');
527527
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
528528

529529
$cachedResults = $this->cache()

tests/Integration/CachedModelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function testWhereHasIsBeingCached()
119119
})
120120
->get();
121121

122-
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook_exists_and_books.author_id_=_authors.id-id_=_1-author');
122+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook-exists-and_books.author_id_=_authors.id-id_=_1-author');
123123
$tags = [
124124
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook',
125125
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',

0 commit comments

Comments
 (0)