Skip to content

Commit

Permalink
Add decorated output test
Browse files Browse the repository at this point in the history
  • Loading branch information
IonBazan committed May 11, 2022
1 parent 1b05bac commit 41537c3
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ jobs:
restore-keys: ${{ runner.os }}-composer-
- name: Install Composer dependencies
run: composer update -n --prefer-dist ${{ matrix.composer-flags }}
- name: Set default branch for tests
run: git config --global init.defaultBranch main
- name: Run Tests
run: vendor/bin/simple-phpunit --coverage-clover coverage.xml --coverage-text
- name: Upload coverage to Codecov
Expand Down
66 changes: 48 additions & 18 deletions tests/Formatter/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace IonBazan\ComposerDiff\Tests\Formatter;

use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\OperationInterface;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\Package\PackageInterface;
Expand Down Expand Up @@ -43,29 +44,22 @@ public function testGetProjectUrlReturnsNullForInvalidOperation()

/**
* @param bool $withUrls
* @param bool $decorated
*
* @testWith [false]
* [true]
* [false, true]
* [true, true]
*/
public function testItRendersTheListOfOperations($withUrls)
public function testItRendersTheListOfOperations($withUrls, $decorated = false)
{
$output = new StreamOutput(fopen('php://memory', 'wb', false));
$formatter = $this->getFormatter($output, $this->getGenerators());
$prodPackages = array(
new InstallOperation($this->getPackage('a/package-1', '1.0.0')),
new InstallOperation($this->getPackage('a/no-link-1', '1.0.0')),
new UpdateOperation($this->getPackage('a/package-2', '1.0.0'), $this->getPackage('a/package-2', '1.2.0')),
new UpdateOperation($this->getPackage('a/package-3', '2.0.0'), $this->getPackage('a/package-3', '1.1.1')),
new UpdateOperation($this->getPackage('a/no-link-2', '2.0.0'), $this->getPackage('a/no-link-2', '1.1.1')),
new UpdateOperation($this->getPackage('php', '>=7.4.6'), $this->getPackage('php', '^8.0')),
$output = new StreamOutput(fopen('php://memory', 'wb', false), OutputInterface::VERBOSITY_NORMAL, $decorated);
$this->getFormatter($output, $this->getGenerators())->render(
$this->getEntries($this->getSampleProdOperations()),
$this->getEntries($this->getSampleDevOperations()),
$withUrls
);
$devPackages = array(
new UpdateOperation($this->getPackage('a/package-5', 'dev-master', 'dev-master 1234567'), $this->getPackage('a/package-5', '1.1.1')),
new UninstallOperation($this->getPackage('a/package-4', '0.1.1')),
new UninstallOperation($this->getPackage('a/no-link-2', '0.1.1')),
);
$formatter->render($this->getEntries($prodPackages), $this->getEntries($devPackages), $withUrls);
$this->assertSame($this->getSampleOutput($withUrls), $this->getDisplay($output));
$this->assertSame($this->getSampleOutput($withUrls, $decorated), $this->getDisplay($output));
}

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

/**
* @param bool $withUrls
* @param bool $decorated
*
* @return string
*/
abstract protected function getSampleOutput($withUrls);
abstract protected function getSampleOutput($withUrls, $decorated);

/**
* @return string
Expand All @@ -107,6 +102,14 @@ protected function getDisplay(OutputInterface $output)
return stream_get_contents($output->getStream());
}

/**
* @return bool
*/
protected function supportsLinks()
{
return method_exists('Symfony\Component\Console\Formatter\OutputFormatterStyle', 'setHref');
}

/**
* @return MockObject|GeneratorContainer
*/
Expand Down Expand Up @@ -138,4 +141,31 @@ protected function getGenerators()

return $generators;
}

