RunnerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 3.3.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase;
  17. use Cake\Http\MiddlewareQueue;
  18. use Cake\Http\Response;
  19. use Cake\Http\Runner;
  20. use Cake\TestSuite\TestCase;
  21. use Psr\Http\Message\ResponseInterface;
  22. use RuntimeException;
  23. /**
  24. * Test case for runner.
  25. */
  26. class RunnerTest extends TestCase
  27. {
  28. /**
  29. * setup
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. parent::setUp();
  36. $this->queue = new MiddlewareQueue();
  37. $this->ok = function ($req, $res, $next) {
  38. return $next($req, $res);
  39. };
  40. $this->pass = function ($req, $res, $next) {
  41. return $next($req, $res);
  42. };
  43. $this->fail = function ($req, $res, $next) {
  44. throw new RuntimeException('A bad thing');
  45. };
  46. }
  47. /**
  48. * Test running a single middleware object.
  49. *
  50. * @return void
  51. */
  52. public function testRunSingle()
  53. {
  54. $this->queue->add($this->ok);
  55. $req = $this->getMockBuilder('Psr\Http\Message\ServerRequestInterface')->getMock();
  56. $runner = new Runner();
  57. $result = $runner->run($this->queue, $req);
  58. $this->assertInstanceof(ResponseInterface::class, $result);
  59. }
  60. /**
  61. * Test that middleware is run in sequence
  62. *
  63. * @return void
  64. */
  65. public function testRunSequencing()
  66. {
  67. $log = [];
  68. $one = function ($req, $handler) use (&$log) {
  69. $log[] = 'one';
  70. return $handler->handle($req);
  71. };
  72. $two = function ($req, $res, $next) use (&$log) {
  73. $log[] = 'two';
  74. return $next($req, $res);
  75. };
  76. $three = function ($req, $res, $next) use (&$log) {
  77. $log[] = 'three';
  78. return $next($req, $res);
  79. };
  80. $this->queue->add($one)->add($two)->add($three);
  81. $runner = new Runner();
  82. $req = $this->getMockBuilder('Psr\Http\Message\ServerRequestInterface')->getMock();
  83. $result = $runner->run($this->queue, $req);
  84. $this->assertInstanceof(Response::class, $result);
  85. $expected = ['one', 'two', 'three'];
  86. $this->assertEquals($expected, $log);
  87. }
  88. /**
  89. * Test that exceptions bubble up.
  90. *
  91. */
  92. public function testRunExceptionInMiddleware()
  93. {
  94. $this->expectException(\RuntimeException::class);
  95. $this->expectExceptionMessage('A bad thing');
  96. $this->queue->add($this->ok)->add($this->fail);
  97. $req = $this->getMockBuilder('Psr\Http\Message\ServerRequestInterface')->getMock();
  98. $runner = new Runner();
  99. $runner->run($this->queue, $req);
  100. }
  101. }