This repository was archived by the owner on Dec 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructTest.php
More file actions
102 lines (96 loc) · 3.18 KB
/
StructTest.php
File metadata and controls
102 lines (96 loc) · 3.18 KB
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
<?php
namespace SK\StructArray\Test\Unit;
use PHPUnit\Framework\TestCase;
use SK\StructArray\Exception\StructValidationException;
use SK\StructArray\Property\Type;
use SK\StructArray\Struct;
use function SK\StructArray\validate;
class StructTest extends TestCase
{
/**
* @dataProvider dataProviderForTest
*/
public function test($data, $struct, $expectedException)
{
if ($expectedException) {
$this->expectException(StructValidationException::class);
}
$this->assertTrue(validate($data, $struct));
}
public function dataProviderForTest(): array
{
$name = 'Test';
return [
[
['name' => 'toasty',],
Struct::of($name, ['name' => 'invalid validator'], true),
true,
],
[
['name' => 10],
Struct::of($name, ['name' => 'is_string'], true),
true,
],
[
[],
Struct::of($name, ['name' => 'is_string'], true),
true,
],
[
['name' => 'toasty', 'age' => 10],
Struct::of($name, ['name' => 'is_string'], true),
true,
],
[
[
'id' => '123',
'type' => 'theatre',
'date' => new \DateTime(),
'price' => [
'value' => 20.5,
'currency' => 'GBP',
],
'tickets' => ['General', 10],
'onSale' => true,
'artist' => 'some guy',
],
Struct::of($name, [
'id' => Type::allOf('is_string', 'is_numeric'),
'type' => 'is_string',
'date' => Type::anyOf(Type::classOf(\DateTime::class), 'is_null'),
'price' => Struct::of('Price', [
'value' => 'is_float',
'currency' => 'is_string'
]),
'tickets' => Type::arrayOf(Type::not('is_null')),
], false),
null
],
[
[
'id' => '123',
'type' => 'theatre',
'date' => new \DateTime(),
'price' => [
'value' => 20.5,
'currency' => 'GBP',
],
'tickets' => ['General', 10],
'onSale' => true,
'artist' => 'some guy',
],
[
'id' => Type::allOf('is_string', 'is_numeric'),
'type' => 'is_string',
'date' => Type::anyOf(Type::classOf(\DateTime::class), 'is_null'),
'price' => Struct::of('Price', [
'value' => 'is_float',
'currency' => 'is_string'
]),
'tickets' => Type::arrayOf(Type::not('is_null')),
],
null
],
];
}
}