Failed Conditions
Branch ignore-unknown-keywords (efa676)
by Stéphane
02:58
created

Draft4Test::applyTestProvider()   C

Complexity

Conditions 9
Paths 8

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 46
rs 5.0942
c 1
b 0
f 1
cc 9
eloc 25
nc 8
nop 0
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;
11
12
use JVal\Testing\BaseTestCase;
13
14
class Draft4Test extends BaseTestCase
15
{
16
    /**
17
     * @dataProvider applyTestProvider
18
     *
19
     * @param string    $file
20
     * @param string    $title
21
     * @param mixed     $instance
22
     * @param \stdClass $schema
23
     * @param bool      $isInstanceValid
24
     */
25
    public function testApply(
26
        $file,
27
        $title,
28
        $instance,
29
        \stdClass $schema,
30
        $isInstanceValid
31
    ) {
32
        $remoteDir = realpath(__DIR__.'/../../vendor/json-schema/test-suite/remotes');
33
        $validator = Validator::buildDefault(function ($uri) use ($remoteDir) {
34
            return str_replace('http://localhost:1234', $this->getLocalUri($remoteDir), $uri);
35
        });
36
        $actualErrors = $validator->validate($instance, $schema, $this->getLocalUri($file));
37
38
        $this->assertValidationResult(
39
            $file,
40
            $title,
41
            $instance,
42
            $schema,
43
            $isInstanceValid,
44
            [],
45
            $actualErrors
46
        );
47
    }
48
49
    /**
50
     * Provider of #testApply().
51
     */
52
    public function applyTestProvider()
53
    {
54
        $testDir = realpath(__DIR__.'/../../vendor/json-schema/test-suite/tests/draft4');
55
        $iterator = new \RecursiveDirectoryIterator($testDir);
56
        $tests = [];
57
58
        foreach (new \RecursiveIteratorIterator($iterator) as $item) {
59
            if ($item->isFile()) {
60
                $whiteList = $this->whiteListFiles();
61
                $blackList = $this->blackListFiles();
62
63
                if ($whiteList !== false && !in_array($item->getBaseName(), $whiteList)) {
64
                    continue;
65
                }
66
67
                if (in_array($item->getBaseName(), $blackList)) {
68
                    continue;
69
                }
70
71
                $caseCount = count($this->loadJsonFromFile($item->getPathname()));
72
73
                for ($i = 0; $i < $caseCount; ++$i) {
74
                    // As validation begins with a normalization step, we cannot
75
                    // share the same schema instance between tests, so we reload
76
                    // it for each case.
77
                    $cases = $this->loadJsonFromFile($item->getPathname());
78
79
                    foreach ($cases[$i]->tests as $test) {
80
                        if (in_array($test->description, $this->blackListTests())) {
81
                            continue;
82
                        }
83
84
                        $tests[] = array(
85
                            $item->getPathName(),
86
                            "{$cases[$i]->description} - {$test->description}",
87
                            $test->data,
88
                            $cases[$i]->schema,
89
                            $test->valid,
90
                        );
91
                    }
92
                }
93
            }
94
        }
95
96
        return $tests;
97
    }
98
99
    private function blackListFiles()
100
    {
101
        return [];
102
    }
103
104
    private function blackListTests()
105
    {
106
        // those two tests won't never pass in PHP (bignums encountered in
107
        // JSON strings are automatically converted to float)
108
        return [
109
            'a bignum is an integer',
110
            'a negative bignum is an integer',
111
        ];
112
    }
113
114
    private function whiteListFiles()
115
    {
116
        return false;
117
    }
118
}
119