How can PHPUnit test a method with no return value?

I'm trying to test methods from the following class that I wrote (there are more functions than shown, basically one function for each is the _ * () method):

class Validate { private static $initialized = false; /** * Construct won't be called inside this class and is uncallable from the outside. This prevents * instantiating this class. This is by purpose, because we want a static class. */ private function __construct() {} /** * If needed, allows the class to initialize itself */ private static function initialize() { if(self::$initialized) { return; } else { self::$initialized = true; //Set any other class static variables here } } ... public static function isString($string) { self::initialize(); if(!is_string($string)) throw new InvalidArgumentException('Expected a string but found ' . gettype($string)); } ... } 

When I check if methods generate an exception from invalid input, it works great! However, when I check if the method works, PHPUnit is expected to complain because I have no statements in the test. Specific error:

 # RISKY This test did not perform any assertions 

However, I have no meaning for the statement, so I'm not sure how to overcome this.

I read some about testing static methods, but mostly it deals with dependencies between static methods. In addition, even non-static methods cannot have a return value, so how can I fix this?

For reference, my test code is:

 class ValidateTest extends PHPUnit_Framework_TestCase { /** * @covers ../data/objects/Validate::isString * @expectedException InvalidArgumentException */ public function testIsStringThrowsExceptionArgumentInvalid() { Validate::isString(NULL); } /** * @covers ../data/objects/Validate::isString */ public function testIsStringNoExceptionArgumentValid() { Validate::isString("I am a string."); } } 
+5
php unit-testing phpunit
source share
3 answers

One of the solutions I came up with is the following, based on Example 2.12 from Chapter 2 of PHPUnit . It seems a bit hacked to me, but it's the best I've found so far. Also, based on this discussion of PHPUnit Gitub , it seems that some other people want this feature, but there are no plans to implement it.

Change testIsStringNoExceptionArgumentValid () to the following:

  /** * @covers ../data/objects/Validate::isString */ public function testIsStringNoExceptionArgumentValid() { try { Validate::isString("I am a string."); } catch (InvalidArgumentException $notExpected) { $this->fail(); } $this->assertTrue(TRUE); } 
+3
source share

Check void function with assertNull :

  /** * @covers ../data/objects/Validate::isString */ public function testIsStringNoExceptionArgumentValid() { $this->assertNull( Validate::isString("I am a string.") ); } 
+2
source share

To prevent a warning about claims, you can use the @doesNotPerformAssertions annotation as described in the documentation: https://phpunit.de/manual/current/en/appendixes.annotations.html#idp1585440

Or, if you prefer to use the code above the annotation: $this->doesNotPerformAssertions();

+1
source share

All Articles