Completed
Push — master ( b1cd7c...5705a3 )
by mw
34:39
created

StringValueFormatterTest::stringValueProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 73
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 73
rs 9.0675
c 1
b 0
f 1
cc 1
eloc 50
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SMW\Tests\DataValues\ValueFormatters;
4
5
use SMW\DataValues\ValueFormatters\StringValueFormatter;
6
use SMWStringValue as StringValue;
7
8
/**
9
 * @covers \SMW\DataValues\ValueFormatters\StringValueFormatter
10
 * @group semantic-mediawiki
11
 *
12
 * @license GNU GPL v2+
13
 * @since 2.4
14
 *
15
 * @author mwjames
16
 */
17
class StringValueFormatterTest extends \PHPUnit_Framework_TestCase {
18
19
	public function testCanConstruct() {
20
21
		$this->assertInstanceOf(
22
			'\SMW\DataValues\ValueFormatters\StringValueFormatter',
23
			new StringValueFormatter()
24
		);
25
	}
26
27
	public function testIsFormatterForValidation() {
28
29
		$stringValue = $this->getMockBuilder( '\SMWStringValue' )
30
			->disableOriginalConstructor()
31
			->getMock();
32
33
		$instance = new StringValueFormatter();
34
35
		$this->assertTrue(
36
			$instance->isFormatterFor( $stringValue )
37
		);
38
	}
39
40
	public function testToUseCaptionOutput() {
41
42
		$stringValue = new StringValue( '_txt' );
43
		$stringValue->setCaption( 'ABC[<>]' );
44
45
		$instance = new StringValueFormatter( $stringValue );
46
47
		$this->assertEquals(
48
			'ABC[<>]',
49
			$instance->format( StringValueFormatter::WIKI_SHORT )
50
		);
51
52
		$this->assertEquals(
53
			'ABC[&lt;&gt;]',
54
			$instance->format( StringValueFormatter::HTML_SHORT )
55
		);
56
	}
57
58
	/**
59
	 * @dataProvider stringValueProvider
60
	 */
61
	public function testFormat( $stringUserValue, $type, $linker, $expected ) {
62
63
		$stringValue = new StringValue( '_txt' );
64
		$stringValue->setUserValue( $stringUserValue );
65
66
		$instance = new StringValueFormatter( $stringValue );
67
68
		$this->assertEquals(
69
			$expected,
70
			$instance->format( $type, $linker )
71
		);
72
	}
73
74
	public function testTryToFormatOnMissingDataValueThrowsException() {
75
76
		$instance = new StringValueFormatter();
77
78
		$this->setExpectedException( 'RuntimeException' );
79
		$instance->format( StringValueFormatter::VALUE );
80
	}
81
82
	public function stringValueProvider() {
83
84
		$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...
85
			'foo',
86
			StringValueFormatter::VALUE,
87
			null,
88
			'foo'
89
		);
90
91
		$provider[] = array(
92
			'foo',
93
			StringValueFormatter::WIKI_SHORT,
94
			null,
95
			'foo'
96
		);
97
98
		$provider[] = array(
99
			'foo',
100
			StringValueFormatter::HTML_SHORT,
101
			null,
102
			'foo'
103
		);
104
105
		$provider[] = array(
106
			'foo',
107
			StringValueFormatter::WIKI_LONG,
108
			null,
109
			'foo'
110
		);
111
112
		$provider[] = array(
113
			'foo',
114
			StringValueFormatter::HTML_LONG,
115
			null,
116
			'foo'
117
		);
118
119
		// > 255
120
		$text = 'Lorem ipsum dolor sit amet consectetuer justo Nam quis lobortis vel. Sapien nulla enim Lorem enim pede ' .
121
		'lorem nulla justo diam wisi. Libero Nam turpis neque leo scelerisque nec habitasse a lacus mattis. Accumsan ' .
122
		'tincidunt Sed adipiscing nec facilisis tortor Nunc Sed ipsum tellus';
123
124
		$provider[] = array(
125
			$text,
126
			StringValueFormatter::HTML_LONG,
127
			null,
128
			'Lorem ipsum dolor sit amet consectetuer ju <span class="smwwarning">…</span> nec facilisis tortor Nunc Sed ipsum tellus'
129
		);
130
131
		$provider[] = array(
132
			$text,
133
			StringValueFormatter::WIKI_LONG,
134
			null,
135
			'Lorem ipsum dolor sit amet consectetuer ju <span class="smwwarning">…</span> nec facilisis tortor Nunc Sed ipsum tellus'
136
		);
137
138
		// XMLContentEncode
139
		$provider[] = array(
140
			'<foo>',
141
			StringValueFormatter::HTML_LONG,
142
			null,
143
			'&lt;foo&gt;'
144
		);
145
146
		$provider[] = array(
147
			'<foo>',
148
			StringValueFormatter::HTML_SHORT,
149
			null,
150
			'&lt;foo&gt;'
151
		);
152
153
		return $provider;
154
	}
155
156
}
157