MediaWiki  1.33.0
AvroValidatorTest.php
Go to the documentation of this file.
1 <?php
15 class AvroValidatorTest extends PHPUnit\Framework\TestCase {
16 
17  use MediaWikiCoversValidator;
18 
19  public function setUp() {
20  if ( !class_exists( 'AvroSchema' ) ) {
21  $this->markTestSkipped( 'Avro is required to run the AvroValidatorTest' );
22  }
23  parent::setUp();
24  }
25 
26  public function getErrorsProvider() {
27  $stringSchema = AvroSchema::parse( json_encode( [ 'type' => 'string' ] ) );
28  $stringArraySchema = AvroSchema::parse( json_encode( [
29  'type' => 'array',
30  'items' => 'string',
31  ] ) );
32  $recordSchema = AvroSchema::parse( json_encode( [
33  'type' => 'record',
34  'name' => 'ut',
35  'fields' => [
36  [ 'name' => 'id', 'type' => 'int', 'required' => true ],
37  ],
38  ] ) );
39  $enumSchema = AvroSchema::parse( json_encode( [
40  'type' => 'record',
41  'name' => 'ut',
42  'fields' => [
43  [ 'name' => 'count', 'type' => [ 'int', 'null' ] ],
44  ],
45  ] ) );
46 
47  return [
48  [
49  'No errors with a simple string serialization',
50  $stringSchema, 'foobar', [],
51  ],
52 
53  [
54  'Cannot serialize integer into string',
55  $stringSchema, 5, 'Expected string, but recieved integer',
56  ],
57 
58  [
59  'Cannot serialize array into string',
60  $stringSchema, [], 'Expected string, but recieved array',
61  ],
62 
63  [
64  'allows and ignores extra fields',
65  $recordSchema, [ 'id' => 4, 'foo' => 'bar' ], [],
66  ],
67 
68  [
69  'detects missing fields',
70  $recordSchema, [], [ 'id' => 'Missing expected field' ],
71  ],
72 
73  [
74  'handles first element in enum',
75  $enumSchema, [ 'count' => 4 ], [],
76  ],
77 
78  [
79  'handles second element in enum',
80  $enumSchema, [ 'count' => null ], [],
81  ],
82 
83  [
84  'rejects element not in union',
85  $enumSchema, [ 'count' => 'invalid' ], [ 'count' => [
86  'Expected any one of these to be true',
87  [
88  'Expected integer, but recieved string',
89  'Expected null, but recieved string',
90  ]
91  ] ]
92  ],
93  [
94  'Empty array is accepted',
95  $stringArraySchema, [], []
96  ],
97  [
98  'correct array element accepted',
99  $stringArraySchema, [ 'fizzbuzz' ], []
100  ],
101  [
102  'incorrect array element rejected',
103  $stringArraySchema, [ '12', 34 ], [ 'Expected string, but recieved integer' ]
104  ],
105  ];
106  }
107 
111  public function testGetErrors( $message, $schema, $datum, $expected ) {
112  $this->assertEquals(
113  $expected,
114  AvroValidator::getErrors( $schema, $datum ),
115  $message
116  );
117  }
118 }
AvroValidatorTest
Tests for IP validity functions.
Definition: AvroValidatorTest.php:15
AvroValidatorTest\setUp
setUp()
Definition: AvroValidatorTest.php:19
AvroValidatorTest\getErrorsProvider
getErrorsProvider()
Definition: AvroValidatorTest.php:26
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
AvroValidatorTest\testGetErrors
testGetErrors( $message, $schema, $datum, $expected)
getErrorsProvider
Definition: AvroValidatorTest.php:111
null
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
AvroValidator\getErrors
static getErrors(AvroSchema $schema, $datum)
Definition: AvroValidator.php:38