Skip to content

Commit c435fb4

Browse files
authored
Fix PHPUnit coverage (#81)
1 parent 0a8ecab commit c435fb4

File tree

6 files changed

+104
-20
lines changed

6 files changed

+104
-20
lines changed

phpunit.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ file that was distributed with this source code.
2626
<directory suffix=".php">./src/</directory>
2727
<exclude>
2828
<file>src/Console/Application.php</file>
29+
<file>src/Console/Configuration.php</file>
30+
<directory>src/Logger</directory>
31+
<directory>src/NodeVisitor</directory>
32+
<file>src/functions.php</file>
2933
</exclude>
3034
</whitelist>
3135
</filter>

src/Scoper/Composer/InstalledPackagesScoper.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,15 @@
1515
namespace Humbug\PhpScoper\Scoper\Composer;
1616

1717
use Humbug\PhpScoper\Scoper;
18-
use LogicException;
1918

2019
final class InstalledPackagesScoper implements Scoper
2120
{
22-
/**
23-
* @var string
24-
*/
25-
private static $filePattern;
21+
private static $filePattern = '/composer\/installed\.json/';
2622

2723
private $decoratedScoper;
2824

2925
public function __construct(Scoper $decoratedScoper)
3026
{
31-
if (null === self::$filePattern) {
32-
self::$filePattern = str_replace(
33-
'/',
34-
DIRECTORY_SEPARATOR,
35-
'~composer/installed\.json~'
36-
);
37-
}
38-
3927
$this->decoratedScoper = $decoratedScoper;
4028
}
4129

@@ -46,10 +34,6 @@ public function __construct(Scoper $decoratedScoper)
4634
*/
4735
public function scope(string $filePath, string $prefix, array $patchers, callable $globalWhitelister): string
4836
{
49-
if (null === self::$filePattern) {
50-
throw new LogicException('Cannot be used without being initialised first.');
51-
}
52-
5337
if (1 !== preg_match(self::$filePattern, $filePath)) {
5438
return $this->decoratedScoper->scope($filePath, $prefix, $patchers, $globalWhitelister);
5539
}

tests/Console/Command/AddPrefixCommandIntegrationTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public function test_scope_in_very_verbose_mode()
234234
PHP Scoper version 12ccf1ac8c7ae8eaf502bd30f95630a112dc713f
235235
236236
* [NO] /path/to/composer/installed.json
237-
Could not parse the file "/path/to/composer/installed.json".: TypeError: Argument 1 passed to Humbug\PhpScoper\Scoper\Composer\AutoloadPrefixer::prefixPackageAutoloads() must be of the type array, string given, called in $dir/src/Scoper/Composer/InstalledPackagesScoper.php on line 73 and defined in $dir/src/Scoper/Composer/AutoloadPrefixer.php:28
237+
Could not parse the file "/path/to/composer/installed.json".: TypeError
238238
Stack trace:
239239
#0
240240
#1
@@ -264,7 +264,7 @@ public function test_scope_in_very_verbose_mode()
264264
#25
265265
* [OK] /path/to/file.php
266266
* [NO] /path/to/invalid-file.php
267-
Could not parse the file "/path/to/invalid-file.php".: PhpParser\Error: Syntax error, unexpected EOF on line 3 in $dir/vendor/nikic/php-parser/lib/PhpParser/ParserAbstract.php:293
267+
Could not parse the file "/path/to/invalid-file.php".: PhpParser
268268
Stack trace:
269269
#0
270270
#1
@@ -302,7 +302,8 @@ public function test_scope_in_very_verbose_mode()
302302
EOF;
303303

304304
$actual = $this->getNormalizeDisplay($this->appTester->getDisplay(true));
305-
$actual = preg_replace('/(#\d+)(.*)(\n)/', '$1$3', $actual);
305+
$actual = preg_replace('/(Could not parse the file ".+?"\.: \w+).*(\n)/', '$1$2', $actual);
306+
$actual = preg_replace('/(#\d+).*(\n)/', '$1$2', $actual);
306307

307308
$this->assertSame($expected, $actual);
308309
$this->assertSame(0, $this->appTester->getStatusCode());

tests/Scoper/Composer/InstalledPackagesScoperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
/**
2929
* @covers \Humbug\PhpScoper\Scoper\Composer\InstalledPackagesScoper
30+
* @covers \Humbug\PhpScoper\Scoper\Composer\AutoloadPrefixer
3031
*/
3132
class InstalledPackagesScoperTest extends TestCase
3233
{

tests/Scoper/Composer/JsonFileScoperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
/**
2929
* @covers \Humbug\PhpScoper\Scoper\Composer\JsonFileScoper
30+
* @covers \Humbug\PhpScoper\Scoper\Composer\AutoloadPrefixer
3031
*/
3132
class JsonFileScoperTest extends TestCase
3233
{

tests/Scoper/PatchScoperTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 <[email protected]>,
9+
* Pádraic Brady <[email protected]>
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 PHPUnit\Framework\Assert;
19+
use PHPUnit\Framework\TestCase;
20+
use Prophecy\Argument;
21+
use Prophecy\Prophecy\ObjectProphecy;
22+
use function Humbug\PhpScoper\create_fake_whitelister;
23+
24+
/**
25+
* @covers \Humbug\PhpScoper\Scoper\PatchScoper
26+
*/
27+
class PatchScoperTest extends TestCase
28+
{
29+
/**
30+
* @var Scoper|ObjectProphecy
31+
*/
32+
private $decoratedScoperProphecy;
33+
34+
/**
35+
* @var Scoper
36+
*/
37+
private $decoratedScoper;
38+
39+
/**
40+
* @inheritdoc
41+
*/
42+
public function setUp()
43+
{
44+
$this->decoratedScoperProphecy = $this->prophesize(Scoper::class);
45+
$this->decoratedScoper = $this->decoratedScoperProphecy->reveal();
46+
}
47+
48+
public function test_is_a_Scoper()
49+
{
50+
$this->assertTrue(is_a(PatchScoper::class, Scoper::class, true));
51+
}
52+
53+
public function test_applies_the_list_of_patches_to_the_scoped_file()
54+
{
55+
$filePath = '/path/to/file.php';
56+
$content = 'Original file content';
57+
$prefix = 'Humbug';
58+
59+
$patchers = [
60+
function (string $patcherFilePath, string $patcherPrefix, string $content) use ($filePath, $prefix): string {
61+
Assert::assertSame($filePath, $patcherFilePath);
62+
Assert::assertSame($prefix, $patcherPrefix);
63+
Assert::assertSame('Original file content', $content);
64+
65+
return 'File content after patch 1';
66+
},
67+
function (string $patcherFilePath, string $patcherPrefix, string $content) use ($filePath, $prefix): string {
68+
Assert::assertSame($filePath, $patcherFilePath);
69+
Assert::assertSame($prefix, $patcherPrefix);
70+
Assert::assertSame('File content after patch 1', $content);
71+
72+
return 'File content after patch 2';
73+
},
74+
];
75+
76+
$whitelister = create_fake_whitelister();
77+
78+
$this->decoratedScoperProphecy
79+
->scope($filePath, $prefix, $patchers, $whitelister)
80+
->willReturn($content)
81+
;
82+
83+
$expected = 'File content after patch 2';
84+
85+
$scoper = new PatchScoper($this->decoratedScoper);
86+
87+
$actual = $scoper->scope($filePath, $prefix, $patchers, $whitelister);
88+
89+
$this->assertSame($expected, $actual);
90+
91+
$this->decoratedScoperProphecy->scope(Argument::cetera())->shouldHaveBeenCalledTimes(1);
92+
}
93+
}

0 commit comments

Comments
 (0)