Passed
Pull Request — master (#117)
by Andreas
07:51 queued 05:09
created

testCheckRendersUnknownSymbolsSorted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 18
rs 9.8666
cc 1
nc 1
nop 0
1
<?php
2
3
namespace ComposerRequireCheckerTest\Cli;
4
5
use ComposerRequireChecker\Cli\Application;
6
use org\bovigo\vfs\vfsStream;
7
use PHPUnit\Framework\TestCase;
8
use Symfony\Component\Console\Helper\Table;
9
use Symfony\Component\Console\Output\BufferedOutput;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Tester\CommandTester;
12
13
class CheckCommandTest extends TestCase
14
{
15
16
    /**
17
     * @var CommandTester
18
     */
19
    private $commandTester;
20
21
    public function setUp(): void
22
    {
23
        $application = new Application();
24
        $command = $application->get('check');
25
26
        $this->commandTester = new CommandTester($command);
27
    }
28
29
    public function testExceptionIfComposerJsonNotFound(): void
30
    {
31
        self::expectException(\InvalidArgumentException::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        self::/** @scrutinizer ignore-call */ 
32
              expectException(\InvalidArgumentException::class);
Loading history...
32
33
        $this->commandTester->execute([
34
            'composer-json' => 'this-will-not-be-found.json'
35
        ]);
36
    }
37
38
    public function testCheckRendersUnknownSymbolsSorted(): void
39
    {
40
        $expectedTable = self::expectedTable([
41
            'Doctrine\Common\Collections\ArrayCollection' => '',
42
            'Example\Library\Dependency' => '',
43
            'FILTER_VALIDATE_URL' => 'ext-filter',
44
            'filter_var' => 'ext-filter',
45
            'Foo\Bar\Baz' => '',
46
            'libxml_clear_errors' => 'ext-libxml',
47
        ]);
48
49
        $this->commandTester->execute([
50
            'composer-json' => __DIR__ . '/../../fixtures/unknownSymbols/composer.json',
51
        ]);
52
53
        $this->assertSame(1, $this->commandTester->getStatusCode());
54
        $this->assertContains('The following unknown symbols were found', $this->commandTester->getDisplay());
55
        $this->assertContains($expectedTable, $this->commandTester->getDisplay());
56
    }
57
58
    public function testSelfCheckShowsNoErrors(): void
59
    {
60
        $this->commandTester->execute([
61
            // that's our own composer.json, lets be sure our self check does not throw errors
62
            'composer-json' => dirname(__DIR__, 3) . '/composer.json'
63
        ]);
64
65
        $this->assertSame(0, $this->commandTester->getStatusCode());
66
        $this->assertContains('no unknown symbols found', $this->commandTester->getDisplay());
67
68
        // verbose output should not be shown
69
        $this->assertNotRegExp('/Collecting defined (vendor|extension) symbols... found \d+ symbols./', $this->commandTester->getDisplay());
70
        $this->assertNotRegExp('/Collecting used symbols... found \d+ symbols./', $this->commandTester->getDisplay());
71
    }
72
73
    public function testVerboseSelfCheckShowsCounts(): void
74
    {
75
        $this->commandTester->execute([
76
            // that's our own composer.json
77
            'composer-json' => dirname(__DIR__, 3) . '/composer.json',
78
        ], [
79
            'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
80
        ]);
81
82
        $this->assertRegExp('/Collecting defined vendor symbols... found \d+ symbols./', $this->commandTester->getDisplay());
83
        $this->assertRegExp('/Collecting defined extension symbols... found \d+ symbols./', $this->commandTester->getDisplay());
84
        $this->assertRegExp('/Collecting used symbols... found \d+ symbols./', $this->commandTester->getDisplay());
85
    }
86
87
    public function testWithAdditionalSourceFiles(): void
88
    {
89
        $root = vfsStream::setup();
90
        vfsStream::create([
91
            'config.json' => <<<JSON
92
{
93
    "scan-files": ["src/ComposerRequireChecker/Cli/CheckCommand.php"]
94
}
95
JSON
96
            ,
97
        ]);
98
99
        $this->commandTester->execute([
100
            // that's our own composer.json
101
            'composer-json' => dirname(__DIR__, 3) . '/composer.json',
102
            '--config-file' => $root->getChild('config.json')->url(),
103
        ]);
104
105
        $this->assertRegExp('/There were no unknown symbols found./', $this->commandTester->getDisplay());
106
    }
107
108
    private static function expectedTable(array $unknownSymbols): string
109
    {
110
        $output = new BufferedOutput();
111
112
        $table = new Table($output);
113
114
        $table
115
            ->setHeaders([
116
                'unknown symbol',
117
                'guessed dependency'
118
            ])
119
            ->setRows(array_map(static function (string $unknownSymbol, string $guessedDependency): array {
120
                return [
121
                    $unknownSymbol,
122
                    $guessedDependency,
123
                ];
124
            }, array_keys($unknownSymbols), array_values($unknownSymbols)));
125
126
        $table->render();
127
128
        return $output->fetch();
129
    }
130
}
131