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

ChangeNotificationFormatterTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 101
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A tearDown() 0 3 1
A testCanConstruct() 0 7 1
A testFormatOnWebDistributionType() 0 21 1
A testFormatOnEmailDistributionType() 0 21 1
A typeProvider() 0 19 1
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(
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...
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