Jeroen De Dauw has submitted this change and it was merged.

Change subject: Throw exceptions instead of giving null data
......................................................................


Throw exceptions instead of giving null data

Change-Id: Ie957991968aeb9200dc4c70644d8f309f8fd24fa
---
M repo/tests/phpunit/includes/api/EntityTestHelper.php
1 file changed, 48 insertions(+), 12 deletions(-)

Approvals:
  Jeroen De Dauw: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/tests/phpunit/includes/api/EntityTestHelper.php 
b/repo/tests/phpunit/includes/api/EntityTestHelper.php
index 886548b..f917ff4 100644
--- a/repo/tests/phpunit/includes/api/EntityTestHelper.php
+++ b/repo/tests/phpunit/includes/api/EntityTestHelper.php
@@ -29,6 +29,10 @@
 
 class EntityTestHelper {
 
+       //@todo allow data to be defined dynamically in tests
+       //this way this class need not contain data but only a way to
+       //manage it for the tests
+
        /**
         * @var array of currently active handles and their current ids
         */
@@ -188,13 +192,14 @@
        );
 
        /**
+        * Get the entity with the given handle
         * @param $handle string of entity to get data for
+        * @throws \OutOfBoundsException
         * @return array of entity data
-        * @throws \MWException
         */
        public static function getEntity( $handle ){
                if( !array_key_exists( $handle, self::$entityData ) ){
-                       throw new \MWException( "No entity defined with handle 
{$handle}" );
+                       throw new \OutOfBoundsException( "No entity defined 
with handle {$handle}" );
                }
                $entity = self::$entityData[ $handle ];
 
@@ -206,32 +211,52 @@
        }
 
        /**
+        * Get the data to pass to the api to clear the entity with the given 
handle
         * @param $handle string of entity to get data for
+        * @throws \OutOfBoundsException
         * @return array|null
         */
        public static function getEntityClear( $handle ){
-               if( array_key_exists( $handle, self::$activeHandles ) ){
-                       $id = self::$activeHandles[ $handle ];
-                       self::unRegisterEntity( $handle );
-                       return array( 'id' => $id, 'data' => '{}', 'clear' => 
'' );
+               if( !array_key_exists( $handle, self::$activeHandles ) ){
+                       throw new \OutOfBoundsException( "No entity clear data 
defined with handle {$handle}" );
                }
-               return null;
+               $id = self::$activeHandles[ $handle ];
+               self::unRegisterEntity( $handle );
+               return array( 'id' => $id, 'data' => '{}', 'clear' => '' );
        }
 
+       /**
+        * Get the data to pass to the api to create the entity with the given 
handle
+        * @param $handle
+        * @return mixed
+        * @throws \OutOfBoundsException
+        */
        public static function getEntityData( $handle ){
                if( !array_key_exists( $handle, self::$entityData ) ){
-                       throw new \MWException( "No entity defined with handle 
{$handle}" );
+                       throw new \OutOfBoundsException( "No entity defined 
with handle {$handle}" );
                }
                return self::$entityData[ $handle ]['data'];
        }
 
+       /**
+        * Get the data of the entity with the given handle we received after 
creation
+        * @param $handle
+        * @return mixed
+        * @throws \OutOfBoundsException
+        */
        public static function getEntityOutput( $handle ){
                if( !array_key_exists( $handle, self::$entityOutput ) ){
-                       throw new \MWException( "No entity output defined with 
handle {$handle}" );
+                       throw new \OutOfBoundsException( "No entity output 
defined with handle {$handle}" );
                }
                return self::$entityOutput[ $handle ];
        }
 
+       /**
+        * Register the entity after it has been created
+        * @param $handle
+        * @param $id
+        * @param null $entity
+        */
        public static function registerEntity( $handle, $id, $entity = null) {
                self::$activeHandles[ $handle ] = $id;
                if( $entity ){
@@ -239,11 +264,20 @@
                }
        }
 
+       /**
+        * Unregister the entity after it has been cleared
+        * @param $handle
+        * @throws \OutOfBoundsException
+        */
        private static function unRegisterEntity( $handle ) {
+               if( !array_key_exists( $handle, self::$activeHandles ) ){
+                       throw new \OutOfBoundsException( "No active entity 
defined with handle {$handle}" );
+               }
                unset( self::$activeHandles[ $handle ] );
        }
 
        /**
+        * Returns an array of currently activated handles
         * @return array of currently active handles
         */
        public static function getActiveHandles(){
@@ -252,14 +286,16 @@
        }
 
        /**
+        * Return the id for the entity with the given handle
         * @param $handle string of handles
+        * @throws \OutOfBoundsException
         * @return null|string id of current handle (if active)
         */
        public static function getId( $handle ){
-               if( array_key_exists( $handle, self::$activeHandles ) ){
-                       return self::$activeHandles[ $handle ];
+               if( !array_key_exists( $handle, self::$activeHandles ) ){
+                       throw new \OutOfBoundsException( "No entity id defined 
with handle {$handle}" );
                }
-               return null;
+               return self::$activeHandles[ $handle ];
        }
 
 }
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/80190
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie957991968aeb9200dc4c70644d8f309f8fd24fa
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Werner <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Michał Łazowik <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to