Passed
Pull Request — master (#65)
by Alexander
27:54 queued 12:56
created

JsonTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
eloc 15
c 3
b 0
f 2
dl 0
loc 39
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testInvalidTypeValidate() 0 6 1
A testInvalidJsonValidate() 0 5 1
A testValidValueValidate() 0 3 1
A testCustomValidationMessage() 0 7 1
A testValidationMessage() 0 7 1
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
        $this->assertTrue((new Json())->validate('{"name": "tester"}')->isValid());
33
    }
34
35
    public function testValidationMessage(): void
36
    {
37
        $this->assertEquals(
38
            [
39
                'The value is not JSON.'
40
            ],
41
            (new Json())->validate('')->getErrors()
42
        );
43
    }
44
45
    public function testCustomValidationMessage(): void
46
    {
47
        $this->assertEquals(
48
            [
49
                'bad json'
50
            ],
51
            (new Json())->message('bad json')->validate('')->getErrors()
52
        );
53
    }
54
}
55