Testing Abstract Classes using Anonymous Classes
TL;DR
Write the entire anonymous (opens in a new tab) class definition on the spot
test.php
$anonymousClass = new class () extends AbstractClass {
public function __construct() {}
};
What is it?
They can pass arguments through to their constructors, extend other classes, implement interfaces, and use traits just like a normal class can
Example
Abstract base class that contains an unimplemented abstract method.
abstractClass.php
abstract class AbstractClass
{
public abstract function abstractMethod();
}
Wrap the abstract method into an anonymous class for execution and testing.
test.php
$anonymousClass = new class extends AbstractClass {
protected function abstractMethod()
{
return 'ok';
}
};
$anonymousClass->abstractMethod();