MiddlewareApplicationTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 4.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Http;
  17. use Cake\Http\MiddlewareApplication;
  18. use Cake\Http\MiddlewareQueue;
  19. use Cake\Http\ServerRequestFactory;
  20. use Cake\TestSuite\TestCase;
  21. use Psr\Http\Message\ResponseInterface;
  22. /**
  23. * MiddlewareApplication test.
  24. */
  25. class MiddlewareApplicationTest extends TestCase
  26. {
  27. /**
  28. * Setup
  29. */
  30. public function setUp(): void
  31. {
  32. parent::setUp();
  33. static::setAppNamespace();
  34. }
  35. /**
  36. * Integration test for a simple controller.
  37. */
  38. public function testHandle(): void
  39. {
  40. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/cakes']);
  41. $request = $request->withAttribute('params', [
  42. 'controller' => 'Cakes',
  43. 'action' => 'index',
  44. 'plugin' => null,
  45. 'pass' => [],
  46. ]);
  47. $app = new class extends MiddlewareApplication {
  48. public function bootstrap(): void
  49. {
  50. }
  51. public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
  52. {
  53. return $middlewareQueue;
  54. }
  55. };
  56. $result = $app->handle($request);
  57. $this->assertInstanceOf(ResponseInterface::class, $result);
  58. $this->assertSame('Not found', '' . $result->getBody());
  59. $this->assertSame(404, $result->getStatusCode());
  60. }
  61. }