Skip to content

Commit f23f235

Browse files
committed
Fix HashArray accepting anything
1 parent ae8a676 commit f23f235

3 files changed

Lines changed: 41 additions & 10 deletions

File tree

src/HashArray.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,17 @@ abstract public function getObjectType();
7070
* @param array|Traversable|null $input
7171
* @param int $flags
7272
* @param string $iterator_class
73+
*
74+
* @throws InvalidArgumentException
7375
*/
7476
public function __construct( $input = null, $flags = 0, $iterator_class = 'ArrayIterator' ) {
7577
parent::__construct( array(), $flags, $iterator_class );
7678

7779
if ( $input !== null ) {
80+
if ( !is_array( $input ) && !( $input instanceof Traversable ) ) {
81+
throw new InvalidArgumentException( '$input must be an array or Traversable' );
82+
}
83+
7884
foreach ( $input as $offset => $value ) {
7985
$this->offsetSet( $offset, $value );
8086
}

tests/unit/Snak/SnakListTest.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Wikibase\DataModel\Tests\Snak;
44

55
use DataValues\StringValue;
6+
use InvalidArgumentException;
67
use Wikibase\DataModel\Entity\PropertyId;
78
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
89
use Wikibase\DataModel\Snak\PropertyValueSnak;
@@ -26,6 +27,7 @@
2627
* @licence GNU GPL v2+
2728
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
2829
* @author Adam Shorland
30+
* @author Thiemo Mättig
2931
*/
3032
class SnakListTest extends HashArrayTest {
3133

@@ -70,6 +72,29 @@ public function constructorProvider() {
7072
);
7173
}
7274

75+
/**
76+
* @dataProvider invalidConstructorArgumentsProvider
77+
* @param mixed $input
78+
* @expectedException InvalidArgumentException
79+
*/
80+
public function testGivenInvalidConstructorArguments_constructorThrowsException( $input ) {
81+
new SnakList( $input );
82+
}
83+
84+
public function invalidConstructorArgumentsProvider() {
85+
$id1 = new PropertyId( 'P1' );
86+
87+
return array(
88+
array( false ),
89+
array( 1 ),
90+
array( 0.1 ),
91+
array( 'string' ),
92+
array( $id1 ),
93+
array( new PropertyNoValueSnak( $id1 ) ),
94+
array( new PropertyValueSnak( $id1, new StringValue( 'a' ) ) ),
95+
);
96+
}
97+
7398
/**
7499
* @dataProvider instanceProvider
75100
* @param SnakList $array
@@ -223,11 +248,12 @@ public function orderByPropertyProvider() {
223248

224249
$arguments = array();
225250

226-
foreach( $rawArguments as $rawArgument ) {
227-
if( !array_key_exists( 2, $rawArgument ) ){
228-
$rawArgument[2] = array();
229-
}
230-
$arguments[] = array( new $class( $rawArgument[0] ), new $class( $rawArgument[1] ), $rawArgument[2] );
251+
foreach ( $rawArguments as $rawArgument ) {
252+
$arguments[] = array(
253+
new $class( $rawArgument[0] ),
254+
new $class( $rawArgument[1] ),
255+
array_key_exists( 2, $rawArgument ) ? $rawArgument[2] : array()
256+
);
231257
}
232258

233259
return $arguments;
@@ -237,7 +263,7 @@ public function orderByPropertyProvider() {
237263
* @dataProvider orderByPropertyProvider
238264
* @param SnakList $snakList
239265
* @param SnakList $expected
240-
* @param array $order
266+
* @param string[] $order
241267
*/
242268
public function testOrderByProperty( SnakList $snakList, SnakList $expected, $order = array() ) {
243269
$initialSnakList = new SnakList( array_values( iterator_to_array( $snakList ) ) );
@@ -250,7 +276,7 @@ public function testOrderByProperty( SnakList $snakList, SnakList $expected, $or
250276

251277
$this->assertEquals( $expected, $orderedSnakList );
252278

253-
if( $orderedSnakList->equals( $initialSnakList ) ) {
279+
if ( $orderedSnakList->equals( $initialSnakList ) ) {
254280
$this->assertTrue( $initialSnakList->getHash() === $snakList->getHash() );
255281
} else {
256282
$this->assertFalse( $initialSnakList->getHash() === $snakList->getHash() );

tests/unit/Statement/StatementTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function instanceProvider() {
117117
$instance = clone $baseInstance;
118118

119119
$instance->setReferences( new ReferenceList( array(
120-
new Reference( new SnakList(
120+
new Reference( array(
121121
new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) )
122122
) )
123123
) ) );
@@ -145,15 +145,14 @@ public function testGetReferences( Statement $statement ) {
145145
*/
146146
public function testSetReferences( Statement $statement ) {
147147
$references = new ReferenceList( array(
148-
new Reference( new SnakList(
148+
new Reference( array(
149149
new PropertyValueSnak(
150150
new PropertyId( 'P1' ),
151151
new StringValue( 'a' )
152152
)
153153
) )
154154
) );
155155

156-
157156
$statement->setReferences( $references );
158157

159158
$this->assertEquals( $references, $statement->getReferences() );

0 commit comments

Comments
 (0)