Skip to content

Commit 41537c3

Browse files
committed
Add decorated output test
1 parent 1b05bac commit 41537c3

File tree

6 files changed

+184
-22
lines changed

6 files changed

+184
-22
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ jobs:
5454
restore-keys: ${{ runner.os }}-composer-
5555
- name: Install Composer dependencies
5656
run: composer update -n --prefer-dist ${{ matrix.composer-flags }}
57+
- name: Set default branch for tests
58+
run: git config --global init.defaultBranch main
5759
- name: Run Tests
5860
run: vendor/bin/simple-phpunit --coverage-clover coverage.xml --coverage-text
5961
- name: Upload coverage to Codecov

tests/Formatter/FormatterTest.php

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace IonBazan\ComposerDiff\Tests\Formatter;
44

55
use Composer\DependencyResolver\Operation\InstallOperation;
6+
use Composer\DependencyResolver\Operation\OperationInterface;
67
use Composer\DependencyResolver\Operation\UninstallOperation;
78
use Composer\DependencyResolver\Operation\UpdateOperation;
89
use Composer\Package\PackageInterface;
@@ -43,29 +44,22 @@ public function testGetProjectUrlReturnsNullForInvalidOperation()
4344

4445
/**
4546
* @param bool $withUrls
47+
* @param bool $decorated
4648
*
4749
* @testWith [false]
4850
* [true]
51+
* [false, true]
52+
* [true, true]
4953
*/
50-
public function testItRendersTheListOfOperations($withUrls)
54+
public function testItRendersTheListOfOperations($withUrls, $decorated = false)
5155
{
52-
$output = new StreamOutput(fopen('php://memory', 'wb', false));
53-
$formatter = $this->getFormatter($output, $this->getGenerators());
54-
$prodPackages = array(
55-
new InstallOperation($this->getPackage('a/package-1', '1.0.0')),
56-
new InstallOperation($this->getPackage('a/no-link-1', '1.0.0')),
57-
new UpdateOperation($this->getPackage('a/package-2', '1.0.0'), $this->getPackage('a/package-2', '1.2.0')),
58-
new UpdateOperation($this->getPackage('a/package-3', '2.0.0'), $this->getPackage('a/package-3', '1.1.1')),
59-
new UpdateOperation($this->getPackage('a/no-link-2', '2.0.0'), $this->getPackage('a/no-link-2', '1.1.1')),
60-
new UpdateOperation($this->getPackage('php', '>=7.4.6'), $this->getPackage('php', '^8.0')),
56+
$output = new StreamOutput(fopen('php://memory', 'wb', false), OutputInterface::VERBOSITY_NORMAL, $decorated);
57+
$this->getFormatter($output, $this->getGenerators())->render(
58+
$this->getEntries($this->getSampleProdOperations()),
59+
$this->getEntries($this->getSampleDevOperations()),
60+
$withUrls
6161
);
62-
$devPackages = array(
63-
new UpdateOperation($this->getPackage('a/package-5', 'dev-master', 'dev-master 1234567'), $this->getPackage('a/package-5', '1.1.1')),
64-
new UninstallOperation($this->getPackage('a/package-4', '0.1.1')),
65-
new UninstallOperation($this->getPackage('a/no-link-2', '0.1.1')),
66-
);
67-
$formatter->render($this->getEntries($prodPackages), $this->getEntries($devPackages), $withUrls);
68-
$this->assertSame($this->getSampleOutput($withUrls), $this->getDisplay($output));
62+
$this->assertSame($this->getSampleOutput($withUrls, $decorated), $this->getDisplay($output));
6963
}
7064

7165
public function testItFailsWithInvalidOperation()
@@ -84,10 +78,11 @@ abstract protected function getFormatter(OutputInterface $output, GeneratorConta
8478

8579
/**
8680
* @param bool $withUrls
81+
* @param bool $decorated
8782
*
8883
* @return string
8984
*/
90-
abstract protected function getSampleOutput($withUrls);
85+
abstract protected function getSampleOutput($withUrls, $decorated);
9186

9287
/**
9388
* @return string
@@ -107,6 +102,14 @@ protected function getDisplay(OutputInterface $output)
107102
return stream_get_contents($output->getStream());
108103
}
109104

105+
/**
106+
* @return bool
107+
*/
108+
protected function supportsLinks()
109+
{
110+
return method_exists('Symfony\Component\Console\Formatter\OutputFormatterStyle', 'setHref');
111+
}
112+
110113
/**
111114
* @return MockObject|GeneratorContainer
112115
*/
@@ -138,4 +141,31 @@ protected function getGenerators()
138141

139142
return $generators;
140143
}
144+
145+
/**
146+
* @return OperationInterface[]
147+
*/
148+
private function getSampleProdOperations()
149+
{
150+
return array(
151+
new InstallOperation($this->getPackage('a/package-1', '1.0.0')),
152+
new InstallOperation($this->getPackage('a/no-link-1', '1.0.0')),
153+
new UpdateOperation($this->getPackage('a/package-2', '1.0.0'), $this->getPackage('a/package-2', '1.2.0')),
154+
new UpdateOperation($this->getPackage('a/package-3', '2.0.0'), $this->getPackage('a/package-3', '1.1.1')),
155+
new UpdateOperation($this->getPackage('a/no-link-2', '2.0.0'), $this->getPackage('a/no-link-2', '1.1.1')),
156+
new UpdateOperation($this->getPackage('php', '>=7.4.6'), $this->getPackage('php', '^8.0')),
157+
);
158+
}
159+
160+
/**
161+
* @return OperationInterface[]
162+
*/
163+
private function getSampleDevOperations()
164+
{
165+
return array(
166+
new UpdateOperation($this->getPackage('a/package-5', 'dev-master', 'dev-master 1234567'), $this->getPackage('a/package-5', '1.1.1')),
167+
new UninstallOperation($this->getPackage('a/package-4', '0.1.1')),
168+
new UninstallOperation($this->getPackage('a/no-link-2', '0.1.1')),
169+
);
170+
}
141171
}

