Skip to content

Commit 1be49b4

Browse files
committed
improve coverage
1 parent b2fbb13 commit 1be49b4

File tree

8 files changed

+118
-11
lines changed

8 files changed

+118
-11
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
with:
3030
php-version: ${{ matrix.php-versions }}
3131
extensions: json
32+
coverage: pcov
3233
- name: Get composer cache directory
3334
id: composer-cache
3435
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
@@ -41,9 +42,9 @@ jobs:
4142
- name: Install Composer dependencies
4243
run: composer update -n --prefer-dist ${{ matrix.composer-flags }}
4344
- name: Run Tests
44-
run: vendor/bin/simple-phpunit
45-
# - name: Upload coverage to Codecov
46-
# uses: codecov/codecov-action@v1
45+
run: vendor/bin/simple-phpunit --coverage-clover coverage.xml
46+
- name: Upload coverage to Codecov
47+
uses: codecov/codecov-action@v1
4748
# - name: Run mutation tests
4849
# if: ${{ matrix.php-versions < 8.0 }}
4950
# env:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
/tests/fixtures/**/vendor/
66
/tests/test-git/
77
/coverage/
8+
/clover.xml
89
/.phpunit.result.cache

phpunit.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@
3030
<directory>./src/</directory>
3131
</whitelist>
3232
</filter>
33+
<logging>
34+
<log type="coverage-clover" target="clover.xml" />
35+
<log type="coverage-html" target="coverage.xml" />
36+
</logging>
3337
</phpunit>

tests/Command/DiffCommandTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace IonBazan\ComposerDiff\Tests\Command;
4+
5+
use Composer\DependencyResolver\Operation\InstallOperation;
6+
use Composer\DependencyResolver\Operation\UninstallOperation;
7+
use Composer\DependencyResolver\Operation\UpdateOperation;
8+
use Composer\Package\PackageInterface;
9+
use IonBazan\ComposerDiff\Command\DiffCommand;
10+
use IonBazan\ComposerDiff\Tests\TestCase;
11+
use PHPUnit\Framework\MockObject\MockObject;
12+
use Symfony\Component\Console\Tester\CommandTester;
13+
14+
class DiffCommandTest extends TestCase
15+
{
16+
public function testItGeneratesTable()
17+
{
18+
$base = 'base-composer.lock';
19+
$target = 'target-composer.lock';
20+
$diff = $this->getMockBuilder('IonBazan\ComposerDiff\PackageDiff')->getMock();
21+
$tester = new CommandTester(new DiffCommand($diff));
22+
$diff->expects($this->once())
23+
->method('getPackageDiff')
24+
->with($base, $target, false, false)
25+
->willReturn(array(
26+
new InstallOperation($this->getPackage('a/package-1', '1.0.0')),
27+
new UpdateOperation($this->getPackage('a/package-2', '1.0.0'), $this->getPackage('a/package-2', '1.2.0')),
28+
new UninstallOperation($this->getPackage('a/package-3', '0.1.1')),
29+
))
30+
;
31+
$result = $tester->execute(array(
32+
'--base' => $base,
33+
'--target' => $target,
34+
'--no-dev' => null,
35+
));
36+
$this->assertSame(0, $result);
37+
$this->assertSame(<<<OUTPUT
38+
| Prod Packages | Base | Target |
39+
|---------------|-------|---------|
40+
| a/package-1 | New | 1.0.0 |
41+
| a/package-2 | 1.0.0 | 1.2.0 |
42+
| a/package-3 | 0.1.1 | Removed |
43+
44+
45+
OUTPUT
46+
, $tester->getDisplay());
47+
}
48+
49+
public function testItFailsWithInvalidOperation()
50+
{
51+
$base = 'base-composer.lock';
52+
$target = 'target-composer.lock';
53+
$diff = $this->getMockBuilder('IonBazan\ComposerDiff\PackageDiff')->getMock();
54+
$tester = new CommandTester(new DiffCommand($diff));
55+
$diff->expects($this->once())
56+
->method('getPackageDiff')
57+
->with($base, $target, false, false)
58+
->willReturn(array(
59+
$this->getMockBuilder('Composer\DependencyResolver\Operation\OperationInterface')->getMock(),
60+
))
61+
;
62+
$this->setExpectedException('InvalidArgumentException', 'Invalid operation');
63+
$tester->execute(array(
64+
'--base' => $base,
65+
'--target' => $target,
66+
'--no-dev' => null,
67+
));
68+
}
69+
70+
/**
71+
* @param string $name
72+
* @param string $version
73+
*
74+
* @return MockObject&PackageInterface
75+
*/
76+
private function getPackage($name, $version)
77+
{
78+
$package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
79+
$package->method('getName')->willReturn($name);
80+
$package->method('getFullPrettyVersion')->willReturn($version);
81+
82+
return $package;
83+
}
84+
}

tests/Integration/DiffCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use IonBazan\ComposerDiff\Command\DiffCommand;
66
use IonBazan\ComposerDiff\PackageDiff;
7-
use PHPUnit\Framework\TestCase;
7+
use IonBazan\ComposerDiff\Tests\TestCase;
88
use Symfony\Component\Console\Tester\CommandTester;
99

1010
class DiffCommandTest extends TestCase

tests/PackageDiffTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Composer\DependencyResolver\Operation\UninstallOperation;
88
use Composer\DependencyResolver\Operation\UpdateOperation;
99
use IonBazan\ComposerDiff\PackageDiff;
10-
use PHPUnit\Framework\TestCase;
1110

1211
class PackageDiffTest extends TestCase
1312
{
@@ -64,11 +63,7 @@ public function testInvalidGitRef()
6463
{
6564
$diff = new PackageDiff();
6665
$this->prepareGit();
67-
if (method_exists($this, 'expectException')) {
68-
$this->expectException('RuntimeException');
69-
} else {
70-
$this->setExpectedException('RuntimeException');
71-
}
66+
$this->setExpectedException('RuntimeException');
7267
$diff->getPackageDiff('invalid-ref', '');
7368
}
7469

tests/PluginTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use IonBazan\ComposerDiff\Command\DiffCommand;
66
use IonBazan\ComposerDiff\PackageDiff;
77
use IonBazan\ComposerDiff\Plugin;
8-
use PHPUnit\Framework\TestCase;
98

109
class PluginTest extends TestCase
1110
{

tests/TestCase.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace IonBazan\ComposerDiff\Tests;
4+
5+
use PHPUnit\Framework\TestCase as BaseTestCase;
6+
7+
abstract class TestCase extends BaseTestCase
8+
{
9+
public function setExpectedException($exception, $message = '', $code = null)
10+
{
11+
if (!class_exists('PHPUnit\Framework\Error\Notice')) {
12+
$exception = str_replace('PHPUnit\\Framework\\Error\\', 'PHPUnit_Framework_Error_', $exception);
13+
}
14+
if (method_exists($this, 'expectException')) {
15+
$this->expectException($exception);
16+
if (strlen($message)) {
17+
$this->expectExceptionMessage($message);
18+
}
19+
} else {
20+
parent::setExpectedException($exception, $message, $code);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)