|
7 | 7 |
|
8 | 8 | class NamespaceReplacerTest extends TestCase |
9 | 9 | { |
| 10 | + const PREFIX = 'My\\Mozart\\Prefix\\'; |
| 11 | + |
10 | 12 | /** @var NamespaceReplacer */ |
11 | 13 | public $replacer; |
12 | 14 |
|
13 | 15 | protected function setUp(): void |
14 | 16 | { |
15 | | - $autoloader = new Psr0(); |
16 | | - $autoloader->namespace = 'Test\\Test'; |
| 17 | + $this->replacer = self::createReplacer('Test\\Test'); |
| 18 | + } |
17 | 19 |
|
| 20 | + /** |
| 21 | + * Creates a NamespaceReplacer, given a namespace and a prefix. |
| 22 | + * |
| 23 | + * @param string $namespace |
| 24 | + * @param string $prefix |
| 25 | + * |
| 26 | + * @return Psr0 |
| 27 | + */ |
| 28 | + protected static function createReplacer(string $namespace, string $prefix = self::PREFIX) : NamespaceReplacer |
| 29 | + { |
| 30 | + $autoloader = new Psr0; |
| 31 | + $autoloader->namespace = $namespace; |
18 | 32 | $replacer = new NamespaceReplacer(); |
19 | 33 | $replacer->setAutoloader($autoloader); |
20 | | - $replacer->dep_namespace = 'Prefix\\'; |
21 | | - $this->replacer = $replacer; |
| 34 | + $replacer->dep_namespace = $prefix; |
| 35 | + |
| 36 | + return $replacer; |
22 | 37 | } |
23 | 38 |
|
24 | 39 | /** @test */ |
25 | 40 | public function it_replaces_namespace_declarations(): void |
26 | 41 | { |
27 | 42 | $contents = 'namespace Test\\Test;'; |
28 | 43 | $contents = $this->replacer->replace($contents); |
29 | | - $this->assertEquals('namespace Prefix\\Test\\Test;', $contents); |
| 44 | + |
| 45 | + $this->assertEquals('namespace My\\Mozart\\Prefix\\Test\\Test;', $contents); |
30 | 46 | } |
31 | 47 |
|
32 | 48 |
|
33 | 49 | /** @test */ |
34 | 50 | public function it_doesnt_replaces_namespace_inside_namespace(): void |
35 | 51 | { |
| 52 | + $replacer = self::createReplacer('Test'); |
36 | 53 |
|
37 | | - $autoloader = new Psr0(); |
38 | | - $autoloader->namespace = 'Test'; |
39 | | - |
40 | | - $replacer = new NamespaceReplacer(); |
41 | | - $replacer->setAutoloader($autoloader); |
42 | | - $replacer->dep_namespace = 'Prefix\\'; |
43 | | - $this->replacer = $replacer; |
| 54 | + $contents = "namespace Test\\Something;\n\nuse Test\\Test;"; |
| 55 | + $contents = $replacer->replace($contents); |
44 | 56 |
|
45 | | - $contents ="namespace Test\\Something;\n\nuse Test\\Test;"; |
46 | | - $contents = $this->replacer->replace($contents); |
47 | | - $this->assertEquals("namespace Prefix\\Test\\Something;\n\nuse Prefix\\Test\\Test;", $contents); |
| 57 | + $this->assertEquals("namespace My\\Mozart\\Prefix\\Test\\Something;\n\nuse My\\Mozart\\Prefix\\Test\\Test;", $contents); |
48 | 58 | } |
49 | 59 |
|
50 | 60 | /** @test */ |
51 | 61 | public function it_replaces_partial_namespace_declarations(): void |
52 | 62 | { |
53 | 63 | $contents = 'namespace Test\\Test\\Another;'; |
54 | 64 | $contents = $this->replacer->replace($contents); |
55 | | - $this->assertEquals('namespace Prefix\\Test\\Test\\Another;', $contents); |
| 65 | + |
| 66 | + $this->assertEquals('namespace My\\Mozart\\Prefix\\Test\\Test\\Another;', $contents); |
56 | 67 | } |
57 | 68 |
|
58 | | - /** @test */ |
| 69 | + /** @test */ |
59 | 70 | public function it_doesnt_prefix_already_prefixed_namespace(): void |
60 | 71 | { |
61 | | - $autoloader = new Psr0(); |
62 | | - $autoloader->namespace = 'Test\\Another'; |
| 72 | + $replacer = self::createReplacer('Test\\Another'); |
63 | 73 |
|
64 | | - $replacer = new NamespaceReplacer(); |
65 | | - $replacer->setAutoloader($autoloader); |
| 74 | + $contents = 'namespace My\\Mozart\\Prefix\\Test\\Another;'; |
| 75 | + $contents = $replacer->replace($contents); |
66 | 76 |
|
67 | | - $contents = 'namespace Prefix\\Test\\Another;'; |
68 | | - $contents = $this->replacer->replace($contents); |
69 | | - $this->assertEquals('namespace Prefix\\Test\\Another;', $contents); |
| 77 | + $this->assertEquals('namespace My\\Mozart\\Prefix\\Test\\Another;', $contents); |
| 78 | + } |
| 79 | + |
| 80 | + /** @test */ |
| 81 | + public function it_doesnt_double_replace_namespaces_that_also_exist_inside_another_namespace(): void |
| 82 | + { |
| 83 | + $chickenReplacer = self::createReplacer('Chicken'); |
| 84 | + $eggReplacer = self::createReplacer('Egg'); |
| 85 | + |
| 86 | + // This is a tricky situation. We are referencing Chicken\Egg, |
| 87 | + // but Egg *also* exists as a separate top level class. |
| 88 | + $contents = 'use Chicken\\Egg;'; |
| 89 | + $expected = 'use My\\Mozart\\Prefix\\Chicken\\Egg;'; |
| 90 | + |
| 91 | + // First, we test that eggReplacer(chickenReplacer()) yields the expected result. |
| 92 | + $this->assertEquals($expected, $eggReplacer->replace($chickenReplacer->replace($contents))); |
| 93 | + |
| 94 | + // Then, we test that chickenReplacer(eggReplacer()) yields the expected result. |
| 95 | + $this->assertEquals($expected, $chickenReplacer->replace($eggReplacer->replace($contents))); |
| 96 | + |
| 97 | + // Now we do the same thing, but with root-relative references. |
| 98 | + $contents = 'use \\Chicken\\Egg;'; |
| 99 | + $expected = 'use \\My\\Mozart\\Prefix\\Chicken\\Egg;'; |
| 100 | + |
| 101 | + // First, we test that eggReplacer(chickenReplacer()) yields the expected result. |
| 102 | + $this->assertEquals($expected, $eggReplacer->replace($chickenReplacer->replace($contents))); |
| 103 | + |
| 104 | + // Then, we test that chickenReplacer(eggReplacer()) yields the expected result. |
| 105 | + $this->assertEquals($expected, $chickenReplacer->replace($eggReplacer->replace($contents))); |
70 | 106 | } |
71 | 107 | } |
0 commit comments