Thiemo Mättig (WMDE) has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/316335

Change subject: Assertion for parameter key types
......................................................................

Assertion for parameter key types

Resubmission of https://github.com/wikimedia/Assert/issues/4

Change-Id: I7810ed72bb058167abcf7cd7ca850fc3be15c52a
---
M src/Assert.php
A src/ParameterKeyTypeException.php
M tests/phpunit/AssertTest.php
3 files changed, 119 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/libs/Assert 
refs/changes/35/316335/1

diff --git a/src/Assert.php b/src/Assert.php
index 77dccd3..96c165e 100644
--- a/src/Assert.php
+++ b/src/Assert.php
@@ -15,6 +15,7 @@
  *
  * @license MIT
  * @author Daniel Kinzler
+ * @author Thiemo Mättig
  * @copyright Wikimedia Deutschland e.V.
  */
 class Assert {
@@ -91,6 +92,29 @@
        }
 
        /**
+        * @param string $type Either "integer" or "string". Mixing 
"integer|string" is not supported
+        *  because this is PHP's default anyway. It is of no value to check 
this.
+        * @param array $value The parameter's actual value. If this is not an 
array, a
+        *  ParameterTypeException is raised.
+        * @param string $name The name of the parameter that was checked.
+        *
+        * @throws ParameterTypeException if one of the keys in the array 
$value is not of type $type.
+        */
+       public static function parameterKeyType( $type, $value, $name ) {
+               self::parameterType( 'array', $value, $name );
+
+               if ( $type !== 'integer' && $type !== 'string' ) {
+                       throw new ParameterAssertionException( 'type', 'must be 
"integer" or "string"' );
+               }
+
+               foreach ( $value as $key => $element ) {
+                       if ( gettype( $key ) !== $type ) {
+                               throw new ParameterKeyTypeException( $name, 
$type );
+                       }
+               }
+       }
+
+       /**
         * Checks the type of all elements of an parameter, assuming the 
parameter is an array,
         * that is, throws a ParameterElementTypeException if $value
         *
diff --git a/src/ParameterKeyTypeException.php 
b/src/ParameterKeyTypeException.php
new file mode 100644
index 0000000..702b73e
--- /dev/null
+++ b/src/ParameterKeyTypeException.php
@@ -0,0 +1,44 @@
+<?php
+
+namespace Wikimedia\Assert;
+
+/**
+ * Exception indicating that a parameter key type assertion failed.
+ * This generally means a disagreement between the caller and the 
implementation of a function.
+ *
+ * @license MIT
+ * @author Daniel Kinzler
+ * @author Thiemo Mättig
+ * @copyright Wikimedia Deutschland e.V.
+ */
+class ParameterKeyTypeException extends ParameterAssertionException {
+
+       /**
+        * @var string
+        */
+       private $type;
+
+       /**
+        * @param string $parameterName
+        * @param string $type
+        *
+        * @throws ParameterTypeException
+        */
+       public function __construct( $parameterName, $type ) {
+               if ( !is_string( $type ) ) {
+                       throw new ParameterTypeException( 'type', 'string' );
+               }
+
+               parent::__construct( $parameterName, "all elements must have 
$type keys" );
+
+               $this->type = $type;
+       }
+
+       /**
+        * @return string
+        */
+       public function getType() {
+               return $this->type;
+       }
+
+}
diff --git a/tests/phpunit/AssertTest.php b/tests/phpunit/AssertTest.php
index 5b68b31..205fdd5 100644
--- a/tests/phpunit/AssertTest.php
+++ b/tests/phpunit/AssertTest.php
@@ -121,6 +121,57 @@
                Assert::parameterType( 'string', 17, 'test' );
        }
 
+       public function validParameterKeyTypeProvider() {
+               return array(
+                       array( 'integer', array() ),
+                       array( 'integer', array( 1 ) ),
+                       array( 'integer', array( 1 => 1 ) ),
+                       array( 'integer', array( 1.0 => 1 ) ),
+                       array( 'integer', array( '0' => 1 ) ),
+                       array( 'integer', array( false => 1 ) ),
+                       array( 'string', array() ),
+                       array( 'string', array( '' => 1 ) ),
+                       array( 'string', array( '0.0' => 1 ) ),
+                       array( 'string', array( 'string' => 1 ) ),
+                       array( 'string', array( null => 1 ) ),
+               );
+       }
+
+       /**
+        * @dataProvider validParameterKeyTypeProvider
+        */
+       public function testParameterKeyType_pass( $type, $value ) {
+               Assert::parameterKeyType( $type, $value, 'test' );
+       }
+
+       public function invalidParameterKeyTypeProvider() {
+               return array(
+                       array( 'integer', array( 0, 'string' => 1 ) ),
+                       array( 'integer', array( 'string' => 0, 1 ) ),
+                       array( 'string', array( 0, 'string' => 1 ) ),
+                       array( 'string', array( 'string' => 0, 1 ) ),
+               );
+       }
+
+       /**
+        * @dataProvider invalidParameterKeyTypeProvider
+        */
+       public function testParameterKeyType_fail( $type, $value ) {
+               $this->setExpectedException(
+                       'Wikimedia\Assert\ParameterKeyTypeException',
+                       'Bad value for parameter test: all elements must have '
+               );
+               Assert::parameterKeyType( $type, $value, 'test' );
+       }
+
+       public function testGivenUnsupportedType_ParameterKeyTypeFails() {
+               $this->setExpectedException(
+                       'Wikimedia\Assert\ParameterAssertionException',
+                       'Bad value for parameter type: must be "integer" or 
"string"'
+               );
+               Assert::parameterKeyType( 'integer|string', array(), 'test' );
+       }
+
        public function validParameterElementTypeProvider() {
                return array(
                        'empty' => array( 'string', array() ),

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7810ed72bb058167abcf7cd7ca850fc3be15c52a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/libs/Assert
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to