From a40ad6f3215aeed218af84c14ba12b24dfdbc1e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 24 Oct 2018 14:17:09 +0200 Subject: [PATCH] Fix with whitelisted files for Configurable Scoper (#268) --- src/Scoper/ConfigurableScoper.php | 10 ++- tests/Scoper/ConfigurableScoperTest.php | 106 ++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 tests/Scoper/ConfigurableScoperTest.php diff --git a/src/Scoper/ConfigurableScoper.php b/src/Scoper/ConfigurableScoper.php index 2e762f2b..ceb097c4 100644 --- a/src/Scoper/ConfigurableScoper.php +++ b/src/Scoper/ConfigurableScoper.php @@ -30,7 +30,15 @@ public function withWhitelistedFiles(string ...$whitelistedFiles): self { $self = clone $this; - return [] === $whitelistedFiles ? $self : new self(new FileWhitelistScoper($self)); + return [] === $whitelistedFiles + ? $self + : new self( + new FileWhitelistScoper( + $self, + ...$whitelistedFiles + ) + ) + ; } /** diff --git a/tests/Scoper/ConfigurableScoperTest.php b/tests/Scoper/ConfigurableScoperTest.php new file mode 100644 index 00000000..ee5012a5 --- /dev/null +++ b/tests/Scoper/ConfigurableScoperTest.php @@ -0,0 +1,106 @@ +, + * Pádraic Brady + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Humbug\PhpScoper\Scoper; + +use Humbug\PhpScoper\Scoper; +use Humbug\PhpScoper\Whitelist; +use PHPUnit\Framework\TestCase; +use Prophecy\Argument; +use Prophecy\Prophecy\ObjectProphecy; + +/** + * @covers \Humbug\PhpScoper\Scoper\ConfigurableScoper + */ +class ConfigurableScoperTest extends TestCase +{ + /** + * @var Scoper|ObjectProphecy + */ + private $decoratedScoperProphecy; + + /** + * @var Scoper + */ + private $decoratedScoper; + + /** + * @inheritdoc + */ + public function setUp() + { + $this->decoratedScoperProphecy = $this->prophesize(Scoper::class); + $this->decoratedScoper = $this->decoratedScoperProphecy->reveal(); + } + + public function test_is_a_Scoper() + { + $this->assertTrue(is_a(ConfigurableScoper::class, Scoper::class, true)); + } + + public function test_it_scopes_the_files_with_the_decorated_scoper() + { + $filePath = '/path/to/file.php'; + $contents = 'Original file content'; + $prefix = 'Humbug'; + $patchers = []; + $whitelist = Whitelist::create(true, true, true, 'Foo'); + + $this->decoratedScoperProphecy + ->scope($filePath, $contents, $prefix, $patchers, $whitelist) + ->willReturn($expected = 'Decorated scoper contents') + ; + + $scoper = new ConfigurableScoper($this->decoratedScoper); + + $actual = $scoper->scope($filePath, $contents, $prefix, $patchers, $whitelist); + + $this->assertSame($expected, $actual); + + $this->decoratedScoperProphecy->scope(Argument::cetera())->shouldHaveBeenCalledTimes(1); + } + + public function test_it_can_create_a_scoper_allowing_to_whitelist_specific_files() + { + $whitelistedFiles = [ + '/path/to/whitelisted-file-1', + '/path/to/whitelisted-file-2', + ]; + + $filePath = '/path/to/file.php'; + $contents = 'Original file content'; + $prefix = 'Humbug'; + $patchers = []; + $whitelist = Whitelist::create(true, true, true, 'Foo'); + + $this->decoratedScoperProphecy + ->scope(Argument::any(), $contents, $prefix, $patchers, $whitelist) + ->willReturn($expected = 'scoped contents') + ; + + $scoper = (new ConfigurableScoper($this->decoratedScoper))->withWhitelistedFiles(...$whitelistedFiles); + + foreach ($whitelistedFiles as $whitelistedFile) { + $actual = $scoper->scope($whitelistedFile, $contents, $prefix, $patchers, $whitelist); + + $this->assertSame($contents, $actual); + } + + $actual = $scoper->scope($filePath, $contents, $prefix, $patchers, $whitelist); + + $this->assertSame($expected, $actual); + + $this->decoratedScoperProphecy->scope(Argument::cetera())->shouldHaveBeenCalledTimes(1); + } +}