|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the JsonSchema package. |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace JsonSchema\Tests\Constraints; |
| 11 | + |
| 12 | +use JsonSchema\Constraints\Constraint; |
| 13 | +use JsonSchema\Constraints\Factory; |
| 14 | +use JsonSchema\SchemaStorage; |
| 15 | +use JsonSchema\Validator; |
| 16 | + |
| 17 | +class DefaultPropertiesTest extends VeryBaseTestCase |
| 18 | +{ |
| 19 | + public function getValidTests() |
| 20 | + { |
| 21 | + return array( |
| 22 | + array(// default value for entire object |
| 23 | + '', |
| 24 | + '{"default":"valueOne"}', |
| 25 | + '"valueOne"' |
| 26 | + ), |
| 27 | + array(// default value in an empty object |
| 28 | + '{}', |
| 29 | + '{"properties":{"propertyOne":{"default":"valueOne"}}}', |
| 30 | + '{"propertyOne":"valueOne"}' |
| 31 | + ), |
| 32 | + array(// default value for top-level property |
| 33 | + '{"propertyOne":"valueOne"}', |
| 34 | + '{"properties":{"propertyTwo":{"default":"valueTwo"}}}', |
| 35 | + '{"propertyOne":"valueOne","propertyTwo":"valueTwo"}' |
| 36 | + ), |
| 37 | + array(// default value for sub-property |
| 38 | + '{"propertyOne":{}}', |
| 39 | + '{"properties":{"propertyOne":{"properties":{"propertyTwo":{"default":"valueTwo"}}}}}', |
| 40 | + '{"propertyOne":{"propertyTwo":"valueTwo"}}' |
| 41 | + ), |
| 42 | + array(// default value for sub-property with sibling |
| 43 | + '{"propertyOne":{"propertyTwo":"valueTwo"}}', |
| 44 | + '{"properties":{"propertyOne":{"properties":{"propertyThree":{"default":"valueThree"}}}}}', |
| 45 | + '{"propertyOne":{"propertyTwo":"valueTwo","propertyThree":"valueThree"}}' |
| 46 | + ), |
| 47 | + array(// default value for top-level property with type check |
| 48 | + '{"propertyOne":"valueOne"}', |
| 49 | + '{"properties":{"propertyTwo":{"default":"valueTwo","type":"string"}}}', |
| 50 | + '{"propertyOne":"valueOne","propertyTwo":"valueTwo"}' |
| 51 | + ), |
| 52 | + array(// default value for top-level property with v3 required check |
| 53 | + '{"propertyOne":"valueOne"}', |
| 54 | + '{"properties":{"propertyTwo":{"default":"valueTwo","required":"true"}}}', |
| 55 | + '{"propertyOne":"valueOne","propertyTwo":"valueTwo"}' |
| 56 | + ), |
| 57 | + array(// default value for top-level property with v4 required check |
| 58 | + '{"propertyOne":"valueOne"}', |
| 59 | + '{"properties":{"propertyTwo":{"default":"valueTwo"}},"required":["propertyTwo"]}', |
| 60 | + '{"propertyOne":"valueOne","propertyTwo":"valueTwo"}' |
| 61 | + ), |
| 62 | + array(//default value for an already set property |
| 63 | + '{"propertyOne":"alreadySetValueOne"}', |
| 64 | + '{"properties":{"propertyOne":{"default":"valueOne"}}}', |
| 65 | + '{"propertyOne":"alreadySetValueOne"}' |
| 66 | + ), |
| 67 | + array(//default item value for an array |
| 68 | + '["valueOne"]', |
| 69 | + '{"type":"array","items":[{},{"type":"string","default":"valueTwo"}]}', |
| 70 | + '["valueOne","valueTwo"]' |
| 71 | + ), |
| 72 | + array(//default item value for an empty array |
| 73 | + '[]', |
| 74 | + '{"type":"array","items":[{"type":"string","default":"valueOne"}]}', |
| 75 | + '["valueOne"]' |
| 76 | + ), |
| 77 | + array(//property without a default available |
| 78 | + '{"propertyOne":"alreadySetValueOne"}', |
| 79 | + '{"properties":{"propertyOne":{"type":"string"}}}', |
| 80 | + '{"propertyOne":"alreadySetValueOne"}' |
| 81 | + ), |
| 82 | + array(// default property value is an object |
| 83 | + '{"propertyOne":"valueOne"}', |
| 84 | + '{"properties":{"propertyTwo":{"default":{}}}}', |
| 85 | + '{"propertyOne":"valueOne","propertyTwo":{}}' |
| 86 | + ), |
| 87 | + array(// default item value is an object |
| 88 | + '[]', |
| 89 | + '{"type":"array","items":[{"default":{}}]}', |
| 90 | + '[{}]' |
| 91 | + ) |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @dataProvider getValidTests |
| 97 | + */ |
| 98 | + public function testValidCases($input, $schema, $expectOutput = null, $validator = null) |
| 99 | + { |
| 100 | + if (is_string($input)) { |
| 101 | + $inputDecoded = json_decode($input); |
| 102 | + } else { |
| 103 | + $inputDecoded = $input; |
| 104 | + } |
| 105 | + |
| 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)); |
| 111 | + |
| 112 | + $this->assertTrue($validator->isValid(), print_r($validator->getErrors(), true)); |
| 113 | + |
| 114 | + if ($expectOutput !== null) { |
| 115 | + $this->assertEquals($expectOutput, json_encode($inputDecoded)); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @dataProvider getValidTests |
| 121 | + */ |
| 122 | + public function testValidCasesUsingAssoc($input, $schema, $expectOutput = null) |
| 123 | + { |
| 124 | + $input = json_decode($input, true); |
| 125 | + |
| 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)); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @dataProvider getValidTests |
| 132 | + */ |
| 133 | + public function testValidCasesUsingAssocWithoutTypeCast($input, $schema, $expectOutput = null) |
| 134 | + { |
| 135 | + $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)); |
| 138 | + } |
| 139 | + |
| 140 | + public function testNoModificationViaReferences() |
| 141 | + { |
| 142 | + $input = json_decode(''); |
| 143 | + $schema = json_decode('{"default":{"propertyOne":"valueOne"}}'); |
| 144 | + |
| 145 | + $validator = new Validator(); |
| 146 | + $validator->validate($input, $schema, Constraint::CHECK_MODE_TYPE_CAST | Constraint::CHECK_MODE_APPLY_DEFAULTS); |
| 147 | + |
| 148 | + $this->assertEquals('{"propertyOne":"valueOne"}', json_encode($input)); |
| 149 | + |
| 150 | + $input->propertyOne = 'valueTwo'; |
| 151 | + $this->assertEquals('valueOne', $schema->default->propertyOne); |
| 152 | + } |
| 153 | +} |
0 commit comments