Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / utils / AvroValidatorTest.php
blob7559e22499b17a97dab54c94418ae24d2f7a29dd
1 <?php
2 /**
3 * Tests for IP validity functions.
5 * Ported from /t/inc/IP.t by avar.
7 * @group IP
8 * @todo Test methods in this call should be split into a method and a
9 * dataprovider.
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' );
17 parent::setUp();
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 ],
32 ] ) );
33 $enumSchema = AvroSchema::parse( json_encode( [
34 'type' => 'record',
35 'name' => 'ut',
36 'fields' => [
37 [ 'name' => 'count', 'type' => [ 'int', 'null' ] ],
39 ] ) );
41 return [
43 'No errors with a simple string serialization',
44 $stringSchema, 'foobar', [],
48 'Cannot serialize integer into string',
49 $stringSchema, 5, 'Expected string, but recieved integer',
53 'Cannot serialize array into string',
54 $stringSchema, [], 'Expected string, but recieved array',
58 'allows and ignores extra fields',
59 $recordSchema, [ 'id' => 4, 'foo' => 'bar' ], [],
63 'detects missing fields',
64 $recordSchema, [], [ 'id' => 'Missing expected field' ],
68 'handles first element in enum',
69 $enumSchema, [ 'count' => 4 ], [],
73 'handles second element in enum',
74 $enumSchema, [ 'count' => null ], [],
78 'rejects element not in union',
79 $enumSchema, [ 'count' => 'invalid' ], [ 'count' => [
80 'Expected any one of these to be true',
82 'Expected integer, but recieved string',
83 'Expected null, but recieved string',
85 ] ]
88 'Empty array is accepted',
89 $stringArraySchema, [], []
92 'correct array element accepted',
93 $stringArraySchema, [ 'fizzbuzz' ], []
96 'incorrect array element rejected',
97 $stringArraySchema, [ '12', 34 ], [ 'Expected string, but recieved integer' ]
103 * @dataProvider getErrorsProvider
105 public function testGetErrors( $message, $schema, $datum, $expected ) {
106 $this->assertEquals(
107 $expected,
108 AvroValidator::getErrors( $schema, $datum ),
109 $message