Completed
Pull Request — master (#240)
by Дмитрий
08:27
created

AnalyzeFixturesTest::provideTestParseAndDump()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 0
dl 0
loc 21
rs 9.3142
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), trim($expected)];
42
        }
43
    }
44
45
    /**
46
     * @dataProvider provideTestParseAndDump
47
     *
48
     * @param $file
49
     * @param string $analyzer
50
     * @param string $expectedDump
51
     * @throws \PHPSA\Exception\RuntimeException
52
     * @throws \Webiny\Component\EventManager\EventManagerException
53
     */
54
    public function testParseAndDump($file, $analyzer, $expectedDump)
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
        $expectedArray = json_decode($expectedDump, true);
88
        $expectedType = $expectedArray[0]["type"];
89
        $issues = array_map(
90
            // @todo Remove after moving all notices on Issue(s)
91
            function (Issue $issue) {
92
                $location = $issue->getLocation();
93
94
                return [
95
                    'type' => $issue->getCheckName(),
96
                    'message' => $issue->getDescription(),
97
                    'file' => $location->getFileName(),
98
                    'line' => $location->getLineStart(),
99
                ];
100
            },
101
            $application->getIssuesCollector()->getIssues()
102
        );
103
        
104
        foreach ($expectedArray as $check) {
105
            self::assertContains($check, $issues, $file); // every expected Issue is in the collector
106
        }
107
108
        foreach ($issues as $check) {
109
            if ($check["type"] == $expectedType) {
110
                self::assertContains($check, $expectedArray, $file); // there is no other issue in the collector with the same type
111
            }
112
        }
113
    }
114
115
    /**
116
     * @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...
117
     * @return EventManager
118
     * @throws \Webiny\Component\EventManager\EventManagerException
119
     */
120
    protected function getEventManager($analyzerName)
121
    {
122
        if (!class_exists($analyzerName, true)) {
123
            throw new \InvalidArgumentException("Analyzer with name: {$analyzerName} doesnot exist");
124
        }
125
126
        /** @var \PHPSA\Analyzer\Pass\Metadata $metaData */
127
        $metaData = $analyzerName::getMetadata();
128
        if (!$metaData->allowsPhpVersion(PHP_VERSION)) {
129
            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...
130
                sprintf(
131
                    'We cannot tests %s with %s because PHP required version is %s',
132
                    $analyzerName,
133
                    PHP_VERSION,
134
                    $metaData->getRequiredPhpVersion()
135
                )
136
            );
137
        }
138
        
139
        $analyzerConfiguration = $metaData->getConfiguration();
140
        $analyzerConfiguration->attribute('enabled', true);
141
142
        $config = [
143
            $analyzerName::getMetadata()->getConfiguration()
144
        ];
145
146
        $em = EventManager::getInstance();
147
        $configuration = new Configuration([], $config);
148
        \PHPSA\Analyzer\Factory::factory($em, $configuration);
149
150
        return $em;
151
    }
152
}
153