Completed
Push — master ( 0cf5bd...a3625c )
by mw
11s
created

testFetchUsersByGroupIteration()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 27
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 17
nc 1
nop 1
1
<?php
2
3
namespace SMW\Notifications\Iterators\Tests;
4
5
use SMW\Notifications\Iterators\RecursiveMembersIterator;
6
use SMW\Tests\TestEnvironment;
7
use RecursiveIteratorIterator;
8
use SMW\Notifications\PropertyRegistry;
9
use SMW\DIWikiPage;
10
use SMW\DIProperty;
11
use SMWDIBlob as DIBlob;
12
use ArrayIterator;
13
14
/**
15
 * @covers \SMW\Notifications\Iterators\RecursiveMembersIterator
16
 * @group semantic-notifications
17
 *
18
 * @license GNU GPL v2+
19
 * @since 1.0
20
 *
21
 * @author mwjames
22
 */
23
class RecursiveMembersIteratorTest extends \PHPUnit_Framework_TestCase {
24
25
	private $store;
26
	private $testEnvironment;
27
28
	protected function setUp() {
29
30
		$this->store = $this->getMockBuilder( '\SMW\Store' )
31
			->disableOriginalConstructor()
32
			->getMockForAbstractClass();
33
34
		$this->testEnvironment = new TestEnvironment();
35
		$this->testEnvironment->registerObject( 'Store', $this->store );
36
	}
37
38
	protected function tearDown() {
39
		$this->testEnvironment->tearDown();
40
	}
41
42
	public function testCanConstruct() {
43
44
		$this->assertInstanceOf(
45
			RecursiveMembersIterator::class,
46
			new RecursiveMembersIterator( array(), $this->store )
47
		);
48
	}
49
50
	public function testEmptyGroup() {
51
52
		$expected = array();
0 ignored issues
show
Unused Code introduced by
$expected is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
53
54
		$instance = new RecursiveMembersIterator(
55
			array(),
56
			$this->store
57
		);
58
59
		$this->assertEmpty(
60
			$instance->next()
61
		);
62
	}
63
64
	/**
65
	 * @dataProvider singleGroupProvider
66
	 */
67
	public function testFetchUsersByGroupIteration( $group ) {
68
69
		$property = new DIProperty(
70
			PropertyRegistry::NOTIFICATIONS_GROUP_MEMBER_OF
71
		);
72
73
		$this->store->expects( $this->once() )
74
			->method( 'getPropertySubjects' )
75
			->with(
76
				$this->equalTo( $property ),
77
				$this->anything() )
78
			->will( $this->returnValue( array( DIWikiPage::newFromText( 'Bar', NS_USER ) ) ) );
79
80
		$instance = new RecursiveMembersIterator(
81
			$group,
82
			$this->store
83
		);
84
85
		$this->assertEmpty(
86
			$instance->next()
87
		);
88
89
		$this->assertEquals(
90
			array( 'Bar' ),
91
			$instance->current()
92
		);
93
	}
94
95
	/**
96
	 * @dataProvider singleGroupProvider
97
	 */
98
	public function testFetchUsersByGroupIterationButExcludeAgent( $group ) {
99
100
		$property = new DIProperty(
101
			PropertyRegistry::NOTIFICATIONS_GROUP_MEMBER_OF
102
		);
103
104
		$this->store->expects( $this->once() )
105
			->method( 'getPropertySubjects' )
106
			->with(
107
				$this->equalTo( $property ),
108
				$this->anything() )
109
			->will( $this->returnValue( array(
110
				DIWikiPage::newFromText( 'Bar', NS_USER ),
111
				DIWikiPage::newFromText( 'Tanaka Hiro', NS_USER ) ) ) );
112
113
		$instance = new RecursiveMembersIterator(
114
			$group,
115
			$this->store
116
		);
117
118
		$instance->setAgentName(
119
			'Tanaka Hiro'
120
		);
121
122
		$this->assertEmpty(
123
			$instance->next()
124
		);
125
126
		$this->assertEquals(
127
			array( 'Bar' ),
128
			$instance->current()
129
		);
130
	}
131
132
	/**
133
	 * @dataProvider singleGroupProvider
134
	 */
135
	public function testAllowToNotifAgent( $group ) {
136
137
		$property = new DIProperty(
138
			PropertyRegistry::NOTIFICATIONS_GROUP_MEMBER_OF
139
		);
140
141
		$this->store->expects( $this->once() )
142
			->method( 'getPropertySubjects' )
143
			->with(
144
				$this->equalTo( $property ),
145
				$this->anything() )
146
			->will( $this->returnValue( array(
147
				DIWikiPage::newFromText( 'Bar', NS_USER ),
148
				DIWikiPage::newFromText( 'Foo', NS_USER ) ) ) );
149
150
		$instance = new RecursiveMembersIterator(
151
			$group,
152
			$this->store
153
		);
154
155
		$instance->notifyAgent(
156
			true
157
		);
158
159
		$instance->setAgentName(
160
			'Foo'
161
		);
162
163
		$this->assertEmpty(
164
			$instance->next()
165
		);
166
167
		$this->assertEquals(
168
			array( 'Bar', 'Foo' ),
169
			$instance->current()
170
		);
171
	}
172
173
	/**
174
	 * @dataProvider multiGroupProvider
175
	 */
176
	public function testFetchUsersByMultiGroupIteration( $group, $count ) {
177
178
		$property = new DIProperty(
179
			PropertyRegistry::NOTIFICATIONS_GROUP_MEMBER_OF
180
		);
181
182
		$this->store->expects( $this->exactly( $count ) )
183
			->method( 'getPropertySubjects' )
184
			->with(
185
				$this->equalTo( $property ),
186
				$this->anything() )
187
			->will( $this->returnValue( array( DIWikiPage::newFromText( 'Bar', NS_USER ) ) ) );
188
189
		$this->store->expects( $this->any() )
190
			->method( 'getPropertyValues' )
191
			->will( $this->returnValue( array() ) );
192
193
		$instance = new RecursiveMembersIterator(
194
			$group,
195
			$this->store
196
		);
197
198
		$instance->setSubject(
199
			DIWikiPage::newFromText( __METHOD__ )
200
		);
201
202
		foreach ( $instance as $value ) {
203
			$this->assertEquals(
204
				array( 'Bar' ),
205
				$value
206
			);
207
		}
208
	}
209
210
	public function testFetchUsersByNotificationsTo() {
211
212
		$group = array();
213
214
		$property = new DIProperty(
215
			PropertyRegistry::NOTIFICATIONS_TO
216
		);
217
218
		$subject = DIWikiPage::newFromText( __METHOD__ );
219
220
		$this->store->expects( $this->exactly( 1 ) )
221
			->method( 'getPropertyValues' )
222
			->with(
223
				$this->equalTo( $subject ),
224
				$this->equalTo( $property ) )
225
			->will( $this->returnValue( array( DIWikiPage::newFromText( 'Bar', NS_USER ) ) ) );
226
227
		$this->store->expects( $this->any() )
228
			->method( 'getPropertySubjects' )
229
			->will( $this->returnValue( array() ) );
230
231
		$instance = new RecursiveMembersIterator(
232
			$group,
233
			$this->store
234
		);
235
236
		$instance->setSubject(
237
			$subject
238
		);
239
240
		foreach ( $instance as $value ) {
241
			$this->assertEquals(
242
				array( 'Bar' ),
243
				$value
244
			);
245
		}
246
	}
247
248
	public function singleGroupProvider() {
249
250
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
251
			array( 'Bar' => array( new DIBlob( 'Foo' ) ) )
252
		);
253
254
		$provider[] = array(
255
			array( new ArrayIterator( array( new DIBlob( 'Foo' ) ) ) )
256
		);
257
258
		return $provider;
259
	}
260
261
	public function multiGroupProvider() {
262
263
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
264
			new ArrayIterator( array( array( new DIBlob( 'Foo' ) ) ) ),
265
			1
266
		);
267
268
		$provider[] = array(
269
			array( 'Bar' => array(
270
				new DIBlob( 'Foo' ),
271
				new DIBlob( 'Foo-2' )
272
			) ),
273
			2
274
		);
275
276
		$provider[] = array(
277
			array(
278
				array(
279
					new DIBlob( 'Foo' ),
280
					new DIBlob( 'Foo-1' ),
281
					new DIBlob( 'Foo-2' ),
282
				)
283
			),
284
			3
285
		);
286
287
		return $provider;
288
	}
289
290
}
291