Completed
Push — master ( a2204f...d0b5dd )
by Stéphane
7s
created

DataTestCase::fileDataProvider()   C

Complexity

Conditions 12
Paths 44

Size

Total Lines 61
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 61
rs 6.2855
nc 44
cc 12
eloc 40
nop 0

How to fix   Long Method    Complexity   

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
/*
4
 * This file is part of the JVal package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace JVal\Testing;
11
12
/**
13
 * Test case for testing against actual JSON data stored in dedicated files.
14
 * Format examples can be found in the tests/Data/constraints directory.
15
 */
16
abstract class DataTestCase extends BaseTestCase
17
{
18
    /**
19
     * @codeCoverageIgnore (data provider is executed before test is launched)
20
     *
21
     * Data provider collecting test cases from JSON files.
22
     */
23
    public function fileDataProvider()
24
    {
25
        $caseDir = realpath($this->getDataDirectory());
26
        $caseNames = $this->getCaseFileNames();
27
        $caseExt = $caseNames ? '.json' : '';
28
        $caseNames = $caseNames ?: array_diff(scandir($caseDir), ['..', '.']);
29
        $tests = [];
30
31
        foreach ($caseNames as $caseName) {
32
            $caseFile = "{$caseDir}/{$caseName}{$caseExt}";
33
            $case = $this->loadJsonFromFile($caseFile);
34
35
            foreach ($case->tests as $test) {
36
                if (!isset($test->valid) && !isset($test->invalid)) {
37
                    throw new \Exception(sprintf(
38
                        'Test case "%s %s" has neither "valid" or "invalid" data (file: %s)',
39
                        $case->title,
40
                        $test->title,
41
                        $caseFile
42
                    ));
43
                }
44
45
                if (isset($test->valid)) {
46
                    foreach ($test->valid as $i => $instance) {
47
                        $tests[] = [
48
                            $caseFile,
49
                            "{$case->title} {$test->title}, valid instance #{$i}",
50
                            $instance,
51
                            $test->schema,
52
                            true,
53
                            [],
54
                        ];
55
                    }
56
                }
57
58
                if (isset($test->invalid)) {
59
                    foreach ($test->invalid as $i => $set) {
60
                        if (!isset($set->violations)) {
61
                            throw new \Exception(sprintf(
62
                                'Invalid test must have a "violations" property in %s',
63
                                $caseFile
64
                            ));
65
                        }
66
67
                        $tests[] = [
68
                            $caseFile,
69
                            "{$case->title} {$test->title}, invalid instance #{$i}",
70
                            $set->instance,
71
                            $test->schema,
72
                            false,
73
                            array_map(function ($violation) {
74
                                return (array) $violation;
75
                            }, $set->violations),
76
                        ];
77
                    }
78
                }
79
            }
80
        }
81
82
        return $tests;
83
    }
84
85
    /**
86
     * Returns the path the test data directory.
87
     *
88
     * @return string
89
     */
90
    abstract protected function getDataDirectory();
91
92
    /**
93
     * Returns the names of the test cases to be loaded. If no names are
94
     * returned, all the cases found in the data directory will be loaded.
95
     *
96
     * @return mixed
97
     */
98
    abstract protected function getCaseFileNames();
99
}
100
101