9
9
use Symfony \Component \HttpKernel \Event \FilterResponseEvent ;
10
10
use Symfony \Component \HttpKernel \Event \GetResponseEvent ;
11
11
use Symfony \Component \HttpKernel \HttpKernelInterface ;
12
+ use Symfony \Component \Routing \Matcher \RequestMatcherInterface ;
12
13
13
14
class UserContextSubscriberTest extends \PHPUnit_Framework_TestCase
14
15
{
@@ -17,21 +18,21 @@ public function testOnKernelRequest()
17
18
$ request = new Request ();
18
19
$ request ->setMethod ('HEAD ' );
19
20
20
- $ requestMatcher = \Mockery::mock ('\Symfony\Component\HttpFoundation\RequestMatcherInterface ' );
21
- $ requestMatcher ->shouldReceive ('matches ' )->with ($ request )->andReturn (true );
21
+ $ requestMatcher = $ this ->getRequestMatcher ($ request , true );
22
+ $ hashGenerator = \Mockery::mock ('\FOS\HttpCache\UserContext\HashGenerator ' );
23
+ $ hashGenerator ->shouldReceive ('generateHash ' )->andReturn ('hash ' );
22
24
23
- $ userContextSubscriber = new UserContextSubscriber ($ requestMatcher , new HashGenerator (), 'X-SessionId ' , 'X-Hash ' );
25
+ $ userContextSubscriber = new UserContextSubscriber ($ requestMatcher , $ hashGenerator , array ( 'X-SessionId ' ) , 'X-Hash ' );
24
26
$ event = $ this ->getKernelRequestEvent ($ request );
25
27
26
28
$ userContextSubscriber ->onKernelRequest ($ event );
27
29
28
30
$ response = $ event ->getResponse ();
29
- $ hash = hash ('sha256 ' , serialize (array ()));
30
31
31
32
32
33
$ this ->assertNotNull ($ response );
33
34
$ this ->assertInstanceOf ('\Symfony\Component\HttpFoundation\Response ' , $ response );
34
- $ this ->assertEquals ($ hash , $ response ->headers ->get ('X-Hash ' ));
35
+ $ this ->assertEquals (' hash ' , $ response ->headers ->get ('X-Hash ' ));
35
36
$ this ->assertNull ($ response ->headers ->get ('Vary ' ));
36
37
$ this ->assertEquals ('max-age=0, no-cache, private ' , $ response ->headers ->get ('Cache-Control ' ));
37
38
}
@@ -41,20 +42,20 @@ public function testOnKernelRequestCached()
41
42
$ request = new Request ();
42
43
$ request ->setMethod ('HEAD ' );
43
44
44
- $ requestMatcher = \Mockery::mock ('\Symfony\Component\HttpFoundation\RequestMatcherInterface ' );
45
- $ requestMatcher ->shouldReceive ('matches ' )->with ($ request )->andReturn (true );
45
+ $ requestMatcher = $ this ->getRequestMatcher ($ request , true );
46
+ $ hashGenerator = \Mockery::mock ('\FOS\HttpCache\UserContext\HashGenerator ' );
47
+ $ hashGenerator ->shouldReceive ('generateHash ' )->andReturn ('hash ' );
46
48
47
- $ userContextSubscriber = new UserContextSubscriber ($ requestMatcher , new HashGenerator (), 'X-SessionId ' , 'X-Hash ' , 30 );
49
+ $ userContextSubscriber = new UserContextSubscriber ($ requestMatcher , $ hashGenerator , array ( 'X-SessionId ' ) , 'X-Hash ' , 30 );
48
50
$ event = $ this ->getKernelRequestEvent ($ request );
49
51
50
52
$ userContextSubscriber ->onKernelRequest ($ event );
51
53
52
54
$ response = $ event ->getResponse ();
53
- $ hash = hash ('sha256 ' , serialize (array ()));
54
55
55
56
$ this ->assertNotNull ($ response );
56
57
$ this ->assertInstanceOf ('\Symfony\Component\HttpFoundation\Response ' , $ response );
57
- $ this ->assertEquals ($ hash , $ response ->headers ->get ('X-Hash ' ));
58
+ $ this ->assertEquals (' hash ' , $ response ->headers ->get ('X-Hash ' ));
58
59
$ this ->assertEquals ('X-SessionId ' , $ response ->headers ->get ('Vary ' ));
59
60
$ this ->assertEquals ('max-age=30, private ' , $ response ->headers ->get ('Cache-Control ' ));
60
61
}
@@ -64,16 +65,15 @@ public function testOnKernelRequestNotMatched()
64
65
$ request = new Request ();
65
66
$ request ->setMethod ('HEAD ' );
66
67
67
- $ requestMatcher = \Mockery:: mock ( ' \Symfony\Component\HttpFoundation\RequestMatcherInterface ' );
68
- $ requestMatcher -> shouldReceive ( ' matches ' )-> with ( $ request )-> andReturn ( false );
68
+ $ requestMatcher = $ this -> getRequestMatcher ( $ request , false );
69
+ $ hashGenerator = \Mockery:: mock ( ' \FOS\HttpCache\UserContext\HashGenerator ' );
69
70
70
- $ userContextSubscriber = new UserContextSubscriber ($ requestMatcher , new HashGenerator (), 'X-SessionId ' , 'X-Hash ' );
71
+ $ userContextSubscriber = new UserContextSubscriber ($ requestMatcher , $ hashGenerator , array ( 'X-SessionId ' ) , 'X-Hash ' );
71
72
$ event = $ this ->getKernelRequestEvent ($ request );
72
73
73
74
$ userContextSubscriber ->onKernelRequest ($ event );
74
75
75
76
$ response = $ event ->getResponse ();
76
- $ hash = hash ('sha256 ' , serialize (array ()));
77
77
78
78
$ this ->assertNull ($ response );
79
79
}
@@ -84,31 +84,34 @@ public function testOnKernelResponse()
84
84
$ request ->setMethod ('HEAD ' );
85
85
$ request ->headers ->set ('X-Hash ' , 'hash ' );
86
86
87
- $ requestMatcher = \Mockery:: mock ( ' \Symfony\Component\HttpFoundation\RequestMatcherInterface ' );
88
- $ requestMatcher -> shouldReceive ( ' matches ' )-> with ( $ request )-> andReturn ( false );
87
+ $ requestMatcher = $ this -> getRequestMatcher ( $ request , false );
88
+ $ hashGenerator = \Mockery:: mock ( ' \FOS\HttpCache\UserContext\HashGenerator ' );
89
89
90
- $ userContextSubscriber = new UserContextSubscriber ($ requestMatcher , new HashGenerator (), 'X-SessionId ' , 'X-Hash ' );
90
+ $ userContextSubscriber = new UserContextSubscriber ($ requestMatcher , $ hashGenerator , array ( 'X-SessionId ' ) , 'X-Hash ' );
91
91
$ event = $ this ->getKernelResponseEvent ($ request );
92
92
93
93
$ userContextSubscriber ->onKernelResponse ($ event );
94
94
95
95
$ this ->assertContains ('X-Hash ' , $ event ->getResponse ()->headers ->get ('Vary ' ));
96
96
}
97
97
98
+ /**
99
+ * If there is no hash in the request, vary on the user identifier.
100
+ */
98
101
public function testOnKernelResponseNotCached ()
99
102
{
100
103
$ request = new Request ();
101
104
$ request ->setMethod ('HEAD ' );
102
105
103
- $ requestMatcher = \Mockery:: mock ( ' \Symfony\Component\HttpFoundation\RequestMatcherInterface ' );
104
- $ requestMatcher -> shouldReceive ( ' matches ' )-> with ( $ request )-> andReturn ( false );
106
+ $ requestMatcher = $ this -> getRequestMatcher ( $ request , false );
107
+ $ hashGenerator = \Mockery:: mock ( ' \FOS\HttpCache\UserContext\HashGenerator ' );
105
108
106
- $ userContextSubscriber = new UserContextSubscriber ($ requestMatcher , new HashGenerator (), 'X-SessionId ' , 'X-Hash ' );
109
+ $ userContextSubscriber = new UserContextSubscriber ($ requestMatcher , $ hashGenerator , array ( 'X-SessionId ' ) , 'X-Hash ' );
107
110
$ event = $ this ->getKernelResponseEvent ($ request );
108
111
109
112
$ userContextSubscriber ->onKernelResponse ($ event );
110
113
111
- $ this ->assertNull ( $ event ->getResponse ()->headers ->get ('Vary ' ));
114
+ $ this ->assertEquals ( ' X-SessionId ' , $ event ->getResponse ()->headers ->get ('Vary ' ));
112
115
}
113
116
114
117
protected function getKernelRequestEvent (Request $ request )
@@ -129,4 +132,18 @@ protected function getKernelResponseEvent(Request $request, Response $response =
129
132
$ response ?: new Response ()
130
133
);
131
134
}
135
+
136
+ /**
137
+ * @param Request $request
138
+ * @param bool $match
139
+ *
140
+ * @return \Mockery\MockInterface|RequestMatcherInterface
141
+ */
142
+ private function getRequestMatcher (Request $ request , $ match )
143
+ {
144
+ $ requestMatcher = \Mockery::mock ('\Symfony\Component\HttpFoundation\RequestMatcherInterface ' );
145
+ $ requestMatcher ->shouldReceive ('matches ' )->with ($ request )->andReturn ($ match );
146
+
147
+ return $ requestMatcher ;
148
+ }
132
149
}
0 commit comments