TermIndexEntryTest::newInstance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Wikibase\Lib\Tests;
4
5
use Wikibase\DataModel\Entity\Item;
6
use Wikibase\DataModel\Entity\ItemId;
7
use Wikibase\DataModel\Entity\PropertyId;
8
use Wikibase\DataModel\Term\Term;
9
use Wikibase\Lib\TermIndexEntry;
10
use Wikimedia\Assert\ParameterAssertionException;
11
12
/**
13
 * @covers \Wikibase\Lib\TermIndexEntry
14
 *
15
 * @group Wikibase
16
 *
17
 * @license GPL-2.0-or-later
18
 * @author Daniel Kinzler <[email protected]>
19
 */
20
class TermIndexEntryTest extends \PHPUnit\Framework\TestCase {
21
22
	public function testConstructor() {
23
		$term = new TermIndexEntry( [
24
			'entityId' => new ItemId( 'Q23' ),
25
			'termType' => TermIndexEntry::TYPE_LABEL,
26
			'termLanguage' => 'en',
27
			'termText' => 'foo',
28
		] );
29
30
		$this->assertEquals( new ItemId( 'Q23' ), $term->getEntityId() );
31
		$this->assertSame( Item::ENTITY_TYPE, $term->getEntityType() );
32
		$this->assertSame( TermIndexEntry::TYPE_LABEL, $term->getTermType() );
33
		$this->assertSame( 'en', $term->getLanguage() );
34
		$this->assertSame( 'foo', $term->getText() );
35
	}
36
37
	public function testGivenInvalidField_constructorThrowsException() {
38
		$this->expectException( ParameterAssertionException::class );
39
		new TermIndexEntry( [
40
			'entityId' => new ItemId( 'Q23' ),
41
			'termType' => TermIndexEntry::TYPE_LABEL,
42
			'termLanguage' => 'en',
43
			'termText' => 'foo',
44
			'fooField' => 'bar',
45
		] );
46
	}
47
48
	public function provideIncompleteFields() {
49
		return [
50
			[
51
				[
52
					'termType' => TermIndexEntry::TYPE_LABEL,
53
					'termLanguage' => 'en',
54
					'termText' => 'foo',
55
				]
56
			],
57
			[
58
				[
59
					'entityId' => new ItemId( 'Q23' ),
60
					'termType' => TermIndexEntry::TYPE_LABEL,
61
				]
62
			],
63
			[
64
				[
65
					'entityId' => new ItemId( 'Q23' ),
66
				]
67
			],
68
			[
69
				[]
70
			],
71
		];
72
	}
73
74
	/**
75
	 * @dataProvider provideIncompleteFields
76
	 */
77
	public function testGivenIncompleteFields_constructorThrowsException( $fields ) {
78
		$this->expectException( ParameterAssertionException::class );
79
		new TermIndexEntry( $fields );
80
	}
81
82
	public function provideInvalidValues() {
83
		$goodFields = [
84
			'entityId' => new ItemId( 'Q23' ),
85
			'termType' => TermIndexEntry::TYPE_LABEL,
86
			'termLanguage' => 'en',
87
			'termText' => 'foo',
88
		];
89
90
		return [
91
			'non-string term type' => [ array_merge( $goodFields, [ 'termType' => 100 ] ) ],
92
			'invalid term type' => [ array_merge( $goodFields, [ 'termType' => 'foo' ] ) ],
93
			'non-string term language' => [ array_merge( $goodFields, [ 'termLanguage' => 100 ] ) ],
94
			'non-string term text' => [ array_merge( $goodFields, [ 'termText' => 100 ] ) ],
95
			'non-EntityId as entity id' => [ array_merge( $goodFields, [ 'entityId' => 'foo' ] ) ],
96
		];
97
	}
98
99
	/**
100
	 * @dataProvider provideInvalidValues
101
	 */
102
	public function testGivenInvalidValues_constructorThrowsException( $fields ) {
103
		$this->expectException( ParameterAssertionException::class );
104
		new TermIndexEntry( $fields );
105
	}
106
107
	public function testClone() {
108
		$term = new TermIndexEntry( [
109
			'entityId' => new ItemId( 'Q23' ),
110
			'termType' => TermIndexEntry::TYPE_LABEL,
111
			'termLanguage' => 'en',
112
			'termText' => 'foo',
113
		] );
114
115
		$clone = clone $term;
116
		$this->assertEquals( $term, $clone, 'clone must be equal to original' );
117
	}
118
119
	/**
120
	 * @param array $extraFields
121
	 *
122
	 * @return TermIndexEntry
123
	 */
124
	private function newInstance( array $extraFields = [] ) {
125
		return new TermIndexEntry( $extraFields + [
126
				'entityId' => new ItemId( 'Q23' ),
127
				'termType' => TermIndexEntry::TYPE_LABEL,
128
				'termLanguage' => 'en',
129
				'termText' => 'foo',
130
			] );
131
	}
132
133
	public function provideCompare() {
134
		$term = $this->newInstance();
135
136
		return [
137
			'the same object' => [
138
				$term,
139
				$term,
140
				true,
141
			],
142
			'clone' => [
143
				$term,
144
				clone $term,
145
				true
146
			],
147
			'other text' => [
148
				$term,
149
				$this->newInstance( [ 'termText' => 'bar' ] ),
150
				false
151
			],
152
			'other entity id' => [
153
				$term,
154
				$this->newInstance( [ 'entityId' => new PropertyId( 'P11' ) ] ),
155
				false
156
			],
157
			'other language' => [
158
				$term,
159
				$this->newInstance( [ 'termLanguage' => 'fr' ] ),
160
				false
161
			],
162
			'other term type' => [
163
				$term,
164
				$this->newInstance( [ 'termType' => TermIndexEntry::TYPE_DESCRIPTION ] ),
165
				false
166
			],
167
		];
168
	}
169
170
	/**
171
	 * @dataProvider provideCompare
172
	 * @depends testClone
173
	 */
174
	public function testCompare( TermIndexEntry $a, TermIndexEntry $b, $equal ) {
175
		$ab = TermIndexEntry::compare( $a, $b );
0 ignored issues
show
Documentation introduced by
$a is of type object<Wikibase\Lib\TermIndexEntry>, but the function expects a object<self>.

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...
Documentation introduced by
$b is of type object<Wikibase\Lib\TermIndexEntry>, but the function expects a object<self>.

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...
176
		$ba = TermIndexEntry::compare( $b, $a );
0 ignored issues
show
Documentation introduced by
$b is of type object<Wikibase\Lib\TermIndexEntry>, but the function expects a object<self>.

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...
Documentation introduced by
$a is of type object<Wikibase\Lib\TermIndexEntry>, but the function expects a object<self>.

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...
177
178
		if ( $equal ) {
179
			$this->assertSame( 0, $ab, 'Comparison of equal terms is expected to return 0' );
180
			$this->assertSame( 0, $ba, 'Comparison of equal terms is expected to return 0' );
181
		} else {
182
			// NOTE: We don't know or care whether this is larger or smaller
183
			$this->assertNotSame( 0, $ab, 'Comparison of unequal terms is expected to not return 0' );
184
			$this->assertSame( -$ab, $ba, 'Comparing A to B should return the inverse of comparing B to A' );
185
		}
186
	}
187
188
	public function testGetTerm() {
189
		$termIndexEntry = new TermIndexEntry( [
190
			'entityId' => new ItemId( 'Q23' ),
191
			'termType' => TermIndexEntry::TYPE_LABEL,
192
			'termLanguage' => 'en',
193
			'termText' => 'foo',
194
		] );
195
		$this->assertEquals( new Term( 'en', 'foo' ), $termIndexEntry->getTerm() );
196
	}
197
198
}
199