Skip to content

Commit b7fbdea

Browse files
committed
Revert "Merge pull request cweagans#267 from cweagans/remove-dependency-patch-resolution"
This reverts commit 4ebb103, reversing changes made to fc8a477.
1 parent 55c4289 commit b7fbdea

File tree

10 files changed

+179
-70
lines changed

10 files changed

+179
-70
lines changed

src/Capability/CoreResolverProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace cweagans\Composer\Capability;
44

5+
use cweagans\Composer\Resolvers\DependencyPatches;
56
use cweagans\Composer\Resolvers\PatchesFile;
67
use cweagans\Composer\Resolvers\RootComposer;
78

@@ -15,6 +16,7 @@ public function getResolvers()
1516
return [
1617
new RootComposer($this->composer, $this->io),
1718
new PatchesFile($this->composer, $this->io),
19+
new DependencyPatches($this->composer, $this->io),
1820
];
1921
}
2022
}

src/Resolvers/DependencyPatches.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \cweagans\Composer\Resolvers\DependencyPatches.
6+
*/
7+
8+
namespace cweagans\Composer\Resolvers;
9+
10+
use Composer\DependencyResolver\Operation\InstallOperation;
11+
use Composer\DependencyResolver\Operation\OperationInterface;
12+
use Composer\DependencyResolver\Operation\UpdateOperation;
13+
use Composer\Installer\PackageEvent;
14+
use Composer\Package\PackageInterface;
15+
use cweagans\Composer\Patch;
16+
use cweagans\Composer\PatchCollection;
17+
use cweagans\Composer\Plugin\Patches;
18+
19+
class DependencyPatches extends ResolverBase
20+
{
21+
/**
22+
* {@inheritDoc}
23+
*/
24+
public function resolve(PatchCollection $collection, PackageEvent $event)
25+
{
26+
$this->io->write(' - <info>Gathering patches from dependencies.</info>');
27+
28+
$operations = $event->getOperations();
29+
foreach ($operations as $operation) {
30+
if ($operation->getJobType() === 'install' || $operation->getJobType() === 'update') {
31+
// @TODO handle exception.
32+
$package = $this->getPackageFromOperation($operation);
33+
/** @var PackageInterface $extra */
34+
$extra = $package->getExtra();
35+
if (isset($extra['patches'])) {
36+
$patches = $this->findPatchesInJson($extra['patches']);
37+
foreach ($patches as $package => $patch_list) {
38+
foreach ($patch_list as $patch) {
39+
$collection->addPatch($patch);
40+
}
41+
}
42+
}
43+
}
44+
}
45+
}
46+
47+
/**
48+
* Get a Package object from an OperationInterface object.
49+
*
50+
* @param OperationInterface $operation
51+
* @return PackageInterface
52+
* @throws \Exception
53+
*
54+
* @todo Will this method ever get something other than an InstallOperation or UpdateOperation?
55+
*/
56+
protected function getPackageFromOperation(OperationInterface $operation)
57+
{
58+
if ($operation instanceof InstallOperation) {
59+
$package = $operation->getPackage();
60+
} elseif ($operation instanceof UpdateOperation) {
61+
$package = $operation->getTargetPackage();
62+
} else {
63+
throw new \Exception('Unknown operation: ' . get_class($operation));
64+
}
65+
66+
return $package;
67+
}
68+
}

tests/_support/_generated/UnitTesterActions.php

Lines changed: 8 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
<?php //[STAMP] 5e4c7302c0b63a4df799dc88da465f39
1+
<?php //[STAMP] 5648e2ecd2668319703f108f1329e8a8
22
namespace _generated;
33

44
// This class was automatically generated by build task
55
// You should not change it manually as it will be overwritten on next build
66
// @codingStandardsIgnoreFile
77

