Skip to content

Commit

Permalink
Fix with whitelisted files for Configurable Scoper (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Oct 24, 2018
1 parent a80085b commit a40ad6f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Scoper/ConfigurableScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
;
}

/**
Expand Down
106 changes: 106 additions & 0 deletions tests/Scoper/ConfigurableScoperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?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\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);
}
}

0 comments on commit a40ad6f

Please sign in to comment.