]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/phpunit/includes/utils/AvroValidatorTest.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.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( [ 'type' => 'string' ] ) );
22                 $stringArraySchema = AvroSchema::parse( json_encode( [
23                         'type' => 'array',
24                         'items' => 'string',
25                 ] ) );
26                 $recordSchema = AvroSchema::parse( json_encode( [
27                         'type' => 'record',
28                         'name' => 'ut',
29                         'fields' => [
30                                 [ 'name' => 'id', 'type' => 'int', 'required' => true ],
31                         ],
32                 ] ) );
33                 $enumSchema = AvroSchema::parse( json_encode( [
34                         'type' => 'record',
35                         'name' => 'ut',
36                         'fields' => [
37                                 [ 'name' => 'count', 'type' => [ 'int', 'null' ] ],
38                         ],
39                 ] ) );
40
41                 return [
42                         [
43                                 'No errors with a simple string serialization',
44                                 $stringSchema, 'foobar', [],
45                         ],
46
47                         [
48                                 'Cannot serialize integer into string',
49                                 $stringSchema, 5, 'Expected string, but recieved integer',
50                         ],
51
52                         [
53                                 'Cannot serialize array into string',
54                                 $stringSchema, [], 'Expected string, but recieved array',
55                         ],
56
57                         [
58                                 'allows and ignores extra fields',
59                                 $recordSchema, [ 'id' => 4, 'foo' => 'bar' ], [],
60                         ],
61
62                         [
63                                 'detects missing fields',
64                                 $recordSchema, [], [ 'id' => 'Missing expected field' ],
65                         ],
66
67                         [
68                                 'handles first element in enum',
69                                 $enumSchema, [ 'count' => 4 ], [],
70                         ],
71
72                         [
73                                 'handles second element in enum',
74                                 $enumSchema, [ 'count' => null ], [],
75                         ],
76
77                         [
78                                 'rejects element not in union',
79                                 $enumSchema, [ 'count' => 'invalid' ], [ 'count' => [
80                                         'Expected any one of these to be true',
81                                         [
82                                                 'Expected integer, but recieved string',
83                                                 'Expected null, but recieved string',
84                                         ]
85                                 ] ]
86                         ],
87                         [
88                                 'Empty array is accepted',
89                                 $stringArraySchema, [], []
90                         ],
91                         [
92                                 'correct array element accepted',
93                                 $stringArraySchema, [ 'fizzbuzz' ], []
94                         ],
95                         [
96                                 'incorrect array element rejected',
97                                 $stringArraySchema, [ '12', 34 ], [ '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 }