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