/**
* @return OperationInterface[]
*/
private function getSampleProdOperations()
{
return array(
new InstallOperation($this->getPackage('a/package-1', '1.0.0')),
new InstallOperation($this->getPackage('a/no-link-1', '1.0.0')),
new UpdateOperation($this->getPackage('a/package-2', '1.0.0'), $this->getPackage('a/package-2', '1.2.0')),
new UpdateOperation($this->getPackage('a/package-3', '2.0.0'), $this->getPackage('a/package-3', '1.1.1')),
new UpdateOperation($this->getPackage('a/no-link-2', '2.0.0'), $this->getPackage('a/no-link-2', '1.1.1')),
new UpdateOperation($this->getPackage('php', '>=7.4.6'), $this->getPackage('php', '^8.0')),
);
}

/**
* @return OperationInterface[]
*/
private function getSampleDevOperations()
{
return array(
new UpdateOperation($this->getPackage('a/package-5', 'dev-master', 'dev-master 1234567'), $this->getPackage('a/package-5', '1.1.1')),
new UninstallOperation($this->getPackage('a/package-4', '0.1.1')),
new UninstallOperation($this->getPackage('a/no-link-2', '0.1.1')),
);
}
}
2 changes: 1 addition & 1 deletion tests/Formatter/GitHubFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class GitHubFormatterTest extends FormatterTest
{
protected function getSampleOutput($withUrls)
protected function getSampleOutput($withUrls, $decorated)
{
if ($withUrls) {
return <<<OUTPUT
Expand Down
2 changes: 1 addition & 1 deletion tests/Formatter/JsonFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function testRenderSingle()
)), $this->getDisplay($output));
}

