forked from humbug/php-scoper
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes humbug#196
- Loading branch information
Showing
7 changed files
with
223 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the humbug/php-scoper package. | ||
* | ||
* Copyright (c) 2017 Théo FIDRY <[email protected]>, | ||
* Pádraic Brady <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Humbug\PhpScoper\Scoper; | ||
|
||
use function array_flip; | ||
use function array_key_exists; | ||
use Humbug\PhpScoper\Scoper; | ||
use Humbug\PhpScoper\Whitelist; | ||
|
||
final class FileWhitelistScoper implements Scoper | ||
{ | ||
private $decoratedScoper; | ||
private $filePaths; | ||
|
||
public function __construct(Scoper $decoratedScoper, string ...$filePaths) | ||
{ | ||
$this->decoratedScoper = $decoratedScoper; | ||
$this->filePaths = array_flip($filePaths); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function scope(string $filePath, string $contents, string $prefix, array $patchers, Whitelist $whitelist): string | ||
{ | ||
if (array_key_exists($filePath, $this->filePaths)) { | ||
return $contents; | ||
} | ||
|
||
return $this->decoratedScoper->scope($filePath, $contents, $prefix, $patchers, $whitelist); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the humbug/php-scoper package. | ||
* | ||
* Copyright (c) 2017 Théo FIDRY <[email protected]>, | ||
* Pádraic Brady <[email protected]> | ||
* | ||
* 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\Assert; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
|
||
/** | ||
* @covers \Humbug\PhpScoper\Scoper\FileWhitelistScoper | ||
*/ | ||
class FileWhitelistScoperTest 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(FileWhitelistScoper::class, Scoper::class, true)); | ||
} | ||
|
||
public function test_it_scopes_the_file_contents_with_the_decorated_scoper_if_file_not_whitelisted_and_the_contents_unchanged_when_is_whitelisted() | ||
{ | ||
$whitelistedFilePath = '/path/to/whitelist-file.php'; | ||
$notWhitelistedFilePath = '/path/to/not-file.php'; | ||
$contents = 'Original file content'; | ||
$prefix = 'Humbug'; | ||
$patchers = []; | ||
$whitelist = Whitelist::create(true, true, true, 'Foo'); | ||
|
||
$this->decoratedScoperProphecy | ||
->scope($notWhitelistedFilePath, $contents, $prefix, $patchers, $whitelist) | ||
->willReturn($scopedContents = 'Decorated scoper contents') | ||
; | ||
|
||
$scoper = new FileWhitelistScoper($this->decoratedScoper, $whitelistedFilePath); | ||
|
||
$this->assertSame( | ||
$scopedContents, | ||
$scoper->scope($notWhitelistedFilePath, $contents, $prefix, $patchers, $whitelist) | ||
); | ||
|
||
$this->assertSame( | ||
$contents, | ||
$scoper->scope($whitelistedFilePath, $contents, $prefix, $patchers, $whitelist) | ||
); | ||
|
||
$this->decoratedScoperProphecy->scope(Argument::cetera())->shouldHaveBeenCalledTimes(1); | ||
} | ||
} |