Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore nonexistent system temporary directory when a valid cache directory is provided via options #84

Merged
merged 3 commits into from
Jul 10, 2024
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
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);
}
}