protected function getSampleOutput($withUrls)
protected function getSampleOutput($withUrls, $decorated)
{
if ($withUrls) {
return self::formatOutput(array(
Expand Down
48 changes: 47 additions & 1 deletion tests/Formatter/MarkdownListFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,32 @@

class MarkdownListFormatterTest extends FormatterTest
{
protected function getSampleOutput($withUrls)
protected function getSampleOutput($withUrls, $decorated)
{
if ($withUrls) {
if ($decorated) {
return <<<OUTPUT
Prod Packages
=============
- Install [a/package-1](https://example.com/r/a/package-1) (1.0.0) [Compare](https://example.com/r/1.0.0)
- Install a/no-link-1 (1.0.0)
- 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)
- 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)
- Downgrade a/no-link-2 (2.0.0 => 1.1.1)
- Change php (>=7.4.6 => ^8.0)
Dev Packages
============
- 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)
- Uninstall [a/package-4](https://example.com/r/a/package-4) (0.1.1) [Compare](https://example.com/r/0.1.1)
- Uninstall a/no-link-2 (0.1.1)
OUTPUT;
}

return <<<OUTPUT
Prod Packages
=============
Expand All @@ -30,6 +53,29 @@ protected function getSampleOutput($withUrls)
- Uninstall a/no-link-2 (0.1.1)
OUTPUT;
}

if ($decorated) {
return <<<OUTPUT
Prod Packages
=============
- Install a/package-1 (1.0.0)
- Install a/no-link-1 (1.0.0)
- Upgrade a/package-2 (1.0.0 => 1.2.0)
- Downgrade a/package-3 (2.0.0 => 1.1.1)
- Downgrade a/no-link-2 (2.0.0 => 1.1.1)
- Change php (>=7.4.6 => ^8.0)
Dev Packages
============
- Change a/package-5 (dev-master 1234567 => 1.1.1)
- Uninstall a/package-4 (0.1.1)
- Uninstall a/no-link-2 (0.1.1)
OUTPUT;
}

Expand Down
86 changes: 85 additions & 1 deletion tests/Formatter/MarkdownTableFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,51 @@

class MarkdownTableFormatterTest extends FormatterTest
{
protected function getSampleOutput($withUrls)
protected function getSampleOutput($withUrls, $decorated)
{
if ($withUrls) {
if ($decorated) {
if ($this->supportsLinks()) {
return <<<OUTPUT
| Prod Packages | Operation | Base | Target | Link |
|--------------------------------------------------|------------|---------|--------|-----------------------------------------------|
| []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) |
| a/no-link-1 | New | - | 1.0.0 | |
| []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) |
| []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) |
| a/no-link-2 | Downgraded | 2.0.0 | 1.1.1 | |
| php | Changed | >=7.4.6 | ^8.0 | |
| Dev Packages | Operation | Base | Target | Link |
|--------------------------------------------------|-----------|--------------------|--------|----------------------------------------------------|
| []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) |
| []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) |
| a/no-link-2 | Removed | 0.1.1 | - | |
OUTPUT;
}

return <<<OUTPUT
| Prod Packages | Operation | Base | Target | Link |
|--------------------------------------------------|------------|---------|--------|-----------------------------------------------|
| [a/package-1](https://example.com/r/a/package-1) | New | - | 1.0.0 | [Compare](https://example.com/r/1.0.0) |
| a/no-link-1 | New | - | 1.0.0 | |
| [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) |
| [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) |
| a/no-link-2 | Downgraded | 2.0.0 | 1.1.1 | |
| php | Changed | >=7.4.6 | ^8.0 | |
| Dev Packages | Operation | Base | Target | Link |
|--------------------------------------------------|-----------|--------------------|--------|----------------------------------------------------|
| [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) |
| [a/package-4](https://example.com/r/a/package-4) | Removed | 0.1.1 | - | [Compare](https://example.com/r/0.1.1) |
| a/no-link-2 | Removed | 0.1.1 | - | |
OUTPUT;
}

return <<<OUTPUT
| Prod Packages | Operation | Base | Target | Link |
|--------------------------------------------------|------------|---------|--------|-----------------------------------------------|
Expand All @@ -28,6 +70,48 @@ protected function getSampleOutput($withUrls)
| a/no-link-2 | Removed | 0.1.1 | - | |
OUTPUT;
}

if ($decorated) {
if ($this->supportsLinks()) {
return <<<OUTPUT
| Prod Packages | Operation | Base | Target |
|---------------|------------|---------|--------|
| ]8;;https://example.com/r/a/package-1\a/package-1]8;;\ | New | - | 1.0.0 |
| a/no-link-1 | New | - | 1.0.0 |
| ]8;;https://example.com/r/a/package-2\a/package-2]8;;\ | Upgraded | 1.0.0 | 1.2.0 |
| ]8;;https://example.com/r/a/package-3\a/package-3]8;;\ | Downgraded | 2.0.0 | 1.1.1 |
| a/no-link-2 | Downgraded | 2.0.0 | 1.1.1 |
| php | Changed | >=7.4.6 | ^8.0 |
| Dev Packages | Operation | Base | Target |
|--------------|-----------|--------------------|--------|
| ]8;;https://example.com/r/a/package-5\a/package-5]8;;\ | Changed | dev-master 1234567 | 1.1.1 |
| ]8;;https://example.com/r/a/package-4\a/package-4]8;;\ | Removed | 0.1.1 | - |
| a/no-link-2 | Removed | 0.1.1 | - |
OUTPUT;
}

return <<<OUTPUT
| Prod Packages | Operation | Base | Target |
|---------------|------------|---------|--------|
| a/package-1 | New | - | 1.0.0 |
| a/no-link-1 | New | - | 1.0.0 |
| a/package-2 | Upgraded | 1.0.0 | 1.2.0 |
| a/package-3 | Downgraded | 2.0.0 | 1.1.1 |
| a/no-link-2 | Downgraded | 2.0.0 | 1.1.1 |
| php | Changed | >=7.4.6 | ^8.0 |
| Dev Packages | Operation | Base | Target |
|--------------|-----------|--------------------|--------|
| a/package-5 | Changed | dev-master 1234567 | 1.1.1 |
| a/package-4 | Removed | 0.1.1 | - |
| a/no-link-2 | Removed | 0.1.1 | - |
OUTPUT;
}

Expand Down

0 comments on commit 41537c3

Please sign in to comment.