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
|
21 |
|
public function assertMiss(ResponseInterface $response, $message = null) |
37
|
|
|
{ |
38
|
21 |
|
\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
|
6 |
|
public function assertHit(ResponseInterface $response, $message = null) |
48
|
|
|
{ |
49
|
6 |
|
\PHPUnit_Framework_TestCase::assertThat($response, self::isCacheHit(), $message); |
50
|
|
|
} |
51
|
|
|
|
52
|
6 |
|
public static function isCacheHit() |
53
|
|
|
{ |
54
|
6 |
|
return new IsCacheHitConstraint(); |
55
|
|
|
} |
56
|
|
|
|
57
|
21 |
|
public static function isCacheMiss() |
58
|
|
|
{ |
59
|
21 |
|
return new IsCacheMissConstraint(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|