Test Failed
Push — claimAliases ( 71499b )
by no
04:12
created

testGivenNotPresentGuid_hasStatementWithGuidReturnsFalse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Statement;
4
5
use ArrayObject;
6
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
7
use Wikibase\DataModel\Statement\Statement;
8
use Wikibase\DataModel\Statement\StatementByGuidMap;
9
10
/**
11
 * @covers Wikibase\DataModel\Statement\StatementByGuidMap
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 * @author Kai Nissen < [email protected] >
16
 */
17
class StatementByGuidMapTest extends \PHPUnit_Framework_TestCase {
18
19
	public function testGivenNotPresentGuid_hasStatementWithGuidReturnsFalse() {
20
		$statements = new StatementByGuidMap();
21
22
		$this->assertFalse( $statements->hasStatementWithGuid( 'some guid' ) );
23
	}
24
25
	public function testGivenPresentGuid_hasStatementWithGuidReturnsTrue() {
26
27
		$statements = new StatementByGuidMap( array(
28
			$this->newStatement( 1, 'some guid' )
29
		) );
30
31
		$this->assertTrue( $statements->hasStatementWithGuid( 'some guid' ) );
32
	}
33
34
	private function newStatement( $propertyId, $guid ) {
35
		$statement = new Statement( new PropertyNoValueSnak( $propertyId ) );
36
		$statement->setGuid( $guid );
37
		return $statement;
38
	}
39
40
	/**
41
	 * @dataProvider nonStringProvider
42
	 */
43
	public function testGivenNonStringGuid_hasStatementWithGuidThrowsException( $nonString ) {
44
		$statements = new StatementByGuidMap();
45
46
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
47
		$statements->hasStatementWithGuid( $nonString );
48
	}
49
50
	public function nonStringProvider() {
51
		return array(
52
			array( null ),
53
			array( 42 ),
54
			array( 4.2 ),
55
			array( array() ),
56
			array( (object)array() ),
57
		);
58
	}
59
60
	public function testGivenGuidOfPresentStatement_getStatementByGuidReturnsStatement() {
61
		$statement = $this->newStatement( 1, 'some guid' );
62
63
		$statements = new StatementByGuidMap( array( $statement ) );
64
65
		$this->assertEquals( $statement, $statements->getStatementByGuid( 'some guid' ) );
66
	}
67
68
	public function testGivenGuidOfNotPresentStatement_getStatementByGuidReturnsNull() {
69
		$statements = new StatementByGuidMap();
70
71
		$this->assertNull( $statements->getStatementByGuid( 'some guid' ) );
72
	}
73
74
	/**
75
	 * @dataProvider nonStringProvider
76
	 */
77
	public function testGivenNonStringGuid_getStatementByGuidThrowsException( $nonString ) {
78
		$statements = new StatementByGuidMap();
79
80
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
81
		$statements->getStatementByGuid( $nonString );
82
	}
83
84
	public function testGivenGuidOfPresentStatement_removeStatementWithGuidRemovesTheStatement() {
85
		$statement = $this->newStatement( 1, 'some guid' );
86
		$statements = new StatementByGuidMap( array( $statement ) );
87
88
		$statements->removeStatementWithGuid( 'some guid' );
89
90
		$this->assertFalse( $statements->hasStatementWithGuid( 'some guid' ) );
91
	}
92
93
	public function testGivenGuidOfNonPresentStatement_removeStatementWithGuidDoesNoOp() {
94
		$statement = $this->newStatement( 1, 'some guid' );
95
		$statements = new StatementByGuidMap( array( $statement ) );
96
97
		$statements->removeStatementWithGuid( '-- different guid --' );
98
99
		$this->assertTrue( $statements->hasStatementWithGuid( 'some guid' ) );
100
	}
101
102
	/**
103
	 * @dataProvider nonStringProvider
104
	 */
105
	public function testGivenNonStringGuid_removeStatementWithGuidThrowsException( $nonString ) {
106
		$statements = new StatementByGuidMap();
107
108
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
109
		$statements->removeStatementWithGuid( $nonString );
110
	}
111
112
	public function testGivenStatementWithNoGuid_constructorThrowsException() {
113
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
114
115
		new StatementByGuidMap( array(
116
			$this->newStatement( 1, null )
117
		) );
118
	}
119
120
	public function testCanConstructWithStatementTraversable() {
121
		$traversable = new ArrayObject( array(
122
			$this->newStatement( 1, 'some guid' )
123
		) );
124
125
		$statementMap = new StatementByGuidMap( $traversable );
126
127
		$this->assertTrue( $statementMap->hasStatementWithGuid( 'some guid' ) );
128
	}
129
130
	public function testWhenMapIsEmpty_countReturnsZero() {
131
		$statements = new StatementByGuidMap();
132
133
		$this->assertSame( 0, $statements->count() );
134
	}
135
136
	public function testMapCanBePassedToCount() {
137
		$statements = new StatementByGuidMap( array(
138
			$this->newStatement( 1, 'some guid' ),
139
			$this->newStatement( 2, 'other guid' )
140
		) );
141
142
		$this->assertSame( 2, count( $statements ) );
143
	}
144
145
	public function testMapCanBeIteratedOver() {
146
		$statement1 = $this->newStatement( 1, 'some guid' );
147
		$statement2 = $this->newStatement( 2, 'other guid' );
148
149
		$statementMap = new StatementByGuidMap( array( $statement1, $statement2 ) );
150
151
		$iteratedStatements = array();
152
153
		foreach ( $statementMap as $guid => $statement ) {
154
			$iteratedStatements[$guid] = $statement;
155
		}
156
157
		$expectedStatements = array(
158
			'some guid' => $statement1,
159
			'other guid' => $statement2
160
		);
161
162
		$this->assertEquals( $expectedStatements, $iteratedStatements );
163
	}
164
165
	public function testGivenNotPresentStatement_addStatementAddsIt() {
166
		$statements = new StatementByGuidMap();
167
168
		$statements->addStatement( $this->newStatement( 1, 'some guid' ) );
169
170
		$this->assertTrue( $statements->hasStatementWithGuid( 'some guid' ) );
171
	}
172
173
	public function testGivenStatementWithPresentGuid_addStatementReplacesThePresentStatement() {
174
		$statement1 = $this->newStatement( 1, 'some guid' );
175
		$statement2 = $this->newStatement( 2, 'some guid' );
176
177
		$statements = new StatementByGuidMap( array( $statement1 ) );
178
179
		$statements->addStatement( $statement2 );
180
181
		$this->assertEquals( $statement2, $statements->getStatementByGuid( 'some guid' ) );
182
	}
183
184
	public function testToArray() {
185
		$statement1 = $this->newStatement( 1, 'some guid' );
186
		$statement2 = $this->newStatement( 2, 'other guid' );
187
188
		$statementMap = new StatementByGuidMap( array( $statement1, $statement2 ) );
189
190
		$expectedStatements = array(
191
			'some guid' => $statement1,
192
			'other guid' => $statement2
193
		);
194
195
		$this->assertEquals( $expectedStatements, $statementMap->toArray() );
196
	}
197
198
}
199