Skip to content

Commit de4ca66

Browse files
committed
Added tests for Symfony RP
1 parent 0e49410 commit de4ca66

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

Tests/Unit/HttpCacheTest.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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;
13+
14+
use FOS\HttpCacheBundle\HttpCache;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\HttpKernelInterface;
18+
19+
class HttpCacheTest extends \PHPUnit_Framework_TestCase
20+
{
21+
/**
22+
* @return \FOS\HttpCacheBundle\HttpCache|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
protected function getHttpCachePartialMock(array $mockedMethods = null)
25+
{
26+
$mock = $this->getMockBuilder( '\FOS\HttpCacheBundle\HttpCache' )
27+
->setMethods( $mockedMethods )
28+
->disableOriginalConstructor()
29+
->getMock();
30+
31+
// Force setting options property via Reflection since we can't use original constructor.
32+
$refMock = new \ReflectionObject($mock);
33+
$refOptions = $refMock
34+
// \FOS\HttpCacheBundle\HttpCache
35+
->getParentClass()
36+
// \Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache
37+
->getParentClass()
38+
// \Symfony\Component\HttpKernel\HttpCache\HttpCache
39+
->getParentClass()
40+
->getProperty('options');
41+
$refOptions->setAccessible(true);
42+
$refOptions->setValue($mock, array(
43+
'debug' => false,
44+
'default_ttl' => 0,
45+
'private_headers' => array('Authorization', 'Cookie'),
46+
'allow_reload' => false,
47+
'allow_revalidate' => false,
48+
'stale_while_revalidate' => 2,
49+
'stale_if_error' => 60,
50+
));
51+
return $mock;
52+
}
53+
54+
public function testGenerateUserHashNotAllowed()
55+
{
56+
$request = new Request();
57+
$request->headers->set('accept', HttpCache::USER_HASH_ACCEPT_HEADER);
58+
$httpCache = $this->getHttpCachePartialMock();
59+
$response = $httpCache->handle($request);
60+
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response);
61+
$this->assertSame(400, $response->getStatusCode());
62+
}
63+
64+
public function testPassingUserHashNotAllowed()
65+
{
66+
$request = new Request();
67+
$request->headers->set(HttpCache::USER_HASH_HEADER, 'foo');
68+
$httpCache = $this->getHttpCachePartialMock();
69+
$response = $httpCache->handle($request);
70+
$this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response);
71+
$this->assertSame(400, $response->getStatusCode());
72+
}
73+
74+
public function testUserHashAnonymous()
75+
{
76+
$request = new Request();
77+
$catch = true;
78+
79+
$httpCache = $this->getHttpCachePartialMock(array('lookup'));
80+
$response = new Response();
81+
$httpCache
82+
->expects($this->once())
83+
->method('lookup')
84+
->with($request, $catch)
85+
->will($this->returnValue($response));
86+
87+
$this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch));
88+
$this->assertTrue($request->headers->has(HttpCache::USER_HASH_HEADER));
89+
$this->assertSame(HttpCache::ANONYMOUS_HASH, $request->headers->get(HttpCache::USER_HASH_HEADER));
90+
}
91+
92+
public function testUserHashUserWithSession()
93+
{
94+
$catch = true;
95+
$sessionId = 'my_session_id';
96+
$cookies = array(
97+
'PHPSESSID' => $sessionId,
98+
'foo' => 'bar'
99+
);
100+
$cookieString = "PHPSESSID=$sessionId; foo=bar";
101+
$request = Request::create('/foo', 'GET', array(), $cookies, array(), array('Cookie' => $cookieString));
102+
$response = new Response();
103+
104+
$hashRequest = Request::create(HttpCache::USER_HASH_URI, HttpCache::USER_HASH_METHOD, array(), $request->cookies->all(), array(), $request->server->all());
105+
$hashRequest->attributes->set('internalRequest', true);
106+
$hashRequest->headers->set('Accept', HttpCache::USER_HASH_ACCEPT_HEADER);
107+
$hashRequest->headers->set('Cookie', $sessionId);
108+
// Ensure request properties have been filled up.
109+
$hashRequest->getPathInfo();
110+
$hashRequest->getMethod();
111+
112+
$expectedContextHash = 'my_generated_hash';
113+
// Just avoid the response to modify the request object, otherwise it's impossible to test objects equality.
114+
/** @var \Symfony\Component\HttpFoundation\Response|\PHPUnit_Framework_MockObject_MockObject $hashResponse */
115+
$hashResponse = $this->getMockBuilder('\Symfony\Component\HttpFoundation\Response')
116+
->setMethods(array('prepare'))
117+
->getMock();
118+
$hashResponse->headers->set(HttpCache::USER_HASH_HEADER, $expectedContextHash );
119+
120+
$httpCache = $this->getHttpCachePartialMock(array('lookup'));
121+
$httpCache
122+
->expects($this->at(0))
123+
->method('lookup')
124+
->with($hashRequest, $catch)
125+
->will($this->returnValue($hashResponse));
126+
$httpCache
127+
->expects($this->at(1))
128+
->method('lookup')
129+
->with($request)
130+
->will($this->returnValue($response));
131+
132+
$this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch));
133+
$this->assertTrue($request->headers->has(HttpCache::USER_HASH_HEADER));
134+
$this->assertSame($expectedContextHash, $request->headers->get(HttpCache::USER_HASH_HEADER));
135+
}
136+
}

0 commit comments

Comments
 (0)