|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ComposerRequireCheckerTest\ASTLocator; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayObject; |
|
6
|
|
|
use ComposerRequireChecker\ASTLocator\LocateASTFromFiles; |
|
7
|
|
|
use org\bovigo\vfs\vfsStream; |
|
8
|
|
|
use org\bovigo\vfs\vfsStreamDirectory; |
|
9
|
|
|
use PhpParser\Error; |
|
10
|
|
|
use PhpParser\ErrorHandler\Collecting; |
|
11
|
|
|
use PhpParser\Lexer; |
|
12
|
|
|
use PhpParser\Parser\Php7; |
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
use RuntimeException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @covers \ComposerRequireChecker\ASTLocator\LocateASTFromFiles |
|
18
|
|
|
*/ |
|
19
|
|
|
final class LocateASTFromFilesTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var LocateASTFromFiles */ |
|
22
|
|
|
private $locator; |
|
23
|
|
|
/** @var vfsStreamDirectory */ |
|
24
|
|
|
private $root; |
|
25
|
|
|
|
|
26
|
|
|
protected function setUp(): void |
|
27
|
|
|
{ |
|
28
|
|
|
parent::setUp(); |
|
29
|
|
|
|
|
30
|
|
|
$this->locator = new LocateASTFromFiles(new Php7(new Lexer()), null); |
|
31
|
|
|
$this->root = vfsStream::setup(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testLocate(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$files = [ |
|
37
|
|
|
$this->createFile('MyClassA', '<?php class MyClassA {}'), |
|
38
|
|
|
$this->createFile('MyClassB', '<?php class MyClassB {}'), |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
$roots = $this->locate($files); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertCount(2, $roots); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testFailOnParseError(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$this->expectException(Error::class); |
|
49
|
|
|
$files = [ |
|
50
|
|
|
$this->createFile('MyBadCode', '<?php this causes a parse error'), |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
$this->locate($files); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testDoNotFailOnParseErrorWithErrorHandler(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$collectingErrorHandler = new Collecting(); |
|
59
|
|
|
$this->locator = new LocateASTFromFiles(new Php7(new Lexer()), $collectingErrorHandler); |
|
60
|
|
|
$files = [ |
|
61
|
|
|
$this->createFile('MyBadCode', '<?php this causes a parse error'), |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
$roots = $this->locate($files); |
|
65
|
|
|
$this->assertCount(1, $roots); // one file should be parsed (partially) |
|
66
|
|
|
$this->assertTrue($collectingErrorHandler->hasErrors()); |
|
67
|
|
|
$this->assertCount(1, $collectingErrorHandler->getErrors()); //should have one parse error |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testFailOnParseErrorWithNullReturn(): void |
|
71
|
|
|
{ |
|
72
|
|
|
$this->expectException(RuntimeException::class); |
|
73
|
|
|
|
|
74
|
|
|
$parserMock = $this->createMock(Php7::class); |
|
75
|
|
|
$parserMock->method('parse')->willReturn(null); |
|
76
|
|
|
|
|
77
|
|
|
$this->locator = new LocateASTFromFiles($parserMock, null); |
|
78
|
|
|
$files = [ |
|
79
|
|
|
$this->createFile( |
|
80
|
|
|
'MyBadCode', |
|
81
|
|
|
'this content is not relevant because the parser is mocked and always returns null' |
|
82
|
|
|
), |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
$this->locate($files); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function createFile(string $path, string $content = null): string |
|
89
|
|
|
{ |
|
90
|
|
|
return vfsStream::newFile($path)->at($this->root)->setContent($content)->url(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string[] $files |
|
95
|
|
|
*/ |
|
96
|
|
|
private function locate(array $files): array |
|
97
|
|
|
{ |
|
98
|
|
|
return iterator_to_array(($this->locator)(new ArrayObject($files))); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|