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

BodyExpectation   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
eloc 9
c 1
b 0
f 1
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A from() 0 15 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