8+
use Codeception\Module\Asserts;
9+
use Helper\Unit;
10+
811
trait UnitTesterActions
912
{
1013
/**
@@ -23,13 +26,13 @@ abstract protected function getScenario();
2326
* Regular example:
2427
* ```php
2528
* <?php
26-
* $I->assertEquals(5, $element->getChildrenCount());
29+
* $I->assertEquals($element->getChildrenCount(), 5);
2730
* ```
2831
*
2932
* Floating-point example:
3033
* ```php
3134
* <?php
32-
* $I->assertEquals(0.3, $calculator->add(0.1, 0.2), 'Calculator should add the two numbers correctly.', 0.01);
35+
* $I->assertEquals($calculator->add(0.1, 0.2), 0.3, 'Calculator should add the two numbers correctly.', 0.01);
3336
* ```
3437
*
3538
* @param $expected
@@ -53,13 +56,13 @@ public function assertEquals($expected, $actual, $message = null, $delta = null)
5356
* Regular example:
5457
* ```php
5558
* <?php
56-
* $I->assertNotEquals(0, $element->getChildrenCount());
59+
* $I->assertNotEquals($element->getChildrenCount(), 0);
5760
* ```
5861
*
5962
* Floating-point example:
6063
* ```php
6164
* <?php
62-
* $I->assertNotEquals(0.4, $calculator->add(0.1, 0.2), 'Calculator should add the two numbers correctly.', 0.01);
65+
* $I->assertNotEquals($calculator->add(0.1, 0.2), 0.4, 'Calculator should add the two numbers correctly.', 0.01);
6366
* ```
6467
*
6568
* @param $expected
@@ -323,20 +326,6 @@ public function assertTrue($condition, $message = null) {
323326
}
324327

325328

326-
/**
327-
* [!] Method is generated. Documentation taken from corresponding module.
328-
*
329-
* Checks that the condition is NOT true (everything but true)
330-
*
331-
* @param $condition
332-
* @param string $message
333-
* @see \Codeception\Module\Asserts::assertNotTrue()
334-
*/
335-
public function assertNotTrue($condition, $message = null) {
336-
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNotTrue', func_get_args()));
337-
}
338-
339-
340329
/**
341330
* [!] Method is generated. Documentation taken from corresponding module.
342331
*
@@ -351,20 +340,6 @@ public function assertFalse($condition, $message = null) {
351340
}
352341

353342

354-
/**
355-
* [!] Method is generated. Documentation taken from corresponding module.
356-
*
357-
* Checks that the condition is NOT false (everything but false)
358-
*
359-
* @param $condition
360-
* @param string $message
361-
* @see \Codeception\Module\Asserts::assertNotFalse()
362-
*/
363-
public function assertNotFalse($condition, $message = null) {
364-
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNotFalse', func_get_args()));
365-
}
366-
367-
368343
/**
369344
* [!] Method is generated. Documentation taken from corresponding module.
370345
*
@@ -565,45 +540,9 @@ public function fail($message) {
565540
*
566541
* @param $exception string or \Exception
567542
* @param $callback
568-
*
569-
* @deprecated Use expectThrowable instead
570543
* @see \Codeception\Module\Asserts::expectException()
571544
*/
572545
public function expectException($exception, $callback) {
573546
return $this->getScenario()->runStep(new \Codeception\Step\Action('expectException', func_get_args()));
574547
}
575-
576-
577-
/**
578-
* [!] Method is generated. Documentation taken from corresponding module.
579-
*
580-
* Handles and checks throwables (Exceptions/Errors) called inside the callback function.
581-
* Either throwable class name or throwable instance should be provided.
582-
*
583-
* ```php
584-
* <?php
585-
* $I->expectThrowable(MyThrowable::class, function() {
586-
* $this->doSomethingBad();
587-
* });
588-
*
589-
* $I->expectThrowable(new MyException(), function() {
590-
* $this->doSomethingBad();
591-
* });
592-
* ```
593-
* If you want to check message or throwable code, you can pass them with throwable instance:
594-
* ```php
595-
* <?php
596-
* // will check that throwable MyError is thrown with "Don't do bad things" message
597-
* $I->expectThrowable(new MyError("Don't do bad things"), function() {
598-
* $this->doSomethingBad();
599-
* });
600-
* ```
601-
*
602-
* @param $throwable string or \Throwable
603-
* @param $callback
604-
* @see \Codeception\Module\Asserts::expectThrowable()
605-
*/
606-
public function expectThrowable($throwable, $callback) {
607-
return $this->getScenario()->runStep(new \Codeception\Step\Action('expectThrowable', func_get_args()));
608-
}
609548
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
$I = new AcceptanceTester($scenario);
3+
$I->wantTo('apply a patch defined in a dependency');
4+
$I->amInPath(realpath(__DIR__ . '/fixtures/apply-patch-from-dependency'));
5+
$I->runShellCommand('composer install');
6+
$I->canSeeFileFound('./vendor/drupal/drupal/.ht.router.php');
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
$I = new AcceptanceTester($scenario);
3+
$I->wantTo('dont apply a patch defined in a dependency if the dependency patch resolver is disabled');
4+
$I->amInPath(realpath(__DIR__ . '/fixtures/dont-apply-patch-from-dependency'));
5+
$I->runShellCommand('composer install');
6+
$I->cantSeeFileFound('./vendor/drupal/drupal/.ht.router.php');
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "cweagans/composer-patches-test-project",
3+
"description": "Project for use in cweagans/composer-patches acceptance tests.",
4+
"type": "project",
5+
"license": "BSD-2-Clause",
6+
"repositories": [
7+
{
8+
"type": "path",
9+
"url": "../composer-patches"
10+
},
11+
{
12+
"type": "path",
13+
"url": "../../../_data/dep-test-package"
14+
}
15+
],
16+
"require": {
17+
"cweagans/composer-patches": "*@dev",
18+
"cweagans/dep-test-package": "*@dev"
19+
}
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "cweagans/composer-patches-test-project",
3+
"description": "Project for use in cweagans/composer-patches acceptance tests.",
4+
"type": "project",
5+
"license": "BSD-2-Clause",
6+
"repositories": [
7+
{
8+
"type": "path",
9+
"url": "../composer-patches"
10+
},
11+
{
12+
"type": "path",
13+
"url": "../../../_data/dep-test-package"
14+
}
15+
],
16+
"require": {
17+
"cweagans/composer-patches": "*@dev",
18+
"cweagans/dep-test-package": "*@dev"
19+
},
20+
"extra": {
21+
"composer-patches": {
22+
"disable-resolvers": ["cweagans\\Composer\\Resolvers\\DependencyPatches"]
23+
}
24+
}
25+
}
Binary file not shown.

