File System Assertions
File system assertions validate files, directories, and file permissions.
Available Assertions
Section titled “Available Assertions”file()
Section titled “file()”Assert that a file exists.
use Cline\Assert\Assertions\Assertion;
Assertion::file('/path/to/file.txt');Assertion::file($configPath, 'Config file not found');directory()
Section titled “directory()”Assert that a directory exists.
Assertion::directory('/path/to/dir');Assertion::directory($uploadsPath, 'Uploads directory not found');readable()
Section titled “readable()”Assert that a file or directory is readable.
Assertion::readable('/path/to/file.txt');Assertion::readable($logFile, 'Cannot read log file');writeable()
Section titled “writeable()”Assert that a file or directory is writeable.
Assertion::writeable('/path/to/file.txt');Assertion::writeable($cacheDir, 'Cache directory is not writeable');Chaining File System Assertions
Section titled “Chaining File System Assertions”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');Common Patterns
Section titled “Common Patterns”Configuration File Validation
Section titled “Configuration File Validation”$configPath = __DIR__ . '/config/app.php';
Assert::that($configPath) ->file('Configuration file not found') ->readable('Cannot read configuration file');
$config = require $configPath;Upload Directory Validation
Section titled “Upload Directory Validation”$uploadPath = storage_path('uploads');
Assert::that($uploadPath) ->directory('Upload directory does not exist') ->writeable('Cannot write to upload directory');Multiple Directory Validation
Section titled “Multiple Directory Validation”$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}");}Permission Checks
Section titled “Permission Checks”Read and Write
Section titled “Read and Write”Assert::that($dataFile) ->file() ->readable('Cannot read data file') ->writeable('Cannot write to data file');Best Practices
Section titled “Best Practices”Check Existence First
Section titled “Check Existence First”Assert::that($file) ->file('File does not exist') ->readable('File is not readable');Validate Before Operations
Section titled “Validate Before Operations”Assert::that($sourceFile)->file()->readable();Assert::that($destDir)->directory()->writeable();
copy($sourceFile, $destDir . '/' . basename($sourceFile));Use Absolute Paths
Section titled “Use Absolute Paths”// Use absolute pathAssertion::file(__DIR__ . '/config/app.php');
// Or use path helperAssertion::file(config_path('app.php'));Application Examples
Section titled “Application Examples”Asset Loading
Section titled “Asset Loading”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);}Cache Management
Section titled “Cache Management”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');}Next Steps
Section titled “Next Steps”- String Assertions - Path validation
- Custom Assertions - Custom file validators
- Lazy Assertions - Validate multiple paths