Completed
Push — indicesAreUpToDate ( 2c2f48 )
by no
15:26 queued 11:27
created

HashArrayWithDuplicatesTest::testIndicesAreUpToDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
3
namespace Wikibase\DataModel\Tests\HashArray;
4
5
use Hashable;
6
use Wikibase\DataModel\Fixtures\HashArrayElement;
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 HashArrayWithDuplicatesTest 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\HashArrayWithDuplicates';
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
		++$elementCount;
53
54
		$this->assertTrue( $array->addElement( $element ), 'Adding an element should always work' );
55
56
		$this->assertEquals( $elementCount, $array->count(), 'Adding an element should always increase the count' );
57
58
		$this->assertTrue( $array->addElement( $element ), 'Adding an element should always work' );
59
	}
60
61
	/**
62
	 * @dataProvider instanceProvider
63
	 * @param HashArray $array
64
	 */
65
	public function testRemoveDuplicates( HashArray $array ) {
66
		$count = count( $array );
67
		$duplicateCount = 0;
68
		$hashes = [];
69
70
		/**
71
		 * @var Hashable $hashable
72
		 */
73
		foreach ( $array as $hashable ) {
74
			if ( in_array( $hashable->getHash(), $hashes ) ) {
75
				$duplicateCount++;
76
			}
77
			else {
78
				$hashes[] = $hashable->getHash();
79
			}
80
		}
81
82
		$array->removeDuplicates();
83
84
		$this->assertEquals(
85
			$count - $duplicateCount,
86
			count( $array ),
87
			'Count should decrease by the number of duplicates after removing duplicates'
88
		);
89
	}
90
91
	/**
92
	 * @dataProvider instanceProvider
93
	 * @param HashArray $array
94
	 */
95
	public function testGetHash( HashArray $array ) {
96
		$hash = $array->getHash();
97
98
		$this->assertEquals( $hash, $array->getHash() );
99
100
		$elements = $this->elementInstancesProvider();
101
		$element = array_shift( $elements );
102
		$element = $element[0][0];
103
104
		$array->addElement( $element );
105
106
		$newHash = $array->getHash();
107
108
		$this->assertNotSame( $hash, $newHash, 'Hash should not be the same after adding an element' );
109
110
		$array->addElement( $element );
111
112
		$this->assertNotSame(
113
			$newHash,
114
			$array->getHash(),
115
			'Hash should not be the same after adding an existing element again'
116
		);
117
	}
118
119
}
120