|
9 | 9 | use Doctrine\Persistence\ManagerRegistry; |
10 | 10 | use PHPUnit\Framework\Attributes\CoversClass; |
11 | 11 | use PHPUnit\Framework\Attributes\DataProvider; |
12 | | -use PHPUnit\Framework\Attributes\TestWith; |
13 | 12 | use PHPUnit\Framework\TestCase; |
14 | 13 | use Psr\Container\ContainerInterface; |
15 | 14 | use Sofascore\PurgatoryBundle\Attribute\PurgeOn; |
| 15 | +use Sofascore\PurgatoryBundle\Attribute\RouteParamValue\ExpressionValues; |
16 | 16 | use Sofascore\PurgatoryBundle\Attribute\RouteParamValue\PropertyValues; |
17 | 17 | use Sofascore\PurgatoryBundle\Attribute\Target\ForProperties; |
18 | 18 | use Sofascore\PurgatoryBundle\Cache\PropertyResolver\SubscriptionResolverInterface; |
@@ -486,22 +486,49 @@ class: 'FooEntity', |
486 | 486 | ]; |
487 | 487 | } |
488 | 488 |
|
489 | | - #[TestWith([ |
490 | | - 'if' => 'invalidObj.getMethod()', |
491 | | - 'expectedMessage' => 'Invalid "if" expression provided for route "foo": "Variable "invalidObj" is not valid around position 1 for expression `invalidObj.getMethod()`."', |
492 | | - ])] |
493 | | - #[TestWith([ |
494 | | - 'if' => 'entity !== null', |
495 | | - 'expectedMessage' => 'Invalid "if" expression provided for route "foo": "Variable "entity" is not valid around position 1 for expression `entity !== null`."', |
496 | | - ])] |
497 | | - #[TestWith([ |
498 | | - 'if' => 'some_function(obj)', |
499 | | - 'expectedMessage' => 'Invalid "if" expression provided for route "foo": "The function "some_function" does not exist around position 1 for expression `some_function(obj)`."', |
500 | | - ])] |
501 | | - #[TestWith([ |
502 | | - 'if' => 'valid_function(author)', |
503 | | - 'expectedMessage' => 'Invalid "if" expression provided for route "foo": "Variable "author" is not valid around position 16 for expression `valid_function(author)`."', |
504 | | - ])] |
| 489 | + #[DataProvider('provideInvalidExpressions')] |
| 490 | + public function testExceptionIsThrownOnInvalidRouteParamsExpression(string $expression, string $expectedMessage): void |
| 491 | + { |
| 492 | + $routeMetadataProvider = self::createStub(RouteMetadataProviderInterface::class); |
| 493 | + $routeMetadataProvider->method('provide') |
| 494 | + ->willReturnCallback(function () use ($expression): iterable { |
| 495 | + yield new RouteMetadata( |
| 496 | + routeName: 'foo', |
| 497 | + route: new Route('/{foo}'), |
| 498 | + purgeOn: new PurgeOn( |
| 499 | + class: 'FooEntity', |
| 500 | + routeParams: ['foo' => new ExpressionValues($expression)], |
| 501 | + ), |
| 502 | + reflectionMethod: null, |
| 503 | + ); |
| 504 | + }); |
| 505 | + |
| 506 | + $purgeSubscriptionProvider = new PurgeSubscriptionProvider( |
| 507 | + subscriptionResolvers: [], |
| 508 | + routeMetadataProviders: [$routeMetadataProvider], |
| 509 | + managerRegistry: self::createStub(ManagerRegistry::class), |
| 510 | + targetResolverLocator: self::createStub(ContainerInterface::class), |
| 511 | + expressionLanguage: new ExpressionLanguage( |
| 512 | + providers: [ |
| 513 | + new class implements ExpressionFunctionProviderInterface { |
| 514 | + public function getFunctions(): array |
| 515 | + { |
| 516 | + return [ |
| 517 | + new ExpressionFunction('valid_function', function () {}, function () {}), |
| 518 | + ]; |
| 519 | + } |
| 520 | + }, |
| 521 | + ], |
| 522 | + ), |
| 523 | + ); |
| 524 | + |
| 525 | + $this->expectException(InvalidIfExpressionException::class); |
| 526 | + $this->expectExceptionMessage($expectedMessage); |
| 527 | + |
| 528 | + [...$purgeSubscriptionProvider->provide()]; |
| 529 | + } |
| 530 | + |
| 531 | + #[DataProvider('provideInvalidExpressions')] |
505 | 532 | public function testExceptionIsThrownOnInvalidIfExpression(string $if, string $expectedMessage): void |
506 | 533 | { |
507 | 534 | $routeMetadataProvider = self::createStub(RouteMetadataProviderInterface::class); |
@@ -542,4 +569,24 @@ public function getFunctions(): array |
542 | 569 |
|
543 | 570 | [...$purgeSubscriptionProvider->provide()]; |
544 | 571 | } |
| 572 | + |
| 573 | + public static function provideInvalidExpressions(): iterable |
| 574 | + { |
| 575 | + yield [ |
| 576 | + 'invalidObj.getMethod()', |
| 577 | + 'Invalid "if" expression provided for route "foo": "Variable "invalidObj" is not valid around position 1 for expression `invalidObj.getMethod()`."', |
| 578 | + ]; |
| 579 | + yield [ |
| 580 | + 'entity !== null', |
| 581 | + 'Invalid "if" expression provided for route "foo": "Variable "entity" is not valid around position 1 for expression `entity !== null`."', |
| 582 | + ]; |
| 583 | + yield [ |
| 584 | + 'some_function(obj)', |
| 585 | + 'Invalid "if" expression provided for route "foo": "The function "some_function" does not exist around position 1 for expression `some_function(obj)`."', |
| 586 | + ]; |
| 587 | + yield [ |
| 588 | + 'valid_function(author)', |
| 589 | + 'Invalid "if" expression provided for route "foo": "Variable "author" is not valid around position 16 for expression `valid_function(author)`."', |
| 590 | + ]; |
| 591 | + } |
545 | 592 | } |
0 commit comments