Skip to content

Commit

Permalink
use current infra better
Browse files Browse the repository at this point in the history
  • Loading branch information
mrceperka committed Oct 19, 2022
1 parent 20f8740 commit 2419af1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
19 changes: 4 additions & 15 deletions src/SqlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ function ($matches) use ($args, &$j, $last): string {
*/
public function processModifier(string $type, $value): string
{
if ($value instanceof \BackedEnum) {
$value = $value->value;
}

if ($type === 'any') {
$type = $this->detectType($value) ?? 'any';
}
Expand Down Expand Up @@ -316,21 +320,6 @@ public function processModifier(string $type, $value): string
case '_like_':
return $this->platform->formatStringLike((string) $value, 0);
}
} elseif ($value instanceof \BackedEnum) {
$valueType = gettype($value->value);
if ($type === 's') {
if ($valueType === 'string') {
// we know for sure that value is string...
// @phpstan-ignore-next-line
return $this->platform->formatString($value->value);
} elseif ($valueType === 'integer') {
return (string) $value->value;
}
} elseif ($type === 'i') {
if ($valueType === 'integer') {
return (string) $value->value;
}
}
}

break;
Expand Down
23 changes: 18 additions & 5 deletions tests/cases/unit/SqlProcessorTest.backed_enum.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ class SqlProcessorBackedEnumTest extends TestCase

$cases = BinaryEnum::cases();
foreach ($cases as $case) {
Assert::same((string) $case->value, $this->parser->processModifier('s', $case));
Assert::exception(
function () use ($case) {
$this->parser->processModifier('s', $case);
},
InvalidArgumentException::class,
'Modifier %s expects value to be string, integer given.'
);
}

}
Expand All @@ -73,8 +79,14 @@ class SqlProcessorBackedEnumTest extends TestCase
->andReturnArg(0);
Assert::same('(left, right)', $this->parser->processModifier('s[]', $cases));

$cases = BinaryEnum::cases();
Assert::same('(0, 1)', $this->parser->processModifier('s[]', $cases));
Assert::exception(
function () {
$cases = BinaryEnum::cases();
$this->parser->processModifier('s[]', $cases);
},
InvalidArgumentException::class,
'Modifier %s expects value to be string, integer given.'
);

}

Expand All @@ -88,7 +100,8 @@ class SqlProcessorBackedEnumTest extends TestCase
$this->parser->processModifier('i', $case);
},
InvalidArgumentException::class,
'Modifier %i expects value to be int, NextrasTests\Dbal\DirectionEnum given.');
'Modifier %i expects value to be int, string given.'
);
}

$cases = BinaryEnum::cases();
Expand All @@ -107,7 +120,7 @@ class SqlProcessorBackedEnumTest extends TestCase
$this->parser->processModifier('i[]', $cases);
},
InvalidArgumentException::class,
'Modifier %i expects value to be int, NextrasTests\Dbal\DirectionEnum given.'
'Modifier %i expects value to be int, string given.'
);

$cases = BinaryEnum::cases();
Expand Down

0 comments on commit 2419af1

Please sign in to comment.