Completed
Pull Request — master (#289)
by Gavin
16:24 queued 13:47
created

CacheAssertions   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 34
ccs 0
cts 10
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A assertMiss() 0 4 1
A assertHit() 0 4 1
A isCacheHit() 0 4 1
A isCacheMiss() 0 4 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;
13
14
use FOS\HttpCache\Test\PHPUnit\IsCacheHitConstraint;
15
use FOS\HttpCache\Test\PHPUnit\IsCacheMissConstraint;
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * Provides cache hit/miss assertions to PHPUnit tests
20
 *
21
 * To enable the assertHit and assertMiss assertions, you need to configure your
22
 * caching proxy to set an X-Cache header with the cache status.
23
 *
24
 * Use this trait in conjunction with either the NginxTest, SymfonyTest or
25
 * VarnishTest trait to reset the cache between tests and properly isolate your
26
 * assertions.
27
 */
28
trait CacheAssertions
29
{
30
    /**
31
     * Assert a cache miss
32
     *
33
     * @param ResponseInterface $response
34
     * @param string            $message Test failure message (optional)
35
     */
36
    public function assertMiss(ResponseInterface $response, $message = null)
37
    {
38
        \PHPUnit_Framework_TestCase::assertThat($response, self::isCacheMiss(), $message);
39
    }
40
41
    /**
42
     * Assert a cache hit
43
     *
44
     * @param ResponseInterface $response
45
     * @param string            $message Test failure message (optional)
46
     */
47
    public function assertHit(ResponseInterface $response, $message = null)
48
    {
49
        \PHPUnit_Framework_TestCase::assertThat($response, self::isCacheHit(), $message);
50
    }
51
52
    public static function isCacheHit()
53
    {
54
        return new IsCacheHitConstraint();
55
    }
56
57
    public static function isCacheMiss()
58
    {
59
        return new IsCacheMissConstraint();
60
    }
61
}
62