ItemSerializerTest::serializationProvider()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 133

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 133
rs 8
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Tests\Wikibase\DataModel\Serializers;
4
5
use Serializers\Serializer;
6
use Wikibase\DataModel\Entity\Item;
7
use Wikibase\DataModel\Entity\ItemId;
8
use Wikibase\DataModel\Entity\Property;
9
use Wikibase\DataModel\Serializers\ItemSerializer;
10
use Wikibase\DataModel\SiteLink;
11
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
12
use Wikibase\DataModel\Statement\StatementList;
13
use Wikibase\DataModel\Term\AliasGroupList;
14
use Wikibase\DataModel\Term\TermList;
15
16
/**
17
 * @covers Wikibase\DataModel\Serializers\ItemSerializer
18
 *
19
 * @license GPL-2.0-or-later
20
 * @author Thomas Pellissier Tanon
21
 * @author Jan Zerebecki < [email protected] >
22
 * @author Bene* < [email protected] >
23
 */
24
class ItemSerializerTest extends DispatchableSerializerTest {
25
26
	protected function buildSerializer( $useObjectsForMaps = false ) {
27
		$termListSerializerMock = $this->getMockBuilder( Serializer::class )->getMock();
28
		$termListSerializerMock->expects( $this->any() )
29
			->method( 'serialize' )
30
			->will( $this->returnCallback( function( TermList $termList ) {
31
				if ( $termList->isEmpty() ) {
32
					return [];
33
				}
34
35
				return [
36
					'en' => [ 'lang' => 'en', 'value' => 'foo' ]
37
				];
38
			} ) );
39
40
		$aliasGroupListSerializerMock = $this->getMockBuilder( Serializer::class )->getMock();
41
		$aliasGroupListSerializerMock->expects( $this->any() )
42
			->method( 'serialize' )
43
			->will( $this->returnCallback( function( AliasGroupList $aliasGroupList ) {
44
				if ( $aliasGroupList->isEmpty() ) {
45
					return [];
46
				}
47
48
				return [
49
					'en' => [ 'lang' => 'en', 'values' => [ 'foo', 'bar' ] ]
50
				];
51
			} ) );
52
53
		$statementListSerializerMock = $this->getMockBuilder( Serializer::class )->getMock();
54
		$statementListSerializerMock->expects( $this->any() )
55
			->method( 'serialize' )
56
			->will( $this->returnCallback( function( StatementList $statementList ) {
57
				if ( $statementList->isEmpty() ) {
58
					return [];
59
				}
60
61
				return [
62
					'P42' => [
63
						[
64
							'mainsnak' => [
65
								'snaktype' => 'novalue',
66
								'property' => 'P42'
67
							],
68
							'type' => 'statement',
69
							'rank' => 'normal'
70
						]
71
					]
72
				];
73
			} ) );
74
75
		$siteLinkSerializerMock = $this->getMockBuilder( Serializer::class )->getMock();
76
		$siteLinkSerializerMock->expects( $this->any() )
77
			->method( 'serialize' )
78
			->with( $this->equalTo( new SiteLink( 'enwiki', 'Nyan Cat' ) ) )
79
			->will( $this->returnValue( [
80
				'site' => 'enwiki',
81
				'title' => 'Nyan Cat',
82
				'badges' => []
83
			] ) );
84
85
		return new ItemSerializer(
86
			$termListSerializerMock,
0 ignored issues
show
Documentation introduced by
$termListSerializerMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Serializers\Serializer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
			$aliasGroupListSerializerMock,
0 ignored issues
show
Documentation introduced by
$aliasGroupListSerializerMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Serializers\Serializer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88
			$statementListSerializerMock,
0 ignored issues
show
Documentation introduced by
$statementListSerializerMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Serializers\Serializer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
			$siteLinkSerializerMock,
0 ignored issues
show
Documentation introduced by
$siteLinkSerializerMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Serializers\Serializer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
			$useObjectsForMaps
91
		);
92
	}
93
94
	public function serializableProvider() {
95
		return [
96
			[ new Item() ],
97
		];
98
	}
99
100
	public function nonSerializableProvider() {
101
		return [
102
			[ 5 ],
103
			[ [] ],
104
			[ Property::newFromType( 'string' ) ],
105
		];
106
	}
107
108
	public function serializationProvider() {
109
		$provider = [
110
			[
111
				[
112
					'type' => 'item',
113
					'labels' => [],
114
					'descriptions' => [],
115
					'aliases' => [],
116
					'claims' => [],
117
					'sitelinks' => [],
118
				],
119
				new Item()
120
			],
121
		];
122
123
		$entity = new Item( new ItemId( 'Q42' ) );
124
		$provider[] = [
125
			[
126
				'type' => 'item',
127
				'id' => 'Q42',
128
				'labels' => [],
129
				'descriptions' => [],
130
				'aliases' => [],
131
				'claims' => [],
132
				'sitelinks' => [],
133
			],
134
			$entity
135
		];
136
137
		$entity = new Item();
138
		$entity->setLabel( 'en', 'foo' );
139
		$provider[] = [
140
			[
141
				'type' => 'item',
142
				'labels' => [
143
					'en' => [
144
						'lang' => 'en',
145
						'value' => 'foo'
146
					]
147
				],
148
				'descriptions' => [],
149
				'aliases' => [],
150
				'claims' => [],
151
				'sitelinks' => [],
152
			],
153
			$entity
154
		];
155
156
		$entity = new Item();
157
		$entity->setDescription( 'en', 'foo' );
158
		$provider[] = [
159
			[
160
				'type' => 'item',
161
				'labels' => [],
162
				'descriptions' => [
163
					'en' => [
164
						'lang' => 'en',
165
						'value' => 'foo'
166
					]
167
				],
168
				'aliases' => [],
169
				'claims' => [],
170
				'sitelinks' => [],
171
			],
172
			$entity
173
		];
174
175
		$entity = new Item();
176
		$entity->setAliases( 'en', [ 'foo', 'bar' ] );
177
		$provider[] = [
178
			[
179
				'type' => 'item',
180
				'labels' => [],
181
				'descriptions' => [],
182
				'aliases' => [
183
					'en' => [
184
						'lang' => 'en',
185
						'values' => [ 'foo', 'bar' ]
186
					]
187
				],
188
				'claims' => [],
189
				'sitelinks' => [],
190
			],
191
			$entity
192
		];
193
194
		$entity = new Item();
195
		$entity->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ), null, null, 'test' );
