|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace FOS\HttpCacheBundle\Tests\Functional\DependencyInjection; |
| 4 | + |
| 5 | +use FOS\HttpCache\ProxyClient\HttpDispatcher; |
| 6 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 7 | +use Symfony\Component\DependencyInjection\Container; |
| 8 | +use Symfony\Component\Filesystem\Filesystem; |
| 9 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 10 | + |
| 11 | +class ServersFromEnvTest extends KernelTestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Boots a special kernel with a compiler pass to make all services public for this test. |
| 15 | + * |
| 16 | + * @return KernelInterface A KernelInterface instance |
| 17 | + */ |
| 18 | + protected function bootDebugKernel() |
| 19 | + { |
| 20 | + static::ensureKernelShutdown(); |
| 21 | + static::$kernel = static::createKernel(); |
| 22 | + assert(static::$kernel instanceof \AppKernel); |
| 23 | + static::$kernel->addCompilerPass(new ServicesPublicPass()); |
| 24 | + $fs = new Filesystem(); |
| 25 | + $fs->remove(static::$kernel->getCacheDir()); |
| 26 | + static::$kernel->boot(); |
| 27 | + |
| 28 | + return static::$kernel; |
| 29 | + } |
| 30 | + |
| 31 | + public function testServersFromEnv() |
| 32 | + { |
| 33 | + // define the kernel config to use for this test |
| 34 | + $_ENV['KERNEL_CONFIG'] = 'config_servers_from_env.yml'; |
| 35 | + |
| 36 | + // test env var as json string, that will get deserialized and injected into http dispatcher |
| 37 | + $_ENV['VARNISH_SERVERS'] = '["localhost:123","https://any.host:456"]'; |
| 38 | + |
| 39 | + /** @var Container $container */ |
| 40 | + $container = $this->bootDebugKernel()->getContainer(); |
| 41 | + |
| 42 | + /** @var HttpDispatcher $fosHttpCache */ |
| 43 | + $fosHttpCache = $container->get('fos_http_cache.proxy_client.varnish.http_dispatcher'); |
| 44 | + |
| 45 | + $reflectionObject = new \ReflectionClass($fosHttpCache); |
| 46 | + $reflectionGetServers = $reflectionObject->getMethod('getServers'); |
| 47 | + $reflectionGetServers->setAccessible(true); |
| 48 | + $uris = $reflectionGetServers->invoke($fosHttpCache); |
| 49 | + $servers = array_map(function ($uri) { return $uri->__toString(); }, $uris); |
| 50 | + |
| 51 | + static::assertEquals(['http://localhost:123', 'https://any.host:456'], $servers); |
| 52 | + |
| 53 | + // unset env vars, so next tests do not fail (KERNEL_CONFIG) |
| 54 | + unset($_ENV['KERNEL_CONFIG'], $_ENV['VARNISH_SERVERS']); |
| 55 | + } |
| 56 | +} |
0 commit comments