Merge "Use wikimedia/cldr-plural-rule-parser"
[lhc/web/wiklou.git] / tests / phpunit / includes / utils / AvroValidatorTest.php
1 <?php
2 /**
3 * Tests for IP validity functions.
4 *
5 * Ported from /t/inc/IP.t by avar.
6 *
7 * @group IP
8 * @todo Test methods in this call should be split into a method and a
9 * dataprovider.
10 */
11
12 class AvroValidatorTest extends PHPUnit_Framework_TestCase {
13 public function setUp() {
14 if ( !class_exists( 'AvroSchema' ) ) {
15 $this->markTestSkipped( 'Avro is required to run the AvroValidatorTest' );
16 }
17 parent::setUp();
18 }
19
20 public function getErrorsProvider() {
21 $stringSchema = AvroSchema::parse( json_encode( array( 'type' => 'string' ) ) );
22 $stringArraySchema = AvroSchema::parse( json_encode( array(
23 'type' => 'array',
24 'items' => 'string',
25 ) ) );
26 $recordSchema = AvroSchema::parse( json_encode( array(
27 'type' => 'record',
28 'name' => 'ut',
29 'fields' => array(
30 array( 'name' => 'id', 'type' => 'int', 'required' => true ),
31 ),
32 ) ) );
33 $enumSchema = AvroSchema::parse( json_encode( array(
34 'type' => 'record',
35 'name' => 'ut',
36 'fields' => array(
37 array( 'name' => 'count', 'type' => array( 'int', 'null' ) ),
38 ),
39 ) ) );
40
41 return array(
42 array(
43 'No errors with a simple string serialization',
44 $stringSchema, 'foobar', array(),
45 ),
46
47 array(
48 'Cannot serialize integer into string',
49 $stringSchema, 5, 'Expected string, but recieved integer',
50 ),
51
52 array(
53 'Cannot serialize array into string',
54 $stringSchema, array(), 'Expected string, but recieved array',
55 ),
56
57 array(
58 'allows and ignores extra fields',
59 $recordSchema, array( 'id' => 4, 'foo' => 'bar' ), array(),
60 ),
61
62 array(
63 'detects missing fields',
64 $recordSchema, array(), array( 'id' => 'Missing expected field' ),
65 ),
66
67 array(
68 'handles first element in enum',
69 $enumSchema, array( 'count' => 4 ), array(),
70 ),
71
72 array(
73 'handles second element in enum',
74 $enumSchema, array( 'count' => null ), array(),
75 ),
76
77 array(
78 'rejects element not in union',
79 $enumSchema, array( 'count' => 'invalid' ), array( 'count' => array(
80 'Expected any one of these to be true',
81 array(
82 'Expected integer, but recieved string',
83 'Expected null, but recieved string',
84 )
85 ) )
86 ),
87 array(
88 'Empty array is accepted',
89 $stringArraySchema, array(), array()
90 ),
91 array(
92 'correct array element accepted',
93 $stringArraySchema, array( 'fizzbuzz' ), array()
94 ),
95 array(
96 'incorrect array element rejected',
97 $stringArraySchema, array( '12', 34 ), array( 'Expected string, but recieved integer' )
98 ),
99 );
100 }
101
102 /**
103 * @dataProvider getErrorsProvider
104 */
105 public function testGetErrors( $message, $schema, $datum, $expected ) {
106 $this->assertEquals(
107 $expected,
108 AvroValidator::getErrors( $schema, $datum ),
109 $message
110 );
111 }
112 }