Test Failed
Pull Request — master (#206)
by
unknown
06:12
created

testExceptionIfComposerJsonIsNotAString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 6
rs 10
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\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 testExceptionIfComposerJsonIsNotAString(): 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-is-a-array-as-input']
39
        ]);
40
    }
41
42
    public function testExceptionIfComposerJsonNotFound(): void
43
    {
44
        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

44
        self::/** @scrutinizer ignore-call */ 
45
              expectException(\InvalidArgumentException::class);
Loading history...
45
46
        $this->commandTester->execute([
47
            'composer-json' => 'this-will-not-be-found.json'
48
        ]);
49
    }
50
51
    public function testExceptionIfNoSymbolsFound(): void
52
    {
53
        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

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