Skip to content

Commit 1d1f399

Browse files
committed
add tests
1 parent 1f06d7d commit 1d1f399

File tree

10 files changed

+4610
-5
lines changed

10 files changed

+4610
-5
lines changed

.github/workflows/test.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Tests
2+
on: [push, pull_request]
3+
jobs:
4+
php:
5+
name: PHP ${{ matrix.php-versions }}
6+
runs-on: ubuntu-latest
7+
strategy:
8+
fail-fast: false
9+
matrix:
10+
php-versions:
11+
- '5.3'
12+
# - '5.4'
13+
# - '5.5'
14+
- '5.6'
15+
# - '7.0'
16+
# - '7.1'
17+
# - '7.2'
18+
# - '7.3'
19+
- '7.4'
20+
# include:
21+
# - php-versions: '8.0'
22+
# composer-flags: '--ignore-platform-reqs'
23+
steps:
24+
- name: Checkout
25+
uses: actions/checkout@v2
26+
- name: Setup PHP, with composer and extensions
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: ${{ matrix.php-versions }}
30+
extensions: json
31+
- name: Get composer cache directory
32+
id: composer-cache
33+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
34+
- name: Cache composer dependencies
35+
uses: actions/cache@v1
36+
with:
37+
path: ${{ steps.composer-cache.outputs.dir }}
38+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
39+
restore-keys: ${{ runner.os }}-composer-
40+
- name: Install Composer dependencies
41+
run: composer install -n --prefer-dist ${{ matrix.composer-flags }}
42+
- name: Run Tests
43+
run: vendor/bin/simple-phpunit
44+
# - name: Upload coverage to Codecov
45+
# uses: codecov/codecov-action@v1
46+
# - name: Run mutation tests
47+
# if: ${{ matrix.php-versions < 8.0 }}
48+
# env:
49+
# STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
50+
# run: vendor/bin/infection --ignore-msi-with-no-mutations --min-covered-msi=100 --min-msi=100 -s -j4

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/composer.lock
33
/.php_cs.cache
44
/.php_cs
5+
/tests/fixtures/**/vendor/

composer.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@
1111
"require": {
1212
"php": "^5.3.2 || ^7.0 || ^8.0",
1313
"ext-json": "*",
14-
"composer-plugin-api": "^1.1 || ^2.0",
14+
"composer-plugin-api": "^1.1 || ^2.0"
15+
},
16+
"require-dev": {
17+
"symfony/phpunit-bridge": "^4.2 || ^5.0",
1518
"composer/composer": "^1.1 || ^2.0",
1619
"symfony/console": "^2.3 || ^3.0 || ^4.0 || ^5.0"
1720
},
18-
"require-dev": {},
1921
"config": {
22+
"platform": {
23+
"php": "5.3.9"
24+
},
2025
"platform-check": false
2126
},
2227
"extra": {
@@ -27,6 +32,11 @@
2732
"IonBazan\\ComposerDiff\\": "src"
2833
}
2934
},
35+
"autoload-dev": {
36+
"psr-4": {
37+
"IonBazan\\ComposerDiff\\Tests\\": "tests/"
38+
}
39+
},
3040
"bin": [
3141
"composer-diff"
3242
]

phpunit.xml.dist

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/5.7/phpunit.xsd"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
beStrictAboutTestsThatDoNotTestAnything="true"
8+
colors="true"
9+
convertErrorsToExceptions="true"
10+
convertNoticesToExceptions="true"
11+
convertWarningsToExceptions="true"
12+
failOnRisky="true"
13+
processIsolation="false"
14+
stopOnFailure="false"
15+
bootstrap="vendor/autoload.php"
16+
>
17+
<php>
18+
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
19+
</php>
20+
<testsuites>
21+
<testsuite name="Composer Diff Test Suite">
22+
<directory>./tests/</directory>
23+
<exclude>./tests/fixtures/</exclude>
24+
<exclude>./tests/test-git/</exclude>
25+
</testsuite>
26+
</testsuites>
27+
28+
<filter>
29+
<whitelist>
30+
<directory>./src/</directory>
31+
</whitelist>
32+
</filter>
33+
</phpunit>

src/PackageDiff.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function getPackageDiff($from, $to, $dev = false, $withPlatform = true)
5757
*
5858
* @return ArrayRepository
5959
*/
60-
protected function loadPackages($path, $dev, $withPlatform = true)
60+
private function loadPackages($path, $dev, $withPlatform = true)
6161
{
6262
$data = \json_decode($this->getFileContents($path), true);
6363
$loader = new ArrayLoader();
@@ -80,9 +80,9 @@ protected function loadPackages($path, $dev, $withPlatform = true)
8080
/**
8181
* @param string $path
8282
*
83-
* @return string|null
83+
* @return string
8484
*/
85-
protected function getFileContents($path)
85+
private function getFileContents($path)
8686
{
8787
$originalPath = $path;
8888

tests/PackageDiffTest.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
}

tests/fixtures/base/composer.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "test/test",
3+
"require": {
4+
"symfony/event-dispatcher": "^2.8",
5+
"doctrine/dbal": "^2.10"
6+
},
7+
"require-dev": {
8+
"phpunit/phpunit": "^9.2"
9+
},
10+
"authors": [
11+
{
12+
"name": "Ion Bazan",
13+
"email": "ion.bazan@gmail.com"
14+
}
15+
]
16+
}

0 commit comments

Comments
 (0)