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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
<?php
namespace MediaWiki\Tests\Json;
use FormatJson;
use InvalidArgumentException;
use JsonSerializable;
use MediaWiki\Json\JsonCodec;
use MediaWiki\Json\JsonConstants;
use MediaWikiUnitTestCase;
use stdClass;
use Title;
use Wikimedia\Assert\PreconditionException;
/**
* @covers \MediaWiki\Json\JsonCodec
* @covers \MediaWiki\Json\JsonUnserializableTrait
* @package MediaWiki\Tests\Json
*/
class JsonCodecTest extends MediaWikiUnitTestCase {
private function getCodec(): JsonCodec {
return new JsonCodec();
}
public function provideSimpleTypes() {
yield 'Integer' => [ 1, json_encode( 1 ) ];
yield 'Boolean' => [ true, json_encode( true ) ];
yield 'Null' => [ null, json_encode( null ) ];
yield 'Array' => [ [ 1, 2, 3 ], json_encode( [ 1, 2, 3 ] ) ];
yield 'Assoc array' => [ [ 'a' => 'b' ], json_encode( [ 'a' => 'b' ] ) ];
$object = (object)[ 'c' => 'd' ];
yield 'Object' => [ (array)$object, json_encode( $object ) ];
}
public function provideInvalidJsonData() {
yield 'Bad string' => [ 'bad string' ];
yield 'No unserialization metadata' => [ [ 'test' => 'test' ] ];
yield 'Unserialization metadata, but class not exist' => [ [
JsonConstants::TYPE_ANNOTATION => 'BadClassNotExist'
] ];
yield 'Unserialization metadata, but class is not JsonUnserializable' => [ [
JsonConstants::TYPE_ANNOTATION => Title::class
] ];
}
/**
* @dataProvider provideSimpleTypes
*/
public function testSimpleTypesUnserialize( $value, string $serialization ) {
$this->assertSame( $value, $this->getCodec()->unserialize( $serialization ) );
}
/**
* @dataProvider provideSimpleTypes
* @dataProvider provideInvalidJsonData
*/
public function testInvalidJsonDataForClassExpectation( $jsonData ) {
$this->expectException( InvalidArgumentException::class );
$this->getCodec()->unserialize( $jsonData, JsonUnserializableSuperClass::class );
}
public function testExpectedClassMustBeUnserializable() {
$this->expectException( PreconditionException::class );
$this->getCodec()->unserialize( '{}', self::class );
}
public function testUnexpectedClassUnserialized() {
$this->expectException( InvalidArgumentException::class );
$superClassInstance = new JsonUnserializableSuperClass( 'Godzilla' );
$this->getCodec()->unserialize(
$superClassInstance->jsonSerialize(),
JsonUnserializableSubClass::class
);
}
public function testExpectedClassUnserialized() {
$subClassInstance = new JsonUnserializableSubClass( 'Godzilla', 'But we are ready!' );
$this->assertNotNull( $this->getCodec()->unserialize(
$subClassInstance->jsonSerialize(),
JsonUnserializableSuperClass::class
) );
$this->assertNotNull( $this->getCodec()->unserialize(
$subClassInstance->jsonSerialize(),
JsonUnserializableSubClass::class
) );
}
public function testRoundTripSuperClass() {
$superClassInstance = new JsonUnserializableSuperClass( 'Super Value' );
$json = $superClassInstance->jsonSerialize();
$superClassUnserialized = $this->getCodec()->unserialize( $json );
$this->assertInstanceOf( JsonUnserializableSuperClass::class, $superClassInstance );
$this->assertSame( $superClassInstance->getSuperClassField(), $superClassUnserialized->getSuperClassField() );
}
public function testRoundTripSuperClassObject() {
$superClassInstance = new JsonUnserializableSuperClass( 'Super Value' );
$json = (object)$superClassInstance->jsonSerialize();
$superClassUnserialized = $this->getCodec()->unserialize( $json );
$this->assertInstanceOf( JsonUnserializableSuperClass::class, $superClassInstance );
$this->assertSame( $superClassInstance->getSuperClassField(), $superClassUnserialized->getSuperClassField() );
}
public function testRoundTripSubClass() {
$subClassInstance = new JsonUnserializableSubClass( 'Super Value', 'Sub Value' );
$json = $subClassInstance->jsonSerialize();
$superClassUnserialized = $this->getCodec()->unserialize( $json );
$this->assertInstanceOf( JsonUnserializableSubClass::class, $subClassInstance );
$this->assertSame( $subClassInstance->getSuperClassField(), $superClassUnserialized->getSuperClassField() );
$this->assertSame( $subClassInstance->getSubClassField(), $superClassUnserialized->getSubClassField() );
}
public function testArrayRoundTrip() {
$array = [
new JsonUnserializableSuperClass( 'Super Value' ),
new JsonUnserializableSubClass( 'Super Value', 'Sub Value' ),
42
];
$serialized = FormatJson::encode( $array );
$unserialized = $this->getCodec()->unserializeArray( FormatJson::decode( $serialized ) );
$this->assertArrayEquals( $array, $unserialized );
}
public function provideValidateSerializable() {
$classInstance = new class() {
};
$serializableClass = new class() implements JsonSerializable {
public function jsonSerialize() {
return [];
}
};
yield 'Number' => [ 1, true, null ];
yield 'Null' => [ null, true, null ];
yield 'Class' => [ $classInstance, false, '$' ];
yield 'Empty array' => [ [], true, null ];
yield 'Empty stdClass' => [ new stdClass(), true, null ];
yield 'Non-empty array' => [ [ 1, 2, 3 ], true, null ];
yield 'Non-empty map' => [ [ 'a' => 'b' ], true, null ];
yield 'Nested, serializable' => [ [ 'a' => [ 'b' => [ 'c' => 'd' ] ] ], true, null ];
yield 'Nested, serializable, with null' => [ [ 'a' => [ 'b' => null ] ], true, null ];
yield 'Nested, serializable, with stdClass' => [ [ 'a' => (object)[ 'b' => [ 'c' => 'd' ] ] ], true, null ];
yield 'Nested, serializable, with stdClass, with null' => [ [ 'a' => (object)[ 'b' => null ] ], true, null ];
yield 'Nested, non-serializable' => [ [ 'a' => [ 'b' => $classInstance ] ], true, '$.a.b' ];
yield 'Nested, non-serializable, in array' => [ [ 'a' => [ 1, 2, $classInstance ] ], true, '$.a.2' ];
yield 'Nested, non-serializable, in stdClass' => [ [ 'a' => (object)[ 1, 2, $classInstance ] ], true, '$.a.2' ];
yield 'JsonUnserializable instance' => [ new JsonUnserializableSuperClass( 'Test' ), true, null ];
yield 'JsonUnserializable instance, in array' =>
[ [ new JsonUnserializableSuperClass( 'Test' ) ], true, null ];
yield 'JsonUnserializable instance, in stdClass' =>
[ (object)[ new JsonUnserializableSuperClass( 'Test' ) ], true, null ];
yield 'JsonSerializable instance' => [ $serializableClass, false, null ];
yield 'JsonSerializable instance, in array' => [ [ $serializableClass ], false, null ];
yield 'JsonSerializable instance, in stdClass' => [ (object)[ $serializableClass ], false, null ];
yield 'JsonSerializable instance, expect unserialize' => [ $serializableClass, true, '$' ];
yield 'JsonSerializable instance, in array, expect unserialize' => [ [ $serializableClass ], true, '$.0' ];
yield 'JsonSerializable instance, in stdClass, expect unserialize' =>
[ (object)[ $serializableClass ], true, '$.0' ];
}
/**
* @dataProvider provideValidateSerializable
* @covers \MediaWiki\Json\JsonCodec::detectNonSerializableData
* @covers \MediaWiki\Json\JsonCodec::detectNonSerializableDataInternal
*/
public function testValidateSerializable( $value, bool $expectUnserialize, ?string $result ) {
$this->assertSame( $result, $this->getCodec()
->detectNonSerializableData( $value, $expectUnserialize ) );
}
public function provideSerializeThrowsOnFailure() {
$classInstance = new class() {
};
yield 'non-serializable class' => [ $classInstance ];
yield 'crash in serialization, gzipped data' => [
"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xcb\x48\xcd\xc9\xc9\x57\x28\xcf\x2f'
. '\xca\x49\x01\x00\x85\x11\x4a\x0d\x0b\x00\x00\x00"
];
}
/**
* @dataProvider provideSerializeThrowsOnFailure
* @covers \MediaWiki\Json\JsonCodec::serialize
*/
public function testSerializeThrowsOnFailure( $value ) {
$this->expectException( InvalidArgumentException::class );
$this->getCodec()->serialize( $value );
}
public function provideSerializeSuccess() {
$serializableInstance = new class() implements JsonSerializable {
public function jsonSerialize() {
return [ 'c' => 'd' ];
}
};
yield 'array' => [ [ 'a' => 'b' ], '{"a":"b"}' ];
yield 'JsonSerializable' => [ $serializableInstance, '{"c":"d"}' ];
}
/**
* @dataProvider provideSerializeSuccess
* @covers \MediaWiki\Json\JsonCodec::serialize
*/
public function testSerializeSuccess( $value, string $expected ) {
$this->assertSame( $expected, $this->getCodec()->serialize( $value ) );
}
}
|