MiddlewareTestCase.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Messenger\Test\Middleware;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Messenger\Envelope;
  13. use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
  14. use Symfony\Component\Messenger\Middleware\StackInterface;
  15. use Symfony\Component\Messenger\Middleware\StackMiddleware;
  16. /**
  17. * @author Nicolas Grekas <p@tchwork.com>
  18. */
  19. abstract class MiddlewareTestCase extends TestCase
  20. {
  21. protected function getStackMock(bool $nextIsCalled = true)
  22. {
  23. if (!$nextIsCalled) {
  24. $stack = $this->createMock(StackInterface::class);
  25. $stack
  26. ->expects($this->never())
  27. ->method('next')
  28. ;
  29. return $stack;
  30. }
  31. $nextMiddleware = $this->createMock(MiddlewareInterface::class);
  32. $nextMiddleware
  33. ->expects($this->once())
  34. ->method('handle')
  35. ->willReturnCallback(function (Envelope $envelope, StackInterface $stack): Envelope {
  36. return $envelope;
  37. })
  38. ;
  39. return new StackMiddleware($nextMiddleware);
  40. }
  41. protected function getThrowingStackMock(\Throwable $throwable = null)
  42. {
  43. $nextMiddleware = $this->createMock(MiddlewareInterface::class);
  44. $nextMiddleware
  45. ->expects($this->once())
  46. ->method('handle')
  47. ->willThrowException($throwable ?? new \RuntimeException('Thrown from next middleware.'))
  48. ;
  49. return new StackMiddleware($nextMiddleware);
  50. }
  51. }