Passed
Push — master ( 327050...09fed8 )
by Sergei
02:44
created

ValidationContextTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 74
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructor() 0 5 1
A testGetDataSetWithoutDataSet() 0 7 1
A testGetParameter() 0 7 1
A testGetRawDataWithoutRawData() 0 7 1
A testSetParameter() 0 6 1
A testSetValidatorAndRawDataOnce() 0 11 1
A testDataSet() 0 8 1
A testValidateWithoutValidator() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
use RuntimeException;
9
use Yiisoft\Validator\DataSet\ArrayDataSet;
10
use Yiisoft\Validator\ValidationContext;
11
use Yiisoft\Validator\Validator;
12
13
final class ValidationContextTest extends TestCase
14
{
15
    public function testGetDataSetWithoutDataSet(): void
16
    {
17
        $context = new ValidationContext();
18
19
        $this->expectException(RuntimeException::class);
20
        $this->expectErrorMessage('Data set in validation context is not set.');
21
        $context->getDataSet();
22
    }
23
24
    public function testConstructor(): void
25
    {
26
        $context = new ValidationContext(['key' => 42]);
27
28
        $this->assertSame(42, $context->getParameter('key'));
29
    }
30
31
    public function testDataSet(): void
32
    {
33
        $dataSet = new ArrayDataSet();
34
35
        $context = new ValidationContext();
36
        $context->setDataSet($dataSet);
37
38
        $this->assertSame($dataSet, $context->getDataSet());
39
    }
40
41
    public function testSetParameter(): void
42
    {
43
        $context = new ValidationContext();
44
        $context->setParameter('key', 42);
45
46
        $this->assertSame(42, $context->getParameter('key'));
47
    }
48
49
    public function testGetParameter(): void
50
    {
51
        $context = new ValidationContext(['key' => 42]);
52
53
        $this->assertSame(42, $context->getParameter('key'));
54
        $this->assertNull($context->getParameter('non-exists'));
55
        $this->assertSame(7, $context->getParameter('non-exists', 7));
56
    }
57
58
    public function testValidateWithoutValidator(): void
59
    {
60
        $context = new ValidationContext();
61
62
        $this->expectException(RuntimeException::class);
63
        $this->expectErrorMessage('Validator and raw data in validation context is not set.');
64
        $context->validate(42);
65
    }
66
67
    public function testGetRawDataWithoutRawData(): void
68
    {
69
        $context = new ValidationContext();
70
71
        $this->expectException(RuntimeException::class);
72
        $this->expectErrorMessage('Validator and raw data in validation context is not set.');
73
        $context->getRawData();
74
    }
75
76
    public function testSetValidatorAndRawDataOnce(): void
77
    {
78
        $validator = new Validator();
79
80
        $context = new ValidationContext();
81
82
        $context
83
            ->setValidatorAndRawDataOnce($validator, 1)
84
            ->setValidatorAndRawDataOnce($validator, 2);
85
86
        $this->assertSame(1, $context->getRawData());
87
    }
88
}
89