Completed
Push — master ( b58f0a...813ff2 )
by mw
366:50 queued 331:40
created

ErrorMsgTextValueTest::testShortWikiText()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 26
rs 8.8571
1
<?php
2
3
namespace SMW\Tests\DataValues;
4
5
use SMW\DataValues\ErrorMsgTextValue;
6
use SMW\DataItemFactory;
7
8
/**
9
 * @covers \SMW\DataValues\ErrorMsgTextValue
10
 * @group semantic-mediawiki
11
 *
12
 * @license GNU GPL v2+
13
 * @since 2.5
14
 *
15
 * @author mwjames
16
 */
17
class ErrorMsgTextValueTest extends \PHPUnit_Framework_TestCase {
18
19
	private $dataItemFactory;
20
21
	protected function setUp() {
22
		$this->dataItemFactory = new DataItemFactory();
23
	}
24
25
	public function testCanConstruct() {
26
27
		$this->assertInstanceOf(
28
			'\SMW\DataValues\ErrorMsgTextValue',
29
			new ErrorMsgTextValue()
30
		);
31
	}
32
33
	public function testErrorOnEmptyUserValue() {
34
35
		$instance = new ErrorMsgTextValue();
36
		$instance->setUserValue( '' );
37
38
		$this->assertNotEmpty(
39
			$instance->getErrors()
40
		);
41
	}
42
43
	public function testShortWikiText() {
44
45
		$instance = new ErrorMsgTextValue();
46
		$instance->setOption( ErrorMsgTextValue::OPT_USER_LANGUAGE, 'en' );
0 ignored issues
show
Documentation introduced by
'en' is of type string, but the function expects a object<mxied>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
		$instance->setUserValue( '[2,"smw-datavalue-uniqueness-constraint-error","Has Url","http:\/\/loremipsum.org\/2","Lorem ipsum\/2"]' );
48
49
		$this->assertContains(
50
			"''http://loremipsum.org/2''",
51
			$instance->getShortWikiText( true )
52
		);
53
54
		$this->assertContains(
55
			"''http://loremipsum.org/2''",
56
			$instance->getShortWikiText( null )
57
		);
58
59
		$this->assertNotContains(
60
			'<a rel="nofollow" class="external free" href="http://loremipsum.org/2">http://loremipsum.org/2</a>',
61
			$instance->getShortWikiText( true )
62
		);
63
64
		$this->assertNotContains(
65
			'<a rel="nofollow" class="external free" href="http://loremipsum.org/2">http://loremipsum.org/2</a>',
66
			$instance->getShortWikiText( null )
67
		);
68
	}
69
70
	/**
71
	 * @dataProvider textProvider
72
	 */
73
	public function testValueOutput( $text, $expected ) {
74
75
		$dataItem = $this->dataItemFactory->newDIBlob( $text );
76
77
		$instance = new ErrorMsgTextValue();
78
		$instance->setDataItem( $dataItem );
79
80
		$this->assertEquals(
81
			$expected,
82
			$instance->getWikiValue()
83
		);
84
85
		$this->assertEquals(
86
			$expected,
87
			$instance->getShortWikiText()
88
		);
89
90
		$this->assertEquals(
91
			$expected,
92
			$instance->getShortHTMLText()
93
		);
94
95
		$this->assertEquals(
96
			$expected,
97
			$instance->getLongWikiText()
98
		);
99
100
		$this->assertEquals(
101
			$expected,
102
			$instance->getLongHTMLText()
103
		);
104
	}
105
106
	public function textProvider() {
107
108
		$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...
109
			'Foo',
110
			'Foo'
111
		);
112
113
		$provider[] = array(
114
			'[2,"Foo"]',
115
			'Foo'
116
		);
117
118
		return $provider;
119
	}
120
121
}
122