Merge pull request #341 from pestphp/hot-hoe

Adds support for Higher Order Expectations in Higher Order Tests
This commit is contained in:
Luke Downing
2021-07-12 11:52:04 +01:00
committed by GitHub
6 changed files with 67 additions and 18 deletions

View File

@ -155,12 +155,30 @@ final class TestCall
return $this; return $this;
} }
/**
* Saves the property accessors to be used on the target.
*/
public function __get(string $name): self
{
return $this->addChain($name);
}
/** /**
* Saves the calls to be used on the target. * Saves the calls to be used on the target.
* *
* @param array<int, mixed> $arguments * @param array<int, mixed> $arguments
*/ */
public function __call(string $name, array $arguments): self public function __call(string $name, array $arguments): self
{
return $this->addChain($name, $arguments);
}
/**
* Add a chain to the test case factory. Omitting the arguments will treat it as a property accessor.
*
* @param array<int, mixed>|null $arguments
*/
private function addChain(string $name, array $arguments = null): self
{ {
$this->testCaseFactory $this->testCaseFactory
->chains ->chains
@ -171,7 +189,9 @@ final class TestCall
if ($this->testCaseFactory->description !== null) { if ($this->testCaseFactory->description !== null) {
$this->testCaseFactory->description .= ' → '; $this->testCaseFactory->description .= ' → ';
} }
$this->testCaseFactory->description .= sprintf('%s %s', $name, $exporter->shortenedRecursiveExport($arguments)); $this->testCaseFactory->description .= $arguments === null
? $name
: sprintf('%s %s', $name, $exporter->shortenedRecursiveExport($arguments));
} }
return $this; return $this;

View File

