Skip to content

refactor: Leverage FidryFileSystem #852

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

Merged
merged 1 commit into from
Oct 13, 2023
Merged
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
"require": {
"php": "^8.1",
"fidry/console": "^0.5.0 || ^0.6.0",
"fidry/filesystem": "^1.1",
"jetbrains/phpstorm-stubs": "^v2022.2",
"nikic/php-parser": "^4.12",
"symfony/console": "^5.2 || ^6.0",
123 changes: 96 additions & 27 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/Configuration/ConfigurationFactoryTest.php
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@

namespace Humbug\PhpScoper\Configuration;

use Fidry\FileSystem\FS;
use Humbug\PhpScoper\Container;
use Humbug\PhpScoper\FileSystemTestCase;
use Humbug\PhpScoper\Patcher\ComposerPatcher;
@@ -24,7 +25,6 @@
use Humbug\PhpScoper\Symbol\SymbolRegistry;
use InvalidArgumentException;
use function array_keys;
use function KevinGH\Box\FileSystem\dump_file;
use function Safe\touch;
use const DIRECTORY_SEPARATOR;

@@ -168,7 +168,7 @@ public function test_it_can_create_a_complete_configuration(): void

private static function dumpStandardConfigFile(string $contents): void
{
dump_file('scoper.inc.php', $contents);
FS::dumpFile('scoper.inc.php', $contents);
}

private function createConfigFromStandardFile(): Configuration
42 changes: 21 additions & 21 deletions tests/Console/Command/AddPrefixCommandTest.php
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@

use Fidry\Console\Application\SymfonyApplication;
use Fidry\Console\Command\SymfonyCommand;
use Fidry\FileSystem\FS;
use Humbug\PhpScoper\Configuration\ConfigurationFactory;
use Humbug\PhpScoper\Configuration\RegexChecker;
use Humbug\PhpScoper\Configuration\SymbolsConfigurationFactory;
@@ -38,7 +39,6 @@
use Symfony\Component\Console\Tester\ApplicationTester;
use Symfony\Component\Filesystem\Filesystem;
use function count;
use function Humbug\PhpScoper\escape_path;
use function Safe\chdir;
use function Safe\file_get_contents;
use function Safe\realpath;
@@ -121,8 +121,8 @@ public function test_scope_the_given_paths(): void
->shouldBeCalled();

foreach ($expectedFiles as $expectedFile => $prefixedContents) {
$inputPath = escape_path($root.'/'.$expectedFile);
$outputPath = escape_path($this->tmp.'/'.$expectedFile);
$inputPath = FS::escapePath($root.'/'.$expectedFile);
$outputPath = FS::escapePath($this->tmp.'/'.$expectedFile);

$inputContents = file_get_contents($inputPath);

@@ -183,8 +183,8 @@ public function test_let_the_file_unchanged_when_cannot_scope_a_file(): void
->shouldBeCalled();

foreach ($expectedFiles as $expectedFile => $prefixedContents) {
$inputPath = escape_path($root.'/'.$expectedFile);
$outputPath = escape_path($this->tmp.'/'.$expectedFile);
$inputPath = FS::escapePath($root.'/'.$expectedFile);
$outputPath = FS::escapePath($this->tmp.'/'.$expectedFile);

$inputContents = file_get_contents($inputPath);

@@ -239,8 +239,8 @@ public function test_do_not_scope_duplicated_given_paths(): void
];

foreach ($expectedFiles as $expectedFile => $prefixedContents) {
$inputPath = escape_path($root.'/'.$expectedFile);
$outputPath = escape_path($this->tmp.'/'.$expectedFile);
$inputPath = FS::escapePath($root.'/'.$expectedFile);
$outputPath = FS::escapePath($this->tmp.'/'.$expectedFile);

$inputContents = file_get_contents($inputPath);

@@ -262,7 +262,7 @@ public function test_do_not_scope_duplicated_given_paths(): void

public function test_scope_the_given_paths_and_the_ones_found_by_the_finder(): void
{
chdir($rootPath = escape_path(self::FIXTURE_PATH.'/set012'));
chdir($rootPath = FS::escapePath(self::FIXTURE_PATH.'/set012'));

$input = [
'add-prefix',
@@ -289,7 +289,7 @@ public function test_scope_the_given_paths_and_the_ones_found_by_the_finder(): v

foreach ($expectedFiles as $inputPath => $outputPath) {
$inputPath = realpath($inputPath);
$outputPath = escape_path($outputPath);
$outputPath = FS::escapePath($outputPath);

$inputContents = file_get_contents($inputPath);
$prefixedFileContents = 'Random string';
@@ -347,8 +347,8 @@ public function test_scope_the_current_working_directory_if_no_path_given(): voi
->shouldBeCalled();

foreach ($expectedFiles as $expectedFile => $prefixedContents) {
$inputPath = escape_path($root.'/'.$expectedFile);
$outputPath = escape_path($this->tmp.'/'.$expectedFile);
$inputPath = FS::escapePath($root.'/'.$expectedFile);
$outputPath = FS::escapePath($this->tmp.'/'.$expectedFile);

$inputContents = file_get_contents($inputPath);

@@ -405,8 +405,8 @@ public function test_an_output_directory_can_be_given(): void
->shouldBeCalled();

foreach ($expectedFiles as $expectedFile => $prefixedContents) {
$inputPath = escape_path($root.'/'.$expectedFile);
$outputPath = escape_path($outDir.'/'.$expectedFile);
$inputPath = FS::escapePath($root.'/'.$expectedFile);
$outputPath = FS::escapePath($outDir.'/'.$expectedFile);

$inputContents = file_get_contents($inputPath);

@@ -464,8 +464,8 @@ public function test_relative_output_directory_are_made_absolute(): void
->shouldBeCalled();

foreach ($expectedFiles as $expectedFile => $prefixedContents) {
$inputPath = escape_path($root.'/'.$expectedFile);
$outputPath = escape_path($this->tmp.DIRECTORY_SEPARATOR.$outDir.'/'.$expectedFile);
$inputPath = FS::escapePath($root.'/'.$expectedFile);
$outputPath = FS::escapePath($this->tmp.DIRECTORY_SEPARATOR.$outDir.'/'.$expectedFile);

$inputContents = file_get_contents($inputPath);

@@ -520,7 +520,7 @@ public function test_throws_an_error_when_passing_a_non_existent_config(): void

public function test_throws_an_error_if_patch_file_returns_an_array_with_invalid_values(): void
{
chdir(escape_path(self::FIXTURE_PATH.'/set009'));
chdir(FS::escapePath(self::FIXTURE_PATH.'/set009'));

$input = [
'add-prefix',
@@ -551,7 +551,7 @@ public function test_throws_an_error_if_patch_file_returns_an_array_with_invalid

public function test_can_scope_projects_with_invalid_files(): void
{
chdir(escape_path($root = self::FIXTURE_PATH.'/set010'));
chdir(FS::escapePath($root = self::FIXTURE_PATH.'/set010'));

$input = [
'add-prefix',
@@ -575,8 +575,8 @@ public function test_can_scope_projects_with_invalid_files(): void
$root = realpath($root);

foreach ($expectedFiles as $expectedFile => $prefixedContents) {
$inputPath = escape_path($root.'/'.$expectedFile);
$outputPath = escape_path($this->tmp.'/'.$expectedFile);
$inputPath = FS::escapePath($root.'/'.$expectedFile);
$outputPath = FS::escapePath($this->tmp.'/'.$expectedFile);

$fileContents = file_get_contents($inputPath);

@@ -635,8 +635,8 @@ public function test_it_outputs_in_the_build_directory_if_no_output_dir_is_given
->shouldBeCalled();

foreach ($expectedFiles as $expectedFile => $prefixedContents) {
$inputPath = escape_path($root.'/'.$expectedFile);
$outputPath = escape_path($this->tmp.DIRECTORY_SEPARATOR.$outDir.'/'.$expectedFile);
$inputPath = FS::escapePath($root.'/'.$expectedFile);
$outputPath = FS::escapePath($this->tmp.DIRECTORY_SEPARATOR.$outDir.'/'.$expectedFile);

$inputContents = file_get_contents($inputPath);

35 changes: 4 additions & 31 deletions tests/FileSystemTestCase.php
Original file line number Diff line number Diff line change
@@ -14,39 +14,12 @@

namespace Humbug\PhpScoper;

use PHPUnit\Framework\TestCase;
use function Safe\chdir;
use function Safe\getcwd;
use function Safe\realpath;
use function str_replace;
use function sys_get_temp_dir;
use Fidry\FileSystem\Test\FileSystemTestCase as FidryFileSystemTestCase;

abstract class FileSystemTestCase extends TestCase
abstract class FileSystemTestCase extends FidryFileSystemTestCase
{
protected string $cwd;

protected string $tmp;

protected function setUp(): void
{
parent::setUp();

// Cleans up whatever was there before. Indeed upon failure PHPUnit fails to trigger the `tearDown()` method
// and as a result some temporary files may still remain.
remove_dir(str_replace('\\', '/', realpath(sys_get_temp_dir())).'/php-scoper');

$this->cwd = getcwd();
$this->tmp = make_tmp_dir('php-scoper', self::class);

chdir($this->tmp);
}

protected function tearDown(): void
public static function getTmpDirNamespace(): string
{
parent::tearDown();

chdir($this->cwd);

remove_dir($this->tmp);
return 'PHP-Scoper';
}
}
66 changes: 0 additions & 66 deletions tests/functions.php
Original file line number Diff line number Diff line change
@@ -15,72 +15,6 @@
namespace Humbug\PhpScoper;

use PhpParser\Parser;
use RuntimeException;
use Symfony\Component\Filesystem\Filesystem;
use function mkdir as native_mkdir;
use function Safe\realpath;
use function sprintf;
use function str_replace;
use function strrpos;
use function substr;
use function sys_get_temp_dir;
use const DIRECTORY_SEPARATOR;

/**
* Creates a temporary directory.
*
* @param string $namespace The directory path in the system's temporary
* directory.
* @param string $className The name of the test class.
*
* @return string The path to the created directory.
*/
function make_tmp_dir(string $namespace, string $className): string
{
if (false !== ($pos = strrpos($className, '\\'))) {
$shortClass = substr($className, $pos + 1);
} else {
$shortClass = $className;
}

// Usage of realpath() is important if the temporary directory is a
// symlink to another directory (e.g. /var => /private/var on some Macs)
// We want to know the real path to avoid comparison failures with
// code that uses real paths only
$systemTempDir = str_replace('\\', '/', realpath(sys_get_temp_dir()));
$basePath = $systemTempDir.'/'.$namespace.'/'.$shortClass;

$i = 0;

while (false === @native_mkdir($tempDir = escape_path($basePath.random_int(10000, 99999)), 0o777, true)) {
// Run until we are able to create a directory
if ($i > 100) {
throw new RuntimeException(
sprintf(
'Could not create temporary directory for "%s:%s" after 100 attempts',
$namespace,
$className,
),
);
}

++$i;
}

return $tempDir;
}

function escape_path(string $path): string
{
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}

function remove_dir(string $path): void
{
$path = escape_path($path);

(new Filesystem())->remove($path);
}

/**
* @private