DataProvider::before()   B
last analyzed

Complexity

Conditions 8
Paths 19

Size

Total Lines 57
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 7.2648
c 0
b 0
f 0
cc 8
eloc 39
nc 19
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Before suite hook that provide dataprovider annotation
4
 * for datadriven tests using non-static data source
5
 */
6
namespace Codeception\Extension;
7
8
use Codeception\Util\Annotation;
9
use Codeception\Step\Comment;
10
use Codeception\Test\Cest as CestFormat;
11
use Codeception\Exception\TestParseException;
12
13
class DataProvider extends \Codeception\Platform\Extension
14
{
15
    // list events to listen to
16
    public static $events = ['suite.before' => 'before'];
17
18
    public function before(\Codeception\Event\SuiteEvent $se)
19
    {
20
        $suite = $se->getSuite();
21
        $tests = $suite->tests();
22
        foreach ($tests as $id => $test) {
23
            if (is_a($test, 'Codeception\Test\Cest')) {
24
25
                $testClass = $test->getTestClass();
26
                $testClassName = get_class($testClass);
27
                $testMethod = $test->getTestMethod();
28
                $testFile = $test->getMetadata()->getFilename();
29
                $testActor = $test->getMetadata()->getCurrent('actor');
30
31
                $dataMethod = Annotation::forMethod($testClass, $testMethod)->fetch('dataprovider');
32
33
                try {
34
                    if (empty($dataMethod)) {
35
                        continue;
36
                    }
37
                    
38
                    if (false === is_callable([$testClass, $dataMethod])) {
39
                        throw new \Exception();
40
                    }
41
42
                    $dataProvider = new \PHPUnit_Framework_TestSuite_DataProvider();
43
                    $examples = $testClassName::$dataMethod();
44
                    foreach ($examples as $example) {
45
                        if ($example === null) {
46
                            throw new TestParseException(
47
                                $testFile, "Invalid values format returned by DataProvider {$dataMethod} for ${testClassName}->${testMethod}."
48
                            );
49
                        }
50
                        $dataTest = new CestFormat($testClass, $testMethod, $testFile);
51
                        $dataTest->getMetadata()->setServices([
52
                            'di'         => $test->getMetadata()->getService('di'),
53
                            'dispatcher' => $test->getMetadata()->getService('dispatcher'),
54
                            'modules'    => $test->getMetadata()->getService('modules')
55
                        ]);
56
                        $dataTest->getMetadata()->setCurrent(['actor' => $testActor, 'example' => $example]);
57
                        $step = new Comment('', $dataTest->getMetadata()->getCurrent('example'));
58
                        $dataTest->getScenario()->setFeature($dataTest->getSpecFromMethod() . ' | ' . $step->getArgumentsAsString(100));
59
                        $groups = Annotation::forMethod($testClass, $testMethod)->fetchAll('group');
60
                        $dataProvider->addTest($dataTest, $groups);
61
                    }
62
                    $tests[$id] = $dataProvider;
63
64
                } catch (\Exception $e) {
65
                    throw new TestParseException(
66
                        $testFile, "DataProvider {$dataMethod} for ${testClassName}->${testMethod} is invalid or not callable."
67
                        . PHP_EOL .
68
                        "Make sure this is a public static function."
69
                    );
70
                }
71
            }
72
        }
73
        $suite->setTests($tests);
74
    }
75
76
}
77