Completed
Push — master ( 9de1be...100987 )
by David
8s
created

Tests/Unit/HttpCacheTest.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
/**
20
 * @group legacy
21
 */
22
class HttpCacheTest extends \PHPUnit_Framework_TestCase
23
{
24
    /**
25
     * @return \FOS\HttpCacheBundle\HttpCache|\PHPUnit_Framework_MockObject_MockObject
26
     */
27
    protected function getHttpCachePartialMock(array $mockedMethods = null)
28
    {
29
        // HttpKernelInterface does not work, as symfony HttpCache requires an isDebug method
30
        $mockKernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
31
        $mock = $this->getMockBuilder('\FOS\HttpCacheBundle\HttpCache')
32
            ->setConstructorArgs(array($mockKernel, sys_get_temp_dir()))
33
            ->setMethods($mockedMethods)
34
            ->getMock()
35
        ;
36
37
        // Force setting options property since we can't use original constructor.
38
        $options = array(
39
            'debug' => false,
40
            'default_ttl' => 0,
41
            'private_headers' => array('Authorization', 'Cookie'),
42
            'allow_reload' => false,
43
            'allow_revalidate' => false,
44
            'stale_while_revalidate' => 2,
45
            'stale_if_error' => 60,
46
        );
47
48
        $refHttpCache = new \ReflectionClass('Symfony\Component\HttpKernel\HttpCache\HttpCache');
49
        // Workaround for Symfony 2.3 where $options property is not defined.
50 View Code Duplication
        if (!$refHttpCache->hasProperty('options')) {
51
            $mock->options = $options;
52
        } else {
53
            $refOptions = $refHttpCache
54
                ->getProperty('options');
55
            $refOptions->setAccessible(true);
56
            $refOptions->setValue($mock, $options);
57
        }
58
59
        return $mock;
60
    }
61
62 View Code Duplication
    public function testGenerateUserHashNotAllowed()
63
    {
64
        $request = new Request();
65
        $request->headers->set('accept', HttpCache::USER_HASH_ACCEPT_HEADER);
66
        $httpCache = $this->getHttpCachePartialMock();
67
        $response = $httpCache->handle($request);
68
        $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response);
69
        $this->assertSame(400, $response->getStatusCode());
70
    }
71
72 View Code Duplication
    public function testPassingUserHashNotAllowed()
73
    {
74
        $request = new Request();
75
        $request->headers->set(HttpCache::USER_HASH_HEADER, 'foo');
76
        $httpCache = $this->getHttpCachePartialMock();
77
        $response = $httpCache->handle($request);
78
        $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response);
79
        $this->assertSame(400, $response->getStatusCode());
80
    }
81
82
    public function testUserHashAnonymous()
83
    {
84
        $request = new Request();
85
        $catch = true;
86
87
        $httpCache = $this->getHttpCachePartialMock(array('lookup'));
88
        $response = new Response();
89
        $httpCache
0 ignored issues
show
The method expects() does not seem to exist on object<FOS\HttpCacheBundle\HttpCache>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
            ->expects($this->once())
91
            ->method('lookup')
92
            ->with($request, $catch)
93
            ->will($this->returnValue($response));
94
95
        $this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch));
96
        $this->assertTrue($request->headers->has(HttpCache::USER_HASH_HEADER));
0 ignored issues
show
Deprecated Code introduced by
The constant FOS\HttpCacheBundle\HttpCache::USER_HASH_HEADER has been deprecated with message: Use the options on UserContextSubscriber instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
97
        $this->assertSame(HttpCache::ANONYMOUS_HASH, $request->headers->get(HttpCache::USER_HASH_HEADER));
98
    }
99
100
    public function testUserHashUserWithSession()
101
    {
102
        $catch = true;
103
        $sessionId1 = 'my_session_id';
104
        $sessionId2 = 'another_session_id';
105
        $cookies = array(
106
            'PHPSESSID' => $sessionId1,
107
            'PHPSESSIDsdiuhsdf4535d4f' => $sessionId2,
108
            'foo' => 'bar',
109
        );
110
        $cookieString = "PHPSESSID=$sessionId1; foo=bar; PHPSESSIDsdiuhsdf4535d4f=$sessionId2";
111
        $request = Request::create('/foo', 'GET', array(), $cookies, array(), array('Cookie' => $cookieString));
112
        $response = new Response();
113
114
        $hashRequest = Request::create(HttpCache::USER_HASH_URI, HttpCache::USER_HASH_METHOD, array(), array(), array(), $request->server->all());
115
        $hashRequest->attributes->set('internalRequest', true);
116
        $hashRequest->headers->set('Accept', HttpCache::USER_HASH_ACCEPT_HEADER);
117
        $hashRequest->headers->set('Cookie', "PHPSESSID=$sessionId1; PHPSESSIDsdiuhsdf4535d4f=$sessionId2");
118
        $hashRequest->cookies->set('PHPSESSID', $sessionId1);
119
        $hashRequest->cookies->set('PHPSESSIDsdiuhsdf4535d4f', $sessionId2);
120
        // Ensure request properties have been filled up.
121
        $hashRequest->getPathInfo();
122
        $hashRequest->getMethod();
123
124
        $expectedContextHash = 'my_generated_hash';
125
        // Just avoid the response to modify the request object, otherwise it's impossible to test objects equality.
126
        /** @var \Symfony\Component\HttpFoundation\Response|\PHPUnit_Framework_MockObject_MockObject $hashResponse */
127
        $hashResponse = $this->getMockBuilder('\Symfony\Component\HttpFoundation\Response')
128
            ->setMethods(array('prepare'))
129
            ->getMock();
130
        $hashResponse->headers->set(HttpCache::USER_HASH_HEADER, $expectedContextHash);
131
132
        $httpCache = $this->getHttpCachePartialMock(array('lookup'));
133
        $httpCache
0 ignored issues
show
The method expects() does not seem to exist on object<FOS\HttpCacheBundle\HttpCache>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
            ->expects($this->at(0))
135
            ->method('lookup')
136
            ->with($hashRequest, $catch)
137
            ->will($this->returnValue($hashResponse));
138
        $httpCache
0 ignored issues
show
The method expects() does not seem to exist on object<FOS\HttpCacheBundle\HttpCache>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
139
            ->expects($this->at(1))
140
            ->method('lookup')
141
            ->with($request)
142
            ->will($this->returnValue($response));
143
144
        $this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch));
145
        $this->assertTrue($request->headers->has(HttpCache::USER_HASH_HEADER));
0 ignored issues
show
Deprecated Code introduced by
The constant FOS\HttpCacheBundle\HttpCache::USER_HASH_HEADER has been deprecated with message: Use the options on UserContextSubscriber instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
146
        $this->assertSame($expectedContextHash, $request->headers->get(HttpCache::USER_HASH_HEADER));
147
    }
148
}
149