Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/BrefKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

abstract class BrefKernel extends Kernel
{
public function __construct(string $environment, bool $debug)
{
parent::__construct($environment, $debug);

$this->configureCacheDir();
}

public function isLambda(): bool
{
return getenv('LAMBDA_TASK_ROOT') !== false;
Expand Down Expand Up @@ -135,6 +142,15 @@ private function ensureLogDir(string $writeLogDir): void
$filesystem->mkdir($writeLogDir);
}

private function configureCacheDir(): void
{
if (isset($_SERVER['APP_CACHE_DIR']) || ! $this->isLambda()) {
return;
}

$_SERVER['APP_CACHE_DIR'] = '/tmp/cache';
}

/**
* This method logs to stderr.
*
Expand Down
84 changes: 84 additions & 0 deletions tests/Unit/BrefKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,42 @@

class BrefKernelTest extends TestCase
{
/** @var string|false */
private $lambdaTaskRoot;

/** @var string|false */
private $appCacheDir;

/** @var string|null */
private $serverAppCacheDir;

protected function setUp(): void
{
parent::setUp();

$this->lambdaTaskRoot = getenv('LAMBDA_TASK_ROOT');
$this->appCacheDir = getenv('APP_CACHE_DIR');
$this->serverAppCacheDir = $_SERVER['APP_CACHE_DIR'] ?? null;

putenv('LAMBDA_TASK_ROOT');
putenv('APP_CACHE_DIR');
unset($_SERVER['APP_CACHE_DIR']);
}

protected function tearDown(): void
{
$this->restoreEnvironmentVariable('LAMBDA_TASK_ROOT', $this->lambdaTaskRoot);
$this->restoreEnvironmentVariable('APP_CACHE_DIR', $this->appCacheDir);

if ($this->serverAppCacheDir === null) {
unset($_SERVER['APP_CACHE_DIR']);
} else {
$_SERVER['APP_CACHE_DIR'] = $this->serverAppCacheDir;
}

parent::tearDown();
}

public function testIsLambda()
{
$kernel = new TestKernel;
Expand All @@ -15,4 +51,52 @@ public function testIsLambda()
putenv('LAMBDA_TASK_ROOT=/var/task');
self::assertTrue($kernel->isLambda());
}

public function testItConfiguresSymfonyCacheDirOnLambda(): void
{
putenv('LAMBDA_TASK_ROOT=/var/task');

new TestKernel;

self::assertSame('/tmp/cache', $_SERVER['APP_CACHE_DIR']);
}

public function testItDoesNotConfigureSymfonyCacheDirOutsideLambda(): void
{
new TestKernel;

self::assertArrayNotHasKey('APP_CACHE_DIR', $_SERVER);
}

public function testItDoesNotOverrideSymfonyCacheDir(): void
{
putenv('LAMBDA_TASK_ROOT=/var/task');
$_SERVER['APP_CACHE_DIR'] = '/custom/cache';

new TestKernel;

self::assertSame('/custom/cache', $_SERVER['APP_CACHE_DIR']);
}

public function testItUsesConfiguredSymfonyCacheDirEnvironmentVariable(): void
{
putenv('LAMBDA_TASK_ROOT=/var/task');
$_SERVER['APP_CACHE_DIR'] = '/custom/cache';

new TestKernel;

self::assertSame('/custom/cache', $_SERVER['APP_CACHE_DIR']);
}

/**
* @param string|false $value
*/
private function restoreEnvironmentVariable(string $name, $value): void
{
if ($value === false) {
putenv($name);
} else {
putenv($name . '=' . $value);
}
}
}
Loading