|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ComposerUnused\ComposerUnused\Test\Unit\OutputFormatter\CompactFormatter; |
| 6 | + |
| 7 | +use ComposerUnused\ComposerUnused\Composer\Package; |
| 8 | +use ComposerUnused\ComposerUnused\Configuration\NamedFilter; |
| 9 | +use ComposerUnused\ComposerUnused\Dependency\DependencyCollection; |
| 10 | +use ComposerUnused\ComposerUnused\Dependency\RequiredDependency; |
| 11 | +use ComposerUnused\ComposerUnused\Filter\FilterCollection; |
| 12 | +use ComposerUnused\ComposerUnused\OutputFormatter\CompactFormatter; |
| 13 | +use ComposerUnused\ComposerUnused\Test\Stubs\TestDependency; |
| 14 | +use ComposerUnused\Contracts\PackageInterface; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Component\Console\Input\ArgvInput; |
| 17 | +use Symfony\Component\Console\Output\BufferedOutput; |
| 18 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 19 | + |
| 20 | +final class CompactFormatterTest extends TestCase |
| 21 | +{ |
| 22 | + private CompactFormatter $compactFormatter; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + $this->compactFormatter = new CompactFormatter(); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @test |
| 31 | + */ |
| 32 | + public function itPrints(): void |
| 33 | + { |
| 34 | + $symfonyStringRequiredDependency = new RequiredDependency( |
| 35 | + new Package('symfony/string'), |
| 36 | + ); |
| 37 | + $symfonyStringRequiredDependency->requiredBy(new TestDependency('symfony/event-dispatcher')); |
| 38 | + $usedDependencyCollection = new DependencyCollection([$symfonyStringRequiredDependency]); |
| 39 | + |
| 40 | + $unusedDependencyCollection = new DependencyCollection([ |
| 41 | + new RequiredDependency(new Package('symfony/console')) |
| 42 | + ]); |
| 43 | + |
| 44 | + $bufferedOutput = new BufferedOutput(); |
| 45 | + $outputStyle = new SymfonyStyle(new ArgvInput(), $bufferedOutput); |
| 46 | + |
| 47 | + $returnStatus = $this->compactFormatter->formatOutput( |
| 48 | + $this->createMock(PackageInterface::class), |
| 49 | + 'composer.json', |
| 50 | + $usedDependencyCollection, |
| 51 | + $unusedDependencyCollection, |
| 52 | + new DependencyCollection(), |
| 53 | + new FilterCollection([NamedFilter::fromString('symfony/zombie')], []), |
| 54 | + $outputStyle |
| 55 | + ); |
| 56 | + $consoleOutput = $bufferedOutput->fetch(); |
| 57 | + |
| 58 | + self::assertSame(1, $returnStatus); |
| 59 | + $lines = \explode(PHP_EOL, trim($consoleOutput)); |
| 60 | + self::assertSame('Unused packages: symfony/console', trim($lines[0])); |
| 61 | + self::assertSame('Zombie exclusions: NamedFilter(userProvided: true, string: symfony/zombie)', trim($lines[1])); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + /** |
| 66 | + * @test |
| 67 | + */ |
| 68 | + public function itPrintsNothingWhenNoErrors(): void |
| 69 | + { |
| 70 | + $symfonyStringRequiredDependency = new RequiredDependency( |
| 71 | + new Package('symfony/string'), |
| 72 | + ); |
| 73 | + $symfonyStringRequiredDependency->requiredBy(new TestDependency('symfony/event-dispatcher')); |
| 74 | + $usedDependencyCollection = new DependencyCollection([$symfonyStringRequiredDependency]); |
| 75 | + |
| 76 | + $bufferedOutput = new BufferedOutput(); |
| 77 | + $outputStyle = new SymfonyStyle(new ArgvInput(), $bufferedOutput); |
| 78 | + |
| 79 | + $returnStatus = $this->compactFormatter->formatOutput( |
| 80 | + $this->createMock(PackageInterface::class), |
| 81 | + 'composer.json', |
| 82 | + $usedDependencyCollection, |
| 83 | + new DependencyCollection([]), |
| 84 | + new DependencyCollection(), |
| 85 | + new FilterCollection([], []), |
| 86 | + $outputStyle |
| 87 | + ); |
| 88 | + $consoleOutput = $bufferedOutput->fetch(); |
| 89 | + |
| 90 | + self::assertSame(0, $returnStatus); |
| 91 | + self::assertSame('', $consoleOutput); |
| 92 | + } |
| 93 | +} |
0 commit comments