Skip to content

Commit 42c39b1

Browse files
committed
Test that patches are removed
1 parent 64705d5 commit 42c39b1

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

src/PackageApplicationRepository.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Composer\Package\AliasPackage;
77
use Composer\Package\PackageInterface;
88
use Composer\Repository\RepositoryInterface;
9+
use Psr\Log\LoggerInterface;
910

1011
class PackageApplicationRepository
1112
{
@@ -22,20 +23,27 @@ class PackageApplicationRepository
2223
* @var PathResolver
2324
*/
2425
private $pathResolver;
26+
/**
27+
* @var LoggerInterface
28+
*/
29+
private $logger;
2530

2631
/**
2732
* @param RepositoryInterface $installedRepository
2833
* @param InstallationManager $installationManager
2934
* @param PathResolver $pathResolver
35+
* @param LoggerInterface $logger
3036
*/
3137
public function __construct(
3238
RepositoryInterface $installedRepository,
3339
InstallationManager $installationManager,
34-
PathResolver $pathResolver
40+
PathResolver $pathResolver,
41+
LoggerInterface $logger
3542
) {
3643
$this->installedRepository = $installedRepository;
3744
$this->installationManager = $installationManager;
3845
$this->pathResolver = $pathResolver;
46+
$this->logger = $logger;
3947
}
4048

4149
/**
@@ -137,7 +145,7 @@ private function createPatchApplication(array $data)
137145
);
138146

139147
if (!$sourcePackage) {
140-
throw new \RuntimeException(sprintf('Could not find source package %s (%s) for installed patch',
148+
$this->logger->debug(sprintf('Could not find source package %s (%s) for installed patch, it was removed probably',
141149
$data['source_package']['name'],
142150
$data['source_package']['version']
143151
));

src/PatchApplication.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class PatchApplication
1515
private $patch;
1616

1717
/**
18-
* @var PackageInterface
18+
* @var PackageInterface|null
1919
*/
2020
private $sourcePackage;
2121

@@ -37,7 +37,7 @@ class PatchApplication
3737
*/
3838
public function __construct(
3939
Patch $patch,
40-
PackageInterface $sourcePackage,
40+
PackageInterface $sourcePackage = null,
4141
PackageInterface $targetPackage,
4242
$hash
4343
) {
@@ -64,7 +64,10 @@ public function getPatch()
6464
}
6565

6666
/**
67-
* @return PackageInterface
67+
* Source package may be null if the patchset has been removed,
68+
* but the patch is still applied.
69+
*
70+
* @return PackageInterface|null
6871
*/
6972
public function getSourcePackage()
7073
{

src/Patcher.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ public function __construct(
116116
$this->packageApplicationRepository = new PackageApplicationRepository(
117117
$this->repositoryManager->getLocalRepository(),
118118
$this->installationManager,
119-
$this->pathResolver
119+
$this->pathResolver,
120+
$this->logger
120121
);
121122

122123
$this->patches = $this->collectPatches();

tests/Functional/CoreFeatureTest.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testPatchesAreAppliedWhenPackageIsAddedToExistingProject()
6363
$this->assertThatComposerRunHasAppliedPatches($requireRun, self::PACKAGEA_PATCH1_APPLICATIONS);
6464
}
6565

66-
public function testThatPatchesAreNotReappliedOnUpdate()
66+
public function testPatchesAreNotReappliedOnUpdate()
6767
{
6868
$project = $this->getSandbox()->createProjectSandBox('test/project-template', 'dev-master', [
6969
'require' => [
@@ -83,7 +83,7 @@ public function testThatPatchesAreNotReappliedOnUpdate()
8383
$this->assertNotContains('Applied patch', $updateRun->getFullOutput(), 'no patches were applied', true);
8484
}
8585

86-
public function testThatLayeredPatchesAreAppliedInCorrectOrder()
86+
public function testLayeredPatchesAreAppliedInCorrectOrder()
8787
{
8888
$project = $this->getSandbox()->createProjectSandBox('test/project-template', 'dev-master', [
8989
'require' => [
@@ -108,7 +108,7 @@ public function testThatLayeredPatchesAreAppliedInCorrectOrder()
108108
$this->assertNotContains('Applied patch', $updateRun->getFullOutput(), 'no patches were applied', true);
109109
}
110110

111-
public function testThatPatchesAreDeduplicated()
111+
public function testPatchesAreDeduplicated()
112112
{
113113
// Same patch coming from multiple patchsets shall be applied only once
114114

@@ -130,4 +130,38 @@ public function testThatPatchesAreDeduplicated()
130130
)
131131
);
132132
}
133+
134+
public function testRemovingPatchsetWillRemovePatches()
135+
{
136+
// Same patch coming from multiple patchsets shall be applied only once
137+
138+
$project = $this->getSandbox()->createProjectSandBox('test/project-template', 'dev-master', [
139+
'require' => [
140+
'test/patchset'=> '~1.0',
141+
'test/patchset-extra'=> '~1.0',
142+
'test/package-a'=> 'dev-master',
143+
'creativestyle/composer-plugin-patchset'=> 'dev-master'
144+
]
145+
]);
146+
147+
$installRun = $project->runComposerCommand('install');
148+
149+
$this->assertThatComposerRunHasAppliedPatches($installRun,
150+
array_merge_recursive(
151+
self::PACKAGEA_PATCH1_APPLICATIONS,
152+
self::PACKAGEA_PATCH2_APPLICATIONS
153+
)
154+
);
155+
156+
$removeRun = $project->runComposerCommand('remove', 'test/patchset-extra');
157+
158+
$this->assertContains('Reinstalling test/package-a', $removeRun->getFullOutput(), 'test/package-a has been reinstalled', true);
159+
160+
// First patch is coming from the first patchset so it still should be applied
161+
$this->assertThatComposerRunHasAppliedPatches($removeRun,
162+
self::PACKAGEA_PATCH1_APPLICATIONS,
163+
self::PACKAGEA_PATCH2_APPLICATIONS
164+
);
165+
166+
}
133167
}

0 commit comments

Comments
 (0)