ItemStoreTest::insertFiveItems()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 8.9599
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\Queryr\EntityStore;
4
5
use Queryr\EntityStore\Data\ItemRow;
6
use Queryr\EntityStore\EntityStoreConfig;
7
use Queryr\EntityStore\EntityStoreFactory;
8
use Queryr\EntityStore\EntityStoreInstaller;
9
use Queryr\EntityStore\ItemStore;
10
use Tests\Queryr\EntityStore\Fixtures\TestFixtureFactory;
11
use Wikibase\DataModel\Entity\ItemId;
12
13
/**
14
 * @covers Queryr\EntityStore\ItemStore
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class ItemStoreTest extends \PHPUnit_Framework_TestCase {
20
21
	const ITEM_ID = '1337';
22
23
	const AND_INSTALL = true;
24
	const WITHOUT_INSTALLING = false;
25
26
	/**
27
	 * @var ItemStore
28
	 */
29
	private $store;
30
31
	/**
32
	 * @var ItemRow
33
	 */
34
	private $itemRow;
35
36
	public function setUp() {
37
		$this->createItemRowField();
38
	}
39
40
	private function createStore( $doDbInstall = false ) {
41
		$connection = TestFixtureFactory::newInstance()->newConnection();
42
		$config = new EntityStoreConfig();
43
44
		if ( $doDbInstall ) {
45
			$installer = new EntityStoreInstaller( $connection->getSchemaManager(), $config );
46
			$installer->install();
47
		}
48
49
		$this->store = ( new EntityStoreFactory( $connection, $config ) )->newItemStore();
50
	}
51
52
	private function createItemRowField() {
53
		$this->itemRow = ( new ItemRow() )
54
			->setPageTitle( 'Item:Q1337' )
55
			->setRevisionId( '424242' )
56
			->setItemType( 1 )
57
			->setRevisionTime( '2014-02-27T11:40:12Z' )
58
			->setEnglishLabel( 'kittens' )
59
			->setEnglishWikipediaTitle( 'cats' )
60
			->setItemJson( 'json be here' )
61
			->setNumericItemId( self::ITEM_ID );
62
	}
63
64
	public function testCanStoreAndRetrieveItemPage() {
65
		$this->createStore( self::AND_INSTALL );
66
67
		$this->store->storeItemRow( $this->itemRow );
68
69
		$newItemRow = $this->store->getItemRowByNumericItemId( self::ITEM_ID );
70
71
		$this->assertInstanceOf( 'Queryr\EntityStore\Data\ItemRow', $newItemRow );
72
73
		$this->assertSame( $this->itemRow->getNumericItemId(), $newItemRow->getNumericItemId() );
74
		$this->assertSame( $this->itemRow->getItemJson(), $newItemRow->getItemJson() );
75
		$this->assertSame( $this->itemRow->getPageTitle(), $newItemRow->getPageTitle() );
76
		$this->assertSame( $this->itemRow->getRevisionId(), $newItemRow->getRevisionId() );
77
		$this->assertSame( $this->itemRow->getRevisionTime(), $newItemRow->getRevisionTime() );
78
		$this->assertSame( $this->itemRow->getItemType(), $newItemRow->getItemType() );
79
		$this->assertSame( $this->itemRow->getEnglishLabel(), $newItemRow->getEnglishLabel() );
80
		$this->assertSame( $this->itemRow->getEnglishWikipediaTitle(), $newItemRow->getEnglishWikipediaTitle() );
81
	}
82
83
	public function testGivenNotKnownId_getItemRowByNumericItemIdReturnsNull() {
84
		$this->createStore( self::AND_INSTALL );
85
		$this->assertNull( $this->store->getItemRowByNumericItemId( '32202' ) );
86
	}