tests/Formatter/GitHubFormatterTest.php

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

99
class GitHubFormatterTest extends FormatterTest
1010
{
11-
protected function getSampleOutput($withUrls)
11+
protected function getSampleOutput($withUrls, $decorated)
1212
{
1313
if ($withUrls) {
1414
return <<<OUTPUT

tests/Formatter/JsonFormatterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function testRenderSingle()
8888
)), $this->getDisplay($output));
8989
}
9090

91-
protected function getSampleOutput($withUrls)
91+
protected function getSampleOutput($withUrls, $decorated)
9292
{
9393
if ($withUrls) {
9494
return self::formatOutput(array(

tests/Formatter/MarkdownListFormatterTest.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,32 @@
88

99
class MarkdownListFormatterTest extends FormatterTest
1010
{
11-
protected function getSampleOutput($withUrls)
11+
protected function getSampleOutput($withUrls, $decorated)
1212
{
1313
if ($withUrls) {
14+
if ($decorated) {
15+
return <<<OUTPUT
16+
Prod Packages
17+
=============
18+
19+
- Install [a/package-1](https://example.com/r/a/package-1) (1.0.0) [Compare](https://example.com/r/1.0.0)
20+
- Install a/no-link-1 (1.0.0)
21+
- Upgrade [a/package-2](https://example.com/r/a/package-2) (1.0.0 => 1.2.0) [Compare](https://example.com/c/1.0.0..1.2.0)
22+
- Downgrade [a/package-3](https://example.com/r/a/package-3) (2.0.0 => 1.1.1) [Compare](https://example.com/c/2.0.0..1.1.1)
23+
- Downgrade a/no-link-2 (2.0.0 => 1.1.1)
24+
- Change php (>=7.4.6 => ^8.0)
25+
26+
Dev Packages
27+
============
28+
29+
- Change [a/package-5](https://example.com/r/a/package-5) (dev-master 1234567 => 1.1.1) [Compare](https://example.com/c/dev-master..1.1.1)
30+
- Uninstall [a/package-4](https://example.com/r/a/package-4) (0.1.1) [Compare](https://example.com/r/0.1.1)
31+
- Uninstall a/no-link-2 (0.1.1)
32+
33+
34+
OUTPUT;
35+
}
36+
1437
return <<<OUTPUT
1538
Prod Packages
1639
=============
@@ -30,6 +53,29 @@ protected function getSampleOutput($withUrls)
3053
- Uninstall a/no-link-2 (0.1.1)
3154
3255
56+
OUTPUT;
57+
}
58+
59+
if ($decorated) {
60+
return <<<OUTPUT
61+
Prod Packages
62+
=============
63+
64+
- Install a/package-1 (1.0.0)
65+
- Install a/no-link-1 (1.0.0)
66+
- Upgrade a/package-2 (1.0.0 => 1.2.0)
67+
- Downgrade a/package-3 (2.0.0 => 1.1.1)
68+
- Downgrade a/no-link-2 (2.0.0 => 1.1.1)
69+
- Change php (>=7.4.6 => ^8.0)
70+
71+
Dev Packages
72+
============
73+
74+
- Change a/package-5 (dev-master 1234567 => 1.1.1)
75+
- Uninstall a/package-4 (0.1.1)
76+
- Uninstall a/no-link-2 (0.1.1)
77+
78+
3379
OUTPUT;
3480
}
3581

tests/Formatter/MarkdownTableFormatterTest.php

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,51 @@
88

99
class MarkdownTableFormatterTest extends FormatterTest
1010
{
11-
protected function getSampleOutput($withUrls)
11+
protected function getSampleOutput($withUrls, $decorated)
1212
{
1313
if ($withUrls) {
14+
if ($decorated) {
15+
if ($this->supportsLinks()) {
16+
return <<<OUTPUT
17+
| Prod Packages | Operation | Base | Target | Link |
18+
|--------------------------------------------------|------------|---------|--------|-----------------------------------------------|
19+
| []8;;https://example.com/r/a/package-1\\a/package-1]8;;\\](https://example.com/r/a/package-1) | New | - | 1.0.0 | [Compare](https://example.com/r/1.0.0) |
20+
| a/no-link-1 | New | - | 1.0.0 | |
21+
| []8;;https://example.com/r/a/package-2\\a/package-2]8;;\\](https://example.com/r/a/package-2) | Upgraded | 1.0.0 | 1.2.0 | [Compare](https://example.com/c/1.0.0..1.2.0) |
22+
| []8;;https://example.com/r/a/package-3\\a/package-3]8;;\\](https://example.com/r/a/package-3) | Downgraded | 2.0.0 | 1.1.1 | [Compare](https://example.com/c/2.0.0..1.1.1) |
23+
| a/no-link-2 | Downgraded | 2.0.0 | 1.1.1 | |
24+
| php | Changed | >=7.4.6 | ^8.0 | |
25+
26+
| Dev Packages | Operation | Base | Target | Link |
27+
|--------------------------------------------------|-----------|--------------------|--------|----------------------------------------------------|
28+
| []8;;https://example.com/r/a/package-5\\a/package-5]8;;\\](https://example.com/r/a/package-5) | Changed | dev-master 1234567 | 1.1.1 | [Compare](https://example.com/c/dev-master..1.1.1) |
29+
| []8;;https://example.com/r/a/package-4\\a/package-4]8;;\\](https://example.com/r/a/package-4) | Removed | 0.1.1 | - | [Compare](https://example.com/r/0.1.1) |
30+
| a/no-link-2 | Removed | 0.1.1 | - | |
31+
32+
33+
OUTPUT;
34+
}
35+
36+
return <<<OUTPUT
37+
| Prod Packages | Operation | Base | Target | Link |
38+
|--------------------------------------------------|------------|---------|--------|-----------------------------------------------|
39+
| [a/package-1](https://example.com/r/a/package-1) | New | - | 1.0.0 | [Compare](https://example.com/r/1.0.0) |
40+
| a/no-link-1 | New | - | 1.0.0 | |
41+
| [a/package-2](https://example.com/r/a/package-2) | Upgraded | 1.0.0 | 1.2.0 | [Compare](https://example.com/c/1.0.0..1.2.0) |
42+
| [a/package-3](https://example.com/r/a/package-3) | Downgraded | 2.0.0 | 1.1.1 | [Compare](https://example.com/c/2.0.0..1.1.1) |
43+
| a/no-link-2 | Downgraded | 2.0.0 | 1.1.1 | |
44+
| php | Changed | >=7.4.6 | ^8.0 | |
45+
46+
| Dev Packages | Operation | Base | Target | Link |
47+
|--------------------------------------------------|-----------|--------------------|--------|----------------------------------------------------|
48+
| [a/package-5](https://example.com/r/a/package-5) | Changed | dev-master 1234567 | 1.1.1 | [Compare](https://example.com/c/dev-master..1.1.1) |
49+
| [a/package-4](https://example.com/r/a/package-4) | Removed | 0.1.1 | - | [Compare](https://example.com/r/0.1.1) |
50+
| a/no-link-2 | Removed | 0.1.1 | - | |
51+
52+
53+
OUTPUT;
54+
}
55+
1456
return <<<OUTPUT
1557
| Prod Packages | Operation | Base | Target | Link |
1658
|--------------------------------------------------|------------|---------|--------|-----------------------------------------------|
@@ -28,6 +70,48 @@ protected function getSampleOutput($withUrls)
2870
| a/no-link-2 | Removed | 0.1.1 | - | |
2971
3072
73+
OUTPUT;
74+
}
75+
76+
if ($decorated) {
77+
if ($this->supportsLinks()) {
78+
return <<<OUTPUT
79+
| Prod Packages | Operation | Base | Target |
80+
|---------------|------------|---------|--------|
81+
| ]8;;https://example.com/r/a/package-1\a/package-1]8;;\ | New | - | 1.0.0 |
82+
| a/no-link-1 | New | - | 1.0.0 |
83+
| ]8;;https://example.com/r/a/package-2\a/package-2]8;;\ | Upgraded | 1.0.0 | 1.2.0 |
84+
| ]8;;https://example.com/r/a/package-3\a/package-3]8;;\ | Downgraded | 2.0.0 | 1.1.1 |
85+
| a/no-link-2 | Downgraded | 2.0.0 | 1.1.1 |
86+
| php | Changed | >=7.4.6 | ^8.0 |
87+
88+
| Dev Packages | Operation | Base | Target |
89+
|--------------|-----------|--------------------|--------|
90+
| ]8;;https://example.com/r/a/package-5\a/package-5]8;;\ | Changed | dev-master 1234567 | 1.1.1 |
91+
| ]8;;https://example.com/r/a/package-4\a/package-4]8;;\ | Removed | 0.1.1 | - |
92+
| a/no-link-2 | Removed | 0.1.1 | - |
93+
94+
95+
OUTPUT;
96+
}
97+
98+
return <<<OUTPUT
99+
| Prod Packages | Operation | Base | Target |
100+
|---------------|------------|---------|--------|
101+
| a/package-1 | New | - | 1.0.0 |
102+
| a/no-link-1 | New | - | 1.0.0 |
103+
| a/package-2 | Upgraded | 1.0.0 | 1.2.0 |
104+
| a/package-3 | Downgraded | 2.0.0 | 1.1.1 |
105+
| a/no-link-2 | Downgraded | 2.0.0 | 1.1.1 |
106+
| php | Changed | >=7.4.6 | ^8.0 |
107+
108+
| Dev Packages | Operation | Base | Target |
109+
|--------------|-----------|--------------------|--------|
110+
| a/package-5 | Changed | dev-master 1234567 | 1.1.1 |
111+
| a/package-4 | Removed | 0.1.1 | - |
112+
| a/no-link-2 | Removed | 0.1.1 | - |
113+
114+
31115
OUTPUT;
32116
}
33117

0 commit comments

Comments
 (0)