Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / vendor / stack / builder / tests / unit / Stack / BuilderTest.php
1 <?php
2
3 namespace Stack;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpFoundation\Response;
7 use Symfony\Component\HttpKernel\HttpKernelInterface;
8 use Symfony\Component\HttpKernel\TerminableInterface;
9
10 /** @covers Stack\Builder */
11 class BuilderTest extends \PHPUnit_Framework_TestCase
12 {
13     /** @test */
14     public function withoutMiddlewaresItShouldReturnOriginalResponse()
15     {
16         $app = $this->getHttpKernelMock(new Response('ok'));
17
18         $stack = new Builder();
19         $resolved = $stack->resolve($app);
20
21         $request = Request::create('/');
22         $response = $resolved->handle($request);
23
24         $this->assertInstanceOf('Stack\StackedHttpKernel', $resolved);
25         $this->assertSame('ok', $response->getContent());
26     }
27
28     /** @test */
29     public function resolvedKernelShouldDelegateTerminateCalls()
30     {
31         $app = $this->getTerminableMock();
32
33         $stack = new Builder();
34         $resolved = $stack->resolve($app);
35
36         $request = Request::create('/');
37         $response = new Response('ok');
38
39         $resolved->handle($request);
40         $resolved->terminate($request, $response);
41     }
42
43     /** @test */
44     public function pushShouldReturnSelf()
45     {
46         $stack = new Builder();
47         $this->assertSame($stack, $stack->push('Stack\AppendA'));
48     }
49
50     /** @test */
51     public function pushShouldThrowOnInvalidInput()
52     {
53         $this->setExpectedException('InvalidArgumentException', 'Missing argument(s) when calling push');
54         $stack = new Builder();
55         $stack->push();
56     }
57
58     /** @test */
59     public function unshiftShouldReturnSelf()
60     {
61         $stack = new Builder();
62         $this->assertSame($stack, $stack->unshift('Stack\AppendA'));
63     }
64
65     /** @test */
66     public function unshiftShouldThrowOnInvalidInput()
67     {
68         $this->setExpectedException('InvalidArgumentException', 'Missing argument(s) when calling unshift');
69         $stack = new Builder();
70         $stack->unshift();
71     }
72
73     /** @test */
74     public function appendMiddlewareShouldAppendToBody()
75     {
76         $app = $this->getHttpKernelMock(new Response('ok'));
77
78         $stack = new Builder();
79         $stack->push('Stack\AppendA');
80         $resolved = $stack->resolve($app);
81
82         $request = Request::create('/');
83         $response = $resolved->handle($request);
84
85         $this->assertSame('ok.A', $response->getContent());
86     }
87
88     /** @test */
89     public function unshiftMiddlewareShouldPutMiddlewareBeforePushed()
90     {
91         $app = $this->getHttpKernelMock(new Response('ok'));
92
93         $stack = new Builder();
94         $stack->push('Stack\Append', '2.');
95         $stack->unshift('Stack\Append', '1.');
96         $resolved = $stack->resolve($app);
97
98         $request = Request::create('/');
99         $response = $resolved->handle($request);
100
101         $this->assertSame('ok2.1.', $response->getContent());
102     }
103
104     /** @test */
105     public function stackedMiddlewaresShouldWrapInReverseOrder()
106     {
107         $app = $this->getHttpKernelMock(new Response('ok'));
108
109         $stack = new Builder();
110         $stack->push('Stack\AppendA');
111         $stack->push('Stack\AppendB');
112         $resolved = $stack->resolve($app);
113
114         $request = Request::create('/');
115         $response = $resolved->handle($request);
116
117         $this->assertSame('ok.B.A', $response->getContent());
118     }
119
120     /** @test */
121     public function resolveShouldPassPushArgumentsToMiddlewareConstructor()
122     {
123         $app = $this->getHttpKernelMock(new Response('ok'));
124
125         $stack = new Builder();
126         $stack->push('Stack\Append', '.foo');
127         $stack->push('Stack\Append', '.bar');
128         $resolved = $stack->resolve($app);
129
130         $request = Request::create('/');
131         $response = $resolved->handle($request);
132
133         $this->assertSame('ok.bar.foo', $response->getContent());
134     }
135
136     /** @test */
137     public function resolveShouldCallSpecFactories()
138     {
139         $app = $this->getHttpKernelMock(new Response('ok'));
140
141         $stack = new Builder();
142         $stack->push(function ($app) { return new Append($app, '.foo'); });
143         $stack->push(function ($app) { return new Append($app, '.bar'); });
144         $resolved = $stack->resolve($app);
145
146         $request = Request::create('/');
147         $response = $resolved->handle($request);
148
149         $this->assertSame('ok.bar.foo', $response->getContent());
150     }
151
152     private function getHttpKernelMock(Response $response)
153     {
154         $app = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
155         $app->expects($this->any())
156             ->method('handle')
157             ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
158             ->will($this->returnValue($response));
159
160         return $app;
161     }
162
163     private function getTerminableMock()
164     {
165         $app = $this->getMock('Stack\TerminableHttpKernel');
166         $app->expects($this->once())
167             ->method('terminate')
168             ->with(
169                 $this->isInstanceOf('Symfony\Component\HttpFoundation\Request'),
170                 $this->isInstanceOf('Symfony\Component\HttpFoundation\Response')
171             );
172
173         return $app;
174     }
175 }
176
177 abstract class TerminableHttpKernel implements HttpKernelInterface, TerminableInterface
178 {
179 }
180
181 class Append implements HttpKernelInterface
182 {
183     private $app;
184     private $appendix;
185
186     public function __construct(HttpKernelInterface $app, $appendix)
187     {
188         $this->app = $app;
189         $this->appendix = $appendix;
190     }
191
192     public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
193     {
194         $response = clone $this->app->handle($request, $type, $catch);
195         $response->setContent($response->getContent().$this->appendix);
196
197         return $response;
198     }
199 }
200
201 class AppendA extends Append
202 {
203     public function __construct(HttpKernelInterface $app)
204     {
205         parent::__construct($app, '.A');
206     }
207 }
208
209 class AppendB extends Append
210 {
211     public function __construct(HttpKernelInterface $app)
212     {
213         parent::__construct($app, '.B');
214     }
215 }