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

JsonTest::testValidationMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 7
rs 10
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