|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SMW\Notifications\Tests\ChangeNotification; |
|
4
|
|
|
|
|
5
|
|
|
use SMW\Notifications\ChangeNotification\ChangeNotificationFormatter; |
|
6
|
|
|
use SMW\Notifications\ChangeNotification\ChangeNotificationFilter; |
|
7
|
|
|
use SMW\DIWikiPage; |
|
8
|
|
|
use SMW\Tests\TestEnvironment; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @covers \SMW\Notifications\ChangeNotification\ChangeNotificationFormatter |
|
12
|
|
|
* @group semantic-notifications |
|
13
|
|
|
* |
|
14
|
|
|
* @license GNU GPL v2+ |
|
15
|
|
|
* @since 1.0 |
|
16
|
|
|
* |
|
17
|
|
|
* @author mwjames |
|
18
|
|
|
*/ |
|
19
|
|
|
class ChangeNotificationFormatterTest 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
|
|
|
/** |
|
39
|
|
|
* @dataProvider typeProvider |
|
40
|
|
|
*/ |
|
41
|
|
|
public function testCanConstruct( $type ) { |
|
42
|
|
|
|
|
43
|
|
|
$this->assertInstanceOf( |
|
44
|
|
|
ChangeNotificationFormatter::class, |
|
45
|
|
|
ChangeNotificationFormatter::factory( $type ) |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @dataProvider typeProvider |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testFormatOnWebDistributionType( $type ) { |
|
53
|
|
|
|
|
54
|
|
|
$instance = ChangeNotificationFormatter::factory( $type ); |
|
55
|
|
|
|
|
56
|
|
|
$echoEvent = $this->getMockBuilder( '\EchoEvent' ) |
|
57
|
|
|
->disableOriginalConstructor() |
|
58
|
|
|
->getMock(); |
|
59
|
|
|
|
|
60
|
|
|
$echoEvent->expects( $this->any() ) |
|
61
|
|
|
->method( 'getType' ) |
|
62
|
|
|
->will( $this->returnValue( $type ) ); |
|
63
|
|
|
|
|
64
|
|
|
$user = $this->getMockBuilder( '\User' ) |
|
65
|
|
|
->disableOriginalConstructor() |
|
66
|
|
|
->getMock(); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertInternalType( |
|
69
|
|
|
'string', |
|
70
|
|
|
$instance->format( $echoEvent, $user, 'web' ) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @dataProvider typeProvider |
|
76
|
|
|
*/ |
|
77
|
|
|
public function testFormatOnEmailDistributionType( $type ) { |
|
78
|
|
|
|
|
79
|
|
|
$instance = ChangeNotificationFormatter::factory( $type ); |
|
80
|
|
|
|
|
81
|
|
|
$echoEvent = $this->getMockBuilder( '\EchoEvent' ) |
|
82
|
|
|
->disableOriginalConstructor() |
|
83
|
|
|
->getMock(); |
|
84
|
|
|
|
|
85
|
|
|
$echoEvent->expects( $this->any() ) |
|
86
|
|
|
->method( 'getType' ) |
|
87
|
|
|
->will( $this->returnValue( $type ) ); |
|
88
|
|
|
|
|
89
|
|
|
$user = $this->getMockBuilder( '\User' ) |
|
90
|
|
|
->disableOriginalConstructor() |
|
91
|
|
|
->getMock(); |
|
92
|
|
|
|
|
93
|
|
|
$this->assertInternalType( |
|
94
|
|
|
'string', |
|
95
|
|
|
$instance->format( $echoEvent, $user, 'email' ) |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function typeProvider() { |
|
100
|
|
|
|
|
101
|
|
|
$provider['specification-prop'] = array( |
|
|
|
|
|
|
102
|
|
|
ChangeNotificationFilter::SPECIFICATION_CHANGE, |
|
103
|
|
|
DIWikiPage::newFromText( 'Foo', SMW_NS_PROPERTY ) |
|
104
|
|
|
); |
|
105
|
|
|
|
|
106
|
|
|
$provider['specification-cat'] = array( |
|
107
|
|
|
ChangeNotificationFilter::SPECIFICATION_CHANGE, |
|
108
|
|
|
DIWikiPage::newFromText( 'Foo', NS_CATEGORY ) |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
$provider['value'] = array( |
|
112
|
|
|
ChangeNotificationFilter::VALUE_CHANGE, |
|
113
|
|
|
DIWikiPage::newFromText( 'Foo' ) |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
return $provider; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
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.