|
| 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 | +use JsonSchema\SchemaStorage; |
| 12 | +use JsonSchema\Validator; |
| 13 | +use JsonSchema\Constraints\Constraint; |
| 14 | +use JsonSchema\Constraints\Factory; |
| 15 | + |
| 16 | +class DefaultPropertiesTest extends VeryBaseTestCase |
| 17 | +{ |
| 18 | + public function getValidTests() |
| 19 | + { |
| 20 | + return array( |
| 21 | + array(// default value for entire object |
| 22 | + '', |
| 23 | + '{"default":"valueOne"}', |
| 24 | + '"valueOne"' |
| 25 | + ), |
| 26 | + array(// default value in an empty object |
| 27 | + '{}', |
| 28 | + '{"properties":{"propertyOne":{"default":"valueOne"}}}', |
| 29 | + '{"propertyOne":"valueOne"}' |
| 30 | + ), |
| 31 | + array(// default value for top-level property |
| 32 | + '{"propertyOne":"valueOne"}', |
| 33 | + '{"properties":{"propertyTwo":{"default":"valueTwo"}}}', |
| 34 | + '{"propertyOne":"valueOne","propertyTwo":"valueTwo"}' |
| 35 | + ), |
| 36 | + array(// default value for sub-property |
| 37 | + '{"propertyOne":{}}', |
| 38 | + '{"properties":{"propertyOne":{"properties":{"propertyTwo":{"default":"valueTwo"}}}}}', |
| 39 | + '{"propertyOne":{"propertyTwo":"valueTwo"}}' |
| 40 | + ), |
| 41 | + array(// default value for sub-property with sibling |
| 42 | + '{"propertyOne":{"propertyTwo":"valueTwo"}}', |
| 43 | + '{"properties":{"propertyOne":{"properties":{"propertyThree":{"default":"valueThree"}}}}}', |
| 44 | + '{"propertyOne":{"propertyTwo":"valueTwo","propertyThree":"valueThree"}}' |
| 45 | + ), |
| 46 | + array(// default value for top-level property with type check |
| 47 | + '{"propertyOne":"valueOne"}', |
| 48 | + '{"properties":{"propertyTwo":{"default":"valueTwo","type":"string"}}}', |
| 49 | + '{"propertyOne":"valueOne","propertyTwo":"valueTwo"}' |
| 50 | + ), |
| 51 | + array(// default value for top-level property with v3 required check |
| 52 | + '{"propertyOne":"valueOne"}', |
| 53 | + '{"properties":{"propertyTwo":{"default":"valueTwo","required":"true"}}}', |
| 54 | + '{"propertyOne":"valueOne","propertyTwo":"valueTwo"}' |
| 55 | + ), |
| 56 | + array(// default value for top-level property with v4 required check |
| 57 | + '{"propertyOne":"valueOne"}', |
| 58 | + '{"properties":{"propertyTwo":{"default":"valueTwo"}},"required":["propertyTwo"]}', |
| 59 | + '{"propertyOne":"valueOne","propertyTwo":"valueTwo"}' |
| 60 | + ), |
| 61 | + array(//default value for an already set property |
| 62 | + '{"propertyOne":"alreadySetValueOne"}', |
| 63 | + '{"properties":{"propertyOne":{"default":"valueOne"}}}', |
| 64 | + '{"propertyOne":"alreadySetValueOne"}' |
| 65 | + ), |
| 66 | + array(//default value is required |
| 67 | + '{"propertyOne":"valueOne"}', |
| 68 | + '{"properties":{"propertyTwo":{"default":"valueTwo","required":true}}}', |
| 69 | + '{"propertyOne":"valueOne","propertyTwo":"valueTwo"}' |
| 70 | + ), |
| 71 | + array(//default item value for an array |
| 72 | + '["valueOne"]', |
| 73 | + '{"type":"array","items":[{},{"type":"string","default":"valueTwo"}]}', |
| 74 | + '["valueOne","valueTwo"]' |
| 75 | + ), |
| 76 | + array(//default item value for an empty array |
| 77 | + '[]', |
| 78 | + '{"type":"array","items":[{"type":"string","default":"valueOne"}]}', |
| 79 | + '["valueOne"]' |
| 80 | + ), |
| 81 | + array(//property without a default available |
| 82 | + '{"propertyOne":"alreadySetValueOne"}', |
| 83 | + '{"properties":{"propertyOne":{"type":"string"}}}', |
| 84 | + '{"propertyOne":"alreadySetValueOne"}' |
| 85 | + ) |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @dataProvider getValidTests |
| 91 | + */ |
| 92 | + public function testValidCases($input, $schema, $expectOutput = null) |
| 93 | + { |
| 94 | + if (is_string($input)) { |
| 95 | + $assoc = false; |
| 96 | + $inputDecoded = json_decode($input); |
| 97 | + } else { |
| 98 | + $assoc = true; |
| 99 | + $inputDecoded = $input; |
| 100 | + } |
| 101 | + |
| 102 | + $validator = new Validator(); |
| 103 | + $validator->coerceDefault($inputDecoded, json_decode($schema)); |
| 104 | + |
| 105 | + $this->assertTrue($validator->isValid(), print_r($validator->getErrors(), true)); |
| 106 | + |
| 107 | + if ($expectOutput !== null) { |
| 108 | + $this->assertEquals($expectOutput, json_encode($inputDecoded)); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @dataProvider getValidTests |
| 114 | + */ |
| 115 | + public function testValidCasesUsingAssoc($input, $schema, $expectOutput = null) |
| 116 | + { |
| 117 | + $input = json_decode($input, true); |
| 118 | + self::testValidCases($input, $schema, $expectOutput); |
| 119 | + } |
| 120 | + |
| 121 | + public function testDontApplyDefaults() |
| 122 | + { |
| 123 | + $f = new Factory(); |
| 124 | + $f->setApplyDefaults(false); |
| 125 | + |
| 126 | + $this->assertEquals(0, $f->getCheckMode() & Constraint::CHECK_MODE_APPLY_DEFAULTS); |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments