Completed
Push — master ( 3abc67...80e892 )
by mw
207:38 queued 172:37
created

titleProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 112
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 78
nc 1
nop 0
dl 0
loc 112
rs 8.2857
c 0
b 0
f 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 SMW\Tests\PropertyAnnotators;
4
5
use SMW\DIWikiPage;
6
use SMW\PropertyAnnotators\EditProtectedPropertyAnnotator;
7
use SMW\PropertyAnnotators\NullPropertyAnnotator;
8
use SMW\Tests\TestEnvironment;
9
use SMW\DataItemFactory;
10
11
/**
12
 * @covers \SMW\PropertyAnnotators\EditProtectedPropertyAnnotator
13
 * @group semantic-mediawiki
14
 *
15
 * @license GNU GPL v2+
16
 * @since 2.5
17
 *
18
 * @author mwjames
19
 */
20
class EditProtectedPropertyAnnotatorTest extends \PHPUnit_Framework_TestCase {
21
22
	private $semanticDataFactory;
23
	private $semanticDataValidator;
24
	private $dataItemFactory;
25
26
	protected function setUp() {
27
		parent::setUp();
28
29
		$testEnvironment = new TestEnvironment();
30
		$this->dataItemFactory = new DataItemFactory();
31
32
		$this->semanticDataFactory = $testEnvironment->getUtilityFactory()->newSemanticDataFactory();
33
		$this->semanticDataValidator = $testEnvironment->getUtilityFactory()->newValidatorFactory()->newSemanticDataValidator();
34
	}
35
36
	public function testCanConstruct() {
37
38
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
39
			->disableOriginalConstructor()
40
			->getMock();
41
42
		$title = $this->getMockBuilder( '\Title' )
43
			->disableOriginalConstructor()
44
			->getMock();
45
46
		$instance = new EditProtectedPropertyAnnotator(
47
			new NullPropertyAnnotator( $semanticData ),
48
			$title
49
		);
50
51
		$this->assertInstanceOf(
52
			'\SMW\PropertyAnnotators\EditProtectedPropertyAnnotator',
53
			$instance
54
		);
55
	}
56
57
	/**
58
	 * @dataProvider titleProvider
59
	 */
60
	public function testAddAnnotationForDisplayTitle( $title, $editProtectionRight, array $expected ) {
61
62
		$semanticData = $this->semanticDataFactory->newEmptySemanticData(
63
			$title
64
		);
65
66
		$instance = new EditProtectedPropertyAnnotator(
67
			new NullPropertyAnnotator( $semanticData ),
68
			$title
69
		);
70
71
		$instance->setEditProtectionRight( $editProtectionRight );
72
		$instance->addAnnotation();
73
74
		$this->semanticDataValidator->assertThatPropertiesAreSet(
75
			$expected,
76
			$instance->getSemanticData()
77
		);
78
	}
79
80
	public function testAddTopIndicatorToFromMatchableRestriction() {
81
82
		$parserOutput = $this->getMockBuilder( '\ParserOutput' )
83
			->disableOriginalConstructor()
84
			->getMock();
85
86
		$parserOutput->expects( $this->once() )
87
			->method( 'setIndicator' );
88
89
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
90
			->disableOriginalConstructor()
91
			->getMock();
92
93
		$title = $this->getMockBuilder( '\Title' )
94
			->disableOriginalConstructor()
95
			->getMock();
96
97
		$title->expects( $this->once() )
98
			->method( 'isProtected' )
99
			->with( $this->equalTo( 'edit' ) )
100
			->will( $this->returnValue( true ) );
101
102
		$title->expects( $this->once() )
103
			->method( 'getRestrictions' )
104
			->will( $this->returnValue( array( 'Foo' ) ) );
105
106
		$instance = new EditProtectedPropertyAnnotator(
107
			new NullPropertyAnnotator( $semanticData ),
108
			$title
109
		);
110
111
		$instance->setEditProtectionRight( 'Foo' );
112
		$instance->addTopIndicatorTo( $parserOutput );
113
	}
114
115
	public function titleProvider() {
116
117
		$title = $this->getMockBuilder( '\Title' )
118
			->disableOriginalConstructor()
119
			->getMock();
120
121
		$title->expects( $this->any() )
122
			->method( 'getDBKey' )
123
			->will( $this->returnValue( 'Foo' ) );
124
125
		$title->expects( $this->any() )
126
			->method( 'getNamespace' )
127
			->will( $this->returnValue( 0 ) );
128
129
		$title->expects( $this->any() )
130
			->method( 'isProtected' )
131
			->with( $this->equalTo( 'edit' ) )
132
			->will( $this->returnValue( true ) );
133
134
		$title->expects( $this->any() )
135
			->method( 'getRestrictions' )
136
			->will( $this->returnValue( array() ) );
137
138
		$provider = array();
139
140
		#0 no EditProtectionRight
141
		$provider[] = array(
142
			$title,
143
			false,
144
			array(
145
				'propertyCount'  => 0,
146
				'propertyKeys'   => array(),
147
				'propertyValues' => array(),
148
			)
149
		);
150
151
		#1
152
		$provider[] = array(
153
			$title,
154
			'Foo',
155
			array(
156
				'propertyCount'  => 0,
157
				'propertyKeys'   => array(),
158
				'propertyValues' => array(),
159
			)
160
		);
161
162
		$title = $this->getMockBuilder( '\Title' )
163
			->disableOriginalConstructor()
164
			->getMock();
165
166
		$title->expects( $this->any() )
167
			->method( 'getDBKey' )
168
			->will( $this->returnValue( 'Foo' ) );
169
170
		$title->expects( $this->any() )
171
			->method( 'getNamespace' )
172
			->will( $this->returnValue( 0 ) );
173
174
		$title->expects( $this->any() )
175
			->method( 'isProtected' )
176
			->with( $this->equalTo( 'edit' ) )
177
			->will( $this->returnValue( true ) );
178
179
		$title->expects( $this->any() )
180
			->method( 'getRestrictions' )
181
			->will( $this->returnValue( array( 'Foo' ) ) );
182
183
		#2
184
		$provider[] = array(
185
			$title,
186
			'Foo',
187
			array(
188
				'propertyCount'  => 1,
189
				'propertyKeys'   => array( '_EDIP' ),
190
				'propertyValues' => array( true ),
191
			)
192
		);
193
194
		$title = $this->getMockBuilder( '\Title' )
195
			->disableOriginalConstructor()
196
			->getMock();
197
198
		$title->expects( $this->any() )
199
			->method( 'getDBKey' )
200
			->will( $this->returnValue( 'Foo' ) );
201
202
		$title->expects( $this->any() )
203
			->method( 'getNamespace' )
204
			->will( $this->returnValue( 0 ) );
205
206
		$title->expects( $this->any() )
207
			->method( 'isProtected' )
208
			->with( $this->equalTo( 'edit' ) )
209
			->will( $this->returnValue( false ) );
210
211
		$title->expects( $this->never() )
212
			->method( 'getRestrictions' );
213
214
		#3
215
		$provider[] = array(
216
			$title,
217
			'Foo',
218
			array(
219
				'propertyCount'  => 0,
220
				'propertyKeys'   => array(),
221
				'propertyValues' => array(),
222
			)
223
		);
224
225
		return $provider;
226
	}
227
228
}
229