testDoLocateEventSubscribersOnEmptyExtra()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
		$this->store->expects( $this->once() )
107
			->method( 'getPropertyValues' )
108
			->will( $this->returnValue( array() ) );
109
110
		$agent = $this->getMockBuilder( '\User' )
111
			->disableOriginalConstructor()
112
			->getMock();
113
114
		$echoEvent = $this->getMockBuilder( '\EchoEvent' )
115
			->disableOriginalConstructor()
116
			->getMock();
117
118
		$echoEvent->expects( $this->once() )
119
			->method( 'getExtra' )
120
			->will( $this->returnValue( $extra ) );
121
122
		$echoEvent->expects( $this->atLeastOnce() )
123
			->method( 'getAgent' )
124
			->will( $this->returnValue( $agent ) );
125
126
		$echoEvent->expects( $this->once() )
127
			->method( 'getType' )
128
			->will( $this->returnValue( ChangeNotificationFilter::SPECIFICATION_CHANGE ) );
129
130
		$it = UserLocator::doLocateEventSubscribers( $echoEvent );
131
132
		foreach ( $it as $user ) {
133
			$this->assertSame(
134
				'UserBar',
135
				$user->getName()
136
			);
137
		}
138
	}
139
140
	public function changeTypeProvider() {
141
142
		$provider[ChangeNotificationFilter::SPECIFICATION_CHANGE] = 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...
143
			ChangeNotificationFilter::SPECIFICATION_CHANGE
144
		);
145
146
		$provider[ChangeNotificationFilter::VALUE_CHANGE] = array(
147
			ChangeNotificationFilter::VALUE_CHANGE
148
		);
149
150
		return $provider;
151
	}
152
153
}
154