@ -34,18 +34,18 @@ final class HigherOrderMessage
public $line; public $line;
/** /**
* The method name. * The method or property name to access.
* *
* @readonly * @readonly
* *
* @var string * @var string
*/ */
public $methodName; public $name;
/** /**
* The arguments. * The arguments.
* *
* @var array<int, mixed> * @var array<int, mixed>|null
* *
* @readonly * @readonly
*/ */
@ -61,13 +61,13 @@ final class HigherOrderMessage
/** /**
* Creates a new higher order message. * Creates a new higher order message.
* *
* @param array<int, mixed> $arguments * @param array<int, mixed>|null $arguments
*/ */
public function __construct(string $filename, int $line, string $methodName, array $arguments) public function __construct(string $filename, int $line, string $methodName, $arguments)
{ {
$this->filename = $filename; $this->filename = $filename;
$this->line = $line; $this->line = $line;
$this->methodName = $methodName; $this->name = $methodName;
$this->arguments = $arguments; $this->arguments = $arguments;
} }
@ -85,21 +85,23 @@ final class HigherOrderMessage
if ($this->hasHigherOrderCallable()) { if ($this->hasHigherOrderCallable()) {
/* @phpstan-ignore-next-line */ /* @phpstan-ignore-next-line */
return (new HigherOrderCallables($target))->{$this->methodName}(...$this->arguments); return (new HigherOrderCallables($target))->{$this->name}(...$this->arguments);
} }
try { try {
return Reflection::call($target, $this->methodName, $this->arguments); return is_array($this->arguments)
? Reflection::call($target, $this->name, $this->arguments)
: $target->{$this->name}; /* @phpstan-ignore-line */
} catch (Throwable $throwable) { } catch (Throwable $throwable) {
Reflection::setPropertyValue($throwable, 'file', $this->filename); Reflection::setPropertyValue($throwable, 'file', $this->filename);
Reflection::setPropertyValue($throwable, 'line', $this->line); Reflection::setPropertyValue($throwable, 'line', $this->line);
if ($throwable->getMessage() === self::getUndefinedMethodMessage($target, $this->methodName)) { if ($throwable->getMessage() === self::getUndefinedMethodMessage($target, $this->name)) {
/** @var ReflectionClass $reflection */ /** @var ReflectionClass $reflection */
$reflection = new ReflectionClass($target); $reflection = new ReflectionClass($target);
/* @phpstan-ignore-next-line */ /* @phpstan-ignore-next-line */
$reflection = $reflection->getParentClass() ?: $reflection; $reflection = $reflection->getParentClass() ?: $reflection;
Reflection::setPropertyValue($throwable, 'message', sprintf('Call to undefined method %s::%s()', $reflection->getName(), $this->methodName)); Reflection::setPropertyValue($throwable, 'message', sprintf('Call to undefined method %s::%s()', $reflection->getName(), $this->name));
} }
throw $throwable; throw $throwable;
@ -125,7 +127,7 @@ final class HigherOrderMessage
*/ */
private function hasHigherOrderCallable() private function hasHigherOrderCallable()
{ {
return in_array($this->methodName, get_class_methods(HigherOrderCallables::class), true); return in_array($this->name, get_class_methods(HigherOrderCallables::class), true);
} }
private static function getUndefinedMethodMessage(object $target, string $methodName): string private static function getUndefinedMethodMessage(object $target, string $methodName): string

View File

@ -17,21 +17,21 @@ final class HigherOrderMessageCollection
/** /**
* Adds a new higher order message to the collection. * Adds a new higher order message to the collection.
* *
* @param array<int, mixed> $arguments * @param array<int, mixed>|null $arguments
*/ */
public function add(string $filename, int $line, string $methodName, array $arguments): void public function add(string $filename, int $line, string $name, array $arguments = null): void
{ {
$this->messages[] = new HigherOrderMessage($filename, $line, $methodName, $arguments); $this->messages[] = new HigherOrderMessage($filename, $line, $name, $arguments);
} }
/** /**
* Adds a new higher order message to the collection if the callable condition is does not return false. * Adds a new higher order message to the collection if the callable condition is does not return false.
* *
* @param array<int, mixed> $arguments * @param array<int, mixed>|null $arguments
*/ */
public function addWhen(callable $condition, string $filename, int $line, string $methodName, array $arguments): void public function addWhen(callable $condition, string $filename, int $line, string $name, array $arguments = null): void
{ {
$this->messages[] = (new HigherOrderMessage($filename, $line, $methodName, $arguments))->when($condition); $this->messages[] = (new HigherOrderMessage($filename, $line, $name, $arguments))->when($condition);
} }
/** /**

View File

@ -67,6 +67,13 @@ it('can handle nested method calls', function () {
->books()->each->toBeArray(); ->books()->each->toBeArray();
}); });
it('works with higher order tests')
->expect(new HasMethods())
->newInstance()->newInstance()->name()->toEqual('Has Methods')->toBeString()
->newInstance()->name()->toEqual('Has Methods')->not->toBeArray
->name()->toEqual('Has Methods')
->books()->each->toBeArray;
class HasMethods class HasMethods
{ {
public function name() public function name()

View File

@ -22,6 +22,13 @@ it('can handle nested methods and properties', function () {
->newInstance()->books()->toBeArray(); ->newInstance()->books()->toBeArray();
}); });
it('works with higher order tests')
->expect(new HasMethodsAndProperties())
->meta->foo->bar->toBeString()->toEqual('baz')->not->toBeInt
->newInstance()->meta->foo->toBeArray
->newInstance()->multiply(2, 2)->toEqual(4)->not->toEqual(5)
->newInstance()->books()->toBeArray();
it('can start a new higher order expectation using the and syntax', function () { it('can start a new higher order expectation using the and syntax', function () {
expect(new HasMethodsAndProperties()) expect(new HasMethodsAndProperties())
->toBeInstanceOf(HasMethodsAndProperties::class) ->toBeInstanceOf(HasMethodsAndProperties::class)
@ -33,6 +40,14 @@ it('can start a new higher order expectation using the and syntax', function ()
expect(static::getCount())->toEqual(4); expect(static::getCount())->toEqual(4);
}); });
it('can start a new higher order expectation using the and syntax in higher order tests')
->expect(new HasMethodsAndProperties())
->toBeInstanceOf(HasMethodsAndProperties::class)
->meta->toBeArray
->and(['foo' => 'bar'])
->toBeArray()
->foo->toEqual('bar');
class HasMethodsAndProperties class HasMethodsAndProperties
{ {
public $name = 'Has Methods and Properties'; public $name = 'Has Methods and Properties';

View File

@ -64,6 +64,11 @@ it('works with nested properties', function () {
->posts->toBeArray()->toHaveCount(2); ->posts->toBeArray()->toHaveCount(2);
}); });
it('works with higher order tests')
->expect(new HasProperties())
->nested->foo->bar->toBeString()->toEqual('baz')
->posts->toBeArray()->toHaveCount(2);
class HasProperties class HasProperties
{ {
public $name = 'foo'; public $name = 'foo';