|
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
|
|
|
use Wikibase\DataModel\Snak\PropertyNoValueSnak; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @covers Wikibase\DataModel\HashArray |
|
12
|
|
|
* |
|
13
|
|
|
* @group Wikibase |
|
14
|
|
|
* @group WikibaseDataModel |
|
15
|
|
|
* @group HashArray |
|
16
|
|
|
* |
|
17
|
|
|
* @license GPL-2.0+ |
|
18
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
19
|
|
|
*/ |
|
20
|
|
|
class HashArrayWithoutDuplicatesTest extends HashArrayTest { |
|
21
|
|
|
|
|
22
|
|
|
public function constructorProvider() { |
|
23
|
|
|
$argLists = []; |
|
24
|
|
|
|
|
25
|
|
|
$argLists[] = [ HashArrayElement::getInstances() ]; |
|
26
|
|
|
$argLists[] = [ array_merge( HashArrayElement::getInstances(), HashArrayElement::getInstances() ) ]; |
|
27
|
|
|
|
|
28
|
|
|
return $argLists; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getInstanceClass() { |
|
32
|
|
|
return 'Wikibase\DataModel\Fixtures\HashArrayWithoutDuplicates'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function elementInstancesProvider() { |
|
36
|
|
|
return $this->arrayWrap( array_merge( |
|
37
|
|
|
$this->arrayWrap( HashArrayElement::getInstances() ), |
|
38
|
|
|
[ HashArrayElement::getInstances() ] |
|
39
|
|
|
) ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @dataProvider instanceProvider |
|
44
|
|
|
* @param HashArray $array |
|
45
|
|
|
*/ |
|
46
|
|
|
public function testAddElement( HashArray $array ) { |
|
47
|
|
|
$elementCount = $array->count(); |
|
48
|
|
|
|
|
49
|
|
|
$elements = $this->elementInstancesProvider(); |
|
50
|
|
|
$element = array_shift( $elements ); |
|
51
|
|
|
$element = $element[0][0]; |
|
52
|
|
|
|
|
53
|
|
|
if ( !$array->hasElement( $element ) ) { |
|
54
|
|
|
++$elementCount; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->assertEquals( |
|
58
|
|
|
!$array->hasElement( $element ), |
|
59
|
|
|
$array->addElement( $element ), |
|
60
|
|
|
'Adding an element should only work if its not already there' |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertEquals( |
|
64
|
|
|
$elementCount, |
|
65
|
|
|
$array->count(), |
|
66
|
|
|
'Element count should only increase if the element was not there yet' |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertFalse( |
|
70
|
|
|
$array->addElement( $element ), |
|
71
|
|
|
'Adding an already present element should not work' |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertEquals( |
|
75
|
|
|
$elementCount, |
|
76
|
|
|
$array->count(), |
|
77
|
|
|
'Element count should not increase if the element is already there' |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @dataProvider instanceProvider |
|
83
|
|
|
* @param HashArray $array |
|
84
|
|
|
*/ |
|
85
|
|
|
public function testHasElement( HashArray $array ) { |
|
86
|
|
|
/** |
|
87
|
|
|
* @var Hashable $hashable |
|
88
|
|
|
*/ |
|
89
|
|
|
foreach ( iterator_to_array( $array ) as $hashable ) { |
|
90
|
|
|
$this->assertTrue( $array->hasElement( $hashable ) ); |
|
91
|
|
|
$this->assertTrue( $array->hasElementHash( $hashable->getHash() ) ); |
|
92
|
|
|
$array->removeElement( $hashable ); |
|
93
|
|
|
$this->assertFalse( $array->hasElement( $hashable ) ); |
|
94
|
|
|
$this->assertFalse( $array->hasElementHash( $hashable->getHash() ) ); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$this->assertTrue( true ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @dataProvider instanceProvider |
|
102
|
|
|
* @param HashArray $array |
|
103
|
|
|
*/ |
|
104
|
|
|
public function testRemoveElement( HashArray $array ) { |
|
105
|
|
|
$elementCount = $array->count(); |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @var Hashable $element |
|
109
|
|
|
*/ |
|
110
|
|
|
foreach ( iterator_to_array( $array ) as $element ) { |
|
111
|
|
|
$this->assertTrue( $array->hasElement( $element ) ); |
|
112
|
|
|
|
|
113
|
|
|
if ( $elementCount % 2 === 0 ) { |
|
114
|
|
|
$array->removeElement( $element ); |
|
115
|
|
|
} |
|
116
|
|
|
else { |
|
117
|
|
|
$array->removeByElementHash( $element->getHash() ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$this->assertFalse( $array->hasElement( $element ) ); |
|
121
|
|
|
$this->assertEquals( --$elementCount, $array->count() ); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$element = new PropertyNoValueSnak( 42 ); |
|
125
|
|
|
|
|
126
|
|
|
$array->removeElement( $element ); |
|
127
|
|
|
$array->removeByElementHash( $element->getHash() ); |
|
128
|
|
|
|
|
129
|
|
|
$this->assertTrue( true ); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @dataProvider instanceProvider |
|
134
|
|
|
* @param HashArray $array |
|
135
|
|
|
*/ |
|
136
|
|
|
public function testGetHash( HashArray $array ) { |
|
137
|
|
|
$hash = $array->getHash(); |
|
138
|
|
|
|
|
139
|
|
|
$this->assertSame( $hash, $array->getHash() ); |
|
140
|
|
|
|
|
141
|
|
|
$elements = $this->elementInstancesProvider(); |
|
142
|
|
|
$element = array_shift( $elements ); |
|
143
|
|
|
$element = $element[0][0]; |
|
144
|
|
|
|
|
145
|
|
|
$array->addElement( $element ); |
|
146
|
|
|
|
|
147
|
|
|
if ( $array->hasElement( $element ) ) { |
|
148
|
|
|
$this->assertSame( $hash, $array->getHash() ); |
|
149
|
|
|
} else { |
|
150
|
|
|
$this->assertNotSame( $hash, $array->getHash() ); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|