Files
pest/src/Support/HigherOrderTapProxy.php
2023-01-10 20:21:36 +00:00

77 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Pest\Support;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Throwable;
/**
* @internal
*
* @template TProxy
*
* @mixin TProxy
*/
final class HigherOrderTapProxy
{
private const UNDEFINED_PROPERTY = 'Undefined property: P\\'; // @phpstan-ignore-line
/**
* Create a new tap proxy instance.
*/
public function __construct(
public TestCase $target
) {
// ..
}
/**
* Dynamically sets properties on the target.
*/
public function __set(string $property, mixed $value): void
{
$this->target->{$property} = $value; // @phpstan-ignore-line
}
/**
* Dynamically pass properties gets to the target.
*
* @return mixed
*/
public function __get(string $property)
{
try {
return $this->target->{$property}; // @phpstan-ignore-line
} catch (Throwable $throwable) { // @phpstan-ignore-line
Reflection::setPropertyValue($throwable, 'file', Backtrace::file());
Reflection::setPropertyValue($throwable, 'line', Backtrace::line());
if (Str::startsWith($message = $throwable->getMessage(), self::UNDEFINED_PROPERTY)) {
/** @var ReflectionClass $reflection */
$reflection = (new ReflectionClass($this->target))->getParentClass();
Reflection::setPropertyValue($throwable, 'message', sprintf('Undefined property %s::$%s', $reflection->getName(), $property));
}
throw $throwable;
}
}
/**
* Dynamically pass method calls to the target.
*
* @param array<int, mixed> $arguments
* @return mixed
*/
public function __call(string $methodName, array $arguments)
{
$filename = Backtrace::file();
$line = Backtrace::line();
return (new HigherOrderMessage($filename, $line, $methodName, $arguments))
->call($this->target);
}
}