summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/utils/AvroValidatorTest.php
blob: 52c242c17d5045fa440a82d14621cd31ae4fe279 (plain)
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
<?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' ) ) );
		$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',
					)
				) )
			),
		);
	}

	/**
	 * @dataProvider getErrorsProvider
	 */
	public function testGetErrors( $message, $schema, $datum, $expected ) {
		$this->assertEquals(
			$expected,
			AvroValidator::getErrors( $schema, $datum ),
			$message
		);
	}
}