Completed
Push — master ( 5f9e66...1f3475 )
by mw
123:57 queued 89:01
created

tests/phpunit/Unit/Query/QueryTokenTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SMW\Tests\Query;
4
5
use SMW\Query\QueryToken;
6
use SMW\DataItemFactory;
7
8
/**
9
 * @covers \SMW\Query\QueryToken
10
 * @group semantic-mediawiki
11
 *
12
 * @license GNU GPL v2+
13
 * @since 2.5
14
 *
15
 * @author mwjames
16
 */
17
class QueryTokenTest extends \PHPUnit_Framework_TestCase {
18
19
	private $dataItemFactory;
20
21
	protected function setUp() {
22
		parent::setUp();
23
24
		$this->dataItemFactory = new DataItemFactory();
25
	}
26
27
	public function testCanConstruct() {
28
29
		$this->assertInstanceOf(
30
			QueryToken::class,
31
			new QueryToken()
32
		);
33
	}
34
35
	/**
36
	 * @dataProvider descriptionProvider
37
	 */
38
	public function testAddFromDesciption( $description, $expected ) {
39
40
		$instance = new QueryToken();
41
42
		$instance->addFromDesciption( $description );
43
44
		$this->assertEquals(
45
			$expected,
46
			$instance->getTokens()
47
		);
48
	}
49
50
	public function testMulitpleAddFromDesciption() {
51
52
		$instance = new QueryToken();
53
54
		$description = $this->getMockBuilder( '\SMW\Query\Language\ValueDescription' )
55
			->disableOriginalConstructor()
56
			->getMock();
57
58
		$description->expects( $this->once() )
59
			->method( 'getComparator' )
60
			->will( $this->returnValue( SMW_CMP_LIKE ) );
61
62
		$description->expects( $this->atLeastOnce() )
63
			->method( 'getDataItem' )
64
			->will( $this->returnValue( $this->dataItemFactory->newDIBlob( 'abc Foo 123' ) ) );
65
66
		$instance->addFromDesciption( $description );
67
68
		$description = $this->getMockBuilder( '\SMW\Query\Language\ValueDescription' )
69
			->disableOriginalConstructor()
70
			->getMock();
71
72
		$description->expects( $this->atLeastOnce() )
73
			->method( 'getDataItem' )
74
			->will( $this->returnValue( $this->dataItemFactory->newDIWikiPage( '~*123 bar 456' ) ) );
75
76
		$instance->addFromDesciption( $description );
77
78
		$this->assertEquals(
79
			array(
80
				'abc' => 0,
81
				'Foo' => 1,
82
				123 => 2,
83
				'bar' => 1,
84
				456 => 2
85
			),
86
			$instance->getTokens()
87
		);
88
	}
89
90
	/**
91
	 * @dataProvider highlightProvider
92
	 */
93
	public function testHighlight( $description, $text, $type, $expected ) {
94
95
		$instance = new QueryToken();
96
97
		$instance->addFromDesciption( $description );
98
		$instance->canHighlight( '-hL' );
99
100
		$this->assertEquals(
101
			$expected,
102
			$instance->highlight( $text, $type )
103
		);
104
	}
105
106
	public function descriptionProvider() {
107
108
		$dataItemFactory = new DataItemFactory();
109
110
		$description = $this->getMockBuilder( '\SMW\Query\Language\ValueDescription' )
111
			->disableOriginalConstructor()
112
			->getMock();
113
114
		$description->expects( $this->once() )
115
			->method( 'getComparator' )
116
			->will( $this->returnValue( SMW_CMP_LIKE ) );
117
118
		$description->expects( $this->atLeastOnce() )
119
			->method( 'getDataItem' )
120
			->will( $this->returnValue( $dataItemFactory->newDIBlob( 'abc Foo 123' ) ) );
121
122
		$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...
123
			$description,
124
			array(
125
				'abc' => 0,
126
				'Foo' => 1,
127
				123 => 2
128
			)
129
		);
130
131
		return $provider;
132
	}
133
134
	public function highlightProvider() {
135
136
		$dataItemFactory = new DataItemFactory();
137
138
		$description = $this->getMockBuilder( '\SMW\Query\Language\ValueDescription' )
139
			->disableOriginalConstructor()
140
			->getMock();
141
142
		$description->expects( $this->any() )
143
			->method( 'getComparator' )
144
			->will( $this->returnValue( SMW_CMP_LIKE ) );
145
146
		$description->expects( $this->any() )
147
			->method( 'getDataItem' )
148
			->will( $this->returnValue( $dataItemFactory->newDIBlob( 'abc Foo 123 foobar' ) ) );
149
150
		$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...
151
			$description,
152
			'Lorem abc foobar',
153
			QueryToken::HL_BOLD,
154
			"Lorem <b>abc</b> <b>foo</b>bar"
155
		);
156
157
		$provider[] = array(
158
			$description,
159
			'Lorem abc foobar',
160
			QueryToken::HL_WIKI,
161
			"Lorem '''abc''' '''foo'''bar"
162
		);
163
164
		$provider[] = array(
165
			$description,
166
			'Lorem abc foobar',
167
			QueryToken::HL_UNDERLINE,
168
			"Lorem <u>abc</u> <u>foo</u>bar"
169
		);
170
171
		$description = $this->getMockBuilder( '\SMW\Query\Language\ValueDescription' )
172
			->disableOriginalConstructor()
173
			->getMock();
174
175
		$description->expects( $this->any() )
176
			->method( 'getComparator' )
177
			->will( $this->returnValue( SMW_CMP_LIKE ) );
178
179
		$description->expects( $this->any() )
180
			->method( 'getDataItem' )
181
			->will( $this->returnValue( $dataItemFactory->newDIBlob( 'integer porttitor portt' ) ) );
182
183
		$provider[] = array(
184
			$description,
185
			'Integer porttitor mi id ante consequat consequat <b>porttitor</b>',
186
			QueryToken::HL_BOLD,
187
			"<b>Integer</b> <b>porttitor</b> mi id ante consequat consequat <b><b>porttitor</b></b>"
188
		);
189
190
		$provider[] = array(
191
			$description,
192
			'Integer porttitor mi id ante consequat consequat <b>porttitor</b>',
193
			QueryToken::HL_SPAN,
194
			"<span class='smw-query-token'>Integer</span> <span class='smw-query-token'>porttitor</span> mi id ante consequat consequat <b><span class='smw-query-token'>porttitor</span></b>"
195
		);
196
197
		return $provider;
198
	}
199
200
}
201