Passed
Pull Request — master (#72)
by Wilmer
11:03
created

JsonTest::testValidValueValidate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 78
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 72
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 78
rs 8.6109

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
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Rule;
6
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Validator\Rule\Json;
9
10
/**
11
 * @group validators
12
 */
13
class JsonTest extends TestCase
14
{
15
    public function testInvalidJsonValidate(): void
16
    {
17
        $val = new Json();
18
        $this->assertFalse($val->validate('{"name": "tester"')->isValid());
19
        $this->assertFalse($val->validate('{"name": tester}')->isValid());
20
    }
21
22
    public function testInvalidTypeValidate(): void
23
    {
24
        $val = new Json();
25
        $this->assertFalse($val->validate(['json'])->isValid());
26
        $this->assertFalse($val->validate(10)->isValid());
27
        $this->assertFalse($val->validate(null)->isValid());
28
    }
29
30
    public function testValidValueValidate(): void
31
    {
32
        // JSON test from http://www.json.org/JSON_checker/test/pass1.json
33
        $json1 = <<<'JSON'
34
[
35
    "JSON Test Pattern pass1",
36
    {"object with 1 member":["array with 1 element"]},
37
    {},
38
    [],
39
    -42,
40
    true,
41
    false,
42
    null,
43
    {
44
        "integer": 1234567890,
45
        "real": -9876.543210,
46
        "e": 0.123456789e-12,
47
        "E": 1.234567890E+34,
48
        "":  23456789012E66,
49
        "zero": 0,
50
        "one": 1,
51
        "space": " ",
52
        "quote": "\"",
53
        "backslash": "\\",
54
        "controls": "\b\f\n\r\t",
55
        "slash": "/ & \/",
56
        "alpha": "abcdefghijklmnopqrstuvwyz",
57
        "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
58
        "digit": "0123456789",
59
        "0123456789": "digit",
60
        "special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
61
        "hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
62
        "true": true,
63
        "false": false,
64
        "null": null,
65
        "array":[  ],
66
        "object":{  },
67
        "address": "50 St. James Street",
68
        "url": "http://www.JSON.org/",
69
        "comment": "// /* <!-- --",
70
        "# -- --> */": " ",
71
        " s p a c e d " :[1,2 , 3
72
73
,
74
75
4 , 5        ,          6           ,7        ],"compact":[1,2,3,4,5,6,7],
76
        "jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
77
        "quotes": "&#34; \u0022 %22 0x22 034 &#x22;",
78
        "\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
79
: "A key can be any string"
80
    },
81
    0.5 ,98.6
82
,
83
99.44
84
,
85
86
1066,
87
1e1,
88
0.1e1,
89
1e-1,
90
1e00,2e+00,2e-00
91
,"rosebud"]
92
JSON;
93
        // JSON test from http://www.json.org/JSON_checker/test/pass2.json
94
        $json2 = '[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]';
95
        // JSON test from http://www.json.org/JSON_checker/test/pass3.json
96
        $json3 = <<<'JSON'
97
{
98
    "JSON Test Pattern pass3": {
99
        "The outermost value": "must be an object or array.",
100
        "In this test": "It is an object."
101
    }
102
}
103
JSON;
104
105
        $this->assertTrue((new Json())->validate($json1)->isValid());
106
        $this->assertTrue((new Json())->validate($json2)->isValid());
107
        $this->assertTrue((new Json())->validate($json3)->isValid());
108
    }
109
110
    public function testValidationMessage(): void
111
    {
112
        $this->assertEquals(
113
            [
114
                'The value is not JSON.'
115
            ],
116
            (new Json())->validate('')->getErrors()
117
        );
118
    }
119
120
    public function testCustomValidationMessage(): void
121
    {
122
        $this->assertEquals(
123
            [
124
                'bad json'
125
            ],
126
            (new Json())->message('bad json')->validate('')->getErrors()
127
        );
128
    }
129
}
130