Skip to content

Commit 9b6d24a

Browse files
committed
Fix database expressions to strings for tests
1 parent 46a8503 commit 9b6d24a

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/CacheKey.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Exception;
44
use GeneaLabs\LaravelModelCaching\Traits\CachePrefixing;
5+
use Illuminate\Database\Query\Expression;
56
use Illuminate\Support\Arr;
67
use Illuminate\Support\Collection;
78
use Illuminate\Support\Str;
@@ -170,14 +171,19 @@ protected function getOrderByClauses() : string
170171
}
171172

172173
$orders = collect($this->query->orders);
173-
174+
174175
return $orders
175176
->reduce(function ($carry, $order) {
176177
if (($order["type"] ?? "") === "Raw") {
177178
return $carry . "_orderByRaw_" . (new Str)->slug($order["sql"]);
178179
}
179180

180-
return $carry . "_orderBy_" . $order["column"] . "_" . $order["direction"];
181+
return sprintf(
182+
'%s_orderBy_%s_%s',
183+
$carry,
184+
$this->expressionToString($order["column"]),
185+
$order["direction"]
186+
);
181187
})
182188
?: "";
183189
}
@@ -211,7 +217,11 @@ protected function getQueryColumns(array $columns) : string
211217
if (property_exists($this->query, "columns")
212218
&& $this->query->columns
213219
) {
214-
return "_" . implode("_", $this->query->columns);
220+
$columns = array_map(function ($column) {
221+
return $this->expressionToString($column);
222+
}, $this->query->columns);
223+
224+
return "_" . implode("_", $columns);
215225
}
216226

217227
return "_" . implode("_", $columns);
@@ -393,12 +403,14 @@ protected function recursiveImplode(array $items, string $glue = ",") : string
393403
return $result;
394404
}
395405

396-
private function processEnum(\BackedEnum|\UnitEnum|string $value): string
406+
private function processEnum(\BackedEnum|\UnitEnum|Expression|string $value): string
397407
{
398408
if ($value instanceof \BackedEnum) {
399409
return $value->value;
400410
} elseif ($value instanceof \UnitEnum) {
401411
return $value->name;
412+
} elseif ($value instanceof Expression) {
413+
return $this->expressionToString($value);
402414
}
403415

404416
return $value;
@@ -408,4 +420,13 @@ private function processEnums(array $values): array
408420
{
409421
return array_map(fn($value) => $this->processEnum($value), $values);
410422
}
423+
424+
private function expressionToString(Expression|string $value): string
425+
{
426+
if (is_string($value)) {
427+
return $value;
428+
}
429+
430+
return $value->getValue($this->query->getConnection()->getQueryGrammar());
431+
}
411432
}

0 commit comments

Comments
 (0)