Failed Conditions
Pull Request — master (#117)
by Andreas
10:51
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 ComposerRequireChecker\DependencyGuesser\DependencyGuesser;
7
use org\bovigo\vfs\vfsStream;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\Console\Helper\Table;
10
use Symfony\Component\Console\Output\BufferedOutput;
11
use Symfony\Component\Console\Output\ConsoleOutput;
12
use Symfony\Component\Console\Output\NullOutput;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Tester\CommandTester;
15
16
class CheckCommandTest extends TestCase
17
{
18
19
    /**
20
     * @var CommandTester
21
     */
22
    private $commandTester;
23
24
    public function setUp()
25
    {
26
        $application = new Application();
27
        $command = $application->get('check');
28
29
        $this->commandTester = new CommandTester($command);
30
    }
31
32
    public function testExceptionIfComposerJsonNotFound()
33
    {
34
        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

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