3 * Tests for IP validity functions.
5 * Ported from /t/inc/IP.t by avar.
8 * @todo Test methods in this call should be split into a method and a
13 * @covers AvroValidator
15 class AvroValidatorTest
extends PHPUnit_Framework_TestCase
{
16 public function setUp() {
17 if ( !class_exists( 'AvroSchema' ) ) {
18 $this->markTestSkipped( 'Avro is required to run the AvroValidatorTest' );
23 public function getErrorsProvider() {
24 $stringSchema = AvroSchema
::parse( json_encode( [ 'type' => 'string' ] ) );
25 $stringArraySchema = AvroSchema
::parse( json_encode( [
29 $recordSchema = AvroSchema
::parse( json_encode( [
33 [ 'name' => 'id', 'type' => 'int', 'required' => true ],
36 $enumSchema = AvroSchema
::parse( json_encode( [
40 [ 'name' => 'count', 'type' => [ 'int', 'null' ] ],
46 'No errors with a simple string serialization',
47 $stringSchema, 'foobar', [],
51 'Cannot serialize integer into string',
52 $stringSchema, 5, 'Expected string, but recieved integer',
56 'Cannot serialize array into string',
57 $stringSchema, [], 'Expected string, but recieved array',
61 'allows and ignores extra fields',
62 $recordSchema, [ 'id' => 4, 'foo' => 'bar' ], [],
66 'detects missing fields',
67 $recordSchema, [], [ 'id' => 'Missing expected field' ],
71 'handles first element in enum',
72 $enumSchema, [ 'count' => 4 ], [],
76 'handles second element in enum',
77 $enumSchema, [ 'count' => null ], [],
81 'rejects element not in union',
82 $enumSchema, [ 'count' => 'invalid' ], [ 'count' => [
83 'Expected any one of these to be true',
85 'Expected integer, but recieved string',
86 'Expected null, but recieved string',
91 'Empty array is accepted',
92 $stringArraySchema, [], []
95 'correct array element accepted',
96 $stringArraySchema, [ 'fizzbuzz' ], []
99 'incorrect array element rejected',
100 $stringArraySchema, [ '12', 34 ], [ 'Expected string, but recieved integer' ]
106 * @dataProvider getErrorsProvider
108 public function testGetErrors( $message, $schema, $datum, $expected ) {
111 AvroValidator
::getErrors( $schema, $datum ),