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