Skip to content

Commit

Permalink
Add interfaceExists assertion (webmozarts#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
BackEndTea authored and Nyholm committed Dec 24, 2018
1 parent 18879ed commit d8ea33e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ Method | Description
----------------------------------------------------- | --------------------------------------------------
`classExists($value, $message = '')` | Check that a value is an existing class name
`subclassOf($value, $class, $message = '')` | Check that a class is a subclass of another
`interfaceExists($value, $message = '')` | Check that a value is an existing interface name
`implementsInterface($value, $class, $message = '')` | Check that a class implements an interface
`propertyExists($value, $property, $message = '')` | Check that a property exists in a class/object
`propertyNotExists($value, $property, $message = '')` | Check that a property does not exist in a class/object
Expand Down
12 changes: 12 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
* @method static void nullOrWritable($value, $message = '')
* @method static void nullOrClassExists($value, $message = '')
* @method static void nullOrSubclassOf($value, $class, $message = '')
* @method static void nullOrInterfaceExists($value, $message = '')
* @method static void nullOrImplementsInterface($value, $interface, $message = '')
* @method static void nullOrPropertyExists($value, $property, $message = '')
* @method static void nullOrPropertyNotExists($value, $property, $message = '')
Expand Down Expand Up @@ -149,6 +150,7 @@
* @method static void allWritable($values, $message = '')
* @method static void allClassExists($values, $message = '')
* @method static void allSubclassOf($values, $class, $message = '')
* @method static void allInterfaceExists($values, $message = '')
* @method static void allImplementsInterface($values, $interface, $message = '')
* @method static void allPropertyExists($values, $property, $message = '')
* @method static void allPropertyNotExists($values, $property, $message = '')
Expand Down Expand Up @@ -849,6 +851,16 @@ public static function subclassOf($value, $class, $message = '')
}
}

public static function interfaceExists($value, $message = '')
{
if (!interface_exists($value)) {
static::reportInvalidArgument(sprintf(
$message ?: 'Expected an existing interface name. got %s',
static::valueToString($value)
));
}
}

public static function implementsInterface($value, $interface, $message = '')
{
if (!in_array($interface, class_implements($value))) {
Expand Down
2 changes: 2 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ public function getTests()
array('classExists', array(__NAMESPACE__.'\Foobar'), false),
array('subclassOf', array(__CLASS__, 'PHPUnit_Framework_TestCase'), true),
array('subclassOf', array(__CLASS__, 'stdClass'), false),
array('interfaceExists', array('\Countable'), true),
array('interfaceExists', array(__CLASS__), false),
array('implementsInterface', array('ArrayIterator', 'Traversable'), true),
array('implementsInterface', array(__CLASS__, 'Traversable'), false),
array('propertyExists', array((object) array('property' => 0), 'property'), true),
Expand Down

0 comments on commit d8ea33e

Please sign in to comment.