Skip to content

Commit 41421ce

Browse files
authored
Add route name to expression invalid return type exception (#106)
1 parent 8c7997b commit 41421ce

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

src/Exception/InvalidIfExpressionResultException.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
final class InvalidIfExpressionResultException extends \TypeError implements PurgatoryException
88
{
9-
private const MESSAGE = 'Expected return value of "if" expression (%s) to be boolean, got %s.';
9+
private const MESSAGE = 'Expected the return value of "if" expression (%s) for route "%s" to be boolean, got %s.';
1010

1111
public function __construct(
12+
public readonly string $routeName,
1213
public readonly string $expression,
1314
public readonly mixed $result,
1415
) {
15-
parent::__construct(\sprintf(self::MESSAGE, $expression, get_debug_type($result)));
16+
parent::__construct(\sprintf(self::MESSAGE, $expression, $routeName, get_debug_type($result)));
1617
}
1718
}

src/RouteProvider/AbstractEntityRouteProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private function processValidSubscriptions(Subscriptions $subscriptions, array $
7575
if (isset($subscription['if'])) {
7676
$result = $this->getExpressionLanguage()->evaluate($subscription['if'], ['obj' => $entity]);
7777
if (!\is_bool($result)) {
78-
throw new InvalidIfExpressionResultException($subscription['if'], $result);
78+
throw new InvalidIfExpressionResultException($subscription['routeName'], $subscription['if'], $result);
7979
}
8080

8181
if (!$result) {

tests/RouteProvider/UpdatedEntityRouteProviderTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,12 @@ public function testRouteParamsWithRawValuesAndEnumValues(): void
365365
self::assertSame(['name' => 'foo_route', 'params' => ['foo' => 'case3']], (array) $routes[5]);
366366
}
367367

368-
#[TestWith([null, 'Expected return value of "if" expression (obj.val) to be boolean, got null.'])]
369-
#[TestWith([1, 'Expected return value of "if" expression (obj.val) to be boolean, got int.'])]
370-
#[TestWith([0.0, 'Expected return value of "if" expression (obj.val) to be boolean, got float.'])]
371-
#[TestWith(['false', 'Expected return value of "if" expression (obj.val) to be boolean, got string.'])]
372-
#[TestWith([[true], 'Expected return value of "if" expression (obj.val) to be boolean, got array.'])]
373-
#[TestWith([new \stdClass(), 'Expected return value of "if" expression (obj.val) to be boolean, got stdClass.'])]
368+
#[TestWith([null, 'Expected the return value of "if" expression (obj.val) for route "foo_route" to be boolean, got null.'])]
369+
#[TestWith([1, 'Expected the return value of "if" expression (obj.val) for route "foo_route" to be boolean, got int.'])]
370+
#[TestWith([0.0, 'Expected the return value of "if" expression (obj.val) for route "foo_route" to be boolean, got float.'])]
371+
#[TestWith(['false', 'Expected the return value of "if" expression (obj.val) for route "foo_route" to be boolean, got string.'])]
372+
#[TestWith([[true], 'Expected the return value of "if" expression (obj.val) for route "foo_route" to be boolean, got array.'])]
373+
#[TestWith([new \stdClass(), 'Expected the return value of "if" expression (obj.val) for route "foo_route" to be boolean, got stdClass.'])]
374374
public function testExceptionIsThrownOnInvalidIfReturnType(mixed $ifResult, string $expectedMessage): void
375375
{
376376
$configurationLoader = $this->createMock(ConfigurationLoaderInterface::class);

0 commit comments

Comments
 (0)