Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frasmage committed Apr 18, 2024
1 parent 24a8111 commit e21ef5b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/DataType/DateTimeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function getNumericValue(mixed $value): null|int|float

public function getStringValue(mixed $value): null|string
{
return $value instanceof DateTimeInterface
return $value instanceof DateTimeInterface
? $value->copy()->setTimezone('UTC')->format(self::FORMAT)
: null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/DataType/ModelCollectionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private function loadModels(array $items)
}

$results[$class] = $class::query()->findMany($keys)
->keyBy(fn(Model $model) => $model->getKey());
->keyBy(fn (Model $model) => $model->getKey());
}

return $results;
Expand Down
1 change: 1 addition & 0 deletions src/DataType/SerializeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ public function isIdempotent(): bool
{
return false;
}

}
56 changes: 39 additions & 17 deletions src/Metable.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,12 @@ function (Builder $q) use ($keys) {
*
* @return void
*/
public function scopeWhereMeta(Builder $q, string $key, mixed $operator, mixed $value = null): void
{
public function scopeWhereMeta(
Builder $q,
string $key,
mixed $operator,
mixed $value = null
): void {
// Shift arguments if no operator is present.
if (!isset($value)) {
$value = $operator;
Expand All @@ -359,11 +363,10 @@ function (Builder $q) use ($key, $operator, $stringValue, $value) {
// If the value is a string and the string value is at the maximum length,
// we can optimize the query by looking up using the index first
// then compare the serialized value (not indexed) afterward to ensure correctness.
if (strlen($stringValue) === config(
'metable.stringValueIndexLength',
255
)
) {
if (strlen($stringValue) >= config(
'metable.stringValueIndexLength',
255
)) {
$handler = $this->getHandlerForValue($value);
if ($handler->isIdempotent()) {
$q->where('value', $operator, $handler->serializeValue($value));
Expand All @@ -385,8 +388,12 @@ function (Builder $q) use ($key, $operator, $stringValue, $value) {
*
* @return void
*/
public function scopeWhereMetaNumeric(Builder $q, string $key, mixed $operator, mixed $value = null): void
{
public function scopeWhereMetaNumeric(
Builder $q,
string $key,
mixed $operator,
mixed $value = null
): void {
// Shift arguments if no operator is present.
if (!isset($value)) {
$value = $operator;
Expand Down Expand Up @@ -468,7 +475,6 @@ public function scopeWhereMetaIsNull(Builder $q, string $key): void
});
}


public function scopeWhereMetaIsModel(
Builder $q,
string $key,
Expand Down Expand Up @@ -612,11 +618,25 @@ private function joinMetaTable(Builder $q, string $key, string $type = 'left'):
}

// Join the meta table to the query
$q->join("{$metaTable} as {$alias}", function (JoinClause $q) use ($relation, $key, $alias) {
$q->on($relation->getQualifiedParentKeyName(), '=', $alias . '.' . $relation->getForeignKeyName())
->where($alias . '.key', '=', $key)
->where($alias . '.' . $relation->getMorphType(), '=', $this->getMorphClass());
}, null, null, $type);
$q->join(
"{$metaTable} as {$alias}",
function (JoinClause $q) use ($relation, $key, $alias) {
$q->on(
$relation->getQualifiedParentKeyName(),
'=',
$alias . '.' . $relation->getForeignKeyName()
)
->where($alias . '.key', '=', $key)
->where(
$alias . '.' . $relation->getMorphType(),
'=',
$this->getMorphClass()
);
},
null,
null,
$type
);

// Return the alias so that the calling context can
// reference the table.
Expand Down Expand Up @@ -660,7 +680,7 @@ public function setRelation($relation, $value)
/**
* Set the entire relations array on the model.
*
* @param array $relations
* @param array $relations
* @return $this
*/
public function setRelations(array $relations)
Expand Down Expand Up @@ -707,7 +727,9 @@ protected function makeMeta(string $key = '', mixed $value = ''): Meta

protected function getAllDefaultMeta(): array
{
return property_exists($this, 'defaultMetaValues') ? $this->defaultMetaValues : [];
return property_exists($this, 'defaultMetaValues')
? $this->defaultMetaValues
: [];
}

private function valueToString(mixed $value): string
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/DataType/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class HandlerTest extends TestCase
{
private static $resource;
static public function handlerProvider(): array
public static function handlerProvider(): array
{
$dateString = '2017-01-01 00:00:00.000000+0000';
$datetime = Carbon::createFromFormat('Y-m-d H:i:s.uO', $dateString);
Expand Down
1 change: 1 addition & 0 deletions tests/Integration/DataType/SerializableHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ public function test_it_configures_allowed_classes(): void
);
$this->assertEquals($incomplete, $handler->unserializeValue($serialized));
}

}

0 comments on commit e21ef5b

Please sign in to comment.