Passed
Push — removeDuplicates ( 27f909...a8cc42 )
by no
10:34 queued 07:29
created

HashArrayWithoutDuplicatesTest::testGetHash()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
namespace Wikibase\DataModel\Tests\HashArray;
4
5
use Wikibase\DataModel\Fixtures\HashArrayElement;
6
use Wikibase\DataModel\Fixtures\MutableHashable;
7
use Wikibase\DataModel\HashArray;
8
9
/**
10
 * @covers Wikibase\DataModel\HashArray
11
 *
12
 * @group Wikibase
13
 * @group WikibaseDataModel
14
 * @group HashArray
15
 *
16
 * @license GPL-2.0+
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class HashArrayWithoutDuplicatesTest extends HashArrayTest {
20
21
	public function constructorProvider() {
22
		$argLists = [];
23
24
		$argLists[] = [ HashArrayElement::getInstances() ];
25
		$argLists[] = [ array_merge( HashArrayElement::getInstances(), HashArrayElement::getInstances() ) ];
26
27
		return $argLists;
28
	}
29
30
	public function getInstanceClass() {
31
		return 'Wikibase\DataModel\Fixtures\HashArrayWithoutDuplicates';
32
	}
33
34
	public function elementInstancesProvider() {
35
		return $this->arrayWrap( array_merge(
36
			$this->arrayWrap( HashArrayElement::getInstances() ),
37
			[ HashArrayElement::getInstances() ]
38
		) );
39
	}
40
41
	/**
42
	 * @dataProvider instanceProvider
43
	 * @param HashArray $array
44
	 */
45
	public function testAddElement( HashArray $array ) {
46
		$elementCount = $array->count();
47
48
		$elements = $this->elementInstancesProvider();
49
		$element = array_shift( $elements );
50
		$element = $element[0][0];
51
52
		if ( !$array->hasElement( $element ) ) {
53
			++$elementCount;
54
		}
55
56
		$this->assertEquals(
57
			!$array->hasElement( $element ),
58
			$array->addElement( $element ),
59
			'Adding an element should only work if its not already there'
60
		);
61
62
		$this->assertEquals(
63
			$elementCount,
64
			$array->count(),
65
			'Element count should only increase if the element was not there yet'
66
		);
67
68
		$this->assertFalse(
69
			$array->addElement( $element ),
70
			'Adding an already present element should not work'
71
		);
72
73
		$this->assertEquals(
74
			$elementCount,
75
			$array->count(),
76
			'Element count should not increase if the element is already there'
77
		);
78
	}
79
80
	/**
81
	 * @dataProvider instanceProvider
82
	 * @param HashArray $array
83
	 */
84
	public function testGetHash( HashArray $array ) {
85
		$hash = $array->getHash();
86
87
		$this->assertSame( $hash, $array->getHash() );
88
89
		$elements = $this->elementInstancesProvider();
90
		$element = array_shift( $elements );
91
		$element = $element[0][0];
92
93
		$array->addElement( $element );
94
95
		if ( $array->hasElement( $element ) ) {
96
			$this->assertSame( $hash, $array->getHash() );
97
		} else {
98
			$this->assertNotSame( $hash, $array->getHash() );
99
		}
100
	}
101
102
	/**
103
	 * @dataProvider instanceProvider
104
	 * @param HashArray $array
105
	 */
106
	public function testIndicesAreUpToDate( HashArray $array ) {
107
		$this->assertInternalType( 'boolean', $array->indicesAreUpToDate() );
108
109
		$mutable = new MutableHashable();
110
111
		$array->addElement( $mutable );
112
113
		$mutable->text = '~[,,_,,]:3';
114
115
		$this->assertFalse( $array->indicesAreUpToDate() );
116
117
		$array->rebuildIndices();
118
119
		$this->assertTrue( $array->indicesAreUpToDate() );
120
	}
121
122
}
123