ToRespondWithMimeTypeExpectation   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 43
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B run() 0 30 3
1
<?php
2
3
namespace Overwatch\ServiceBundle\Expectation;
4
5
use GuzzleHttp\Client as HttpClient;
6
use Overwatch\ExpectationBundle\Exception as Result;
7
use Overwatch\ExpectationBundle\Expectation\ExpectationInterface;
8
9
/**
10
 * ToRespondWithMimeTypeExpectation
11
 * Expectation that a URL will respond to a HTTP request with the given mime type.
12
 */
13
class ToRespondWithMimeTypeExpectation implements ExpectationInterface
14
{
15
    private $expectationConfig;
16
17
    private $httpClient;
18
19 32
    public function __construct(array $expectationConfig, array $httpClientConfig = [])
20
    {
21 32
        $this->expectationConfig = $expectationConfig;
22 32
        $this->httpClient = new HttpClient($httpClientConfig);
23 32
    }
24
25 5
    public function run($actual, $expected = null)
26
    {
27 5
        $actual = filter_var($actual, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED);
28
29 5
        if ($actual === false) {
30 1
            throw new \InvalidArgumentException('The actual value provided is not a valid URL');
31
        }
32
33 4
        $response = $this->httpClient->get(
34 4
            $actual,
35
            [
36 4
                'allow_redirects' => false,
37 4
                'http_errors'     => !$this->expectationConfig['allow_errors'],
38 4
                'timeout'         => $this->expectationConfig['timeout']
39 4
            ]
40 4
        );
41
42 3
        $result = $response->getHeader('Content-Type')[0];
43
44 3
        if ($expected !== $result) {
45 1
            throw new Result\ExpectationFailedException(sprintf(
46 1
                'Expected %s to respond with mime type "%s", actually responded "%s"',
47 1
                $actual,
48 1
                $expected,
49
                $result
50 1
            ));
51
        }
52
53 2
        return sprintf('Responded with mime type: "%s"', $result);
54
    }
55
}
56