wiki.techinc.nl/tests/phpunit/includes/utils/AvroValidatorTest.php
Erik Bernhardson 91aebb6072 Remove $this reference in static method
Adds a couple tests to demonstrate the problem and fixes
it.

Change-Id: Ib15088e83ad333fb126446fad86f97ae12ff6e74
2015-09-23 21:08:36 +00:00

112 lines
2.7 KiB
PHP

<?php
/**
* Tests for IP validity functions.
*
* Ported from /t/inc/IP.t by avar.
*
* @group IP
* @todo Test methods in this call should be split into a method and a
* dataprovider.
*/
class AvroValidatorTest extends PHPUnit_Framework_TestCase {
public function setUp() {
if ( !class_exists( 'AvroSchema' ) ) {
$this->markTestSkipped( 'Avro is required to run the AvroValidatorTest' );
}
parent::setUp();
}
public function getErrorsProvider() {
$stringSchema = AvroSchema::parse( json_encode( array( 'type' => 'string' ) ) );
$stringArraySchema = AvroSchema::parse( json_encode( array(
'type' => 'array',
'items' => 'string',
) ) );
$recordSchema = AvroSchema::parse( json_encode( array(
'type' => 'record',
'name' => 'ut',
'fields' => array(
array( 'name' => 'id', 'type' => 'int', 'required' => true ),
),
) ) );
$enumSchema = AvroSchema::parse( json_encode( array(
'type' => 'record',
'name' => 'ut',
'fields' => array(
array( 'name' => 'count', 'type' => array( 'int', 'null' ) ),
),
) ) );
return array(
array(
'No errors with a simple string serialization',
$stringSchema, 'foobar', array(),
),
array(
'Cannot serialize integer into string',
$stringSchema, 5, 'Expected string, but recieved integer',
),
array(
'Cannot serialize array into string',
$stringSchema, array(), 'Expected string, but recieved array',
),
array(
'allows and ignores extra fields',
$recordSchema, array( 'id' => 4, 'foo' => 'bar' ), array(),
),
array(
'detects missing fields',
$recordSchema, array(), array( 'id' => 'Missing expected field' ),
),
array(
'handles first element in enum',
$enumSchema, array( 'count' => 4 ), array(),
),
array(
'handles second element in enum',
$enumSchema, array( 'count' => null ), array(),
),
array(
'rejects element not in union',
$enumSchema, array( 'count' => 'invalid' ), array( 'count' => array(
'Expected any one of these to be true',
array(
'Expected integer, but recieved string',
'Expected null, but recieved string',
)
) )
),
array(
'Empty array is accepted',
$stringArraySchema, array(), array()
),
array(
'correct array element accepted',
$stringArraySchema, array( 'fizzbuzz' ), array()
),
array(
'incorrect array element rejected',
$stringArraySchema, array( '12', 34 ), array( 'Expected string, but recieved integer' )
),
);
}
/**
* @dataProvider getErrorsProvider
*/
public function testGetErrors( $message, $schema, $datum, $expected ) {
$this->assertEquals(
$expected,
AvroValidator::getErrors( $schema, $datum ),
$message
);
}
}