Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix with whitelisted files for Configurable Scoper #268

Merged
merged 3 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}