Completed
Push — master ( f1a924...85530c )
by mw
07:12
created

testAllowToNotifAgent()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 37
rs 8.8571
cc 1
eloc 23
nc 1
nop 1
1
<?php
2
3
namespace SMW\Notifications\Iterators\Tests;
4
5
use SMW\Notifications\Iterators\RecursiveGroupMembersIterator;
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\RecursiveGroupMembersIterator
16
 * @group semantic-notifications
17
 *
18
 * @license GNU GPL v2+
19
 * @since 1.0
20
 *
21
 * @author mwjames
22
 */
23
class RecursiveGroupMembersIteratorTest 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
			RecursiveGroupMembersIterator::class,
46
			new RecursiveGroupMembersIterator( 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 RecursiveGroupMembersIterator(
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 RecursiveGroupMembersIterator(
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 RecursiveGroupMembersIterator(
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 RecursiveGroupMembersIterator(
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
		$instance = new RecursiveGroupMembersIterator(
190
			$group,
191
			$this->store
192
		);
193
194
		$instance->setSubject(
195
			DIWikiPage::newFromText( __METHOD__ )
196
		);
197
198
		foreach ( $instance as $value ) {
199
			$this->assertEquals(
200
				array( 'Bar' ),
201
				$value
202
			);
203
		}
204
	}
205
206
	public function singleGroupProvider() {
207
208
		$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...
209
			array( 'Bar' => array( new DIBlob( 'Foo' ) ) )
210
		);
211
212
		$provider[] = array(
213
			array( new ArrayIterator( array( new DIBlob( 'Foo' ) ) ) )
214
		);
215
216
		return $provider;
217
	}
218
219
	public function multiGroupProvider() {
220
221
		$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...
222
			new ArrayIterator( array( array( new DIBlob( 'Foo' ) ) ) ),
223
			1
224
		);
225
226
		$provider[] = array(
227
			array( 'Bar' => array(
228
				new DIBlob( 'Foo' ),
229
				new DIBlob( 'Foo-2' )
230
			) ),
231
			2
232
		);
233
234
		$provider[] = array(
235
			array(
236
				array(
237
					new DIBlob( 'Foo' ),
238
					new DIBlob( 'Foo-1' ),
239
					new DIBlob( 'Foo-2' ),
240
				)
241
			),
242
			3
243
		);
244
245
		return $provider;
246
	}
247
248
}
249