Completed
Push — master ( e28430...c0c2e2 )
by Marco
05:49 queued 11s
created

CheckCommandTest::testUnknownSymbolsFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 14
rs 9.9332
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\Output\OutputInterface;
9
use Symfony\Component\Console\Tester\CommandTester;
10
11
final class CheckCommandTest extends TestCase
12
{
13
14
    /**
15
     * @var CommandTester
16
     */
17
    private $commandTester;
18
19
    protected function setUp(): void
20
    {
21
        $application = new Application();
22
        $command = $application->get('check');
23
24
        $this->commandTester = new CommandTester($command);
25
    }
26
27
    public function testExceptionIfComposerJsonNotFound(): void
28
    {
29
        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

29
        self::/** @scrutinizer ignore-call */ 
30
              expectException(\InvalidArgumentException::class);
Loading history...
30
31
        $this->commandTester->execute([
32
            'composer-json' => 'this-will-not-be-found.json'
33
        ]);
34
    }
35
36
    public function testExceptionIfNoSymbolsFound(): void
37
    {
38
        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

38
        self::/** @scrutinizer ignore-call */ 
39
              expectException(\LogicException::class);
Loading history...
39
40
        $this->commandTester->execute([
41
            'composer-json' => dirname(__DIR__, 2) . '/fixtures/noSymbols/composer.json'
42
        ]);
43
    }
44
45
    public function testUnknownSymbolsFound(): void
46
    {
47
        $this->commandTester->execute([
48
            'composer-json' => dirname(__DIR__, 2) . '/fixtures/unknownSymbols/composer.json'
49
        ]);
50
51
        $this->assertSame(1, $this->commandTester->getStatusCode());
52
        $this->assertStringContainsString('The following unknown symbols were found:', $this->commandTester->getDisplay());
53
        $this->assertStringContainsString('Doctrine\Common\Collections\ArrayCollection', $this->commandTester->getDisplay());
54
        $this->assertStringContainsString('Example\Library\Dependency', $this->commandTester->getDisplay());
55
        $this->assertStringContainsString('FILTER_VALIDATE_URL', $this->commandTester->getDisplay());
56
        $this->assertStringContainsString('filter_var', $this->commandTester->getDisplay());
57
        $this->assertStringContainsString('Foo\Bar\Baz', $this->commandTester->getDisplay());
58
        $this->assertStringContainsString('libxml_clear_errors', $this->commandTester->getDisplay());
59
    }
60
61
    public function testSelfCheckShowsNoErrors(): void
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->assertStringContainsString('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(): void
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(): void
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
    public function testSourceFileThatUsesDevDependency(): void
112
    {
113
        $root = vfsStream::setup();
114
        vfsStream::create(['config.json' => '{"scan-files":["test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php"]}']);
115
116
        $exitCode = $this->commandTester->execute([
117
            // that's our own composer.json
118
            'composer-json' => dirname(__DIR__, 3) . '/composer.json',
119
            '--config-file' => $root->getChild('config.json')->url(),
120
        ]);
121
122
        $this->assertNotEquals(0, $exitCode);
123
        $this->assertRegExp('/The following unknown symbols were found.*PHPUnit\\\\Framework\\\\TestCase/s', $this->commandTester->getDisplay());
124
    }
125
}
126