Completed
Pull Request — master (#240)
by Дмитрий
08:39 queued 02:29
created

AnalyzeFixturesTest::getEventManager()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 19
nc 3
nop 1
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Tests\PHPSA;
4
5
use PhpParser\ParserFactory;
6
use PHPSA\Analyzer;
7
use PHPSA\Analyzer\EventListener\ExpressionListener;
8
use PHPSA\Analyzer\EventListener\StatementListener;
9
use PHPSA\Application;
10
use PHPSA\Configuration;
11
use PHPSA\Context;
12
use PHPSA\Definition\FileParser;
13
use PHPSA\Issue;
14
use RecursiveDirectoryIterator;
15
use RecursiveIteratorIterator;
16
use Webiny\Component\EventManager\EventManager;
17
use PHPSA\Compiler;
18
use PhpParser\Node;
19
use PHPSA\Analyzer\Pass as AnalyzerPass;
20
21
class AnalyzeFixturesTest extends TestCase
22
{
23
    public function provideTestParseAndDump()
24
    {
25
        $iter = new RecursiveIteratorIterator(
26
            new RecursiveDirectoryIterator(
27
                __DIR__ . '/../analyze-fixtures'
28
            ),
29
            RecursiveIteratorIterator::LEAVES_ONLY
30
        );
31
32
        /** @var \SplFileInfo $file */
33
        foreach ($iter as $file) {
34
            if (!$file->isFile()) {
35
                continue;
36
            }
37
38
            $contents = file_get_contents($file);
39
            list (, $analyzer, $expected) = explode('----------------------------', $contents);
40
41
            yield [$file->getPathname(), trim($analyzer), json_decode(trim($expected), true)];
42
        }
43
    }
44
45
    /**
46
     * @dataProvider provideTestParseAndDump
47
     *
48
     * @param $file
49
     * @param string $analyzer
50
     * @param array $expectedIssues
51
     * @throws \PHPSA\Exception\RuntimeException
52
     * @throws \Webiny\Component\EventManager\EventManagerException
53
     */
54
    public function testParseAndDump($file, $analyzer, array $expectedIssues)
55
    {
56
        $compiler = new Compiler();
57
58
        $fileParser = new FileParser(
59
            (new ParserFactory())->create(
60
                ParserFactory::PREFER_PHP7,
61
                new \PhpParser\Lexer\Emulative(
62
                    array(
63
                        'usedAttributes' => array(
64
                            'comments',
65
                            'startLine',
66
                            'endLine',
67
                            'startTokenPos',
68
                            'endTokenPos'
69
                        )
70
                    )
71
                )
72
            ),
73
            $compiler
74
        );
75
76
        $context = new Context(
77
            new \Symfony\Component\Console\Output\NullOutput(),
78
            $application = new Application(),
79
            $this->getEventManager($analyzer)
80
        );
81
        $application->compiler = $compiler;
82
83
        $fileParser->parserFile($file, $context);
84
85
        $compiler->compile($context);
86
87
        $issues = array_map(
88
            // @todo Remove after moving all notices on Issue(s)
89
            function (Issue $issue) {
90
                $location = $issue->getLocation();
91
92
                return [
93
                    'type' => $issue->getCheckName(),
94
                    'message' => $issue->getDescription(),
95
                    'file' => $location->getFileName(),
96
                    'line' => $location->getLineStart(),
97
                ];
98
            },
99
            $application->getIssuesCollector()->getIssues()
100
        );
101
        
102
        foreach ($expectedIssues as $check) {
103
            self::assertContains($check, $issues, $file); // every expected Issue is in the collector
104
        }
105
    }
106
107
    /**
108
     * @param string $analyzer
0 ignored issues
show
Documentation introduced by
There is no parameter named $analyzer. Did you maybe mean $analyzerName?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
109
     * @return EventManager
110
     * @throws \Webiny\Component\EventManager\EventManagerException
111
     */
112
    protected function getEventManager($analyzerName)
113
    {
114
        if (!class_exists($analyzerName, true)) {
115
            throw new \InvalidArgumentException("Analyzer with name: {$analyzerName} doesnot exist");
116
        }
117
118
        /** @var \PHPSA\Analyzer\Pass\Metadata $metaData */
119
        $metaData = $analyzerName::getMetadata();
120
        if (!$metaData->allowsPhpVersion(PHP_VERSION)) {
121
            parent::markTestSkipped(
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (markTestSkipped() instead of getEventManager()). Are you sure this is correct? If so, you might want to change this to $this->markTestSkipped().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
122
                sprintf(
123
                    'We cannot tests %s with %s because PHP required version is %s',
124
                    $analyzerName,
125
                    PHP_VERSION,
126
                    $metaData->getRequiredPhpVersion()
127
                )
128
            );
129
        }
130
131
        $analyzerConfiguration = $metaData->getConfiguration();
132
        $analyzerConfiguration->attribute('enabled', true);
133
134
        $config = [
135
            $analyzerName::getMetadata()->getConfiguration()
136
        ];
137
138
        $em = EventManager::getInstance();
139
        $configuration = new Configuration([], $config);
140
        \PHPSA\Analyzer\Factory::factory($em, $configuration);
141
142
        return $em;
143
    }
144
}
145