|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Chorale\Tests\Diff; |
| 6 | + |
| 7 | +use Chorale\Config\ConfigDefaultsInterface; |
| 8 | +use Chorale\Diff\ConfigDiffer; |
| 9 | +use Chorale\Discovery\PackageIdentityInterface; |
| 10 | +use Chorale\Discovery\PatternMatcherInterface; |
| 11 | +use Chorale\Repo\RepoResolverInterface; |
| 12 | +use Chorale\Rules\ConflictDetectorInterface; |
| 13 | +use Chorale\Rules\RequiredFilesCheckerInterface; |
| 14 | +use Chorale\Util\PathUtilsInterface; |
| 15 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 16 | +use PHPUnit\Framework\Attributes\Group; |
| 17 | +use PHPUnit\Framework\Attributes\Small; |
| 18 | +use PHPUnit\Framework\Attributes\Test; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +#[CoversClass(ConfigDiffer::class)] |
| 22 | +#[Group('unit')] |
| 23 | +#[Small] |
| 24 | +final class ConfigDifferTest extends TestCase |
| 25 | +{ |
| 26 | + private function defaults(): array |
| 27 | + { |
| 28 | + return [ |
| 29 | + 'repo_host' => 'git@github.com', |
| 30 | + 'repo_vendor' => 'Acme', |
| 31 | + 'repo_name_template' => '{name:kebab}.git', |
| 32 | + 'default_repo_template' => 'git@github.com:{repo_vendor}/{name:kebab}.git', |
| 33 | + 'default_branch' => 'main', |
| 34 | + 'splitter' => 'splitsh', |
| 35 | + 'tag_strategy' => 'inherit-monorepo-tag', |
| 36 | + 'rules' => [ |
| 37 | + 'keep_history' => true, |
| 38 | + 'skip_if_unchanged' => true, |
| 39 | + 'require_files' => ['composer.json','LICENSE'], |
| 40 | + ], |
| 41 | + ]; |
| 42 | + } |
| 43 | + |
| 44 | + private function stubPaths(): PathUtilsInterface |
| 45 | + { |
| 46 | + return new class implements PathUtilsInterface { |
| 47 | + public function normalize(string $path): string |
| 48 | + { |
| 49 | + return $path; |
| 50 | + } |
| 51 | + |
| 52 | + public function isUnder(string $path, string $root): bool |
| 53 | + { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + public function match(string $pattern, string $path): bool |
| 58 | + { |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + public function leaf(string $path): string |
| 63 | + { |
| 64 | + $pos = strrpos($path, '/'); |
| 65 | + return $pos === false ? $path : substr($path, $pos + 1); |
| 66 | + } |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + private function newDiffer( |
| 71 | + ConfigDefaultsInterface $defaults, |
| 72 | + PatternMatcherInterface $matcher, |
| 73 | + RepoResolverInterface $resolver, |
| 74 | + PackageIdentityInterface $identity, |
| 75 | + RequiredFilesCheckerInterface $required, |
| 76 | + ConflictDetectorInterface $conflicts |
| 77 | + ): ConfigDiffer { |
| 78 | + return new ConfigDiffer($defaults, $matcher, $resolver, $identity, $required, $conflicts, $this->stubPaths()); |
| 79 | + } |
| 80 | + |
| 81 | + #[Test] |
| 82 | + public function testClassifiesNewWhenNotInConfig(): void |
| 83 | + { |
| 84 | + $defaults = $this->createMock(ConfigDefaultsInterface::class); |
| 85 | + $defaults->method('resolve')->willReturn($this->defaults()); |
| 86 | + |
| 87 | + $matcher = $this->createMock(PatternMatcherInterface::class); |
| 88 | + $matcher->method('allMatches')->willReturn([]); |
| 89 | + |
| 90 | + $resolver = $this->createMock(RepoResolverInterface::class); |
| 91 | + $resolver->method('resolve')->willReturn('git@github.com:Acme/foo.git'); |
| 92 | + |
| 93 | + $identity = $this->createMock(PackageIdentityInterface::class); |
| 94 | + $required = $this->createMock(RequiredFilesCheckerInterface::class); |
| 95 | + $required->method('missing')->willReturn([]); |
| 96 | + $conflicts = $this->createMock(ConflictDetectorInterface::class); |
| 97 | + |
| 98 | + $differ = $this->newDiffer($defaults, $matcher, $resolver, $identity, $required, $conflicts); |
| 99 | + |
| 100 | + $out = $differ->diff(['targets' => [], 'patterns' => []], ['src/Acme/Foo'], []); |
| 101 | + $this->assertSame('src/Acme/Foo', $out['new'][0]['path']); |
| 102 | + } |
| 103 | + |
| 104 | + #[Test] |
| 105 | + public function testDetectsRenameWhenIdentityMatches(): void |
| 106 | + { |
| 107 | + $defaults = $this->createMock(ConfigDefaultsInterface::class); |
| 108 | + $defaults->method('resolve')->willReturn($this->defaults()); |
| 109 | + |
| 110 | + $matcher = $this->createMock(PatternMatcherInterface::class); |
| 111 | + $matcher->method('allMatches')->willReturn([]); |
| 112 | + |
| 113 | + $resolver = $this->createMock(RepoResolverInterface::class); |
| 114 | + // Called for new path and old path |
| 115 | + $resolver->method('resolve')->willReturnOnConsecutiveCalls( |
| 116 | + 'git@github.com:Acme/new.git', |
| 117 | + 'git@github.com:Acme/old.git', |
| 118 | + 'git@github.com:Acme/old.git' |
| 119 | + ); |
| 120 | + |
| 121 | + $identity = $this->createMock(PackageIdentityInterface::class); |
| 122 | + $identity->method('identityFor')->willReturn('same-id'); |
| 123 | + |
| 124 | + $required = $this->createMock(RequiredFilesCheckerInterface::class); |
| 125 | + $required->method('missing')->willReturn([]); |
| 126 | + $conflicts = $this->createMock(ConflictDetectorInterface::class); |
| 127 | + |
| 128 | + $config = [ |
| 129 | + 'targets' => [ |
| 130 | + ['path' => 'src/Acme/Old'], |
| 131 | + ], |
| 132 | + 'patterns' => [], |
| 133 | + ]; |
| 134 | + $discovered = ['src/Acme/New']; |
| 135 | + |
| 136 | + $differ = $this->newDiffer($defaults, $matcher, $resolver, $identity, $required, $conflicts); |
| 137 | + $out = $differ->diff($config, $discovered, []); |
| 138 | + |
| 139 | + $this->assertSame('src/Acme/Old', $out['renamed'][0]['from']); |
| 140 | + } |
| 141 | + |
| 142 | + #[Test] |
| 143 | + public function testReportsIssuesWhenRequiredFilesMissing(): void |
| 144 | + { |
| 145 | + $defaults = $this->createMock(ConfigDefaultsInterface::class); |
| 146 | + $defaults->method('resolve')->willReturn($this->defaults()); |
| 147 | + |
| 148 | + $matcher = $this->createMock(PatternMatcherInterface::class); |
| 149 | + $matcher->method('allMatches')->willReturn([]); |
| 150 | + |
| 151 | + $resolver = $this->createMock(RepoResolverInterface::class); |
| 152 | + $resolver->method('resolve')->willReturn('git@github.com:Acme/foo.git'); |
| 153 | + |
| 154 | + $identity = $this->createMock(PackageIdentityInterface::class); |
| 155 | + $required = $this->createMock(RequiredFilesCheckerInterface::class); |
| 156 | + $required->method('missing')->willReturn(['composer.json']); |
| 157 | + $conflicts = $this->createMock(ConflictDetectorInterface::class); |
| 158 | + |
| 159 | + $differ = $this->newDiffer($defaults, $matcher, $resolver, $identity, $required, $conflicts); |
| 160 | + |
| 161 | + $out = $differ->diff(['targets' => [['path' => 'src/Acme/Foo']], 'patterns' => []], ['src/Acme/Foo'], []); |
| 162 | + $this->assertSame(['composer.json'], $out['issues'][0]['missing']); |
| 163 | + } |
| 164 | + |
| 165 | + #[Test] |
| 166 | + public function testReportsDriftWhenRedundantOverridePresent(): void |
| 167 | + { |
| 168 | + $defaults = $this->createMock(ConfigDefaultsInterface::class); |
| 169 | + $defaults->method('resolve')->willReturn($this->defaults()); |
| 170 | + |
| 171 | + $matcher = $this->createMock(PatternMatcherInterface::class); |
| 172 | + $matcher->method('allMatches')->willReturn([]); |
| 173 | + |
| 174 | + $resolver = $this->createMock(RepoResolverInterface::class); |
| 175 | + $resolver->method('resolve')->willReturn('git@github.com:Acme/foo.git'); |
| 176 | + |
| 177 | + $identity = $this->createMock(PackageIdentityInterface::class); |
| 178 | + $required = $this->createMock(RequiredFilesCheckerInterface::class); |
| 179 | + $required->method('missing')->willReturn([]); |
| 180 | + $conflicts = $this->createMock(ConflictDetectorInterface::class); |
| 181 | + |
| 182 | + $config = [ |
| 183 | + 'targets' => [ |
| 184 | + ['path' => 'src/Acme/Foo', 'repo_vendor' => 'Acme'], |
| 185 | + ], |
| 186 | + 'patterns' => [], |
| 187 | + ]; |
| 188 | + $discovered = ['src/Acme/Foo']; |
| 189 | + |
| 190 | + $differ = $this->newDiffer($defaults, $matcher, $resolver, $identity, $required, $conflicts); |
| 191 | + $out = $differ->diff($config, $discovered, []); |
| 192 | + |
| 193 | + $this->assertSame('src/Acme/Foo', $out['drift'][0]['path']); |
| 194 | + } |
| 195 | +} |
0 commit comments