|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JsonSchema\Tests; |
| 4 | + |
| 5 | +use JsonSchema\Constraints\Constraint; |
| 6 | +use JsonSchema\Validator; |
| 7 | + |
| 8 | +class ValidatorTest extends \PHPUnit_Framework_TestCase |
| 9 | +{ |
| 10 | + public function testValidateWithAssocSchema() |
| 11 | + { |
| 12 | + $schema = json_decode('{"properties":{"propertyOne":{"type":"array","items":[{"type":"string"}]}}}', true); |
| 13 | + $data = json_decode('{"propertyOne":[42]}', true); |
| 14 | + |
| 15 | + $validator = new Validator(); |
| 16 | + $validator->validate($data, $schema); |
| 17 | + |
| 18 | + $this->assertFalse($validator->isValid(), 'Validation succeeded, but should have failed.'); |
| 19 | + } |
| 20 | + |
| 21 | + public function testBadAssocSchemaInput() |
| 22 | + { |
| 23 | + if (version_compare(phpversion(), '5.5.0', '<')) { |
| 24 | + $this->markTestSkipped('PHP versions < 5.5.0 trigger an error on json_encode issues'); |
| 25 | + } |
| 26 | + if (defined('HHVM_VERSION')) { |
| 27 | + $this->markTestSkipped('HHVM has no problem with encoding resources'); |
| 28 | + } |
| 29 | + $schema = array('propertyOne' => fopen('php://stdout', 'w')); |
| 30 | + $data = json_decode('{"propertyOne":[42]}', true); |
| 31 | + |
| 32 | + $validator = new Validator(); |
| 33 | + |
| 34 | + $this->setExpectedException('\JsonSchema\Exception\InvalidArgumentException'); |
| 35 | + $validator->validate($data, $schema); |
| 36 | + } |
| 37 | + |
| 38 | + public function testCheck() |
| 39 | + { |
| 40 | + $schema = json_decode('{"type":"string"}'); |
| 41 | + $data = json_decode('42'); |
| 42 | + |
| 43 | + $validator = new Validator(); |
| 44 | + $validator->check($data, $schema); |
| 45 | + |
| 46 | + $this->assertFalse($validator->isValid(), 'Validation succeeded, but should have failed.'); |
| 47 | + } |
| 48 | + |
| 49 | + public function testCoerce() |
| 50 | + { |
| 51 | + $schema = json_decode('{"type":"integer"}'); |
| 52 | + $data = json_decode('"42"'); |
| 53 | + |
| 54 | + $validator = new Validator(); |
| 55 | + $validator->coerce($data, $schema); |
| 56 | + |
| 57 | + $this->assertTrue($validator->isValid(), 'Validation failed, but should have succeeded.'); |
| 58 | + } |
| 59 | +} |
0 commit comments