Failed Conditions
Pull Request — master (#163)
by Guillaume
07:51
created

testSourceFileThatUsesDevDependency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
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\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 testSelfCheckShowsNoErrors(): void
37
    {
38
        $this->commandTester->execute([
39
            // that's our own composer.json, lets be sure our self check does not throw errors
40
            'composer-json' => dirname(__DIR__, 3) . '/composer.json'
41
        ]);
42
43
        $this->assertSame(0, $this->commandTester->getStatusCode());
44
        $this->assertStringContainsString('no unknown symbols found', $this->commandTester->getDisplay());
45
46
        // verbose output should not be shown
47
        $this->assertNotRegExp('/Collecting defined (vendor|extension) symbols... found \d+ symbols./', $this->commandTester->getDisplay());
48
        $this->assertNotRegExp('/Collecting used symbols... found \d+ symbols./', $this->commandTester->getDisplay());
49
    }
50
51
    public function testSelfCheckIncludingDevShowsNoErrors(): void
52
    {
53
        $this->commandTester->execute([
54
            // that's our own composer.json, lets be sure our self check does not throw errors
55
            'composer-json' => dirname(__DIR__, 3) . '/composer.json'
56
        ], [
57
            'include-dev' => true
58
        ]);
59
60
        $this->assertSame(0, $this->commandTester->getStatusCode());
61
        $this->assertStringContainsString('no unknown symbols found', $this->commandTester->getDisplay());
62
63
        // verbose output should not be shown
64
        $this->assertNotRegExp('/Collecting defined (vendor|extension) symbols... found \d+ symbols./', $this->commandTester->getDisplay());
65
        $this->assertNotRegExp('/Collecting used symbols... found \d+ symbols./', $this->commandTester->getDisplay());
66
    }
67
68
    public function testVerboseSelfCheckShowsCounts(): void
69
    {
70
        $this->commandTester->execute([
71
            // that's our own composer.json
72
            'composer-json' => dirname(__DIR__, 3) . '/composer.json',
73
        ], [
74
            'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
75
        ]);
76
77
        $this->assertRegExp('/Collecting defined vendor symbols... found \d+ symbols./', $this->commandTester->getDisplay());
78
        $this->assertRegExp('/Collecting defined extension symbols... found \d+ symbols./', $this->commandTester->getDisplay());
79
        $this->assertRegExp('/Collecting used symbols... found \d+ symbols./', $this->commandTester->getDisplay());
80
    }
81
82
    public function testWithAdditionalSourceFiles(): void
83
    {
84
        $root = vfsStream::setup();
85
        vfsStream::create([
86
            'config.json' => <<<JSON
87
{
88
    "scan-files": ["src/ComposerRequireChecker/Cli/CheckCommand.php"]
89
}
90
JSON
91
            ,
92
        ]);
93
94
        $this->commandTester->execute([
95
            // that's our own composer.json
96
            'composer-json' => dirname(__DIR__, 3) . '/composer.json',
97
            '--config-file' => $root->getChild('config.json')->url(),
98
        ]);
99
100
        $this->assertRegExp('/There were no unknown symbols found./', $this->commandTester->getDisplay());
101
    }
102
103
    public function testSourceFileThatUsesDevDependency(): void
104
    {
105
        $root = vfsStream::setup();
106
        vfsStream::create(['config.json' => '{"scan-files":["test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php"]}']);
107
108
        $exitCode = $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->assertNotEquals(0, $exitCode);
115
        $this->assertRegExp('/The following unknown symbols were found.*PHPUnit\\\\Framework\\\\TestCase/s', $this->commandTester->getDisplay());
116
    }
117
}
118