Issues (22)

Cli/CheckCommandTest.php (2 issues)

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\Command\Command;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Tester\CommandTester;
11
12
use function dirname;
13
use function file_put_contents;
14
use function unlink;
15
use function version_compare;
16
17
final class CheckCommandTest extends TestCase
18
{
19
20
    /**
21
     * @var CommandTester
22
     */
23
    private $commandTester;
24
25
    protected function setUp(): void
26
    {
27
        $application = new Application();
28
        $command = $application->get('check');
29
30
        $this->commandTester = new CommandTester($command);
31
    }
32
33
    public function testExceptionIfComposerJsonNotFound(): void
34
    {
35
        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

35
        self::/** @scrutinizer ignore-call */ 
36
              expectException(\InvalidArgumentException::class);
Loading history...
36
37
        $this->commandTester->execute([
38
            'composer-json' => 'this-will-not-be-found.json'
39
        ]);
40
    }
41
42
    public function testExceptionIfNoSymbolsFound(): void
43
    {
44
        self::expectException(\LogicException::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

44
        self::/** @scrutinizer ignore-call */ 
45
              expectException(\LogicException::class);
Loading history...
45
46
        $this->commandTester->execute([
47
            'composer-json' => dirname(__DIR__, 2) . '/fixtures/noSymbols/composer.json'
48
        ]);
49
    }
50
51
    public function testUnknownSymbolsFound(): void
52
    {
53
        $this->commandTester->execute([
54
            'composer-json' => dirname(__DIR__, 2) . '/fixtures/unknownSymbols/composer.json'
55
        ]);
56
57
        $this->assertSame(1, $this->commandTester->getStatusCode());
58
        $this->assertStringContainsString('The following unknown symbols were found:', $this->commandTester->getDisplay());
59
        $this->assertStringContainsString('Doctrine\Common\Collections\ArrayCollection', $this->commandTester->getDisplay());
60
        $this->assertStringContainsString('Example\Library\Dependency', $this->commandTester->getDisplay());
61
        $this->assertStringContainsString('FILTER_VALIDATE_URL', $this->commandTester->getDisplay());
62
        $this->assertStringContainsString('filter_var', $this->commandTester->getDisplay());
63
        $this->assertStringContainsString('Foo\Bar\Baz', $this->commandTester->getDisplay());
64
        $this->assertStringContainsString('libxml_clear_errors', $this->commandTester->getDisplay());
65
    }
66
67
    public function testSelfCheckShowsNoErrors(): void
68
    {
69
        $this->commandTester->execute([
70
            // that's our own composer.json, lets be sure our self check does not throw errors
71
            'composer-json' => dirname(__DIR__, 3) . '/composer.json'
72
        ]);
73
74
        $this->assertSame(0, $this->commandTester->getStatusCode());
75
        $this->assertStringContainsString('no unknown symbols found', $this->commandTester->getDisplay());
76
77
        // verbose output should not be shown
78
        $this->assertNotRegExp('/Collecting defined (vendor|extension) symbols... found \d+ symbols./', $this->commandTester->getDisplay());
79
        $this->assertNotRegExp('/Collecting used symbols... found \d+ symbols./', $this->commandTester->getDisplay());
80
    }
81
82
    public function testVerboseSelfCheckShowsCounts(): void
83
    {
84
        $this->commandTester->execute([
85
            // that's our own composer.json
86
            'composer-json' => dirname(__DIR__, 3) . '/composer.json',
87
        ], [
88
            'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
89
        ]);
90
91
        $this->assertRegExp('/Collecting defined vendor symbols... found \d+ symbols./', $this->commandTester->getDisplay());
92
        $this->assertRegExp('/Collecting defined extension symbols... found \d+ symbols./', $this->commandTester->getDisplay());
93
        $this->assertRegExp('/Collecting used symbols... found \d+ symbols./', $this->commandTester->getDisplay());
94
    }
95
96
    public function testWithAdditionalSourceFiles(): void
97
    {
98
        $root = vfsStream::setup();
99
        vfsStream::create([
100
            'config.json' => <<<JSON
101
{
102
    "scan-files": ["src/ComposerRequireChecker/Cli/CheckCommand.php"]
103
}
104
JSON
105
            ,
106
        ]);
107
108
        $this->commandTester->execute([
109
            // that's our own composer.json
110
            'composer-json' => dirname(__DIR__, 3) . '/composer.json',
111
            '--config-file' => $root->getChild('config.json')->url(),
112
        ]);
113
114
        $this->assertRegExp('/There were no unknown symbols found./', $this->commandTester->getDisplay());
115
    }
116
117
    public function testSourceFileThatUsesDevDependency(): void
118
    {
119
        $root = vfsStream::setup();
120
        vfsStream::create(['config.json' => '{"scan-files":["test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php"]}']);
121
122
        $exitCode = $this->commandTester->execute([
123
            // that's our own composer.json
124
            'composer-json' => dirname(__DIR__, 3) . '/composer.json',
125
            '--config-file' => $root->getChild('config.json')->url(),
126
        ]);
127
128
        $this->assertNotEquals(0, $exitCode);
129
        $this->assertRegExp('/The following unknown symbols were found.*PHPUnit\\\\Framework\\\\TestCase/s', $this->commandTester->getDisplay());
130
    }
131
132
    public function testNoUnknownSymbolsFound(): void
133
    {
134
        $baseDir = dirname(__DIR__, 2) . '/fixtures/noUnknownSymbols/';
135
        $this->commandTester->execute(['composer-json' => $baseDir . 'composer.json']);
136
137
        self::assertSame(Command::SUCCESS, $this->commandTester->getStatusCode());
138
        self::assertStringContainsString('There were no unknown symbols found.', $this->commandTester->getDisplay());
139
    }
140
141
    public function testReservedKeywordInPhp8DoesNotThrowExceptionInPhp7(): void
142
    {
143
        if (version_compare(PHP_VERSION, '8.0.0') >= 0) {
144
            self::markTestSkipped('This test does not work in PHP8');
145
        }
146
        $baseDir = dirname(__DIR__, 2) . '/fixtures/noUnknownSymbols/';
147
        $tmpFile = $baseDir . 'src/Match.php';
148
        file_put_contents($tmpFile, '<?php class Match { }');
149
150
        $this->commandTester->execute(['composer-json' => $baseDir . 'composer.json']);
151
152
        unlink($tmpFile);
153
        self::assertSame(Command::SUCCESS, $this->commandTester->getStatusCode());
154
    }
155
}
156