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

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

Change subject: Non-empty string assertion
......................................................................

Non-empty string assertion

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

Change-Id: Ifb8e1e4b510a4894b8dd2826becef6085e40025a
---
M src/Assert.php
M tests/phpunit/AssertTest.php
2 files changed, 51 insertions(+), 0 deletions(-)


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

diff --git a/src/Assert.php b/src/Assert.php
index 77dccd3..32a7dbf 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 {
@@ -122,6 +123,18 @@
        }
 
        /**
+        * @param string $value
+        * @param string $name
+        *
+        * @throws ParameterTypeException if $value is not a non-empty string.
+        */
+       public static function nonEmptyString( $value, $name ) {
+               if ( !is_string( $value ) || $value === '' ) {
+                       throw new ParameterTypeException( $name, 'non-empty 
string' );
+               }
+       }
+
+       /**
         * Checks a postcondition, that is, throws a PostconditionException if 
$condition is false.
         * This is very similar Assert::invariant() but is intended for use 
only after a computation
         * is complete.
diff --git a/tests/phpunit/AssertTest.php b/tests/phpunit/AssertTest.php
index 5b68b31..a920f58 100644
--- a/tests/phpunit/AssertTest.php
+++ b/tests/phpunit/AssertTest.php
@@ -167,6 +167,44 @@
                Assert::parameterElementType( 'string', 'foo', 'test' );
        }
 
+       function validNonEmptyStringProvider() {
+               return array(
+                       array( '0' ),
+                       array( '0.0' ),
+                       array( ' ' ),
+                       array( "\n" ),
+                       array( 'test' ),
+               );
+       }
+
+       /**
+        * @dataProvider validNonEmptyStringProvider
+        */
+       public function testNonEmptyString_pass( $value ) {
+               Assert::nonEmptyString( $value, 'test' );
+       }
+
+       function invalidNonEmptyStringProvider() {
+               return array(
+                       array( null ),
+                       array( false ),
+                       array( 0 ),
+                       array( 0.0 ),
+                       array( '' ),
+               );
+       }
+
+       /**
+        * @dataProvider invalidNonEmptyStringProvider
+        */
+       public function testNonEmptyString_fail( $value ) {
+               $this->setExpectedException(
+                       'Wikimedia\Assert\ParameterTypeException',
+                       'Bad value for parameter test: must be a non-empty 
string'
+               );
+               Assert::nonEmptyString( $value, 'test' );
+       }
+
        public function testInvariant_pass() {
                Assert::invariant( true, 'test' );
        }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifb8e1e4b510a4894b8dd2826becef6085e40025a
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