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
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @covers \ComposerRequireChecker\ASTLocator\LocateASTFromFiles |
17
|
|
|
*/ |
18
|
|
|
class LocateASTFromFilesTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** @var LocateASTFromFiles */ |
21
|
|
|
private $locator; |
22
|
|
|
/** @var vfsStreamDirectory */ |
23
|
|
|
private $root; |
24
|
|
|
|
25
|
|
|
protected function setUp() |
26
|
|
|
{ |
27
|
|
|
parent::setUp(); |
28
|
|
|
|
29
|
|
|
$this->locator = new LocateASTFromFiles(new Php7(new Lexer())); |
30
|
|
|
$this->root = vfsStream::setup(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testLocate() |
34
|
|
|
{ |
35
|
|
|
$files = [ |
36
|
|
|
$this->createFile('MyClassA', '<?php class MyClassA {}'), |
37
|
|
|
$this->createFile('MyClassB', '<?php class MyClassB {}'), |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
$roots = $this->locate($files); |
41
|
|
|
|
42
|
|
|
$this->assertCount(2, $roots); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testFailOnParseError() |
46
|
|
|
{ |
47
|
|
|
self::expectException(Error::class); |
|
|
|
|
48
|
|
|
$files = [ |
49
|
|
|
$this->createFile('MyBadCode', '<?php this causes a parse error'), |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
$this->locate($files); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testDoNotFailOnParseErrorWithErrorHandler() |
56
|
|
|
{ |
57
|
|
|
$collectingErrorHandler = new Collecting(); |
58
|
|
|
$this->locator = new LocateASTFromFiles(new Php7(new Lexer()), $collectingErrorHandler); |
59
|
|
|
$files = [ |
60
|
|
|
$this->createFile('MyBadCode', '<?php this causes a parse error'), |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
$roots = $this->locate($files); |
64
|
|
|
$this->assertCount(1, $roots); // one file should be parsed (partially) |
65
|
|
|
$this->assertTrue($collectingErrorHandler->hasErrors()); |
66
|
|
|
$this->assertCount(1, $collectingErrorHandler->getErrors()); //should have one parse error |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function createFile(string $path, string $content = null): string |
70
|
|
|
{ |
71
|
|
|
return vfsStream::newFile($path)->at($this->root)->setContent($content)->url(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string[] $files |
76
|
|
|
*/ |
77
|
|
|
private function locate(array $files): array |
78
|
|
|
{ |
79
|
|
|
return iterator_to_array(($this->locator)(new ArrayObject($files))); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|