Skip to content

Commit e0892a6

Browse files
authored
Add Configuration::withPrefix() (#629)
1 parent f408241 commit e0892a6

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

src/Configuration/Configuration.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ public function getPath(): ?string
7474
return $this->path;
7575
}
7676

77+
/**
78+
* @param non-empty-string $prefix
79+
*/
80+
public function withPrefix(string $prefix): self
81+
{
82+
return new self(
83+
$this->path,
84+
$prefix,
85+
$this->filesWithContents,
86+
$this->excludedFilesWithContents,
87+
$this->patcher,
88+
$this->symbolsConfiguration,
89+
);
90+
}
91+
7792
/**
7893
* @return non-empty-string
7994
*/

src/PhpParser/Node/PrefixedName.php

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

1717
use PhpParser\Node\Name\FullyQualified;
1818

19+
// TODO:review
1920
final class PrefixedName extends FullyQualified
2021
{
2122
private FullyQualified $prefixedName;

tests/Configuration/ConfigurationTest.php

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
namespace Humbug\PhpScoper\Configuration;
1616

17+
use Humbug\PhpScoper\Patcher\FakePatcher;
18+
use Humbug\PhpScoper\Patcher\Patcher;
1719
use Humbug\PhpScoper\Patcher\PatcherChain;
18-
use Humbug\PhpScoper\Whitelist;
1920
use InvalidArgumentException;
2021
use PHPUnit\Framework\TestCase;
2122

@@ -42,12 +43,34 @@ public function test_it_validates_the_prefix(
4243
[],
4344
new PatcherChain([]),
4445
SymbolsConfiguration::create(),
45-
[],
46-
[],
47-
[],
4846
);
4947
}
5048

49+
public function test_it_can_create_a_new_instance_with_a_different_prefix(): void
50+
{
51+
$values = [
52+
'/path/to/config',
53+
'initialPrefix',
54+
['/path/to/fileA' => ['/path/to/fileA', 'fileAContent']],
55+
['/path/to/fileB' => ['/path/to/fileB', 'fileBContent']],
56+
new FakePatcher(),
57+
SymbolsConfiguration::create(),
58+
];
59+
60+
$config = new Configuration(...$values);
61+
62+
// Sanity check
63+
self::assertStateIs($config, ...$values);
64+
65+
$newConfig = $config->withPrefix('newPrefix');
66+
67+
$expectedNewConfigValues = $values;
68+
$expectedNewConfigValues[1] = 'newPrefix';
69+
70+
self::assertStateIs($config, ...$values);
71+
self::assertStateIs($newConfig, ...$expectedNewConfigValues);
72+
}
73+
5174
public static function prefixProvider(): iterable
5275
{
5376
yield [
@@ -60,4 +83,30 @@ public static function prefixProvider(): iterable
6083
'Invalid namespace separator sequence. Got "App\\\\Foo"',
6184
];
6285
}
86+
87+
private static function assertStateIs(
88+
Configuration $configuration,
89+
?string $expectedPath,
90+
string $expectedPrefix,
91+
array $expectedFilesWithContents,
92+
array $expectedExcludedFilesWithContents,
93+
Patcher $expectedPatcher,
94+
SymbolsConfiguration $expectedSymbolsConfiguration
95+
): void {
96+
self::assertSame($expectedPath, $configuration->getPath());
97+
self::assertSame($expectedPrefix, $configuration->getPrefix());
98+
self::assertEqualsCanonicalizing(
99+
$expectedFilesWithContents,
100+
$configuration->getFilesWithContents(),
101+
);
102+
self::assertEqualsCanonicalizing(
103+
$expectedExcludedFilesWithContents,
104+
$configuration->getExcludedFilesWithContents(),
105+
);
106+
self::assertEquals($expectedPatcher, $configuration->getPatcher());
107+
self::assertEquals(
108+
$expectedSymbolsConfiguration,
109+
$configuration->getSymbolsConfiguration(),
110+
);
111+
}
63112
}

0 commit comments

Comments
 (0)