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

CacheAssertions::assertHit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
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