PHP code example of fidry / filesystem

1. Go to this page and download the library: Download fidry/filesystem library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fidry / filesystem example snippets


interface FileSystem extends SymfonyFileSystem
{
    /**
     * Replaces the path directory separator with the system one.
     *
     * For example, on Windows:
     * 'C:/path/to/file' => 'C:\path\to\file',
     */
    public function escapePath(string $path): string;

    /**
     * Returns the absolute path, but the path will not be normalized.
     *
     * For example, `::realpath('C:\Users\Name\file.txt')` on Windows will
     * return "C:\Users\Name\file.txt" (backslashes).
     *
     * @see https://php.net/manual/en/function.realpath.php
     *
     * @throws IOException When the file or symlink target does not exist.
     */
    public function realPath(string $file): string;

    /**
     * Returns the absolute normalized path.
     *
     * For example, `::realpath('C:\Users\Name\file.txt')` on Windows will
     * return "C:/Users/Name/file.txt".
     *
     * @see https://php.net/manual/en/function.realpath.php
     *
     * @throws IOException When the file or symlink target does not exist.
     */
    public function normalizedRealPath(string $file): string;

    /**
     * Creates a temporary file with support for custom stream wrappers. Same as tempnam(),
     * but targets the system default temporary directory by default and has a more consistent
     * name with tmpDir.
     *
     * For example:
     *
     *  

     * tmpDir('build')
     *
     * // on OSX
     * => '/var/folders/p3/lkw0cgjj2fq0656q_9rd0mk80000gn/T/build8d9e0f1a'
     * // on Windows
     * => C:\Windows\Temp\build8d9e0f1a.tmp
     * 


 declare(strict_types=1);

namespace App\Tests;

use Fidry\FileSystem\FS;
use Fidry\FileSystem\Test\FileSystemTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Component\Finder\Finder;
use function getenv;
use function is_string;

final class MyAppFileSystemTest extends FileSystemTestCase
{
    public function test_it_works(): void
    {
        // The temporary directory can be changed by overriding `::getTmpDirPrefix()`.
    
        // This file is dumped into a temporary directory. Here,
        // something like '/private/var/folders/p3/lkw0cgjj2fq0656q_9rd0mk80000gn/T/AppTestsMyAppFileSystemTest10000'
        // on OSX.
        FS::dumpFile('file1', '');
        
        $files = Finder::create()
            ->files()
            ->in($this->tmp);

        self::assertSame(['file1'], $this->normalizePaths($files));
    }
    
    // Utility methods available:
    /**
     * @param iterable<string|Stringable> $paths
     *
     * @return list<string> File real paths relative to the current temporary directory
     */
    function normalizePaths(iterable $paths): array;
    
    static function safeChdir(string $directory): void;
    static function safeGetCurrentWorkingDirectory(): string;
}


use PHPUnit\Framework\TestCase;

class DemoTest extends TestCase {

    function test_it_allows_to_compare_finder_splfileinfo_files(): void
    {
        $actual = $myService->getFileInfo();
        
        $expected = SplFileInfoFactory::fromPath('/path/to/expected', __DIR__);

        self::assertEquals($expected, $actual);
    }
}

// Write operations will throw a `DomainException` exception.
new ReadOnlyFileSystem(failOnWrite: true);

// Write operations will do nothing. Methods that return a path, e.g.
// `::tempnam()` will return an empty string.
new ReadOnlyFileSystem(failOnWrite: false);

FS::touch('file');

// instead of
(new NativeFileSystem)->touch('file');
diff
- private function createSplFileInfoMock(string $file): SplFileInfo&MockObject
+ private function createSplFileInfo(string $file): SplFileInfo
{
-    $splFileInfoMock = $this->createMock(SplFileInfo::class);
-    $splFileInfoMock->method('__toString')->willReturn($file);
-    $splFileInfoMock->method('getFilename')->willReturn($file);
-    $splFileInfoMock->method('getRealPath')->willReturn($file);
-    $splFileInfoMock->method('getContents')->willReturn(
-        file_exists($file) ? file_get_contents($file) : 'content',
-    );
-
-    return $splFileInfoMock;
+    return SplFileInfoBuilder::withTestData()
+        ->withFile($file)
+        ->withContents(
+            file_exists($file) ? file_get_contents($file) : 'content',
+        )
+        ->build();
}