ApplicationTest.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace functional;
  3. use Application;
  4. use PHPUnit\Framework\TestCase;
  5. use Stack\Builder;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpKernel\HttpKernelInterface;
  8. class ApplicationTest extends TestCase
  9. {
  10. public function testWithAppendMiddlewares()
  11. {
  12. $request = Request::create('/foo');
  13. $app = new Application();
  14. $finished = false;
  15. $app->finish(function () use (&$finished) {
  16. $finished = true;
  17. });
  18. $stack = new Builder();
  19. $stack
  20. ->push('functional\Append', '.A')
  21. ->push('functional\Append', '.B');
  22. $app = $stack->resolve($app);
  23. $response = $app->handle($request);
  24. $app->terminate($request, $response);
  25. $this->assertSame('bar.B.A', $response->getContent());
  26. $this->assertTrue($finished);
  27. }
  28. }
  29. class Append implements HttpKernelInterface
  30. {
  31. private $app;
  32. private $appendix;
  33. public function __construct(HttpKernelInterface $app, $appendix)
  34. {
  35. $this->app = $app;
  36. $this->appendix = $appendix;
  37. }
  38. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  39. {
  40. $response = clone $this->app->handle($request, $type, $catch);
  41. $response->setContent($response->getContent().$this->appendix);
  42. return $response;
  43. }
  44. }