Completed
Pull Request — master (#38)
by Matthias
03:49
created

testDoNotFailOnParseErrorWithErrorHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
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);
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

47
        self::/** @scrutinizer ignore-call */ 
48
              expectException(Error::class);
Loading history...
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