|
17 | 17 | use PHPUnit\Framework\TestCase;
|
18 | 18 | use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
|
19 | 19 | use Symfony\Component\DependencyInjection\ChildDefinition;
|
| 20 | +use Symfony\Component\DependencyInjection\Compiler\ResolveEnvPlaceholdersPass; |
| 21 | +use Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass; |
20 | 22 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
21 | 23 | use Symfony\Component\DependencyInjection\Definition;
|
22 | 24 | use Symfony\Component\DependencyInjection\DefinitionDecorator;
|
23 |
| -use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
| 25 | +use Symfony\Component\DependencyInjection\Exception\RuntimeException; |
| 26 | +use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag; |
24 | 27 | use Symfony\Component\DependencyInjection\Reference;
|
25 | 28 | use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
|
26 | 29 | use Symfony\Component\HttpKernel\Kernel;
|
@@ -662,10 +665,81 @@ public function testVarnishCustomTagsHeader()
|
662 | 665 | $this->assertEquals(['tag_mode' => 'ban', 'tags_header' => 'myheader'], $container->getParameter('fos_http_cache.proxy_client.varnish.options'));
|
663 | 666 | }
|
664 | 667 |
|
| 668 | + /** |
| 669 | + * @param array|null $serversValue array that contains servers, `null` if not set |
| 670 | + * @param string|null $serversFromJsonEnvValue string that should contain an env var (use `VARNISH_SERVERS` for this test), `null` if not set |
| 671 | + * @param string|mixed|null $envValue _ENV['VARNISH_SERVERS'] will be set to this value; only used if `$serversFromJsonEnvValue` is used; should be a string, otherwise an error will show up |
| 672 | + * @param array|null $expectedServersValue expected servers value the http dispatcher receives |
| 673 | + * @param string|null $expectExceptionClass the exception class the configuration might throw, `null` if no exception is thrown |
| 674 | + * @param string|null $expectExceptionMessage the message the exception throws, anything if no exception is thrown |
| 675 | + * |
| 676 | + * @dataProvider dataVarnishServersConfig |
| 677 | + */ |
| 678 | + public function testVarnishServersConfig($serversValue, $serversFromJsonEnvValue, $envValue, $expectedServersValue, $expectExceptionClass, $expectExceptionMessage): void |
| 679 | + { |
| 680 | + $_ENV['VARNISH_SERVERS'] = $envValue; |
| 681 | + $container = $this->createContainer(); |
| 682 | + |
| 683 | + // workaround to get the possible env string into the EnvPlaceholderParameterBag |
| 684 | + $container->setParameter('triggerServersValue', $serversValue); |
| 685 | + $container->setParameter('triggerServersFromJsonEnvValue', $serversFromJsonEnvValue); |
| 686 | + (new ResolveParameterPlaceHoldersPass())->process($container); |
| 687 | + |
| 688 | + $config = $this->getBaseConfig(); |
| 689 | + |
| 690 | + if (null === $serversValue) { |
| 691 | + unset($config['proxy_client']['varnish']['http']['servers']); |
| 692 | + } else { |
| 693 | + $config['proxy_client']['varnish']['http']['servers'] = $container->getParameter('triggerServersValue'); |
| 694 | + } |
| 695 | + if (null !== $serversFromJsonEnvValue) { |
| 696 | + $config['proxy_client']['varnish']['http']['servers_from_jsonenv'] = $container->getParameter('triggerServersFromJsonEnvValue'); |
| 697 | + } |
| 698 | + |
| 699 | + if ($expectExceptionClass) { |
| 700 | + $this->expectException($expectExceptionClass); |
| 701 | + $this->expectExceptionMessage($expectExceptionMessage); |
| 702 | + } |
| 703 | + |
| 704 | + $this->extension->load([$config], $container); |
| 705 | + |
| 706 | + // Note: until here InvalidConfigurationException should be thrown |
| 707 | + if (InvalidConfigurationException::class === $expectExceptionClass) { |
| 708 | + return; |
| 709 | + } |
| 710 | + |
| 711 | + (new ResolveEnvPlaceholdersPass())->process($container); |
| 712 | + |
| 713 | + // Note: now all expected exceptions should be thrown |
| 714 | + if ($expectExceptionClass) { |
| 715 | + return; |
| 716 | + } |
| 717 | + |
| 718 | + $definition = $container->getDefinition('fos_http_cache.proxy_client.varnish.http_dispatcher'); |
| 719 | + static::assertEquals($expectedServersValue, $definition->getArgument(0)); |
| 720 | + } |
| 721 | + |
| 722 | + public function dataVarnishServersConfig() |
| 723 | + { |
| 724 | + return [ |
| 725 | + // working case before implementing the feature 'env vars in servers key' |
| 726 | + 'regular array as servers value allowed' => [['my-server-1', 'my-server-2'], null, null, ['my-server-1', 'my-server-2'], null, null], |
| 727 | + // testing the feature 'env vars in servers_from_jsonenv key' |
| 728 | + 'env var with json array as servers value allowed' => [null, '%env(json:VARNISH_SERVERS)%', '["my-server-1","my-server-2"]', ['my-server-1', 'my-server-2'], null, null], |
| 729 | + // not allowed cases (servers_from_jsonenv) |
| 730 | + 'plain string as servers value is forbidden' => [null, 'plain_string_not_allowed_as_servers_from_jsonenv_value', null, null, InvalidConfigurationException::class, 'Not a valid Varnish servers_from_jsonenv configuration: plain_string_not_allowed_as_servers_from_jsonenv_value'], |
| 731 | + 'an int as servers value is forbidden' => [null, 1, 'env_value_not_used', null, InvalidConfigurationException::class, 'The "http.servers" or "http.servers_from_jsonenv" section must be defined for the proxy "varnish"'], |
| 732 | + 'env var with string as servers value is forbidden (at runtime)' => [null, '%env(json:VARNISH_SERVERS)%', 'wrong_usage_of_env_value', 'no_servers_value', RuntimeException::class, 'Invalid JSON in env var "VARNISH_SERVERS": Syntax error'], |
| 733 | + // more cases |
| 734 | + 'no definition leads to error' => [null, null, 'not_used', 'not_used', InvalidConfigurationException::class, 'The "http.servers" or "http.servers_from_jsonenv" section must be defined for the proxy "varnish"'], |
| 735 | + 'both servers and servers_from_jsonenv defined leads to error' => [['my-server-1', 'my-server-2'], '%env(json:VARNISH_SERVERS)%', 'not_used', 'not_used', InvalidConfigurationException::class, 'You can only set one of "http.servers" or "http.servers_from_jsonenv" but not both to avoid ambiguity for the proxy "varnish"'], |
| 736 | + ]; |
| 737 | + } |
| 738 | + |
665 | 739 | private function createContainer()
|
666 | 740 | {
|
667 | 741 | $container = new ContainerBuilder(
|
668 |
| - new ParameterBag(['kernel.debug' => false]) |
| 742 | + new EnvPlaceholderParameterBag(['kernel.debug' => false]) |
669 | 743 | );
|
670 | 744 |
|
671 | 745 | // The cache_manager service depends on the router service
|
|
0 commit comments