-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #569 from tft7000/fix_servers_from_env_vars
Fix servers from env vars
- Loading branch information
Showing
9 changed files
with
93 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
tests/Functional/DependencyInjection/ServersFromEnvTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace FOS\HttpCacheBundle\Tests\Functional\DependencyInjection; | ||
|
||
use FOS\HttpCache\ProxyClient\HttpDispatcher; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
|
||
class ServersFromEnvTest extends KernelTestCase | ||
{ | ||
/** | ||
* Boots a special kernel with a compiler pass to make all services public for this test. | ||
* | ||
* @return KernelInterface A KernelInterface instance | ||
*/ | ||
protected function bootDebugKernel() | ||
{ | ||
static::ensureKernelShutdown(); | ||
static::$kernel = static::createKernel(); | ||
assert(static::$kernel instanceof \AppKernel); | ||
static::$kernel->addCompilerPass(new ServicesPublicPass()); | ||
$fs = new Filesystem(); | ||
$fs->remove(static::$kernel->getCacheDir()); | ||
static::$kernel->boot(); | ||
|
||
return static::$kernel; | ||
} | ||
|
||
public function testServersFromEnv() | ||
{ | ||
// define the kernel config to use for this test | ||
$_ENV['KERNEL_CONFIG'] = 'config_servers_from_env.yml'; | ||
|
||
// test env var as json string, that will get deserialized and injected into http dispatcher | ||
$_ENV['VARNISH_SERVERS'] = '["localhost:123","https://any.host:456"]'; | ||
|
||
/** @var Container $container */ | ||
$container = $this->bootDebugKernel()->getContainer(); | ||
|
||
/** @var HttpDispatcher $fosHttpCache */ | ||
$fosHttpCache = $container->get('fos_http_cache.proxy_client.varnish.http_dispatcher'); | ||
|
||
$reflectionObject = new \ReflectionClass($fosHttpCache); | ||
$reflectionGetServers = $reflectionObject->getMethod('getServers'); | ||
$reflectionGetServers->setAccessible(true); | ||
$uris = $reflectionGetServers->invoke($fosHttpCache); | ||
$servers = array_map(function ($uri) { return $uri->__toString(); }, $uris); | ||
|
||
static::assertEquals(['http://localhost:123', 'https://any.host:456'], $servers); | ||
|
||
// unset env vars, so next tests do not fail (KERNEL_CONFIG) | ||
unset($_ENV['KERNEL_CONFIG'], $_ENV['VARNISH_SERVERS']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
tests/Functional/DependencyInjection/ServicesPublicPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace FOS\HttpCacheBundle\Tests\Functional\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
class ServicesPublicPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
foreach ($container->getServiceIds() as $id) { | ||
if (strncmp('fos_http_cache.', $id, 15)) { | ||
continue; | ||
} | ||
if ($container->hasDefinition($id)) { | ||
$container->getDefinition($id)->setPublic(true); | ||
} elseif ($container->hasAlias($id)) { | ||
$container->getAlias($id)->setPublic(true); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
imports: | ||
- { resource: config.yml } | ||
|
||
framework: | ||
router: | ||
resource: "%kernel.root_dir%/config/routing.yml" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
tests/Functional/Fixtures/app/config/config_servers_from_env.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
framework: | ||
secret: fos | ||
|
||
fos_http_cache: | ||
proxy_client: | ||
varnish: | ||
http: | ||
servers_from_jsonenv: '%env(json:VARNISH_SERVERS)%' |