|
6 | 6 |
|
7 | 7 | use PHPUnit\Framework\Attributes\CoversClass; |
8 | 8 | use PHPUnit\Framework\Attributes\RequiresMethod; |
| 9 | +use PHPUnit\Framework\Attributes\TestWith; |
9 | 10 | use PHPUnit\Framework\TestCase; |
| 11 | +use Psr\Container\ContainerInterface; |
10 | 12 | use Sofascore\PurgatoryBundle\Attribute\RouteParamValue\CompoundValues; |
11 | 13 | use Sofascore\PurgatoryBundle\Attribute\RouteParamValue\EnumValues; |
12 | 14 | use Sofascore\PurgatoryBundle\Attribute\RouteParamValue\PropertyValues; |
13 | 15 | use Sofascore\PurgatoryBundle\Attribute\RouteParamValue\RawValues; |
14 | 16 | use Sofascore\PurgatoryBundle\Cache\Configuration\Configuration; |
15 | 17 | use Sofascore\PurgatoryBundle\Cache\Configuration\ConfigurationLoaderInterface; |
| 18 | +use Sofascore\PurgatoryBundle\Exception\InvalidIfExpressionResultException; |
16 | 19 | use Sofascore\PurgatoryBundle\Exception\LogicException; |
17 | 20 | use Sofascore\PurgatoryBundle\Listener\Enum\Action; |
18 | 21 | use Sofascore\PurgatoryBundle\RouteParamValueResolver\CompoundValuesResolver; |
|
27 | 30 | use Symfony\Component\DependencyInjection\ServiceLocator; |
28 | 31 | use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
29 | 32 | use Symfony\Component\PropertyAccess\PropertyAccess; |
| 33 | +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
30 | 34 | use Symfony\Component\PropertyAccess\PropertyPath; |
31 | 35 |
|
32 | 36 | #[CoversClass(AbstractEntityRouteProvider::class)] |
@@ -361,6 +365,43 @@ public function testRouteParamsWithRawValuesAndEnumValues(): void |
361 | 365 | self::assertSame(['name' => 'foo_route', 'params' => ['foo' => 'case3']], (array) $routes[5]); |
362 | 366 | } |
363 | 367 |
|
| 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.'])] |
| 374 | + public function testExceptionIsThrownOnInvalidIfReturnType(mixed $ifResult, string $expectedMessage): void |
| 375 | + { |
| 376 | + $configurationLoader = $this->createMock(ConfigurationLoaderInterface::class); |
| 377 | + $configurationLoader->expects(self::once()) |
| 378 | + ->method('load') |
| 379 | + ->willReturn(new Configuration([ |
| 380 | + 'stdClass' => [ |
| 381 | + [ |
| 382 | + 'routeName' => 'foo_route', |
| 383 | + 'if' => 'obj.val', |
| 384 | + ], |
| 385 | + ], |
| 386 | + ])); |
| 387 | + |
| 388 | + $expressionLanguage = $this->createMock(ExpressionLanguage::class); |
| 389 | + $expressionLanguage->expects(self::once()) |
| 390 | + ->method('evaluate') |
| 391 | + ->willReturn($ifResult); |
| 392 | + |
| 393 | + $routeProvider = new UpdatedEntityRouteProvider( |
| 394 | + configurationLoader: $configurationLoader, |
| 395 | + expressionLanguage: $expressionLanguage, |
| 396 | + routeParamValueResolverLocator: $this->createMock(ContainerInterface::class), |
| 397 | + propertyAccessor: $this->createMock(PropertyAccessorInterface::class), |
| 398 | + ); |
| 399 | + |
| 400 | + $this->expectException(InvalidIfExpressionResultException::class); |
| 401 | + $this->expectExceptionMessage($expectedMessage); |
| 402 | + [...$routeProvider->provideRoutesFor(Action::Update, new \stdClass(), [])]; |
| 403 | + } |
| 404 | + |
364 | 405 | private function createRouteProvider(array $configuration, bool $withExpressionLang): UpdatedEntityRouteProvider |
365 | 406 | { |
366 | 407 | $configurationLoader = $this->createMock(ConfigurationLoaderInterface::class); |
|
0 commit comments