*/ final class OppositeExpectation { /** * Creates a new opposite expectation. * * @param Expectation $original */ public function __construct(private readonly Expectation $original) { } /** * Asserts that the value array not has the provided $keys. * * @param array> $keys * @return Expectation */ public function toHaveKeys(array $keys): Expectation { foreach ($keys as $k => $key) { try { if (is_array($key)) { $this->toHaveKeys(array_keys(Arr::dot($key, $k.'.'))); } else { $this->original->toHaveKey($key); } } catch (ExpectationFailedException) { continue; } $this->throwExpectationFailedException('toHaveKey', [$key]); } return $this->original; } /** * Handle dynamic method calls into the original expectation. * * @param array $arguments * @return Expectation|Expectation|never */ public function __call(string $name, array $arguments): Expectation { try { /* @phpstan-ignore-next-line */ $this->original->{$name}(...$arguments); } catch (ExpectationFailedException) { return $this->original; } $this->throwExpectationFailedException($name, $arguments); } /** * Handle dynamic properties gets into the original expectation. * * @return Expectation|Expectation|never */ public function __get(string $name): Expectation { try { $this->original->{$name}; // @phpstan-ignore-line } catch (ExpectationFailedException) { // @phpstan-ignore-line return $this->original; } $this->throwExpectationFailedException($name); } /** * Creates a new expectation failed exception with a nice readable message. * * @param array $arguments * @return never */ private function throwExpectationFailedException(string $name, array $arguments = []): void { $exporter = new Exporter(); $toString = fn ($argument): string => $exporter->shortenedExport($argument); throw new ExpectationFailedException(sprintf('Expecting %s not %s %s.', $toString($this->original->value), strtolower((string) preg_replace('/(? $toString($argument), $arguments)))); } }