From: jeroendedauw Date: Tue, 4 Sep 2012 13:53:05 +0000 (+0200) Subject: Added MediaWikiTestCase::assertTypeOrValue to facilitate common type checks X-Git-Tag: 1.31.0-rc.0~22384^2 X-Git-Url: https://git.cyclocoop.org/%28%28?a=commitdiff_plain;h=ad6ccbcaf386c8aea58092b8a39d225562ce3c31;p=lhc%2Fweb%2Fwiklou.git Added MediaWikiTestCase::assertTypeOrValue to facilitate common type checks Also added MediaWikiTestCase::assertType which accepts both internal types, classes and interfaces Change-Id: I168ef17ad2da3b744a106742760ef34cc683bf69 --- diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index 49c2a70109..29895e49fd 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -478,4 +478,46 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { } } + /** + * Asserts that the provided variable is of the specified + * internal type or equals the $value argument. This is useful + * for testing return types of functions that return a certain + * type or *value* when not set or on error. + * + * @since 1.20 + * + * @param string $type + * @param mixed $actual + * @param mixed $value + * @param string $message + */ + protected function assertTypeOrValue( $type, $actual, $value = false, $message = '' ) { + if ( $actual === $value ) { + $this->assertTrue( true, $message ); + } + else { + $this->assertType( $type, $actual, $message ); + } + } + + /** + * Asserts the type of the provided value. This can be either + * in internal type such as boolean or integer, or a class or + * interface the value extends or implements. + * + * @since 1.20 + * + * @param string $type + * @param mixed $actual + * @param string $message + */ + protected function assertType( $type, $actual, $message = '' ) { + if ( is_object( $actual ) ) { + $this->assertInstanceOf( $type, $actual, $message ); + } + else { + $this->assertInternalType( $type, $actual, $message ); + } + } + }