5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpFoundation\Response;
7 use Symfony\Component\HttpKernel\HttpKernelInterface;
8 use Symfony\Component\HttpKernel\TerminableInterface;
10 /** @covers Stack\Builder */
11 class BuilderTest extends \PHPUnit_Framework_TestCase
14 public function withoutMiddlewaresItShouldReturnOriginalResponse()
16 $app = $this->getHttpKernelMock(new Response('ok'));
18 $stack = new Builder();
19 $resolved = $stack->resolve($app);
21 $request = Request::create('/');
22 $response = $resolved->handle($request);
24 $this->assertInstanceOf('Stack\StackedHttpKernel', $resolved);
25 $this->assertSame('ok', $response->getContent());
29 public function resolvedKernelShouldDelegateTerminateCalls()
31 $app = $this->getTerminableMock();
33 $stack = new Builder();
34 $resolved = $stack->resolve($app);
36 $request = Request::create('/');
37 $response = new Response('ok');
39 $resolved->handle($request);
40 $resolved->terminate($request, $response);
44 public function pushShouldReturnSelf()
46 $stack = new Builder();
47 $this->assertSame($stack, $stack->push('Stack\AppendA'));
51 public function pushShouldThrowOnInvalidInput()
53 $this->setExpectedException('InvalidArgumentException', 'Missing argument(s) when calling push');
54 $stack = new Builder();
59 public function unshiftShouldReturnSelf()
61 $stack = new Builder();
62 $this->assertSame($stack, $stack->unshift('Stack\AppendA'));
66 public function unshiftShouldThrowOnInvalidInput()
68 $this->setExpectedException('InvalidArgumentException', 'Missing argument(s) when calling unshift');
69 $stack = new Builder();
74 public function appendMiddlewareShouldAppendToBody()
76 $app = $this->getHttpKernelMock(new Response('ok'));
78 $stack = new Builder();
79 $stack->push('Stack\AppendA');
80 $resolved = $stack->resolve($app);
82 $request = Request::create('/');
83 $response = $resolved->handle($request);
85 $this->assertSame('ok.A', $response->getContent());
89 public function unshiftMiddlewareShouldPutMiddlewareBeforePushed()
91 $app = $this->getHttpKernelMock(new Response('ok'));
93 $stack = new Builder();
94 $stack->push('Stack\Append', '2.');
95 $stack->unshift('Stack\Append', '1.');
96 $resolved = $stack->resolve($app);
98 $request = Request::create('/');
99 $response = $resolved->handle($request);
101 $this->assertSame('ok2.1.', $response->getContent());
105 public function stackedMiddlewaresShouldWrapInReverseOrder()
107 $app = $this->getHttpKernelMock(new Response('ok'));
109 $stack = new Builder();
110 $stack->push('Stack\AppendA');
111 $stack->push('Stack\AppendB');
112 $resolved = $stack->resolve($app);
114 $request = Request::create('/');
115 $response = $resolved->handle($request);
117 $this->assertSame('ok.B.A', $response->getContent());
121 public function resolveShouldPassPushArgumentsToMiddlewareConstructor()
123 $app = $this->getHttpKernelMock(new Response('ok'));
125 $stack = new Builder();
126 $stack->push('Stack\Append', '.foo');
127 $stack->push('Stack\Append', '.bar');
128 $resolved = $stack->resolve($app);
130 $request = Request::create('/');
131 $response = $resolved->handle($request);
133 $this->assertSame('ok.bar.foo', $response->getContent());
137 public function resolveShouldCallSpecFactories()
139 $app = $this->getHttpKernelMock(new Response('ok'));
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);
146 $request = Request::create('/');
147 $response = $resolved->handle($request);
149 $this->assertSame('ok.bar.foo', $response->getContent());
152 private function getHttpKernelMock(Response $response)
154 $app = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
155 $app->expects($this->any())
157 ->with($this->isInstanceOf('Symfony\Component\HttpFoundation\Request'))
158 ->will($this->returnValue($response));
163 private function getTerminableMock()
165 $app = $this->getMock('Stack\TerminableHttpKernel');
166 $app->expects($this->once())
167 ->method('terminate')
169 $this->isInstanceOf('Symfony\Component\HttpFoundation\Request'),
170 $this->isInstanceOf('Symfony\Component\HttpFoundation\Response')
177 abstract class TerminableHttpKernel implements HttpKernelInterface, TerminableInterface
181 class Append implements HttpKernelInterface
186 public function __construct(HttpKernelInterface $app, $appendix)
189 $this->appendix = $appendix;
192 public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
194 $response = clone $this->app->handle($request, $type, $catch);
195 $response->setContent($response->getContent().$this->appendix);
201 class AppendA extends Append
203 public function __construct(HttpKernelInterface $app)
205 parent::__construct($app, '.A');
209 class AppendB extends Append
211 public function __construct(HttpKernelInterface $app)
213 parent::__construct($app, '.B');