tests/unit/CoreResolverProviderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ public function testGetResolvers()
1818

1919
$resolvers = $resolverProvider->getResolvers();
2020

21-
$this->assertCount(2, $resolvers);
21+
$this->assertCount(3, $resolvers);
2222
$this->assertInstanceOf(\cweagans\Composer\Resolvers\RootComposer::class, $resolvers[0]);
2323
$this->assertInstanceOf(\cweagans\Composer\Resolvers\PatchesFile::class, $resolvers[1]);
24+
$this->assertInstanceOf(\cweagans\Composer\Resolvers\DependencyPatches::class, $resolvers[2]);
2425
}
2526
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Test the RootComposer resolver.
6+
*/
7+
namespace cweagans\Composer\Tests;
8+
9+
use Codeception\Test\Unit;
10+
use Codeception\Util\Stub;
11+
use Composer\Composer;
12+
use Composer\Installer\PackageEvent;
13+
use Composer\IO\NullIO;
14+
use Composer\Package\RootPackage;
15+
use cweagans\Composer\PatchCollection;
16+
use cweagans\Composer\Resolvers\DependencyPatches;
17+
use cweagans\Composer\Resolvers\RootComposer;
18+
19+
class DependencyPatchesResolverTest extends Unit
20+
{
21+
public function testResolve()
22+
{
23+
$patch_collection = new PatchCollection();
24+
$root_package = new RootPackage('test/package', '1.0.0.0', '1.0.0');
25+
$root_package->setExtra(['patches' => []]);
26+
$composer = new Composer();
27+
$composer->setPackage($root_package);
28+
$io = new NullIO();
29+
$event = Stub::make(PackageEvent::class, [
30+
'getOperations' => function () {
31+
return [];
32+
},
33+
]);
34+
35+
// Empty patch list.
36+
$resolver = new DependencyPatches($composer, $io);
37+
$resolver->resolve($patch_collection, $event);
38+
$this->assertCount(0, $patch_collection->getPatchesForPackage('test/package'));
39+
40+
// @TODO: Add operations to the event and test that the resolver finds patches appropriately.
41+
}
42+
}

0 commit comments

Comments
 (0)