<?php
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Constraint\Constraint;
/**
* Constraint, that checks if a class has one or more interfaces
*/
class HasInterface extends Constraint
{
protected function matches($other) : bool
{
if (!class_exists($other)) {
return false;
}
return count(class_implements($other)) !== 0;
}
public function toString() : string
{
return 'has one or more interfaces';
}
}
class ConstraintTest extends TestCase
{
/**
* fails, because 6 is no class
*/
public function testIsNoClass() : void
{
$this->assertThat(6, new HasInterface());
}
/**
* fails, because SoapClient has no interface
*/
public function testHasNoInterface() : void
{
$this->assertThat('SoapClient', new HasInterface());
}
/**
* works, because ArrayIterator has one or more interfaces
*/
public function testHasInterface() : void
{
$this->assertThat('ArrayIterator', new HasInterface());
}
}
Result
Time: 00:00.009, Memory: 22.00 MB
There were 2 failures:
1) ConstraintTest::testIsNoClass
Failed asserting that 6 has one or more interfaces.
ConstraintTest.php:33
2) ConstraintTest::testHasNoInterface
Failed asserting that 'SoapClient' has one or more interfaces.
ConstraintTest.php:41
FAILURES!
Tests: 3, Assertions: 0, Failures: 2.