ParamIsExpectation::from()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 10
cc 4
nc 1
nop 1
crap 4
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 ParamIsExpectation 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 117
            parse_str($request->getUri()->getQuery(), $params);
17
18 117
            foreach ($expectation->notEmptyQueryParamsIterator() as $param => $value) {
19 16
                if (!array_key_exists($param, $params)) {
20 4
                    return new RejectedPromise('param \'' . $param . '\' is missing');
21
                }
22
23 12
                if ($params[$param] !== $value) {
24 7
                    return new RejectedPromise('param \'' . $param . '\' does not match expectation');
25
                }
26
            }
27
28 106
            return $request;
29 121
        };
30
    }
31
}
32