Completed
Push — master ( aad109...a11cd2 )
by mw
02:20
created

testGetContentOnQuantityTypeWithSameLabelForMode_PRINT_PROP()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 70
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 70
rs 9.1724
cc 1
eloc 51
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 SEQL\ByHttpRequest\Tests;
4
5
use SEQL\ByHttpRequest\CannedResultArray;
6
use SMW\DIWikiPage;
7
use SMW\DIProperty;
8
use SMWDINumber as DINumber;
9
10
/**
11
 * @covers \SEQL\ByHttpRequest\CannedResultArray
12
 * @group semantic-external-query-lookup
13
 *
14
 * @license GNU GPL v2+
15
 * @since 1.0
16
 *
17
 * @author mwjames
18
 */
19
class CannedResultArrayTest extends \PHPUnit_Framework_TestCase {
20
21
	private $jsonResponseParser;
22
23
	protected function setUp() {
24
25
		$this->jsonResponseParser = $this->getMockBuilder( '\SEQL\ByHttpRequest\JsonResponseParser' )
26
			->disableOriginalConstructor()
27
			->getMock();
28
	}
29
30
	public function testCanConstruct() {
31
32
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
33
			->disableOriginalConstructor()
34
			->getMock();
35
36
		$this->assertInstanceOf(
37
			'\SEQL\ByHttpRequest\CannedResultArray',
38
			new CannedResultArray( new DIWikiPage( 'Foo', NS_MAIN ), $printRequest, $this->jsonResponseParser )
39
		);
40
	}
41
42
	public function testGetResultSubject() {
43
44
		$subject = new DIWikiPage( 'Foo', NS_MAIN );
45
46
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
47
			->disableOriginalConstructor()
48
			->getMock();
49
50
		$instance = new CannedResultArray(
51
			$subject,
52
			$printRequest,
53
			$this->jsonResponseParser
54
		);
55
56
		$this->assertEquals(
57
			$subject,
58
			$instance->getResultSubject()
59
		);
60
	}
61
62
	public function testGetContentForMode_PRINT_THIS() {
63
64
		$subject = new DIWikiPage( 'Foo', NS_MAIN );
65
66
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
67
			->disableOriginalConstructor()
68
			->getMock();
69
70
		$printRequest->expects( $this->any() )
71
			->method( 'getMode' )
72
			->will( $this->returnValue( \SMW\Query\PrintRequest::PRINT_THIS ) );
73
74
		$instance = new CannedResultArray(
75
			$subject,
76
			$printRequest,
77
			$this->jsonResponseParser
78
		);
79
80
		$this->assertDataItem(
81
			$subject,
82
			$instance
83
		);
84
	}
85
86
	public function testGetContentForMode_PRINT_CCAT() {
87
88
		$subject = new DIWikiPage( 'Foo', NS_MAIN );
89
		$category = new DIWikiPage( 'Bar', NS_CATEGORY, 'mw-foo' );
90
91
		$this->jsonResponseParser->expects( $this->any() )
92
			->method( 'getPropertyValuesFor' )
93
			->with(
94
				$this->equalTo( $subject ),
95
				$this->equalTo( new DIProperty( '_INST' ) ) )
96
			->will( $this->returnValue( array( $category ) ) );
97
98
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
99
			->disableOriginalConstructor()
100
			->getMock();
101
102
		$printRequest->expects( $this->any() )
103
			->method( 'getMode' )
104
			->will( $this->returnValue( \SMW\Query\PrintRequest::PRINT_CCAT ) );
105
106
		$instance = new CannedResultArray(
107
			$subject,
108
			$printRequest,
109
			$this->jsonResponseParser
110
		);
111
112
		$this->assertDataItem(
113
			$category,
114
			$instance
115
		);
116
	}
117
118
	public function testGetContentOnDifferentPropertyLabelForMode_PRINT_PROP() {
119
120
		$subject = new DIWikiPage( 'Foo', NS_MAIN );
121
		$dataItem = new DINumber( 1001 );
122
123
		$propertyValue = $this->getMockBuilder( '\SMWPropertyValue' )
124
			->disableOriginalConstructor()
125
			->getMock();
126
127
		$propertyValue->expects( $this->any() )
128
			->method( 'isValid' )
129
			->will( $this->returnValue( true ) );
130
131
		$propertyValue->expects( $this->any() )
132
			->method( 'getDataItem' )
133
			->will( $this->returnValue( new DIProperty( 'ABC' ) ) );
134
135
		$dataValue = $this->getMockBuilder( '\SMWDataValue' )
136
			->disableOriginalConstructor()
137
			->setMethods( array( 'getDataItem' ) )
138
			->getMockForAbstractClass();
139
140
		$dataValue->expects( $this->any() )
141
			->method( 'getDataItem' )
142
			->will( $this->returnValue( $dataItem ) );
143
144
		$this->jsonResponseParser->expects( $this->any() )
145
			->method( 'getPropertyValuesFor' )
146
			->with(
147
				$this->equalTo( $subject ),
148
				$this->equalTo( new DIProperty( 'isPropertyFromInMemoryExternalRepositoryCache' ) ) )
149
			->will( $this->returnValue( array( $dataValue ) ) );
150
151
		$this->jsonResponseParser->expects( $this->any() )
152
			->method( 'findPropertyFromInMemoryExternalRepositoryCache' )
153
			->with(
154
				$this->equalTo( new DIProperty( 'withDifferentPropertyLabel' ) ) )
155
			->will( $this->returnValue( new DIProperty( 'isPropertyFromInMemoryExternalRepositoryCache' ) ) );
156
157
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
158
			->disableOriginalConstructor()
159
			->getMock();
160
161
		$printRequest->expects( $this->any() )
162
			->method( 'getMode' )
163
			->will( $this->returnValue( \SMW\Query\PrintRequest::PRINT_PROP ) );
164
165
		$printRequest->expects( $this->any() )
166
			->method( 'getData' )
167
			->will( $this->returnValue( $propertyValue ) );
168
169
		$printRequest->expects( $this->atLeastOnce() )
170
			->method( 'getLabel' )
171
			->will( $this->returnValue( 'withDifferentPropertyLabel' ) );
172
173
		$instance = new CannedResultArray(
174
			$subject,
175
			$printRequest,
176
			$this->jsonResponseParser
177
		);
178
179
		$this->assertDataItem(
180
			$dataItem,
181
			$instance
182
		);
183
	}
184
185
	public function testGetContentOnQuantityTypeWithSameLabelForMode_PRINT_PROP() {
186
187
		$subject = new DIWikiPage( 'Foo', NS_MAIN );
188
189
		$property = new DIProperty( 'QuantityType' );
190
		$property->setPropertyTypeId( '_qty' );
191
192
		$dataItem = new DINumber( 1001 );
193
194
		$propertyValue = $this->getMockBuilder( '\SMWPropertyValue' )
195
			->disableOriginalConstructor()
196
			->getMock();
197
198
		$propertyValue->expects( $this->any() )
199
			->method( 'isValid' )
200
			->will( $this->returnValue( true ) );
201
202
		$propertyValue->expects( $this->any() )
203
			->method( 'getDataItem' )
204
			->will( $this->returnValue( new DIProperty( 'ABC' ) ) );
205
206
		$dataValue = $this->getMockBuilder( '\SMWDataValue' )
207
			->disableOriginalConstructor()
208
			->setMethods( array( 'getDataItem' ) )
209
			->getMockForAbstractClass();
210
211
		$dataValue->expects( $this->any() )
212
			->method( 'getDataItem' )
213
			->will( $this->returnValue( $dataItem ) );
214
215
		$this->jsonResponseParser->expects( $this->any() )
216
			->method( 'getPropertyValuesFor' )
217
			->with(
218
				$this->equalTo( $subject ),
219
				$this->equalTo( $property ) )
220
			->will( $this->returnValue( array( $dataValue ) ) );
221
222
		$this->jsonResponseParser->expects( $this->any() )
223
			->method( 'findPropertyFromInMemoryExternalRepositoryCache' )
224
			->with(
225
				$this->equalTo( new DIProperty( 'ABC' ) ) )
226
			->will( $this->returnValue( $property ) );
227
228
		$printRequest = $this->getMockBuilder( '\SMW\Query\PrintRequest' )
229
			->disableOriginalConstructor()
230
			->getMock();
231
232
		$printRequest->expects( $this->any() )
233
			->method( 'getMode' )
234
			->will( $this->returnValue( \SMW\Query\PrintRequest::PRINT_PROP ) );
235
236
		$printRequest->expects( $this->any() )
237
			->method( 'getData' )
238
			->will( $this->returnValue( $propertyValue ) );
239
240
		$printRequest->expects( $this->atLeastOnce() )
241
			->method( 'getLabel' )
242
			->will( $this->returnValue( '' ) );
243
244
		$instance = new CannedResultArray(
245
			$subject,
246
			$printRequest,
247
			$this->jsonResponseParser
248
		);
249
250
		$this->assertDataItem(
251
			$dataItem,
252
			$instance
253
		);
254
	}
255
256
	private function assertDataItem( $dataItem, $instance ) {
257
258
		$this->assertEquals(
259
			$dataItem,
260
			$instance->getNextDataItem()
261
		);
262
263
		$instance->reset();
264
265
		$this->assertInstanceOf(
266
			'\SMWDataValue',
267
			$instance->getNextDataValue()
268
		);
269
270
		$this->assertEquals(
271
			array( $dataItem ),
272
			$instance->getContent()
273
		);
274
	}
275
276
}
277