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