Skip to content

File System Assertions

File system assertions validate files, directories, and file permissions.

Assert that a file exists.

use Cline\Assert\Assertions\Assertion;
Assertion::file('/path/to/file.txt');
Assertion::file($configPath, 'Config file not found');

Assert that a directory exists.

Assertion::directory('/path/to/dir');
Assertion::directory($uploadsPath, 'Uploads directory not found');

Assert that a file or directory is readable.

Assertion::readable('/path/to/file.txt');
Assertion::readable($logFile, 'Cannot read log file');

Assert that a file or directory is writeable.

Assertion::writeable('/path/to/file.txt');
Assertion::writeable($cacheDir, 'Cache directory is not writeable');
use Cline\Assert\Assert;
Assert::that($configFile)
->string()
->notEmpty()
->file('Config file does not exist')
->readable('Config file is not readable');
Assert::that($uploadDir)
->directory('Upload directory missing')
->writeable('Upload directory is not writeable');
$configPath = __DIR__ . '/config/app.php';
Assert::that($configPath)
->file('Configuration file not found')
->readable('Cannot read configuration file');
$config = require $configPath;
$uploadPath = storage_path('uploads');
Assert::that($uploadPath)
->directory('Upload directory does not exist')
->writeable('Cannot write to upload directory');
$directories = [
storage_path('cache'),
storage_path('sessions'),
storage_path('views'),
];
foreach ($directories as $dir) {
Assert::that($dir)
->directory("Directory does not exist: {$dir}")
->writeable("Directory not writeable: {$dir}");
}
Assert::that($dataFile)
->file()
->readable('Cannot read data file')
->writeable('Cannot write to data file');
Assert::that($file)
->file('File does not exist')
->readable('File is not readable');
Assert::that($sourceFile)->file()->readable();
Assert::that($destDir)->directory()->writeable();
copy($sourceFile, $destDir . '/' . basename($sourceFile));
// Use absolute path
Assertion::file(__DIR__ . '/config/app.php');
// Or use path helper
Assertion::file(config_path('app.php'));
public function loadAsset(string $name): string
{
$assetPath = public_path("assets/{$name}");
Assert::that($assetPath)
->file("Asset not found: {$name}")
->readable("Cannot read asset: {$name}");
return file_get_contents($assetPath);
}
public function setupCache(): void
{
$cacheDir = storage_path('framework/cache');
if (!is_dir($cacheDir)) {
mkdir($cacheDir, 0755, true);
}
Assert::that($cacheDir)
->directory('Failed to create cache directory')
->writeable('Cache directory must be writeable');
}