87
88
	public function testWhenStoreNotInitialized_storeItemRowThrowsException() {
89
		$this->createStore( self::WITHOUT_INSTALLING );
90
		$this->setExpectedException( 'Queryr\EntityStore\EntityStoreException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
91
		$this->store->storeItemRow( $this->itemRow );
92
	}
93
94
	public function testWhenStoreNotInitialized_getItemRowByNumericItemIdThrowsException() {
95
		$this->createStore( self::WITHOUT_INSTALLING );
96
		$this->setExpectedException( 'Queryr\EntityStore\EntityStoreException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
97
		$this->store->getItemRowByNumericItemId( 1 );
98
	}
99
100
	public function testWhenStoreNotInitialized_getItemInfoThrowsException() {
101
		$this->createStore( self::WITHOUT_INSTALLING );
102
		$this->setExpectedException( 'Queryr\EntityStore\EntityStoreException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
103
		$this->store->getItemInfo( 10, 0 );
104
	}
105
106
	public function testWhenStoreNotInitialized_getItemTypesThrowsException() {
107
		$this->createStore( self::WITHOUT_INSTALLING );
108
		$this->setExpectedException( 'Queryr\EntityStore\EntityStoreException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
109
		$this->store->getItemTypes();
110
	}
111
112
	public function testWhenNoItemsInStore_getItemTypesReturnsEmptyArray() {
113
		$this->createStore( self::AND_INSTALL );
114
		$this->assertSame( [], $this->store->getItemTypes() );
115
	}
116
117
	private function insertFiveItems() {
118
		$this->store->storeItemRow(
119
			( new ItemRow() )
120
				->setPageTitle( 'Item:Q1000' )
121
				->setRevisionId( '123456' )
122
				->setItemType( 5 )
123
				->setRevisionTime( '2014-02-27T11:40:12Z' )
124
				->setEnglishLabel( 'kittens' )
125
				->setItemJson( 'json be here' )
126
				->setNumericItemId( 1000 )
127
		);
128
129
		$this->store->storeItemRow(
130
			( new ItemRow() )
131
				->setPageTitle( 'Item:Q2000' )
132
				->setRevisionId( '234567' )
133
				->setItemType( 5 )
134
				->setRevisionTime( '2014-02-27T11:40:12Z' )
135
				->setEnglishLabel( 'cats' )
136
				->setItemJson( 'json be here' )
137
				->setNumericItemId( 2000 )
138
		);
139
140
		$this->store->storeItemRow(
141
			( new ItemRow() )
142
				->setPageTitle( 'Item:Q3000' )
143
				->setRevisionId( '345678' )
144
				->setItemType( 1 )
145
				->setRevisionTime( '2014-02-27T11:40:12Z' )
146
				->setEnglishLabel( 'more cats' )
147
				->setItemJson( 'json be here' )
148
				->setNumericItemId( 3000 )
149
		);
150
151
		$this->store->storeItemRow(
152
			( new ItemRow() )
153
				->setPageTitle( 'Item:Q4000' )
154
				->setRevisionId( '456789' )
155
				->setItemType( null )
156
				->setRevisionTime( '2014-02-27T11:40:12Z' )
157
				->setEnglishLabel( 'more kittens' )
158
				->setItemJson( 'json be here' )
159
				->setNumericItemId( 4000 )
160
		);
161
162
		$this->store->storeItemRow(
163
			( new ItemRow() )
164
				->setPageTitle( 'Item:Q5000' )
165
				->setRevisionId( '567890' )
166
				->setItemType( 3 )
167
				->setRevisionTime( '2014-02-27T11:40:12Z' )
168
				->setEnglishLabel( 'all kittens' )
169
				->setItemJson( 'json be here' )
170
				->setNumericItemId( 5000 )
171
		);
172
	}
173
174
	public function testGetItemTypesReturnsDistinctNonNullTypes() {
175
		$this->createStore( self::AND_INSTALL );
176
		$this->insertFiveItems();
177
178
		$this->assertSame( [ 1, 3, 5 ], $this->store->getItemTypes() );
179
180
		$this->assertSame( [ 1 ], $this->store->getItemTypes( 1, 0 ) );
181
		$this->assertSame( [ 3, 5 ], $this->store->getItemTypes( 10, 1 ) );
182
	}
183
184
	public function testGivenNullTypeFilter_getItemInfoReturnsAllItems() {
185
		$this->createStore( self::AND_INSTALL );
186
		$this->insertFiveItems();
187
188
		$this->assertCount( 5, $this->store->getItemInfo( 10, 0, null ) );
189
	}
190
191
	public function testGivenMatchingTypeFilter_getItemInfoReturnsOnlyMatchingItems() {
192
		$this->createStore( self::AND_INSTALL );
193
		$this->insertFiveItems();
194
195
		$itemInfo = $this->store->getItemInfo( 10, 0, 5 );
196
197
		$this->assertCount( 2, $itemInfo );
198
		$this->assertSame( 1000, $itemInfo[0]->getNumericItemId() );
199
		$this->assertSame( 2000, $itemInfo[1]->getNumericItemId() );
200
	}
201
202
	public function testGivenNonMatchingTypeFilter_getItemInfoReturnsEmptyArray() {
203
		$this->createStore( self::AND_INSTALL );
204
		$this->insertFiveItems();
205
206
		$this->assertSame( [], $this->store->getItemInfo( 10, 0, 1337 ) );
207
	}
208
209
	public function testInsertingExistingItemOverridesTheOriginalOne() {
210
		$this->createStore( self::AND_INSTALL );
211
212
		$this->store->storeItemRow(
213
			( new ItemRow() )
214
				->setPageTitle( 'Item:Q1000' )
215
				->setRevisionId( '123456' )
216
				->setItemType( 5 )
217
				->setRevisionTime( '2014-02-27T11:40:12Z' )
218
				->setEnglishLabel( 'kittens' )
219
				->setItemJson( 'json be here' )
220
				->setNumericItemId( 1000 )
221
		);
222
223
		$this->store->storeItemRow(
224
			( new ItemRow() )
225
				->setPageTitle( 'Item:Q1000' )
226
				->setRevisionId( '123456789' )
227
				->setItemType( 4 )
228
				->setRevisionTime( '2014-02-27T11:40:12Z' )
229
				->setEnglishLabel( 'cats' )
230
				->setItemJson( 'json be here' )
231
				->setNumericItemId( 1000 )
232
		);
233
234
		$infoSets = $this->store->getItemInfo(  10, 0 );
235
		$this->assertCount( 1, $infoSets );
236
		$this->assertSame( 4, $infoSets[0]->getItemType() );
237
	}
238
239
	public function testWhenStoreNotInitialized_deleteItemByIdThrowsException() {
240
		$this->createStore( self::WITHOUT_INSTALLING );
241
		$this->setExpectedException( 'Queryr\EntityStore\EntityStoreException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
242
		$this->store->deleteItemById( new ItemId( 'Q1' ) );
243
	}
244
245
	public function testGivenNonExistingId_deleteItemByIdDoesNotDeleteItems() {
246
		$this->createStore( self::AND_INSTALL );
247
248
		$this->store->storeItemRow(
249
			( new ItemRow() )
250
				->setPageTitle( 'Item:Q1000' )
251
				->setRevisionId( '123456' )
252
				->setItemType( 5 )
253
				->setRevisionTime( '2014-02-27T11:40:12Z' )
254
				->setEnglishLabel( 'kittens' )
255
				->setItemJson( 'json be here' )
256
				->setNumericItemId( 1000 )
257
		);
258
259
		$this->store->deleteItemById( new ItemId( 'Q1001' ) );
260
261
		$this->assertCount( 1, $this->store->getItemInfo(  10, 0 ) );
262
	}
263
264
	public function testGivenExistingId_deleteItemByIdDeletesItem() {
265
		$this->createStore( self::AND_INSTALL );
266
267
		$this->store->storeItemRow(
268
			( new ItemRow() )
269
				->setPageTitle( 'Item:Q1000' )
270
				->setRevisionId( '123456' )
271
				->setItemType( 5 )
272
				->setRevisionTime( '2014-02-27T11:40:12Z' )
273
				->setEnglishLabel( 'kittens' )
274
				->setItemJson( 'json be here' )
275
				->setNumericItemId( 1000 )
276
		);
277
278
		$this->store->deleteItemById( new ItemId( 'Q1000' ) );
279
280
		$this->assertCount( 0, $this->store->getItemInfo(  10, 0 ) );
281
	}
282
283
	public function testGivenNotExistingPage_getIdForEnWikiPageReturnsNull() {
284
		$this->createStore( self::AND_INSTALL );
285
286
		$this->assertNull( $this->store->getIdForEnWikiPage( 'kittens' ) );
287
	}
288
289
	public function testWhenStoreNotInitialized_getIdForEnWikiPageThrowsException() {
290
		$this->createStore( self::WITHOUT_INSTALLING );
291
		$this->setExpectedException( 'Queryr\EntityStore\EntityStoreException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
292
		$this->assertNull( $this->store->getIdForEnWikiPage( 'kittens' ) );
293
	}
294
295
	public function testGivenExistingPage_getIdForEnWikiPageReturnsPage() {
296
		$this->createStore( self::AND_INSTALL );
297
298
		$this->store->storeItemRow(
299
			( new ItemRow() )
300
				->setPageTitle( 'Item:Q55555' )
301
				->setRevisionId( '55555555' )
302
				->setItemType( 5 )
303
				->setRevisionTime( '2014-02-27T11:40:12Z' )
304
				->setEnglishWikipediaTitle( 'cats' )
305
				->setItemJson( 'json be here' )
306
				->setNumericItemId( 55555 )
307
		);
308
309
		$this->store->storeItemRow(
310
			( new ItemRow() )
311
				->setPageTitle( 'Item:Q1000' )
312
				->setRevisionId( '123456' )
313
				->setItemType( 1 )
314
				->setRevisionTime( '2014-02-27T11:40:12Z' )
315
				->setEnglishWikipediaTitle( 'kittens' )
316
				->setItemJson( 'json be here' )
317
				->setNumericItemId( 1000 )
318
		);
319
320
		$this->store->storeItemRow(
321
			( new ItemRow() )
322
				->setPageTitle( 'Item:Q999999' )
323
				->setRevisionId( '9999999' )
324
				->setItemType( 9 )
325
				->setRevisionTime( '2014-02-27T11:40:12Z' )
326
				->setEnglishWikipediaTitle( 'fluff' )
327
				->setItemJson( 'json be here' )
328
				->setNumericItemId( 999999 )
329
		);
330
331
		$this->assertEquals(
332
			new ItemId( 'Q1000' ),
333
			$this->store->getIdForEnWikiPage( 'kittens' )
334
		);
335
	}
336
337
}
338