1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Validation\Rules; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class ExactlyOneTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
/** @var ExactlyOne - System Under Test */ |
12
|
|
|
protected ExactlyOne $sut; |
13
|
|
|
|
14
|
|
|
public function setUp(): void |
15
|
|
|
{ |
16
|
|
|
parent::setUp(); |
17
|
|
|
|
18
|
|
|
$this->sut = new ExactlyOne(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return array[] |
23
|
|
|
*/ |
24
|
|
|
public function passesProvider(): array |
25
|
|
|
{ |
26
|
|
|
return [ |
27
|
|
|
'non-empty-missing' => ['foo', [], ['field2']], |
28
|
|
|
'non-empty-empty' => ['foo', ['field2' => ''], ['field2']], |
29
|
|
|
'empty-non-empty' => ['', ['field2' => 'foo'], ['field2']], |
30
|
|
|
'first-of-four-others-missing' => ['foo', [], ['a', 'b', 'c']], |
31
|
|
|
'first-of-four-others-empty' => ['foo', ['a' => '', 'b' => '', 'c' => ''], ['a', 'b', 'c']], |
32
|
|
|
'third-of-four-others-missing' => ['', ['b' => 'foo'], ['a', 'b', 'c']], |
33
|
|
|
'third-of-four-others-empty' => ['', ['a' => '', 'b' => 'foo', 'c' => ''], ['a', 'b', 'c']], |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @dataProvider passesProvider |
39
|
|
|
* |
40
|
|
|
* @param $value |
41
|
|
|
* @param array $allValues |
42
|
|
|
* @param array $args |
43
|
|
|
*/ |
44
|
|
|
public function testPasses($value, array $allValues, array $args): void |
45
|
|
|
{ |
46
|
|
|
$this->sut->setArgs($args); |
47
|
|
|
|
48
|
|
|
$actualResult = $this->sut->passes($value, $allValues); |
49
|
|
|
|
50
|
|
|
$this->assertEquals(true, $actualResult); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array[] |
55
|
|
|
*/ |
56
|
|
|
public function failureProvider(): array |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
|
|
'empty-missing' => ['', [], ['field2']], |
60
|
|
|
'empty-empty' => ['', ['field2' => ''], ['field2']], |
61
|
|
|
'zero-missing' => ['0', [], ['field2']], |
62
|
|
|
'zero-zero' => ['0', ['field2' => '0'], ['field2']], |
63
|
|
|
'both-truthy' => ['foo', ['field2' => 'bar'], ['field2']], |
64
|
|
|
'two-of-four-others-missing' => ['foo', ['b' => 'foo'], ['a', 'b', 'c']], |
65
|
|
|
'four-of-four' => ['foo', ['a' => 'bar', 'b' => 'baz', 'c' => 'quix'], ['a', 'b', 'c']], |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @dataProvider failureProvider |
71
|
|
|
* |
72
|
|
|
* @param $value |
73
|
|
|
* @param array $allValues |
74
|
|
|
* @param array $args |
75
|
|
|
*/ |
76
|
|
|
public function testFailures($value, array $allValues, array $args): void |
77
|
|
|
{ |
78
|
|
|
$this->sut->setArgs($args); |
79
|
|
|
|
80
|
|
|
$actualResult = $this->sut->passes($value, $allValues); |
81
|
|
|
|
82
|
|
|
$this->assertEquals(false, $actualResult); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testGetErrorPlaceholders(): void |
86
|
|
|
{ |
87
|
|
|
$this->sut->setArgs(['field2', 'field3']); |
88
|
|
|
|
89
|
|
|
$errorPlaceholders = $this->sut->getErrorPlaceholders(); |
90
|
|
|
|
91
|
|
|
$this->assertEquals(['other1' => 'field2', 'other2' => 'field3'], $errorPlaceholders); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testSetArgsThrowsExceptionWithoutArgs(): void |
95
|
|
|
{ |
96
|
|
|
$this->expectException(\InvalidArgumentException::class); |
97
|
|
|
|
98
|
|
|
$this->sut->setArgs([]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testSetArgsThrowsExceptionWithNonStringArgs(): void |
102
|
|
|
{ |
103
|
|
|
$this->expectException(\InvalidArgumentException::class); |
104
|
|
|
|
105
|
|
|
$this->sut->setArgs([123]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testGetSlug(): void |
109
|
|
|
{ |
110
|
|
|
$actualResult = $this->sut->getSlug(); |
111
|
|
|
|
112
|
|
|
$this->assertSame('exactlyOne', $actualResult); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|