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
|
|
|
|