Skip to content

Commit a40ad6f

Browse files
authored
Fix with whitelisted files for Configurable Scoper (#268)
1 parent a80085b commit a40ad6f

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

src/Scoper/ConfigurableScoper.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ public function withWhitelistedFiles(string ...$whitelistedFiles): self
3030
{
3131
$self = clone $this;
3232

33-
return [] === $whitelistedFiles ? $self : new self(new FileWhitelistScoper($self));
33+
return [] === $whitelistedFiles
34+
? $self
35+
: new self(
36+
new FileWhitelistScoper(
37+
$self,
38+
...$whitelistedFiles
39+
)
40+
)
41+
;
3442
}
3543

3644
/**
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <theo.fidry@gmail.com>,
9+
* Pádraic Brady <padraic.brady@gmail.com>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Humbug\PhpScoper\Scoper;
16+
17+
use Humbug\PhpScoper\Scoper;
18+
use Humbug\PhpScoper\Whitelist;
19+
use PHPUnit\Framework\TestCase;
20+
use Prophecy\Argument;
21+
use Prophecy\Prophecy\ObjectProphecy;
22+
23+
/**
24+
* @covers \Humbug\PhpScoper\Scoper\ConfigurableScoper
25+
*/
26+
class ConfigurableScoperTest extends TestCase
27+
{
28+
/**
29+
* @var Scoper|ObjectProphecy
30+
*/
31+
private $decoratedScoperProphecy;
32+
33+
/**
34+
* @var Scoper
35+
*/
36+
private $decoratedScoper;
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
public function setUp()
42+
{
43+
$this->decoratedScoperProphecy = $this->prophesize(Scoper::class);
44+
$this->decoratedScoper = $this->decoratedScoperProphecy->reveal();
45+
}
46+
47+
public function test_is_a_Scoper()
48+
{
49+
$this->assertTrue(is_a(ConfigurableScoper::class, Scoper::class, true));
50+
}
51+
52+
public function test_it_scopes_the_files_with_the_decorated_scoper()
53+
{
54+
$filePath = '/path/to/file.php';
55+
$contents = 'Original file content';
56+
$prefix = 'Humbug';
57+
$patchers = [];
58+
$whitelist = Whitelist::create(true, true, true, 'Foo');
59+
60+
$this->decoratedScoperProphecy
61+
->scope($filePath, $contents, $prefix, $patchers, $whitelist)
62+
->willReturn($expected = 'Decorated scoper contents')
63+
;
64+
65+
$scoper = new ConfigurableScoper($this->decoratedScoper);
66+
67+
$actual = $scoper->scope($filePath, $contents, $prefix, $patchers, $whitelist);
68+
69+
$this->assertSame($expected, $actual);
70+
71+
$this->decoratedScoperProphecy->scope(Argument::cetera())->shouldHaveBeenCalledTimes(1);
72+
}
73+
74+
public function test_it_can_create_a_scoper_allowing_to_whitelist_specific_files()
75+
{
76+
$whitelistedFiles = [
77+
'/path/to/whitelisted-file-1',
78+
'/path/to/whitelisted-file-2',
79+
];
80+
81+
$filePath = '/path/to/file.php';
82+
$contents = 'Original file content';
83+
$prefix = 'Humbug';
84+
$patchers = [];
85+
$whitelist = Whitelist::create(true, true, true, 'Foo');
86+
87+
$this->decoratedScoperProphecy
88+
->scope(Argument::any(), $contents, $prefix, $patchers, $whitelist)
89+
->willReturn($expected = 'scoped contents')
90+
;
91+
92+
$scoper = (new ConfigurableScoper($this->decoratedScoper))->withWhitelistedFiles(...$whitelistedFiles);
93+
94+
foreach ($whitelistedFiles as $whitelistedFile) {
95+
$actual = $scoper->scope($whitelistedFile, $contents, $prefix, $patchers, $whitelist);
96+
97+
$this->assertSame($contents, $actual);
98+
}
99+
100+
$actual = $scoper->scope($filePath, $contents, $prefix, $patchers, $whitelist);
101+
102+
$this->assertSame($expected, $actual);
103+
104+
$this->decoratedScoperProphecy->scope(Argument::cetera())->shouldHaveBeenCalledTimes(1);
105+
}
106+
}

0 commit comments

Comments
 (0)