|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SMW\Notifications\Tests\ChangeNotification; |
|
4
|
|
|
|
|
5
|
|
|
use SMW\Notifications\ChangeNotification\UserLocator; |
|
6
|
|
|
use SMW\Notifications\ChangeNotification\ChangeNotificationFilter; |
|
7
|
|
|
use SMW\DIWikiPage; |
|
8
|
|
|
use SMW\Tests\TestEnvironment; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @covers \SMW\Notifications\ChangeNotification\UserLocator |
|
12
|
|
|
* @group semantic-notifications |
|
13
|
|
|
* |
|
14
|
|
|
* @license GNU GPL v2+ |
|
15
|
|
|
* @since 1.0 |
|
16
|
|
|
* |
|
17
|
|
|
* @author mwjames |
|
18
|
|
|
*/ |
|
19
|
|
|
class UserLocatorTest extends \PHPUnit_Framework_TestCase { |
|
20
|
|
|
|
|
21
|
|
|
private $store; |
|
22
|
|
|
private $testEnvironment; |
|
23
|
|
|
|
|
24
|
|
|
protected function setUp() { |
|
25
|
|
|
|
|
26
|
|
|
$this->store = $this->getMockBuilder( '\SMW\Store' ) |
|
27
|
|
|
->disableOriginalConstructor() |
|
28
|
|
|
->getMockForAbstractClass(); |
|
29
|
|
|
|
|
30
|
|
|
$this->testEnvironment = new TestEnvironment(); |
|
31
|
|
|
$this->testEnvironment->registerObject( 'Store', $this->store ); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function tearDown() { |
|
35
|
|
|
$this->testEnvironment->tearDown(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testCanConstruct() { |
|
39
|
|
|
|
|
40
|
|
|
$this->assertInstanceOf( |
|
41
|
|
|
UserLocator::class, |
|
42
|
|
|
new UserLocator() |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testDoLocateEventSubscribersOnEmptyExtra() { |
|
47
|
|
|
|
|
48
|
|
|
$echoEvent = $this->getMockBuilder( '\EchoEvent' ) |
|
49
|
|
|
->disableOriginalConstructor() |
|
50
|
|
|
->getMock(); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertEmpty( |
|
53
|
|
|
UserLocator::doLocateEventSubscribers( $echoEvent ) |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @dataProvider changeTypeProvider |
|
59
|
|
|
*/ |
|
60
|
|
|
public function testDoLocateEventSubscribersOnChange( $type ) { |
|
61
|
|
|
|
|
62
|
|
|
$extra = array( |
|
63
|
|
|
'subject' => DIWikiPage::newFromText( __METHOD__ ), |
|
64
|
|
|
'properties' => array() |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
$agent = $this->getMockBuilder( '\User' ) |
|
68
|
|
|
->disableOriginalConstructor() |
|
69
|
|
|
->getMock(); |
|
70
|
|
|
|
|
71
|
|
|
$echoEvent = $this->getMockBuilder( '\EchoEvent' ) |
|
72
|
|
|
->disableOriginalConstructor() |
|
73
|
|
|
->getMock(); |
|
74
|
|
|
|
|
75
|
|
|
$echoEvent->expects( $this->once() ) |
|
76
|
|
|
->method( 'getExtra' ) |
|
77
|
|
|
->will( $this->returnValue( $extra ) ); |
|
78
|
|
|
|
|
79
|
|
|
$echoEvent->expects( $this->atLeastOnce() ) |
|
80
|
|
|
->method( 'getAgent' ) |
|
81
|
|
|
->will( $this->returnValue( $agent ) ); |
|
82
|
|
|
|
|
83
|
|
|
$echoEvent->expects( $this->once() ) |
|
84
|
|
|
->method( 'getType' ) |
|
85
|
|
|
->will( $this->returnValue( $type ) ); |
|
86
|
|
|
|
|
87
|
|
|
$it = UserLocator::doLocateEventSubscribers( $echoEvent ); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertInstanceOf( |
|
90
|
|
|
'\Iterator', |
|
91
|
|
|
$it |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testIterationOnPropertyChangeGroup() { |
|
96
|
|
|
|
|
97
|
|
|
$extra = array( |
|
98
|
|
|
'subject' => DIWikiPage::newFromText( __METHOD__ ), |
|
99
|
|
|
'properties' => array() |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
$this->store->expects( $this->once() ) |
|
103
|
|
|
->method( 'getPropertySubjects' ) |
|
104
|
|
|
->will( $this->returnValue( array( DIWikiPage::newFromText( 'UserBar', NS_USER ) ) ) ); |
|
105
|
|
|
|
|
106
|
|
|
$agent = $this->getMockBuilder( '\User' ) |
|
107
|
|
|
->disableOriginalConstructor() |
|
108
|
|
|
->getMock(); |
|
109
|
|
|
|
|
110
|
|
|
$echoEvent = $this->getMockBuilder( '\EchoEvent' ) |
|
111
|
|
|
->disableOriginalConstructor() |
|
112
|
|
|
->getMock(); |
|
113
|
|
|
|
|
114
|
|
|
$echoEvent->expects( $this->once() ) |
|
115
|
|
|
->method( 'getExtra' ) |
|
116
|
|
|
->will( $this->returnValue( $extra ) ); |
|
117
|
|
|
|
|
118
|
|
|
$echoEvent->expects( $this->atLeastOnce() ) |
|
119
|
|
|
->method( 'getAgent' ) |
|
120
|
|
|
->will( $this->returnValue( $agent ) ); |
|
121
|
|
|
|
|
122
|
|
|
$echoEvent->expects( $this->once() ) |
|
123
|
|
|
->method( 'getType' ) |
|
124
|
|
|
->will( $this->returnValue( ChangeNotificationFilter::SPECIFICATION_CHANGE ) ); |
|
125
|
|
|
|
|
126
|
|
|
$it = UserLocator::doLocateEventSubscribers( $echoEvent ); |
|
127
|
|
|
|
|
128
|
|
|
foreach ( $it as $user ) { |
|
129
|
|
|
$this->assertSame( |
|
130
|
|
|
'UserBar', |
|
131
|
|
|
$user->getName() |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function changeTypeProvider() { |
|
137
|
|
|
|
|
138
|
|
|
$provider[ChangeNotificationFilter::SPECIFICATION_CHANGE] = array( |
|
|
|
|
|
|
139
|
|
|
ChangeNotificationFilter::SPECIFICATION_CHANGE |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
$provider[ChangeNotificationFilter::VALUE_CHANGE] = array( |
|
143
|
|
|
ChangeNotificationFilter::VALUE_CHANGE |
|
144
|
|
|
); |
|
145
|
|
|
|
|
146
|
|
|
return $provider; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
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:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.