From a59b5fbd24973674d5ab30b1c79b2f085e4eec31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Fri, 2 Feb 2018 23:24:48 +0000 Subject: [PATCH] Add file contents to the Scoper (#146) Continuing some work for humbug/box#31. The Scoper now takes the file contents as an argument. This allows the scoper to be used with existent files (e.g. virtual files) or files for which we are modifying the content like in Box. Another benefit is that it removes the Scopers dependency to the file system layer. --- src/Console/Command/AddPrefixCommand.php | 4 +- src/Scoper.php | 5 +- .../Composer/InstalledPackagesScoper.php | 9 +-- src/Scoper/Composer/JsonFileScoper.php | 9 +-- src/Scoper/NullScoper.php | 4 +- src/Scoper/PatchScoper.php | 10 +-- src/Scoper/PhpScoper.php | 16 ++-- .../Console/Command/AddPrefixCommandTest.php | 31 +++++++- .../Composer/InstalledPackagesScoperTest.php | 54 ++------------ tests/Scoper/Composer/JsonFileScoperTest.php | 60 +++------------ tests/Scoper/FakeScoper.php | 2 +- tests/Scoper/NullScoperTest.php | 45 +---------- tests/Scoper/PatchScoperTest.php | 16 ++-- tests/Scoper/PhpScoperTest.php | 74 +++++-------------- 14 files changed, 100 insertions(+), 239 deletions(-) diff --git a/src/Console/Command/AddPrefixCommand.php b/src/Console/Command/AddPrefixCommand.php index fa688c8d..8a9f5492 100644 --- a/src/Console/Command/AddPrefixCommand.php +++ b/src/Console/Command/AddPrefixCommand.php @@ -246,10 +246,8 @@ private function scopeFile( bool $stopOnFailure, ConsoleLogger $logger ): void { - // TODO: use $inputContents instead of doing file_get_contents in the Scopers - try { - $scoppedContent = $this->scoper->scope($inputFilePath, $prefix, $patchers, $whitelist, $globalWhitelister); + $scoppedContent = $this->scoper->scope($inputFilePath, $inputContents, $prefix, $patchers, $whitelist, $globalWhitelister); } catch (Throwable $error) { $exception = new ParsingException( sprintf( diff --git a/src/Scoper.php b/src/Scoper.php index 7ff0ff11..bbdd17b5 100644 --- a/src/Scoper.php +++ b/src/Scoper.php @@ -22,6 +22,7 @@ interface Scoper * Scope AKA. apply the given prefix to the file in the appropriate way. * * @param string $filePath File to scope + * @param string $contents File contents * @param string $prefix Prefix to apply to the file * @param callable[] $patchers * @param string[] $whitelist List of classes to exclude from the scoping. @@ -31,7 +32,7 @@ interface Scoper * * @throws ParsingException * - * @return string Content of the file with the prefix applied + * @return string Contents of the file with the prefix applied */ - public function scope(string $filePath, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string; + public function scope(string $filePath, string $contents, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string; } diff --git a/src/Scoper/Composer/InstalledPackagesScoper.php b/src/Scoper/Composer/InstalledPackagesScoper.php index 13491367..b735ed9b 100644 --- a/src/Scoper/Composer/InstalledPackagesScoper.php +++ b/src/Scoper/Composer/InstalledPackagesScoper.php @@ -32,16 +32,13 @@ public function __construct(Scoper $decoratedScoper) * * {@inheritdoc} */ - public function scope(string $filePath, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string + public function scope(string $filePath, string $contents, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string { if (1 !== preg_match(self::$filePattern, $filePath)) { - return $this->decoratedScoper->scope($filePath, $prefix, $patchers, $whitelist, $globalWhitelister); + return $this->decoratedScoper->scope($filePath, $contents, $prefix, $patchers, $whitelist, $globalWhitelister); } - $decodedJson = json_decode( - file_get_contents($filePath), - true - ); + $decodedJson = json_decode($contents, true); $decodedJson = $this->prefixLockPackages($decodedJson, $prefix); diff --git a/src/Scoper/Composer/JsonFileScoper.php b/src/Scoper/Composer/JsonFileScoper.php index 222295d5..792703cd 100644 --- a/src/Scoper/Composer/JsonFileScoper.php +++ b/src/Scoper/Composer/JsonFileScoper.php @@ -30,16 +30,13 @@ public function __construct(Scoper $decoratedScoper) * * {@inheritdoc} */ - public function scope(string $filePath, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string + public function scope(string $filePath, string $contents, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string { if (1 !== preg_match('/composer\.json$/', $filePath)) { - return $this->decoratedScoper->scope($filePath, $prefix, $patchers, $whitelist, $globalWhitelister); + return $this->decoratedScoper->scope($filePath, $contents, $prefix, $patchers, $whitelist, $globalWhitelister); } - $decodedJson = json_decode( - file_get_contents($filePath), - true - ); + $decodedJson = json_decode($contents, true); $decodedJson = AutoloadPrefixer::prefixPackageAutoloads($decodedJson, $prefix); diff --git a/src/Scoper/NullScoper.php b/src/Scoper/NullScoper.php index b50434d5..de934442 100644 --- a/src/Scoper/NullScoper.php +++ b/src/Scoper/NullScoper.php @@ -21,8 +21,8 @@ final class NullScoper implements Scoper /** * @inheritdoc */ - public function scope(string $filePath, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string + public function scope(string $filePath, string $contents, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string { - return file_get_contents($filePath); + return $contents; } } diff --git a/src/Scoper/PatchScoper.php b/src/Scoper/PatchScoper.php index 252417cb..5e5102ed 100644 --- a/src/Scoper/PatchScoper.php +++ b/src/Scoper/PatchScoper.php @@ -28,16 +28,16 @@ public function __construct(Scoper $decoratedScoper) /** * @inheritdoc */ - public function scope(string $filePath, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string + public function scope(string $filePath, string $contents, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string { - $content = $this->decoratedScoper->scope($filePath, $prefix, $patchers, $whitelist, $globalWhitelister); + $contents = $this->decoratedScoper->scope($filePath, $contents, $prefix, $patchers, $whitelist, $globalWhitelister); return array_reduce( $patchers, - function (string $content, callable $patcher) use ($filePath, $prefix): string { - return $patcher($filePath, $prefix, $content); + function (string $contents, callable $patcher) use ($filePath, $prefix): string { + return $patcher($filePath, $prefix, $contents); }, - $content + $contents ); } } diff --git a/src/Scoper/PhpScoper.php b/src/Scoper/PhpScoper.php index e13ad544..cab5f9df 100644 --- a/src/Scoper/PhpScoper.php +++ b/src/Scoper/PhpScoper.php @@ -43,15 +43,13 @@ public function __construct(Parser $parser, Scoper $decoratedScoper, TraverserFa * * @throws PhpParserError */ - public function scope(string $filePath, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string + public function scope(string $filePath, string $contents, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string { - if (false === $this->isPhpFile($filePath)) { - return $this->decoratedScoper->scope($filePath, $prefix, $patchers, $whitelist, $globalWhitelister); + if (false === $this->isPhpFile($filePath, $contents)) { + return $this->decoratedScoper->scope($filePath, $contents, $prefix, $patchers, $whitelist, $globalWhitelister); } - $content = file_get_contents($filePath); - - $statements = $this->parser->parse($content); + $statements = $this->parser->parse($contents); $traverser = $this->traverserFactory->create($prefix, $whitelist, $globalWhitelister); @@ -62,7 +60,7 @@ public function scope(string $filePath, string $prefix, array $patchers, array $ return $prettyPrinter->prettyPrintFile($statements)."\n"; } - private function isPhpFile(string $filePath): bool + private function isPhpFile(string $filePath, string $contents): bool { if (1 === preg_match(self::FILE_PATH_PATTERN, $filePath)) { return true; @@ -72,8 +70,6 @@ private function isPhpFile(string $filePath): bool return false; } - $content = file_get_contents($filePath); - - return 1 === preg_match(self::PHP_BINARY, $content); + return 1 === preg_match(self::PHP_BINARY, $contents); } } diff --git a/tests/Console/Command/AddPrefixCommandTest.php b/tests/Console/Command/AddPrefixCommandTest.php index b5d741bb..0bf328f6 100644 --- a/tests/Console/Command/AddPrefixCommandTest.php +++ b/tests/Console/Command/AddPrefixCommandTest.php @@ -24,6 +24,7 @@ use Symfony\Component\Console\Exception\RuntimeException; use Symfony\Component\Console\Tester\ApplicationTester; use Symfony\Component\Filesystem\Filesystem; +use function file_get_contents; use function Humbug\PhpScoper\escape_path; use function Humbug\PhpScoper\make_tmp_dir; use function Humbug\PhpScoper\remove_dir; @@ -195,9 +196,12 @@ public function test_scope_the_given_paths() $inputPath = escape_path($root.'/'.$expectedFile); $outputPath = escape_path($this->tmp.'/'.$expectedFile); + $inputContents = file_get_contents($inputPath); + $this->scoperProphecy ->scope( $inputPath, + $inputContents, 'MyPrefix', [], [], @@ -255,10 +259,13 @@ public function test_let_the_file_unchanged_when_cannot_scope_a_file() $inputPath = escape_path($root.'/'.$expectedFile); $outputPath = escape_path($this->tmp.'/'.$expectedFile); + $inputContents = file_get_contents($inputPath); + if (null !== $prefixedContents) { $this->scoperProphecy ->scope( $inputPath, + $inputContents, 'MyPrefix', [], [], @@ -272,6 +279,7 @@ public function test_let_the_file_unchanged_when_cannot_scope_a_file() $this->scoperProphecy ->scope( $inputPath, + $inputContents, 'MyPrefix', [], [], @@ -280,7 +288,7 @@ public function test_let_the_file_unchanged_when_cannot_scope_a_file() ->willThrow(new \RuntimeException('Scoping of the file failed')) ; - $this->fileSystemProphecy->dumpFile($outputPath, file_get_contents($inputPath))->shouldBeCalled(); + $this->fileSystemProphecy->dumpFile($outputPath, $inputContents)->shouldBeCalled(); } } @@ -327,9 +335,12 @@ public function test_do_not_scope_duplicated_given_paths() $inputPath = escape_path($root.'/'.$expectedFile); $outputPath = escape_path($this->tmp.'/'.$expectedFile); + $inputContents = file_get_contents($inputPath); + $this->scoperProphecy ->scope( $inputPath, + $inputContents, 'MyPrefix', [], [], @@ -385,11 +396,13 @@ public function test_scope_the_given_paths_and_the_ones_found_by_the_finder() $inputPath = realpath($inputPath); $outputPath = escape_path($outputPath); + $inputContents = file_get_contents($inputPath); $prefixedFileContents = 'Random string'; $this->scoperProphecy ->scope( $inputPath, + $inputContents, 'MyPrefix', [], [], @@ -434,6 +447,7 @@ public function test_applies_a_random_prefix_when_none_given() $this->scoperProphecy ->scope( + Argument::any(), Argument::any(), Argument::that( function (string $prefix): bool { @@ -497,9 +511,12 @@ public function test_scope_the_current_working_directory_if_no_path_given() $inputPath = escape_path($root.'/'.$expectedFile); $outputPath = escape_path($this->tmp.'/'.$expectedFile); + $inputContents = file_get_contents($inputPath); + $this->scoperProphecy ->scope( $inputPath, + $inputContents, 'MyPrefix', [], [], @@ -545,6 +562,7 @@ public function test_prefix_can_end_by_a_backslash() $this->scoperProphecy ->scope( + Argument::any(), Argument::any(), 'MyPrefix', [], @@ -588,6 +606,7 @@ public function test_prefix_can_end_by_multiple_backslashes() $this->scoperProphecy ->scope( + Argument::any(), Argument::any(), 'MyPrefix', [], @@ -642,9 +661,12 @@ public function test_an_output_directory_can_be_given() $inputPath = escape_path($root.'/'.$expectedFile); $outputPath = escape_path($outDir.'/'.$expectedFile); + $inputContents = file_get_contents($inputPath); + $this->scoperProphecy ->scope( $inputPath, + $inputContents, 'MyPrefix', [], [], @@ -702,9 +724,12 @@ public function test_relative_output_directory_are_made_absolute() $inputPath = escape_path($root.'/'.$expectedFile); $outputPath = escape_path($this->tmp.DIRECTORY_SEPARATOR.$outDir.'/'.$expectedFile); + $inputContents = file_get_contents($inputPath); + $this->scoperProphecy ->scope( $inputPath, + $inputContents, 'MyPrefix', [], [], @@ -822,9 +847,12 @@ public function test_attempts_to_use_patch_file_in_current_directory() $inputPath = escape_path($root.'/'.$expectedFile); $outputPath = escape_path($this->tmp.'/'.$expectedFile); + $inputContents = file_get_contents($inputPath); + $this->scoperProphecy ->scope( $inputPath, + $inputContents, 'MyPrefix', Argument::that(function ($arg) use (&$patchersFound) { $patchersFound = $arg; @@ -914,6 +942,7 @@ public function test_can_scope_projects_with_invalid_files() $this->scoperProphecy ->scope( $inputPath, + $fileContents, 'MyPrefix', [], [], diff --git a/tests/Scoper/Composer/InstalledPackagesScoperTest.php b/tests/Scoper/Composer/InstalledPackagesScoperTest.php index 50772456..53eb6bc3 100644 --- a/tests/Scoper/Composer/InstalledPackagesScoperTest.php +++ b/tests/Scoper/Composer/InstalledPackagesScoperTest.php @@ -21,9 +21,6 @@ use Prophecy\Prophecy\ObjectProphecy; use function Humbug\PhpScoper\create_fake_patcher; use function Humbug\PhpScoper\create_fake_whitelister; -use function Humbug\PhpScoper\escape_path; -use function Humbug\PhpScoper\make_tmp_dir; -use function Humbug\PhpScoper\remove_dir; /** * @covers \Humbug\PhpScoper\Scoper\Composer\InstalledPackagesScoper @@ -31,39 +28,6 @@ */ class InstalledPackagesScoperTest extends TestCase { - /** - * @var string - */ - private $cwd; - - /** - * @var string - */ - private $tmp; - - /** - * @inheritdoc - */ - public function setUp() - { - if (null == $this->tmp) { - $this->cwd = getcwd(); - $this->tmp = make_tmp_dir('scoper', __CLASS__); - } - - chdir($this->tmp); - } - - /** - * @inheritdoc - */ - public function tearDown() - { - chdir($this->cwd); - - remove_dir($this->tmp); - } - public function test_it_is_a_Scoper() { $this->assertTrue(is_a(InstalledPackagesScoper::class, Scoper::class, true)); @@ -71,19 +35,17 @@ public function test_it_is_a_Scoper() public function test_delegates_scoping_to_the_decorated_scoper_if_is_not_a_installed_file() { - $filePath = escape_path($this->tmp.'/file.php'); + $filePath = 'file.php'; + $fileContents = ''; $prefix = 'Humbug'; $patchers = [create_fake_patcher()]; $whitelist = ['Foo']; $whitelister = create_fake_whitelister(); - touch($filePath); - file_put_contents($filePath, ''); - /** @var Scoper|ObjectProphecy $decoratedScoperProphecy */ $decoratedScoperProphecy = $this->prophesize(Scoper::class); $decoratedScoperProphecy - ->scope($filePath, $prefix, $patchers, $whitelist, $whitelister) + ->scope($filePath, $fileContents, $prefix, $patchers, $whitelist, $whitelister) ->willReturn( $expected = 'Scoped content' ) @@ -93,7 +55,7 @@ public function test_delegates_scoping_to_the_decorated_scoper_if_is_not_a_insta $scoper = new InstalledPackagesScoper($decoratedScoper); - $actual = $scoper->scope($filePath, $prefix, $patchers, $whitelist, $whitelister); + $actual = $scoper->scope($filePath, $fileContents, $prefix, $patchers, $whitelist, $whitelister); $this->assertSame($expected, $actual); @@ -103,11 +65,9 @@ public function test_delegates_scoping_to_the_decorated_scoper_if_is_not_a_insta /** * @dataProvider provideInstalledPackagesFiles */ - public function test_it_prefixes_the_composer_autoloaders(string $fileContent, string $expected) + public function test_it_prefixes_the_composer_autoloaders(string $fileContents, string $expected) { - mkdir(escape_path($this->tmp.'/composer')); - touch($filePath = escape_path($this->tmp.'/composer/installed.json')); - file_put_contents($filePath, $fileContent); + $filePath = 'composer/installed.json'; $scoper = new InstalledPackagesScoper(new FakeScoper()); @@ -116,7 +76,7 @@ public function test_it_prefixes_the_composer_autoloaders(string $fileContent, s $whitelist = ['Foo']; $whitelister = create_fake_whitelister(); - $actual = $scoper->scope($filePath, $prefix, $patchers, $whitelist, $whitelister); + $actual = $scoper->scope($filePath, $fileContents, $prefix, $patchers, $whitelist, $whitelister); $this->assertSame($expected, $actual); } diff --git a/tests/Scoper/Composer/JsonFileScoperTest.php b/tests/Scoper/Composer/JsonFileScoperTest.php index 07f7da4d..9d2c5405 100644 --- a/tests/Scoper/Composer/JsonFileScoperTest.php +++ b/tests/Scoper/Composer/JsonFileScoperTest.php @@ -21,9 +21,6 @@ use Prophecy\Prophecy\ObjectProphecy; use function Humbug\PhpScoper\create_fake_patcher; use function Humbug\PhpScoper\create_fake_whitelister; -use function Humbug\PhpScoper\escape_path; -use function Humbug\PhpScoper\make_tmp_dir; -use function Humbug\PhpScoper\remove_dir; /** * @covers \Humbug\PhpScoper\Scoper\Composer\JsonFileScoper @@ -31,39 +28,6 @@ */ class JsonFileScoperTest extends TestCase { - /** - * @var string - */ - private $cwd; - - /** - * @var string - */ - private $tmp; - - /** - * @inheritdoc - */ - public function setUp() - { - if (null == $this->tmp) { - $this->cwd = getcwd(); - $this->tmp = make_tmp_dir('scoper', __CLASS__); - } - - chdir($this->tmp); - } - - /** - * @inheritdoc - */ - public function tearDown() - { - chdir($this->cwd); - - remove_dir($this->tmp); - } - public function test_it_is_a_Scoper() { $this->assertTrue(is_a(JsonFileScoper::class, Scoper::class, true)); @@ -71,19 +35,17 @@ public function test_it_is_a_Scoper() public function test_delegates_scoping_to_the_decorated_scoper_if_is_not_a_composer_file() { - $filePath = escape_path($this->tmp.'/file.php'); + $filePath = 'file.php'; + $fileContents = ''; $prefix = 'Humbug'; $patchers = [create_fake_patcher()]; $whitelist = ['Foo']; $whitelister = create_fake_whitelister(); - touch($filePath); - file_put_contents($filePath, ''); - /** @var Scoper|ObjectProphecy $decoratedScoperProphecy */ $decoratedScoperProphecy = $this->prophesize(Scoper::class); $decoratedScoperProphecy - ->scope($filePath, $prefix, $patchers, $whitelist, $whitelister) + ->scope($filePath, $fileContents, $prefix, $patchers, $whitelist, $whitelister) ->willReturn( $expected = 'Scoped content' ) @@ -93,7 +55,7 @@ public function test_delegates_scoping_to_the_decorated_scoper_if_is_not_a_compo $scoper = new JsonFileScoper($decoratedScoper); - $actual = $scoper->scope($filePath, $prefix, $patchers, $whitelist, $whitelister); + $actual = $scoper->scope($filePath, $fileContents, $prefix, $patchers, $whitelist, $whitelister); $this->assertSame($expected, $actual); @@ -103,10 +65,9 @@ public function test_delegates_scoping_to_the_decorated_scoper_if_is_not_a_compo /** * @dataProvider provideComposerFiles */ - public function test_it_prefixes_the_composer_autoloaders(string $fileContent, string $expected) + public function test_it_prefixes_the_composer_autoloaders(string $fileContents, string $expected) { - touch($filePath = escape_path($this->tmp.'/composer.json')); - file_put_contents($filePath, $fileContent); + $filePath = 'composer.json'; $scoper = new JsonFileScoper(new FakeScoper()); @@ -115,7 +76,7 @@ public function test_it_prefixes_the_composer_autoloaders(string $fileContent, s $whitelist = ['Foo']; $whitelister = create_fake_whitelister(); - $actual = $scoper->scope($filePath, $prefix, $patchers, $whitelist, $whitelister); + $actual = $scoper->scope($filePath, $fileContents, $prefix, $patchers, $whitelist, $whitelister); $this->assertSame($expected, $actual); } @@ -175,10 +136,9 @@ public function provideComposerFiles() /** * @dataProvider providePSR0ComposerFiles */ - public function test_it_prefixes_psr0_autoloaders(string $fileContent, string $expected) + public function test_it_prefixes_psr0_autoloaders(string $fileContents, string $expected) { - touch($filePath = escape_path($this->tmp.'/composer.json')); - file_put_contents($filePath, $fileContent); + $filePath = 'composer.json'; $scoper = new JsonFileScoper(new FakeScoper()); @@ -187,7 +147,7 @@ public function test_it_prefixes_psr0_autoloaders(string $fileContent, string $e $whitelist = ['Foo']; $whitelister = create_fake_whitelister(); - $actual = $scoper->scope($filePath, $prefix, $patchers, $whitelist, $whitelister); + $actual = $scoper->scope($filePath, $fileContents, $prefix, $patchers, $whitelist, $whitelister); $this->assertSame($expected, $actual); } diff --git a/tests/Scoper/FakeScoper.php b/tests/Scoper/FakeScoper.php index 6b7d07fa..881d91ef 100644 --- a/tests/Scoper/FakeScoper.php +++ b/tests/Scoper/FakeScoper.php @@ -22,7 +22,7 @@ final class FakeScoper implements Scoper /** * @inheritdoc */ - public function scope(string $filePath, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string + public function scope(string $filePath, string $contents, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string { throw new LogicException(); } diff --git a/tests/Scoper/NullScoperTest.php b/tests/Scoper/NullScoperTest.php index 78b95784..8648be08 100644 --- a/tests/Scoper/NullScoperTest.php +++ b/tests/Scoper/NullScoperTest.php @@ -18,48 +18,12 @@ use PHPUnit\Framework\TestCase; use function Humbug\PhpScoper\create_fake_patcher; use function Humbug\PhpScoper\create_fake_whitelister; -use function Humbug\PhpScoper\escape_path; -use function Humbug\PhpScoper\make_tmp_dir; -use function Humbug\PhpScoper\remove_dir; /** * @covers \Humbug\PhpScoper\Scoper\NullScoper */ class NullScoperTest extends TestCase { - /** - * @var string - */ - private $cwd; - - /** - * @var string - */ - private $tmp; - - /** - * @inheritdoc - */ - public function setUp() - { - if (null === $this->tmp) { - $this->cwd = getcwd(); - $this->tmp = make_tmp_dir('scoper', __CLASS__); - } - - chdir($this->tmp); - } - - /** - * @inheritdoc - */ - public function tearDown() - { - chdir($this->cwd); - - remove_dir($this->tmp); - } - public function test_is_a_Scoper() { $this->assertTrue(is_a(NullScoper::class, Scoper::class, true)); @@ -67,11 +31,8 @@ public function test_is_a_Scoper() public function test_returns_the_file_content_unchanged() { - $filePath = escape_path($this->tmp.'/file'); - $content = $expected = 'File content'; - - touch($filePath); - file_put_contents($filePath, $content); + $filePath = 'file'; + $contents = $expected = 'File content'; $prefix = 'Humbug'; @@ -83,7 +44,7 @@ public function test_returns_the_file_content_unchanged() $scoper = new NullScoper(); - $actual = $scoper->scope($filePath, $prefix, $patchers, $whitelist, $whitelister); + $actual = $scoper->scope($filePath, $contents, $prefix, $patchers, $whitelist, $whitelister); $this->assertSame($expected, $actual); } diff --git a/tests/Scoper/PatchScoperTest.php b/tests/Scoper/PatchScoperTest.php index 9b75dad1..5c1dbffb 100644 --- a/tests/Scoper/PatchScoperTest.php +++ b/tests/Scoper/PatchScoperTest.php @@ -53,21 +53,21 @@ public function test_is_a_Scoper() public function test_applies_the_list_of_patches_to_the_scoped_file() { $filePath = '/path/to/file.php'; - $content = 'Original file content'; + $contents = 'Original file content'; $prefix = 'Humbug'; $patchers = [ - function (string $patcherFilePath, string $patcherPrefix, string $content) use ($filePath, $prefix): string { + function (string $patcherFilePath, string $patcherPrefix, string $contents) use ($filePath, $prefix): string { Assert::assertSame($filePath, $patcherFilePath); Assert::assertSame($prefix, $patcherPrefix); - Assert::assertSame('Original file content', $content); + Assert::assertSame('Decorated scoper contents', $contents); return 'File content after patch 1'; }, - function (string $patcherFilePath, string $patcherPrefix, string $content) use ($filePath, $prefix): string { + function (string $patcherFilePath, string $patcherPrefix, string $contents) use ($filePath, $prefix): string { Assert::assertSame($filePath, $patcherFilePath); Assert::assertSame($prefix, $patcherPrefix); - Assert::assertSame('File content after patch 1', $content); + Assert::assertSame('File content after patch 1', $contents); return 'File content after patch 2'; }, @@ -78,15 +78,15 @@ function (string $patcherFilePath, string $patcherPrefix, string $content) use ( $whitelister = create_fake_whitelister(); $this->decoratedScoperProphecy - ->scope($filePath, $prefix, $patchers, $whitelist, $whitelister) - ->willReturn($content) + ->scope($filePath, $contents, $prefix, $patchers, $whitelist, $whitelister) + ->willReturn('Decorated scoper contents') ; $expected = 'File content after patch 2'; $scoper = new PatchScoper($this->decoratedScoper); - $actual = $scoper->scope($filePath, $prefix, $patchers, $whitelist, $whitelister); + $actual = $scoper->scope($filePath, $contents, $prefix, $patchers, $whitelist, $whitelister); $this->assertSame($expected, $actual); diff --git a/tests/Scoper/PhpScoperTest.php b/tests/Scoper/PhpScoperTest.php index 6a315763..1fd0f5f1 100644 --- a/tests/Scoper/PhpScoperTest.php +++ b/tests/Scoper/PhpScoperTest.php @@ -31,8 +31,6 @@ use function Humbug\PhpScoper\create_fake_whitelister; use function Humbug\PhpScoper\create_parser; use function Humbug\PhpScoper\escape_path; -use function Humbug\PhpScoper\make_tmp_dir; -use function Humbug\PhpScoper\remove_dir; class PhpScoperTest extends TestCase { @@ -105,11 +103,6 @@ public function setUp() new TraverserFactory() ); - if (null === $this->tmp) { - $this->cwd = getcwd(); - $this->tmp = make_tmp_dir('scoper', __CLASS__); - } - $this->decoratedScoperProphecy = $this->prophesize(Scoper::class); $this->decoratedScoper = $this->decoratedScoperProphecy->reveal(); @@ -121,18 +114,6 @@ public function setUp() $this->parserProphecy = $this->prophesize(Parser::class); $this->parser = $this->parserProphecy->reveal(); - - chdir($this->tmp); - } - - /** - * @inheritdoc - */ - public function tearDown() - { - chdir($this->cwd); - - remove_dir($this->tmp); } public function test_is_a_Scoper() @@ -148,19 +129,16 @@ public function test_can_scope_a_PHP_file() $whitelist = ['Foo']; $whitelister = create_fake_whitelister(); - $content = <<<'PHP' + $contents = <<<'PHP' echo "Humbug!"; PHP; - touch($filePath); - file_put_contents($filePath, $content); - $expected = <<<'PHP' echo "Humbug!"; PHP; - $actual = $this->scoper->scope($filePath, $prefix, $patchers, $whitelist, $whitelister); + $actual = $this->scoper->scope($filePath, $contents, $prefix, $patchers, $whitelist, $whitelister); $this->assertSame($expected, $actual); } @@ -168,13 +146,14 @@ public function test_can_scope_a_PHP_file() public function test_does_not_scope_file_if_is_not_a_PHP_file() { $filePath = 'file.yaml'; + $fileContents = ''; $prefix = 'Humbug'; $patchers = [create_fake_patcher()]; $whitelist = ['Foo']; $whitelister = create_fake_whitelister(); $this->decoratedScoperProphecy - ->scope($filePath, $prefix, $patchers, $whitelist, $whitelister) + ->scope($filePath, $fileContents, $prefix, $patchers, $whitelist, $whitelister) ->willReturn( $expected = 'Scoped content' ) @@ -191,7 +170,7 @@ public function test_does_not_scope_file_if_is_not_a_PHP_file() $this->traverserFactory ); - $actual = $scoper->scope($filePath, $prefix, $patchers, $whitelist, $whitelister); + $actual = $scoper->scope($filePath, $fileContents, $prefix, $patchers, $whitelist, $whitelister); $this->assertSame($expected, $actual); @@ -206,16 +185,13 @@ public function test_can_scope_PHP_binary_files() $whitelist = ['Foo']; $whitelister = create_fake_whitelister(); - $content = <<<'PHP' + $contents = <<<'PHP' #!/usr/bin/env php scoper->scope($filePath, $prefix, $patchers, $whitelist, $whitelister); + $actual = $this->scoper->scope($filePath, $contents, $prefix, $patchers, $whitelist, $whitelister); $this->assertSame($expected, $actual); } @@ -240,18 +216,15 @@ public function test_does_not_scope_a_non_PHP_binary_files() $whitelister = create_fake_whitelister(); - $content = <<<'PHP' + $contents = <<<'PHP' #!/usr/bin/env bash decoratedScoperProphecy - ->scope($filePath, $prefix, $patchers, $whitelist, $whitelister) + ->scope($filePath, $contents, $prefix, $patchers, $whitelist, $whitelister) ->willReturn( $expected = 'Scoped content' ) @@ -268,7 +241,7 @@ public function test_does_not_scope_a_non_PHP_binary_files() $this->traverserFactory ); - $actual = $scoper->scope($filePath, $prefix, $patchers, $whitelist, $whitelister); + $actual = $scoper->scope($filePath, $contents, $prefix, $patchers, $whitelist, $whitelister); $this->assertSame($expected, $actual); @@ -278,23 +251,20 @@ public function test_does_not_scope_a_non_PHP_binary_files() public function test_cannot_scope_an_invalid_PHP_file() { $filePath = escape_path($this->tmp.'/invalid-file.php'); - $content = <<<'PHP' + $contents = <<<'PHP' scoper->scope($filePath, $prefix, $patchers, $whitelist, $whitelister); + $this->scoper->scope($filePath, $contents, $prefix, $patchers, $whitelist, $whitelister); $this->fail('Expected exception to have been thrown.'); } catch (PhpParserError $error) { @@ -320,7 +290,7 @@ public function test_creates_a_new_traverser_for_each_file() $whitelister = create_fake_whitelister(); $this->decoratedScoperProphecy - ->scope(Argument::any(), $prefix, $patchers, $whitelist, $whitelister) + ->scope(Argument::any(), Argument::any(), $prefix, $patchers, $whitelist, $whitelister) ->willReturn( $expected = 'Scoped content' ) @@ -380,11 +350,8 @@ function (...$args) use (&$i): bool { $this->traverserFactory ); - foreach ($files as $file => $content) { - touch($file); - file_put_contents($file, $content); - - $scoper->scope($file, $prefix, $patchers, $whitelist, $whitelister); + foreach ($files as $file => $contents) { + $scoper->scope($file, $contents, $prefix, $patchers, $whitelist, $whitelister); } $this->parserProphecy->parse(Argument::cetera())->shouldHaveBeenCalledTimes(2); @@ -396,20 +363,17 @@ function (...$args) use (&$i): bool { /** * @dataProvider provideValidFiles */ - public function test_can_scope_valid_files(string $spec, string $content, string $prefix, array $whitelist, string $expected) + public function test_can_scope_valid_files(string $spec, string $contents, string $prefix, array $whitelist, string $expected) { $filePath = escape_path($this->tmp.'/file.php'); - touch($filePath); - file_put_contents($filePath, $content); - $patchers = [create_fake_patcher()]; $whitelister = function (string $className) { return 'AppKernel' === $className; }; - $actual = $this->scoper->scope($filePath, $prefix, $patchers, $whitelist, $whitelister); + $actual = $this->scoper->scope($filePath, $contents, $prefix, $patchers, $whitelist, $whitelister); $titleSeparator = str_repeat( '=', @@ -431,7 +395,7 @@ public function test_can_scope_valid_files(string $spec, string $content, string $titleSeparator INPUT $titleSeparator -$content +$contents $titleSeparator EXPECTED @@ -480,8 +444,6 @@ public function provideValidFiles() ); } } - - return; } /**