Skip to content

Commit

Permalink
Merge pull request #84 from bcremer/sys_tmp_dir-fix
Browse files Browse the repository at this point in the history
Ignore nonexistent system temporary directory when a valid cache directory is provided via options
  • Loading branch information
boesing committed Jul 10, 2024
2 parents 16050a7 + eee255b commit 6b017c4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/FilesystemOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ public function __construct($options = null)
$this->dirPermission = false;
}

$this->setCacheDir(null);

parent::__construct($options);

if ($this->cacheDir === null) {
$this->setCacheDir(null);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

require_once dirname(__DIR__, 4) . '/vendor/autoload.php';
use Laminas\Cache\Storage\Adapter\FilesystemOptions;

$option = new FilesystemOptions(['cacheDir' => '/./tmp']);
echo $option->getCacheDir();
34 changes: 34 additions & 0 deletions test/unit/FilesystemOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
use const DIRECTORY_SEPARATOR;
use const PHP_EOL;
use const PHP_OS;
use const PHP_OS_FAMILY;

/**
* @template-extends AbstractAdapterOptionsTest<FilesystemOptions>
*/
final class FilesystemOptionsTest extends AbstractAdapterOptionsTest
{
private const MACOS_FAMILY = 'Darwin';

/** @var string */
protected $keyPattern = FilesystemOptions::KEY_PATTERN;

Expand All @@ -55,6 +58,12 @@ protected function createAdapterOptions(): AdapterOptions
return new FilesystemOptions();
}

public function testSetCacheDirToSystemsTempDirWhenNoCacheDirIsProvided(): void
{
$options = new FilesystemOptions();
self::assertEquals(realpath(sys_get_temp_dir()), $options->getCacheDir());
}

public function testSetCacheDirToSystemsTempDirWithNull(): void
{
$this->options->setCacheDir(null);
Expand Down Expand Up @@ -230,4 +239,29 @@ public function testTagSuffixIsMutable(): void
$this->options->setTagSuffix('.cache');
self::assertSame('.cache', $this->options->getTagSuffix());
}

/**
* The test asset script does provide a temporary directory and thus, the nonexistent system temp dir should
* not be used at all.
*/
public function testFilesystemOptionsInstantiationWithNonExistentSystemTemporaryDirectory(): void
{
/**
* Due to the usage of `realpath` {@see FilesystemOptions::normalizeCacheDirectory()}, the directory is changed
* depending on the host system. As there might be developers using MacOS brew PHP to execute these tests, we
* allow MacOS `/private/tmp` as well.
*/
$expectedTemporaryDirectory = match (PHP_OS_FAMILY) {
default => '/tmp',
self::MACOS_FAMILY => '/private/tmp',
};

$cacheDirectoryFromOptions = exec(
'TMPDIR=nonexistent php test/unit/Filesystem/TestAsset/instantiate_filesystem_options_with_cache_dir.php',
$output,
$exitCode,
);
self::assertSame(0, $exitCode);
self::assertSame($expectedTemporaryDirectory, $cacheDirectoryFromOptions);
}
}

0 comments on commit 6b017c4

Please sign in to comment.