|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Messenger\Tests\Middleware; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Messenger\Middleware\ValidationMiddleware; |
| 16 | +use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; |
| 17 | +use Symfony\Component\Validator\ConstraintViolationListInterface; |
| 18 | +use Symfony\Component\Validator\Validator\ValidatorInterface; |
| 19 | + |
| 20 | +class ValidationMiddlewareTest extends TestCase |
| 21 | +{ |
| 22 | + public function testValidateAndNextMiddleware() |
| 23 | + { |
| 24 | + $message = new DummyMessage('Hey'); |
| 25 | + |
| 26 | + $validator = $this->createMock(ValidatorInterface::class); |
| 27 | + $validator |
| 28 | + ->expects($this->once()) |
| 29 | + ->method('validate') |
| 30 | + ->with($message) |
| 31 | + ->willReturn($this->createMock(ConstraintViolationListInterface::class)) |
| 32 | + ; |
| 33 | + $next = $this->createPartialMock(\stdClass::class, array('__invoke')); |
| 34 | + $next |
| 35 | + ->expects($this->once()) |
| 36 | + ->method('__invoke') |
| 37 | + ->with($message) |
| 38 | + ->willReturn('Hello') |
| 39 | + ; |
| 40 | + |
| 41 | + $result = (new ValidationMiddleware($validator))->handle($message, $next); |
| 42 | + |
| 43 | + $this->assertSame('Hello', $result); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @expectedException \Symfony\Component\Messenger\Exception\ValidationFailedException |
| 48 | + * @expectedExceptionMessage Message of type "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage" failed validation. |
| 49 | + */ |
| 50 | + public function testValidationFailedException() |
| 51 | + { |
| 52 | + $message = new DummyMessage('Hey'); |
| 53 | + |
| 54 | + $violationList = $this->createMock(ConstraintViolationListInterface::class); |
| 55 | + $violationList |
| 56 | + ->expects($this->once()) |
| 57 | + ->method('count') |
| 58 | + ->willReturn(1) |
| 59 | + ; |
| 60 | + $validator = $this->createMock(ValidatorInterface::class); |
| 61 | + $validator |
| 62 | + ->expects($this->once()) |
| 63 | + ->method('validate') |
| 64 | + ->with($message) |
| 65 | + ->willReturn($violationList) |
| 66 | + ; |
| 67 | + $next = $this->createPartialMock(\stdClass::class, array('__invoke')); |
| 68 | + $next |
| 69 | + ->expects($this->never()) |
| 70 | + ->method('__invoke') |
| 71 | + ; |
| 72 | + |
| 73 | + (new ValidationMiddleware($validator))->handle($message, $next); |
| 74 | + } |
| 75 | +} |
0 commit comments