Viewing File: /usr/local/cpanel/3rdparty/wpinstaller/tests/FileManagerTest.php

<?php
declare(strict_types=1);

final class FileManagerTest extends PHPUnit\Framework\TestCase {
    public function testTransformFile() {
        // Create a temp file to play with
        $tempfile = tmpfile();
        fwrite($tempfile, 'We don’t serve their kind here! What? Your droids.' . PHP_EOL);
        fwrite($tempfile, 'They’ll have to wait outside. We don’t want them here.' . PHP_EOL);
        fwrite($tempfile, 'Listen, why don’t you wait out by the speeder.' . PHP_EOL);
        fwrite($tempfile, 'We don’t want any trouble. I heartily agree with you sir.' . PHP_EOL);
        fflush($tempfile);

        $temp_path = stream_get_meta_data($tempfile)['uri'];
        $content = file_get_contents($temp_path);

        // Transform the file with FileManager::transform_file()
        $fm = new \WpInstaller\FileManager();
        $trans_func = function ( $line ) {
            $new_line = str_replace('We', 'Han Solo', $line);
            return str_replace('droids', 'jabberwocky', $new_line);
        };
        $fm->transform_file($temp_path,  $trans_func);

        // Test that new and expected content matches
        $new_content      = file_get_contents($temp_path);
        $expected_content = $trans_func($content);
        $this->assertEquals($new_content, $expected_content);

        if (file_exists($temp_path)) {
            // transform_file destroys our original handle so we need to make sure we're cleaning
            // up after ourselves.
            unlink($temp_path);
        }
    }
}
?>
Back to Directory File Manager