Passed
Push — main ( e55558...6e74a4 )
by Darío
54s queued 12s
created

BodyExpectation::from()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 10
cc 3
nc 1
nop 1
crap 3
1
<?php
2
3
namespace EasyHttp\MockBuilder\Expectations;
4
5
use EasyHttp\MockBuilder\Contracts\ExpectationMatcher;
6
use EasyHttp\MockBuilder\Expectation;
7
use GuzzleHttp\Promise\RejectedPromise;
8
use Psr\Http\Message\RequestInterface;
9
10
class BodyExpectation implements ExpectationMatcher
11
{
12 121
    public static function from(Expectation $expectation): callable
13
    {
14 121
        return function ($request) use ($expectation) {
15
            /** @var RequestInterface $request */
16 58
            $stream = $request->getBody();
17 58
            $stream->rewind();
18 58
            $body = $stream->getContents();
19
20 58
            foreach ($expectation->bodyHandlersIterator() as $handler) {
21 2
                if (!$handler($body)) {
22 1
                    return new RejectedPromise('body does not match expectation');
23
                }
24
            }
25
26 57
            return $request;
27
        };
28
    }
29
}
30