Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce5b3e7

Browse files
minor #26913 [Messenger] Add tests for ValidationMiddleware (yceruto)
This PR was merged into the 4.1-dev branch. Discussion ---------- [Messenger] Add tests for ValidationMiddleware | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 4a3f8f4 Add tests for ValidationMiddleware
2 parents f394b45 + 4a3f8f4 commit ce5b3e7

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

src/Symfony/Component/Messenger/Tests/Middleware/HandleMessageMiddlewareTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public function testItCallsTheHandlerAndNextMiddleware()
2222
{
2323
$message = new DummyMessage('Hey');
2424

25-
$handler = $this->createPartialMock(\stdClass::class, ['__invoke']);
25+
$handler = $this->createPartialMock(\stdClass::class, array('__invoke'));
2626
$handler->method('__invoke')->willReturn('Hello');
2727

28-
$next = $this->createPartialMock(\stdClass::class, ['__invoke']);
28+
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
2929

3030
$middleware = new HandleMessageMiddleware(new HandlerLocator(array(
3131
DummyMessage::class => $handler,
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}

src/Symfony/Component/Messenger/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"symfony/property-access": "~3.4|~4.0",
2626
"symfony/var-dumper": "~3.4|~4.0",
2727
"symfony/property-access": "~3.4|~4.0",
28-
"symfony/process": "~4.0"
28+
"symfony/process": "~4.0",
29+
"symfony/validator": "~4.0"
2930
},
3031
"suggest": {
3132
"sroze/enqueue-bridge": "For using the php-enqueue library as an adapter."

0 commit comments

Comments
 (0)