|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSHttpCacheBundle 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\HttpCacheBundle\Tests\Unit\Http; |
|
13
|
|
|
|
|
14
|
|
|
use FOS\HttpCacheBundle\Http\RuleMatcher; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\RequestMatcher; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
18
|
|
|
|
|
19
|
|
|
class RuleMatcherTest extends \PHPUnit_Framework_TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
public function testRequestMatcherCalled() |
|
22
|
|
|
{ |
|
23
|
|
|
$requestMatcher = new RequestMatcher(null, null, null, null, array('_controller' => '^AcmeBundle:Default:index$')); |
|
24
|
|
|
$ruleMatcher = new RuleMatcher($requestMatcher, array()); |
|
25
|
|
|
|
|
26
|
|
|
$request = new Request(); |
|
27
|
|
|
$request->attributes->set('_controller', 'AcmeBundle:Default:index'); |
|
28
|
|
|
|
|
29
|
|
|
$this->assertTrue($ruleMatcher->matches($request, new Response())); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testAdditionalCacheableStatus() |
|
33
|
|
|
{ |
|
34
|
|
|
$ruleMatcher = new RuleMatcher(new RequestMatcher(), array('additional_cacheable_status' => array(400, 500))); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertFalse($ruleMatcher->matches(new Request(), new Response('', 504))); |
|
37
|
|
|
$this->assertTrue($ruleMatcher->matches(new Request(), new Response('', 500))); |
|
38
|
|
|
$this->assertTrue($ruleMatcher->matches(new Request(), new Response('', 200))); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testMatchResponse() |
|
42
|
|
|
{ |
|
43
|
|
|
$ruleMatcher = new RuleMatcher(new RequestMatcher(), array('match_response' => 'response.getStatusCode() >= 300')); |
|
44
|
|
|
|
|
45
|
|
|
$this->assertFalse($ruleMatcher->matches(new Request(), new Response('', 100))); |
|
46
|
|
|
$this->assertTrue($ruleMatcher->matches(new Request(), new Response('', 500))); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testMatchResponseRequest() |
|
50
|
|
|
{ |
|
51
|
|
|
$ruleMatcher = new RuleMatcher(new RequestMatcher(), array('match_response' => 'response.getStatusCode() == 201 & request.getMethod() == \'PUT\'')); |
|
52
|
|
|
$request = new Request(); |
|
53
|
|
|
|
|
54
|
|
|
$request->setMethod('POST'); |
|
55
|
|
|
$this->assertFalse($ruleMatcher->matches($request, new Response('', 201))); |
|
56
|
|
|
|
|
57
|
|
|
$request->setMethod('PUT'); |
|
58
|
|
|
$this->assertTrue($ruleMatcher->matches($request, new Response('', 201))); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|