Completed
Pull Request — master (#298)
by Jonas
06:29 queued 03:27
created

AbstractCacheConstraint   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 64%

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 7
c 6
b 1
f 0
lcom 1
cbo 0
dl 0
loc 63
ccs 16
cts 25
cp 0.64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A matches() 0 20 4
A __construct() 0 8 2
getPattern() 0 1 ?
A failureDescription() 0 8 1
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCache package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCache\Test\PHPUnit;
13
14
use Psr\Http\Message\ResponseInterface;
15
16
/**
17
 * Abstract cache constraint.
18
 */
19
abstract class AbstractCacheConstraint extends \PHPUnit_Framework_Constraint
20
{
21
    protected $header = 'X-Cache';
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param string $header Cache debug header; defaults to X-Cache-Debug
27
     */
28 30
    public function __construct($header = null)
29
    {
30 30
        if ($header) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $header of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
31 3
            $this->header = $header;
32 3
        }
33
34 30
        parent::__construct();
35 30
    }
36
37
    /**
38
     * Get cache header value.
39
     *
40
     * @return string
41
     */
42
    abstract public function getPattern();
43
44
    /**
45
     * {@inheritdoc}
46
     *
47
     * @param Response $other The guzzle response object
48
     */
49 30
    protected function matches($other)
50
    {
51 30
        if (!$other instanceof ResponseInterface) {
52
            throw new \RuntimeException(sprintf('Expected a GuzzleHttp\Psr7\Response but got %s', get_class($other)));
53
        }
54 30
        if (!$other->hasHeader($this->header)) {
55 1
            $message = sprintf(
56
                'Response has no "%s" header. Configure your caching proxy '
57 1
                .'to set the header with cache hit/miss status.',
58 1
                $this->header
59 1
            );
60 1
            if (200 !== $other->getStatusCode()) {
61
                $message .= sprintf("\nStatus code of response is %s.", $other->getStatusCode());
62
            }
63
64 1
            throw new \RuntimeException($message);
65
        }
66
67 29
        return preg_match($this->getPattern(), (string) $other->getHeader($this->header)) === 1;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    protected function failureDescription($other)
74
    {
75
        return sprintf(
76
            'response (with status code %s) %s',
77
            $other->getStatusCode(),
78
            $this->toString()
79
        );
80
    }
81
}
82