build: Updating mediawiki/mediawiki-codesniffer to 16.0.0
[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 /**
13 * @covers AvroValidator
14 */
15 class AvroValidatorTest extends PHPUnit\Framework\TestCase {
16
17 use MediaWikiCoversValidator;
18
19 public function setUp() {
20 if ( !class_exists( 'AvroSchema' ) ) {
21 $this->markTestSkipped( 'Avro is required to run the AvroValidatorTest' );
22 }
23 parent::setUp();
24 }
25
26 public function getErrorsProvider() {
27 $stringSchema = AvroSchema::parse( json_encode( [ 'type' => 'string' ] ) );
28 $stringArraySchema = AvroSchema::parse( json_encode( [
29 'type' => 'array',
30 'items' => 'string',
31 ] ) );
32 $recordSchema = AvroSchema::parse( json_encode( [
33 'type' => 'record',
34 'name' => 'ut',
35 'fields' => [
36 [ 'name' => 'id', 'type' => 'int', 'required' => true ],
37 ],
38 ] ) );
39 $enumSchema = AvroSchema::parse( json_encode( [
40 'type' => 'record',
41 'name' => 'ut',
42 'fields' => [
43 [ 'name' => 'count', 'type' => [ 'int', 'null' ] ],
44 ],
45 ] ) );
46
47 return [
48 [
49 'No errors with a simple string serialization',
50 $stringSchema, 'foobar', [],
51 ],
52
53 [
54 'Cannot serialize integer into string',
55 $stringSchema, 5, 'Expected string, but recieved integer',
56 ],
57
58 [
59 'Cannot serialize array into string',
60 $stringSchema, [], 'Expected string, but recieved array',
61 ],
62
63 [
64 'allows and ignores extra fields',
65 $recordSchema, [ 'id' => 4, 'foo' => 'bar' ], [],
66 ],
67
68 [
69 'detects missing fields',
70 $recordSchema, [], [ 'id' => 'Missing expected field' ],
71 ],
72
73 [
74 'handles first element in enum',
75 $enumSchema, [ 'count' => 4 ], [],
76 ],
77
78 [
79 'handles second element in enum',
80 $enumSchema, [ 'count' => null ], [],
81 ],
82
83 [
84 'rejects element not in union',
85 $enumSchema, [ 'count' => 'invalid' ], [ 'count' => [
86 'Expected any one of these to be true',
87 [
88 'Expected integer, but recieved string',
89 'Expected null, but recieved string',
90 ]
91 ] ]
92 ],
93 [
94 'Empty array is accepted',
95 $stringArraySchema, [], []
96 ],
97 [
98 'correct array element accepted',
99 $stringArraySchema, [ 'fizzbuzz' ], []
100 ],
101 [
102 'incorrect array element rejected',
103 $stringArraySchema, [ '12', 34 ], [ 'Expected string, but recieved integer' ]
104 ],
105 ];
106 }
107
108 /**
109 * @dataProvider getErrorsProvider
110 */
111 public function testGetErrors( $message, $schema, $datum, $expected ) {
112 $this->assertEquals(
113 $expected,
114 AvroValidator::getErrors( $schema, $datum ),
115 $message
116 );
117 }
118 }