@@ -19,95 +19,116 @@ class DefaultPropertiesTest extends VeryBaseTestCase
19
19
public function getValidTests ()
20
20
{
21
21
return array (
22
- array (// default value for entire object
22
+ array (// #0 default value for entire object
23
23
'' ,
24
24
'{"default":"valueOne"} ' ,
25
25
'"valueOne" '
26
26
),
27
- array (// default value in an empty object
27
+ array (// #1 default value in an empty object
28
28
'{} ' ,
29
29
'{"properties":{"propertyOne":{"default":"valueOne"}}} ' ,
30
30
'{"propertyOne":"valueOne"} '
31
31
),
32
- array (// default value for top-level property
32
+ array (// #2 default value for top-level property
33
33
'{"propertyOne":"valueOne"} ' ,
34
34
'{"properties":{"propertyTwo":{"default":"valueTwo"}}} ' ,
35
35
'{"propertyOne":"valueOne","propertyTwo":"valueTwo"} '
36
36
),
37
- array (// default value for sub-property
37
+ array (// #3 default value for sub-property
38
38
'{"propertyOne":{}} ' ,
39
39
'{"properties":{"propertyOne":{"properties":{"propertyTwo":{"default":"valueTwo"}}}}} ' ,
40
40
'{"propertyOne":{"propertyTwo":"valueTwo"}} '
41
41
),
42
- array (// default value for sub-property with sibling
42
+ array (// #4 default value for sub-property with sibling
43
43
'{"propertyOne":{"propertyTwo":"valueTwo"}} ' ,
44
44
'{"properties":{"propertyOne":{"properties":{"propertyThree":{"default":"valueThree"}}}}} ' ,
45
45
'{"propertyOne":{"propertyTwo":"valueTwo","propertyThree":"valueThree"}} '
46
46
),
47
- array (// default value for top-level property with type check
47
+ array (// #5 default value for top-level property with type check
48
48
'{"propertyOne":"valueOne"} ' ,
49
49
'{"properties":{"propertyTwo":{"default":"valueTwo","type":"string"}}} ' ,
50
50
'{"propertyOne":"valueOne","propertyTwo":"valueTwo"} '
51
51
),
52
- array (// default value for top-level property with v3 required check
52
+ array (// #6 default value for top-level property with v3 required check
53
53
'{"propertyOne":"valueOne"} ' ,
54
54
'{"properties":{"propertyTwo":{"default":"valueTwo","required":"true"}}} ' ,
55
55
'{"propertyOne":"valueOne","propertyTwo":"valueTwo"} '
56
56
),
57
- array (// default value for top-level property with v4 required check
57
+ array (// #7 default value for top-level property with v4 required check
58
58
'{"propertyOne":"valueOne"} ' ,
59
59
'{"properties":{"propertyTwo":{"default":"valueTwo"}},"required":["propertyTwo"]} ' ,
60
60
'{"propertyOne":"valueOne","propertyTwo":"valueTwo"} '
61
61
),
62
- array (//default value for an already set property
62
+ array (// #8 default value for an already set property
63
63
'{"propertyOne":"alreadySetValueOne"} ' ,
64
64
'{"properties":{"propertyOne":{"default":"valueOne"}}} ' ,
65
65
'{"propertyOne":"alreadySetValueOne"} '
66
66
),
67
- array (//default item value for an array
67
+ array (// #9 default item value for an array
68
68
'["valueOne"] ' ,
69
69
'{"type":"array","items":[{},{"type":"string","default":"valueTwo"}]} ' ,
70
70
'["valueOne","valueTwo"] '
71
71
),
72
- array (//default item value for an empty array
72
+ array (// #10 default item value for an empty array
73
73
'[] ' ,
74
74
'{"type":"array","items":[{"type":"string","default":"valueOne"}]} ' ,
75
75
'["valueOne"] '
76
76
),
77
- array (//property without a default available
77
+ array (// #11 property without a default available
78
78
'{"propertyOne":"alreadySetValueOne"} ' ,
79
79
'{"properties":{"propertyOne":{"type":"string"}}} ' ,
80
80
'{"propertyOne":"alreadySetValueOne"} '
81
81
),
82
- array (// default property value is an object
82
+ array (// #12 default property value is an object
83
83
'{"propertyOne":"valueOne"} ' ,
84
84
'{"properties":{"propertyTwo":{"default":{}}}} ' ,
85
85
'{"propertyOne":"valueOne","propertyTwo":{}} '
86
86
),
87
- array (// default item value is an object
87
+ array (// #13 default item value is an object
88
88
'[] ' ,
89
89
'{"type":"array","items":[{"default":{}}]} ' ,
90
90
'[{}] '
91
+ ),
92
+ array (// #14 only set required values (draft-04)
93
+ '{} ' ,
94
+ '{
95
+ "properties": {
96
+ "propertyOne": {"default": "valueOne"},
97
+ "propertyTwo": {"default": "valueTwo"}
98
+ },
99
+ "required": ["propertyTwo"]
100
+ } ' ,
101
+ '{"propertyTwo":"valueTwo"} ' ,
102
+ Constraint::CHECK_MODE_ONLY_REQUIRED_DEFAULTS
103
+ ),
104
+ array (// #15 only set required values (draft-03)
105
+ '{} ' ,
106
+ '{
107
+ "properties": {
108
+ "propertyOne": {"default": "valueOne"},
109
+ "propertyTwo": {"default": "valueTwo", "required": true}
110
+ }
111
+ } ' ,
112
+ '{"propertyTwo":"valueTwo"} ' ,
113
+ Constraint::CHECK_MODE_ONLY_REQUIRED_DEFAULTS
91
114
)
92
115
);
93
116
}
94
117
95
118
/**
96
119
* @dataProvider getValidTests
97
120
*/
98
- public function testValidCases ($ input , $ schema , $ expectOutput = null , $ validator = null )
121
+ public function testValidCases ($ input , $ schema , $ expectOutput = null , $ checkMode = 0 )
99
122
{
100
123
if (is_string ($ input )) {
101
124
$ inputDecoded = json_decode ($ input );
102
125
} else {
103
126
$ inputDecoded = $ input ;
104
127
}
105
128
106
- if ($ validator === null ) {
107
- $ factory = new Factory (null , null , Constraint::CHECK_MODE_APPLY_DEFAULTS );
108
- $ validator = new Validator ($ factory );
109
- }
110
- $ validator ->validate ($ inputDecoded , json_decode ($ schema ));
129
+ $ checkMode |= Constraint::CHECK_MODE_APPLY_DEFAULTS ;
130
+ $ validator = new Validator ();
131
+ $ validator ->validate ($ inputDecoded , json_decode ($ schema ), $ checkMode );
111
132
112
133
$ this ->assertTrue ($ validator ->isValid (), print_r ($ validator ->getErrors (), true ));
113
134
@@ -119,22 +140,22 @@ public function testValidCases($input, $schema, $expectOutput = null, $validator
119
140
/**
120
141
* @dataProvider getValidTests
121
142
*/
122
- public function testValidCasesUsingAssoc ($ input , $ schema , $ expectOutput = null )
143
+ public function testValidCasesUsingAssoc ($ input , $ schema , $ expectOutput = null , $ checkMode = 0 )
123
144
{
124
145
$ input = json_decode ($ input , true );
125
146
126
- $ factory = new Factory ( null , null , Constraint::CHECK_MODE_TYPE_CAST | Constraint:: CHECK_MODE_APPLY_DEFAULTS ) ;
127
- self ::testValidCases ($ input , $ schema , $ expectOutput , new Validator ( $ factory ) );
147
+ $ checkMode |= Constraint::CHECK_MODE_TYPE_CAST ;
148
+ self ::testValidCases ($ input , $ schema , $ expectOutput , $ checkMode );
128
149
}
129
150
130
151
/**
131
152
* @dataProvider getValidTests
132
153
*/
133
- public function testValidCasesUsingAssocWithoutTypeCast ($ input , $ schema , $ expectOutput = null )
154
+ public function testValidCasesUsingAssocWithoutTypeCast ($ input , $ schema , $ expectOutput = null , $ checkMode = 0 )
134
155
{
135
156
$ input = json_decode ($ input , true );
136
- $ factory = new Factory ( null , null , Constraint:: CHECK_MODE_APPLY_DEFAULTS );
137
- self ::testValidCases ($ input , $ schema , $ expectOutput , new Validator ( $ factory ) );
157
+
158
+ self ::testValidCases ($ input , $ schema , $ expectOutput , $ checkMode );
138
159
}
139
160
140
161
public function testNoModificationViaReferences ()
0 commit comments