Permalink
Browse files

Added HashArray tests

Change-Id: I425691d63cfab84a709c83b17c61ea92889fc6c6
  • Loading branch information...
JeroenDeDauw committed Aug 20, 2012
1 parent 3886f13 commit 9e837dfd7061bb578e0102789e0defecb8e4a8c9
@@ -155,6 +155,7 @@ public static function registerUnitTests( array &$files ) {
'query/QueryObject',
+ 'reference/ReferenceList',
'reference/ReferenceObject',
'snak/PropertyValueSnak',
View
@@ -164,6 +164,7 @@
$wgAutoloadClasses['Wikibase\SubclassOfSnak'] = $dir . 'includes/snak/SubclassOfSnak.php';
// tests
+$wgAutoloadClasses['Wikibase\Test\HashArrayTest'] = $dir . 'tests/phpunit/HashArrayTest.php';
$wgAutoloadClasses['Wikibase\Test\TestItems'] = $dir . 'tests/phpunit/item/TestItems.php';
$wgAutoloadClasses['Wikibase\Test\EntityObjectTest'] = $dir . 'tests/phpunit/entity/EntityObjectTest.php';
$wgAutoloadClasses['Wikibase\Test\EntityRefreshTest'] = $dir . 'tests/phpunit/changes/EntityRefreshTest.php';
View
@@ -72,6 +72,30 @@ public function hasElementHash( $elementHash ) {
return array_key_exists( $elementHash, $this->offsetHashes );
}
+ /**
+ * Returns if there is an element with the same hash as the provided element in the list.
+ *
+ * @since 0.1
+ *
+ * @param Hashable $element
+ *
+ * @return boolean
+ */
+ public function hasElement( Hashable $element ) {
+ return $this->hasElementHash( $element->getHash() );
+ }
+
+ /**
+ * Removes the element with the hash of the provided element, if there is such an element in the list.
+ *
+ * @since 0.1
+ *
+ * @param Hashable $element
+ */
+ public function removeElement( Hashable $element ) {
+ return $this->removeByElementHash( $element->getHash() );
+ }
+
/**
* Removes the element with the provided hash, if there is such an element in the list.
*
@@ -85,6 +109,25 @@ public function removeByElementHash( $elementHash ) {
}
}
+ /**
+ * Adds the provided element to the list if there is no element with the same hash yet.
+ *
+ * @since 0.1
+ *
+ * @param Hashable $element
+ *
+ * @return boolean Indicates if the element was added or not.
+ */
+ public function addElement( Hashable $element ) {
+ if ( $this->hasElementHash( $element->getHash() ) ) {
+ return false;
+ }
+ else {
+ $this->append( $element );
+ return true;
+ }
+ }
+
/**
* Returns the element with the provided hash or false if there is no such element.
*
@@ -61,13 +61,7 @@ public function removeReferenceHash( $referenceHash ) {
* @return boolean Indicates if the reference was added or not.
*/
public function addReference( Reference $reference ) {
- if ( $this->hasReference( $reference ) ) {
- return false;
- }
- else {
- $this->append( $reference );
- return true;
- }
+ return $this->addElement( $reference );
}
/**
@@ -61,13 +61,7 @@ public function removeSnakHash( $snakHash ) {
* @return boolean Indicates if the snak was added or not.
*/
public function addSnak( Snak $snak ) {
- if ( $this->hasSnak( $snak ) ) {
- return false;
- }
- else {
- $this->append( $snak );
- return true;
- }
+ return $this->addElement( $snak );
}
/**
@@ -0,0 +1,130 @@
+<?php
+
+namespace Wikibase\Test;
+use Wikibase\HashArray as HashArray;
+use Wikibase\Hashable as Hashable;
+
+/**
+ * Tests for the Wikibase\HashArray class.
+ *
+ * @file
+ * @since 0.1
+ *
+ * @ingroup Wikibase
+ * @ingroup Test
+ *
+ * @group Wikibase
+ * @group WikibaseLib
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
+ */
+abstract class HashArrayTest extends \GenericArrayObjectTest {
+
+ public abstract function constructorProvider();
+
+ public function instanceProvider() {
+ $class = $this->getInstanceClass();
+
+ $instances = array();
+
+ foreach ( $this->constructorProvider() as $args ) {
+ $instances[] = array( new $class( array_key_exists( 0, $args ) ? $args[0] : null ) );
+ }
+
+ return $instances;
+ }
+
+ /**
+ * @dataProvider instanceProvider
+ *
+ * @param \Wikibase\HashArray $array
+ */
+ public function testHasSnak( HashArray $array ) {
+ /**
+ * @var Hashable $hashable
+ */
+ foreach ( iterator_to_array( $array ) as $hashable ) {
+ $this->assertTrue( $array->hasElement( $hashable ) );
+ $this->assertTrue( $array->hasElementHash( $hashable->getHash() ) );
+ $array->removeElement( $hashable );
+ $this->assertFalse( $array->hasElement( $hashable ) );
+ $this->assertFalse( $array->hasElementHash( $hashable->getHash() ) );
+ }
+ }
+
+ /**
+ * @dataProvider instanceProvider
+ *
+ * @param \Wikibase\HashArray $array
+ */
+ public function testRemoveElement( HashArray $array ) {
+ $elementCount = $array->count();
+
+ /**
+ * @var Hashable $element
+ */
+ foreach ( iterator_to_array( $array ) as $element ) {
+ $this->assertTrue( $array->hasElement( $element ) );
+
+ if ( $elementCount % 2 === 0 ) {
+ $array->removeElement( $element );
+ }
+ else {
+ $array->removeByElementHash( $element->getHash() );
+ }
+
+ $this->assertFalse( $array->hasElement( $element ) );
+ $this->assertEquals( --$elementCount, $array->count() );
+ }
+
+ $element = new \Wikibase\InstanceOfSnak( 42 );
+
+ $array->removeElement( $element );
+ $array->removeByElementHash( $element->getHash() );
+ }
+
+ /**
+ * @dataProvider instanceProvider
+ *
+ * @param \Wikibase\HashArray $array
+ */
+ public function testAddElement( HashArray $array ) {
+ $elementCount = $array->count();
+
+ $elements = $this->elementInstancesProvider();
+ $element = array_shift( $elements );
+
+ if ( !$array->hasElement( $element ) ) {
+ ++$elementCount;
+ }
+
+ $this->assertEquals( !$array->hasElement( $element ), $array->addElement( $element ) );
+
+ $this->assertEquals( $elementCount, $array->count() );
+
+ $this->assertFalse( $array->addElement( $element ) );
+
+ $this->assertEquals( $elementCount, $array->count() );
+ }
+
+ /**
+ * @dataProvider instanceProvider
+ *
+ * @param \Wikibase\HashArray $array
+ */
+ public function testGetHash( HashArray $array ) {
+ $hash = $array->getHash();
+
+ $this->assertEquals( $hash, $array->getHash() );
+
+ $elements = $this->elementInstancesProvider();
+ $element = array_shift( $elements );
+
+ $hasElement = $array->hasElement( $element );
+ $array->addElement( $element );
+
+ $this->assertTrue( ( $hash === $array->getHash() ) === $hasElement );
+ }
+
+}
@@ -0,0 +1,49 @@
+<?php
+
+namespace Wikibase\Test;
+use Wikibase\ReferenceList as ReferenceList;
+use Wikibase\References as References;
+use Wikibase\Reference as Reference;
+use Wikibase\ReferenceObject as ReferenceObject;
+
+/**
+ * Tests for the Wikibase\ReferenceList class.
+ *
+ * @file
+ * @since 0.1
+ *
+ * @ingroup WikibaseLib
+ * @ingroup Test
+ *
+ * @group Wikibase
+ * @group WikibaseLib
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
+ */
+class ReferenceListTest extends HashArrayTest {
+
+ /**
+ * @see GenericArrayObjectTest::getInstanceClass
+ */
+ public function getInstanceClass() {
+ return '\Wikibase\ReferenceList';
+ }
+
+ /**
+ * @see GenericArrayObjectTest::elementInstancesProvider
+ */
+ public function elementInstancesProvider() {
+ return array(
+ new ReferenceObject(),
+ );
+ }
+
+ public function constructorProvider() {
+ return array(
+ array(),
+ array( new \Wikibase\ReferenceObject() ),
+ );
+ }
+
+}
Oops, something went wrong.

0 comments on commit 9e837df

Please sign in to comment.