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