196
		$provider[] = [
197
			[
198
				'type' => 'item',
199
				'labels' => [],
200
				'descriptions' => [],
201
				'aliases' => [],
202
				'claims' => [
203
					'P42' => [
204
						[
205
							'mainsnak' => [
206
								'snaktype' => 'novalue',
207
								'property' => 'P42'
208
							],
209
							'type' => 'statement',
210
							'rank' => 'normal'
211
						]
212
					]
213
				],
214
				'sitelinks' => [],
215
			],
216
			$entity
217
		];
218
219
		$item = new Item();
220
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Nyan Cat' );
221
		$provider[] = [
222
			[
223
				'type' => 'item',
224
				'labels' => [],
225
				'descriptions' => [],
226
				'aliases' => [],
227
				'claims' => [],
228
				'sitelinks' => [
229
					'enwiki' => [
230
						'site' => 'enwiki',
231
						'title' => 'Nyan Cat',
232
						'badges' => []
233
					]
234
				],
235
			],
236
			$item
237
		];
238
239
		return $provider;
240
	}
241
242
	public function testItemSerializerWithOptionObjectsForMaps() {
243
		$serializer = $this->buildSerializer( true );
244
245
		$item = new Item();
246
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Nyan Cat' );
247
248
		$sitelinks = new \stdClass();
249
		$sitelinks->enwiki = [
250
			'site' => 'enwiki',
251
			'title' => 'Nyan Cat',
252
			'badges' => [],
253
		];
254
255
		$serial = [
256
			'type' => 'item',
257
			'labels' => [],
258
			'descriptions' => [],
259
			'aliases' => [],
260
			'claims' => [],
261
			'sitelinks' => $sitelinks,
262
		];
263
264
		$this->assertEquals( $serial, $serializer->serialize( $item ) );
265
	}
266
267
}
268