-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAssertIsATypeTest.php
242 lines (207 loc) · 9.14 KB
/
AssertIsATypeTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php declare(strict_types=1);
/**
* This file is part of DataTypeValidator, a PHP Experts, Inc., Project.
*
* Copyright © 2025 PHP Experts, Inc.
* Author: Theodore R. Smith <theodore@phpexperts.pro>
* GPG Fingerprint: 4BF8 2613 1C34 87AC D28F 2AD8 EB24 A91D D612 5690
* https://www.phpexperts.pro/
* https://github.com/phpexpertsinc/DataTypeValidator
*
* This file is licensed under the MIT License.
*/
namespace PHPExperts\DataTypeValidator\Tests;
use PHPExperts\DataTypeValidator\DataTypeValidator;
use PHPExperts\DataTypeValidator\InvalidDataTypeException;
use PHPExperts\DataTypeValidator\IsAFuzzyDataType;
use PHPExperts\DataTypeValidator\IsAStrictDataType;
use PHPUnit\Framework\TestCase;
// Dummy classes for object validation tests.
class TestDummy {}
class AnotherDummy {}
/** @testdox Extended assertIsAType Tests */
class AssertIsATypeTest extends TestCase
{
private $strict;
private $fuzzy;
protected function setUp(): void
{
parent::setUp();
$this->strict = new DataTypeValidator(new IsAStrictDataType());
$this->fuzzy = new DataTypeValidator(new IsAFuzzyDataType());
}
private function assertPassValues(DataTypeValidator $validator, array $values, string $type): void
{
foreach ($values as $value) {
try {
$validator->assertIsType($value, $type);
} catch (InvalidDataTypeException $e) {
$this->fail("Expected " . var_export($value, true) . " to be accepted as $type: " . $e->getMessage());
}
}
$this->assertTrue(true);
}
private function assertFailValues(DataTypeValidator $validator, array $values, string $type): void
{
foreach ($values as $value) {
try {
$validator->assertIsType($value, $type);
$this->fail("Expected " . var_export($value, true) . " to be rejected as $type.");
} catch (InvalidDataTypeException $e) {
$this->assertTrue(true);
}
if (is_resource($value)) {
fclose($value);
}
}
}
// === Strict Tests ===
/** @testdox has extended tests for asserting it is a strict string */
public function testAssertIsStrictString(): void
{
$values = ["hello", "", "0", "123", "true", "false", " "];
$this->assertPassValues($this->strict, $values, "string");
}
/** @testdox has extended tests for asserting it is a strict int */
public function testAssertIsStrictInt(): void
{
$values = [123, -456, 0];
$this->assertPassValues($this->strict, $values, "int");
}
/** @testdox has extended tests for asserting it is a strict bool */
public function testAssertIsStrictBool(): void
{
$values = [true, false];
$this->assertPassValues($this->strict, $values, "bool");
}
/** @testdox has extended tests for asserting it is a strict array */
public function testAssertIsStrictArray(): void
{
$values = [[], [1, 2, 3], ["a", "b"], [1 => 'a', 2 => 'b']];
$this->assertPassValues($this->strict, $values, "array");
}
/** @testdox has extended tests for asserting it is a strict specific object */
public function testAssertIsStrictSpecificObject(): void
{
$values = [new TestDummy()];
$this->assertPassValues($this->strict, $values, TestDummy::class);
}
// === Fuzzy Tests ===
/** @testdox has extended tests for asserting it is a fuzzy string */
public function testAssertIsFuzzyString(): void
{
$values = ["hello", "", "0", "123", "true", "false", " "];
$this->assertPassValues($this->fuzzy, $values, "string");
}
/** @testdox has extended tests for asserting it is a fuzzy int */
public function testAssertIsFuzzyInt(): void
{
$values = [123, -456, 0, "123", "-456", "0"];
$this->assertPassValues($this->fuzzy, $values, "int");
}
/** @testdox has extended tests for asserting it is a fuzzy bool */
public function testAssertIsFuzzyBool(): void
{
$values = [true, false, null, 0, 1, "0", "1", 0.1, 123];
$this->assertPassValues($this->fuzzy, $values, "bool");
}
/** @testdox has extended tests for asserting it is a fuzzy array */
public function testAssertIsFuzzyArray(): void
{
$values = [[], [1, 2, 3], ["a", "b"], [1 => 'a', 2 => 'b']];
$this->assertPassValues($this->fuzzy, $values, "array");
}
/** @testdox has extended tests for asserting it is a fuzzy object */
public function testAssertIsFuzzyObject(): void
{
$values = [new TestDummy()];
$this->assertPassValues($this->fuzzy, $values, 'TestDummy');
}
/** @testdox has extended tests for asserting it is a fuzzy specific object */
public function testAssertIsFuzzySpecificObject(): void
{
$values = [new TestDummy()];
$this->assertPassValues($this->fuzzy, $values, TestDummy::class);
}
// === NOT Strict Tests ===
/** @testdox has extended tests for asserting it is not a strict string */
public function testAssertIsNotStrictString(): void
{
$values = [123, 1.23, true, false, null, [], [1, 2, 3], new \stdClass(), fopen('php://memory', 'r')];
$this->assertFailValues($this->strict, $values, "string");
}
/** @testdox has extended tests for asserting it is not a strict int */
public function testAssertIsNotStrictInt(): void
{
$values = ["123", "abc", 1.23, true, false, null, [], [1, 2, 3], new \stdClass(), fopen('php://memory', 'r')];
$this->assertFailValues($this->strict, $values, "int");
}
/** @testdox has extended tests for asserting it is not a strict bool */
public function testAssertIsNotStrictBool(): void
{
$values = ["true", "false", 0, 1, null, [], [1, 2, 3], new \stdClass(), fopen('php://memory', 'r')];
$this->assertFailValues($this->strict, $values, "bool");
}
/** @testdox has extended tests for asserting it is not a strict array */
public function testAssertIsNotStrictArray(): void
{
$values = ["not an array", 123, 1.23, true, false, null, new \stdClass(), fopen('php://memory', 'r')];
$this->assertFailValues($this->strict, $values, "array");
}
/** @testdox has extended tests for asserting it is not a strict specific object */
public function testAssertIsNotStrictSpecificObject(): void
{
$values = [new AnotherDummy(), "not an object", 123, 1.23, null, [], [1, 2, 3], new \stdClass(), fopen('php://memory', 'r')];
$this->assertFailValues($this->strict, $values, TestDummy::class);
}
/** @testdox has extended tests for asserting it is not a fuzzy string */
public function testAssertIsNotFuzzyString(): void
{
$values = [123, 1.23, true, false, null, [], [1, 2, 3], new \stdClass(), fopen('php://memory', 'r')];
$this->assertFailValues($this->fuzzy, $values, "string");
}
/** @testdox has extended tests for asserting it is not a fuzzy int */
public function testAssertIsNotFuzzyInt(): void
{
$values = ["abc", 1.23, -1.23, true, false, null, [], [1, 2, 3], new \stdClass(), fopen('php://memory', 'r')];
$this->assertFailValues($this->fuzzy, $values, "int");
}
/** @testdox has extended tests for asserting it is not a fuzzy bool */
public function testAssertIsNotFuzzyBool(): void
{
$values = [new \stdClass(), fopen('php://memory', 'r')];
$this->assertFailValues($this->fuzzy, $values, "bool");
}
/** @testdox has extended tests for asserting it is not a fuzzy array */
public function testAssertIsNotFuzzyArray(): void
{
$values = ["not an array", 123, 1.23, true, false, null, new \stdClass(), fopen('php://memory', 'r')];
$this->assertFailValues($this->fuzzy, $values, "array");
}
/** @testdox has extended tests for asserting it is not a fuzzy object */
public function testAssertIsNotFuzzyObject(): void
{
$values = ["not an object", 123, 1.23, null, [], [1, 2, 3], new AnotherDummy(), fopen('php://memory', 'r')];
$this->assertFailValues($this->fuzzy, $values, 'TestDummy');
}
/** @testdox has extended tests for asserting it is not a fuzzy specific object */
public function testAssertIsNotFuzzySpecificObject(): void
{
$values = ["not an object", 123, 1.23, null, [], [1, 2, 3], new \stdClass(), fopen('php://memory', 'r')];
$this->assertFailValues($this->fuzzy, $values, TestDummy::class);
}
/** @testdox has extended tests for asserting it is a mixed type */
public function testAssertIsMixedType()
{
$values = [
123, -456, 0, "123", "-456", "0", "1.2",
123, 1.23, true, false, null, [], [1, 2, 3],
"hello", "", "0", "123", "true", "false", " ",
[], [1, 2, 3], ["a", "b"], [1 => 'a', 2 => 'b'],
new \stdClass(), new TestDummy(), new AnotherDummy(),
"not an array", 123, 1.23, true, false, null, new \stdClass(), fopen('php://memory', 'r')
];
$this->assertPassValues($this->strict, $values, 'mixed');
$this->assertPassValues($this->fuzzy, $values, 'mixed');
}
}