Merge "Fixes for RedisBagOStuff when using twemproxy"
[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 $recordSchema = AvroSchema::parse( json_encode( array(
23 'type' => 'record',
24 'name' => 'ut',
25 'fields' => array(
26 array( 'name' => 'id', 'type' => 'int', 'required' => true ),
27 ),
28 ) ) );
29 $enumSchema = AvroSchema::parse( json_encode( array(
30 'type' => 'record',
31 'name' => 'ut',
32 'fields' => array(
33 array( 'name' => 'count', 'type' => array( 'int', 'null' ) ),
34 ),
35 ) ) );
36
37 return array(
38 array(
39 'No errors with a simple string serialization',
40 $stringSchema, 'foobar', array(),
41 ),
42
43 array(
44 'Cannot serialize integer into string',
45 $stringSchema, 5, 'Expected string, but recieved integer',
46 ),
47
48 array(
49 'Cannot serialize array into string',
50 $stringSchema, array(), 'Expected string, but recieved array',
51 ),
52
53 array(
54 'allows and ignores extra fields',
55 $recordSchema, array( 'id' => 4, 'foo' => 'bar' ), array(),
56 ),
57
58 array(
59 'detects missing fields',
60 $recordSchema, array(), array( 'id' => 'Missing expected field' ),
61 ),
62
63 array(
64 'handles first element in enum',
65 $enumSchema, array( 'count' => 4 ), array(),
66 ),
67
68 array(
69 'handles second element in enum',
70 $enumSchema, array( 'count' => null ), array(),
71 ),
72
73 array(
74 'rejects element not in union',
75 $enumSchema, array( 'count' => 'invalid' ), array( 'count' => array(
76 'Expected any one of these to be true',
77 array(
78 'Expected integer, but recieved string',
79 'Expected null, but recieved string',
80 )
81 ) )
82 ),
83 );
84 }
85
86 /**
87 * @dataProvider getErrorsProvider
88 */
89 public function testGetErrors( $message, $schema, $datum, $expected ) {
90 $this->assertEquals(
91 $expected,
92 AvroValidator::getErrors( $schema, $datum ),
93 $message
94 );
95 }
96 }