|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace IonBazan\ComposerDiff\Tests; |
| 4 | + |
| 5 | +use Composer\DependencyResolver\Operation\InstallOperation; |
| 6 | +use Composer\DependencyResolver\Operation\OperationInterface; |
| 7 | +use Composer\DependencyResolver\Operation\UninstallOperation; |
| 8 | +use Composer\DependencyResolver\Operation\UpdateOperation; |
| 9 | +use IonBazan\ComposerDiff\PackageDiff; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +class PackageDiffTest extends TestCase |
| 13 | +{ |
| 14 | + const TEST_GIT_DIR = __DIR__.'/test-git'; |
| 15 | + const FIXTURES_BASE = __DIR__.'/fixtures/base'; |
| 16 | + /** |
| 17 | + * @param string[] $expected |
| 18 | + * @param bool $dev |
| 19 | + * @param bool $withPlatform |
| 20 | + * |
| 21 | + * @dataProvider operationsProvider |
| 22 | + */ |
| 23 | + public function testBasicUsage(array $expected, $dev, $withPlatform) |
| 24 | + { |
| 25 | + $diff = new PackageDiff(); |
| 26 | + $operations = $diff->getPackageDiff( |
| 27 | + self::FIXTURES_BASE.'/composer.lock', |
| 28 | + self::FIXTURES_TARGET.'/composer.lock', |
| 29 | + $dev, |
| 30 | + $withPlatform |
| 31 | + ); |
| 32 | + |
| 33 | + $this->assertSame($expected, array_map(array($this, 'operationToString'), $operations)); |
| 34 | + } |
| 35 | + |
| 36 | + const FIXTURES_TARGET = __DIR__.'/fixtures/target'; |
| 37 | + |
| 38 | + public function testSameBaseAndTarget() |
| 39 | + { |
| 40 | + $diff = new PackageDiff(); |
| 41 | + $operations = $diff->getPackageDiff( |
| 42 | + self::FIXTURES_BASE.'/composer.lock', |
| 43 | + self::FIXTURES_BASE.'/composer.lock', |
| 44 | + true, |
| 45 | + true |
| 46 | + ); |
| 47 | + |
| 48 | + $this->assertEmpty($operations); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param string[] $expected |
| 53 | + * @param bool $dev |
| 54 | + * @param bool $withPlatform |
| 55 | + * |
| 56 | + * @dataProvider operationsProvider |
| 57 | + */ |
| 58 | + public function testGitUsage(array $expected, $dev, $withPlatform) |
| 59 | + { |
| 60 | + $diff = new PackageDiff(); |
| 61 | + chdir(self::TEST_GIT_DIR); |
| 62 | + exec('git init'); |
| 63 | + file_put_contents(self::TEST_GIT_DIR.'/composer.lock', file_get_contents(self::FIXTURES_BASE.'/composer.lock')); |
| 64 | + exec('git add composer.lock && git commit -m "init"'); |
| 65 | + file_put_contents(self::TEST_GIT_DIR.'/composer.lock', file_get_contents(self::FIXTURES_TARGET.'/composer.lock')); |
| 66 | + $operations = $diff->getPackageDiff('HEAD:composer.lock', 'composer.lock', $dev, $withPlatform); |
| 67 | + |
| 68 | + $this->assertSame($expected, array_map(array($this, 'operationToString'), $operations)); |
| 69 | + } |
| 70 | + |
| 71 | + public function operationsProvider() |
| 72 | + { |
| 73 | + return array( |
| 74 | + 'prod, with platform' => array( |
| 75 | + array( |
| 76 | + 'install psr/event-dispatcher 1.0.0', |
| 77 | + 'install symfony/deprecation-contracts v2.1.2', |
| 78 | + 'update symfony/event-dispatcher from v2.8.52 to v5.1.2', |
| 79 | + 'install symfony/event-dispatcher-contracts v2.1.2', |
| 80 | + 'install symfony/polyfill-php80 v1.17.1', |
| 81 | + 'install php >=5.3', |
| 82 | + ), |
| 83 | + false, |
| 84 | + true, |
| 85 | + ), |
| 86 | + 'prod, no platform' => array( |
| 87 | + array( |
| 88 | + 'install psr/event-dispatcher 1.0.0', |
| 89 | + 'install symfony/deprecation-contracts v2.1.2', |
| 90 | + 'update symfony/event-dispatcher from v2.8.52 to v5.1.2', |
| 91 | + 'install symfony/event-dispatcher-contracts v2.1.2', |
| 92 | + 'install symfony/polyfill-php80 v1.17.1', |
| 93 | + ), |
| 94 | + false, |
| 95 | + false, |
| 96 | + ), |
| 97 | + 'dev, no platform' => array( |
| 98 | + array( |
| 99 | + 'update phpunit/php-code-coverage from 8.0.2 to 7.0.10', |
| 100 | + 'update phpunit/php-file-iterator from 3.0.2 to 2.0.2', |
| 101 | + 'update phpunit/php-text-template from 2.0.1 to 1.2.1', |
| 102 | + 'update phpunit/php-timer from 5.0.0 to 2.1.2', |
| 103 | + 'update phpunit/php-token-stream from 4.0.2 to 3.1.1', |
| 104 | + 'update phpunit/phpunit from 9.2.5 to 8.5.8', |
| 105 | + 'update sebastian/code-unit-reverse-lookup from 2.0.1 to 1.0.1', |
| 106 | + 'update sebastian/comparator from 4.0.2 to 3.0.2', |
| 107 | + 'update sebastian/diff from 4.0.1 to 3.0.2', |
| 108 | + 'update sebastian/environment from 5.1.1 to 4.2.3', |
| 109 | + 'update sebastian/exporter from 4.0.1 to 3.1.2', |
| 110 | + 'update sebastian/global-state from 4.0.0 to 3.0.0', |
| 111 | + 'update sebastian/object-enumerator from 4.0.1 to 3.0.3', |
| 112 | + 'update sebastian/object-reflector from 2.0.1 to 1.1.1', |
| 113 | + 'update sebastian/recursion-context from 4.0.1 to 3.0.0', |
| 114 | + 'update sebastian/resource-operations from 3.0.1 to 2.0.1', |
| 115 | + 'update sebastian/type from 2.1.0 to 1.1.3', |
| 116 | + 'update sebastian/version from 3.0.0 to 2.0.1', |
| 117 | + 'uninstall phpunit/php-invoker 3.0.1', |
| 118 | + 'uninstall sebastian/code-unit 1.0.3', |
| 119 | + ), |
| 120 | + true, |
| 121 | + false, |
| 122 | + ), |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + private function operationToString(OperationInterface $operation) |
| 127 | + { |
| 128 | + if ($operation instanceof InstallOperation) { |
| 129 | + return sprintf('install %s %s', $operation->getPackage()->getName(), $operation->getPackage()->getPrettyVersion()); |
| 130 | + } |
| 131 | + |
| 132 | + if ($operation instanceof UpdateOperation) { |
| 133 | + return sprintf('update %s from %s to %s', $operation->getInitialPackage()->getName(), $operation->getInitialPackage()->getPrettyVersion(), $operation->getTargetPackage()->getPrettyVersion()); |
| 134 | + } |
| 135 | + |
| 136 | + if ($operation instanceof UninstallOperation) { |
| 137 | + return sprintf('uninstall %s %s', $operation->getPackage()->getName(), $operation->getPackage()->getPrettyVersion()); |
| 138 | + } |
| 139 | + |
| 140 | + throw new \InvalidArgumentException('Invalid operation provided'); |
| 141 | + } |
| 142 | +} |
0 commit comments