martin-helmich/phpunit-psr7-assert

View on GitHub
src/Constraint/BodyMatchesConstraint.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php
declare(strict_types=1);
namespace Helmich\Psr7Assert\Constraint;

use PHPUnit\Framework\Constraint\Constraint;
use Psr\Http\Message\MessageInterface;

class BodyMatchesConstraint extends Constraint
{

    /** @var Constraint */
    private $constraint;

    public function __construct(Constraint $constraint)
    {
        $this->constraint = $constraint;
    }

    /**
     * Returns a string representation of the object.
     *
     * @return string
     */
    public function toString(): string
    {
        return 'message body matches ' . $this->constraint->toString();
    }

    protected function matches($other): bool
    {
        if (!$other instanceof MessageInterface) {
            return false;
        }

        $other->getBody()->rewind();
        $body = $other->getBody()->getContents();
        return $this->constraint->evaluate($body, '', true);
    }
}