ToRespondHttpExpectation   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 59
ccs 30
cts 31
cp 0.9677
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
D run() 0 42 9
A isValidStatusCode() 0 4 1
1
<?php
2
3
namespace Overwatch\ServiceBundle\Expectation;
4
5
use GuzzleHttp\Client;
6
use Overwatch\ExpectationBundle\Exception as Result;
7
use Overwatch\ExpectationBundle\Expectation\ExpectationInterface;
8
use Symfony\Component\HttpFoundation\Response;
9
10
/**
11
 * ToResolveToExpectation
12
 * Expectation classes are the actual runners of tests.
13
 * This is the runner for the "toRespondHttp" expectation.
14
 */
15
class ToRespondHttpExpectation implements ExpectationInterface
16
{
17
    private $config;
18
    private $client;
19
    
20 36
    public function __construct($config, $client_options = [])
0 ignored issues
show
Coding Style Naming introduced by
The parameter $client_options is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
21
    {
22 36
        $this->config = $config;
23 36
        $this->client = new Client($client_options);
24 36
    }
25
    
26 9
    public function run($actual, $expected = null)
27
    {
28 9
        $actual = filter_var($actual, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED);
29
        
30 9
        if ($actual === false) {
31 1
            throw new \InvalidArgumentException('The actual value provided is not a valid URL');
32
        }
33
34
        try {
35 8
            $response = $this->client->get(
36 8
                $actual,
37
                [
38 8
                    'allow_redirects' => false,
39 8
                    'http_errors'     => false,
40 8
                    'timeout'         => $this->config['timeout']
41 8
                ]
42 8
            );
43 8
        } catch (\Exception $e) {
44
            //Transform exception under certain circumstances, else re-throw.
45 1
            if ($e instanceof \GuzzleHttp\Exception\RequestException && !$e->hasResponse()) {
46 1
                throw new Result\ExpectationFailedException("Expected $actual to respond HTTP $expected, actually failed to respond", 0, $e);
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $actual instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $expected instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
47
            }
48
            
49
            throw $e;
50
        }
51
        
52 7
        $result = $response->getStatusCode();
53
        
54 7
        if ($this->isValidStatusCode($expected)) {
55 2
            if ((int) $expected !== $result) {
56 1
                throw new Result\ExpectationFailedException("Expected $actual to respond HTTP $expected, actually responded HTTP $result");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $actual instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $expected instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $result instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
57
            }
58 1
        } else {
59 5
            if (in_array($result, $this->config['unsatisfactory_codes'])) {
60 1
                throw new Result\ExpectationUnsatisfactoryException("$actual responded HTTP $result, which is configured as unsatisfactory");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $actual instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $result instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
61 4
            } elseif (!in_array($result, $this->config['allowable_codes'])) {
62 2
                throw new Result\ExpectationFailedException("$actual responded HTTP $result, which is configured as a failed result");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $actual instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $result instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
63
            }
64
        }
65
        
66 3
        return "Responded HTTP $result " . $response->getReasonPhrase();
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $result instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
67
    }
68
    
69 7
    private function isValidStatusCode($code)
70
    {
71 7
        return in_array($code, array_keys(Response::$statusTexts));
72
    }
73
}
74