-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
ValidationMiddlewareTest.php
102 lines (89 loc) · 3.45 KB
/
ValidationMiddlewareTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Messenger\Tests\Middleware;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration;
use Symfony\Component\Messenger\Middleware\ValidationMiddleware;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class ValidationMiddlewareTest extends TestCase
{
public function testValidateAndNextMiddleware()
{
$message = new DummyMessage('Hey');
$envelope = Envelope::wrap($message);
$validator = $this->createMock(ValidatorInterface::class);
$validator
->expects($this->once())
->method('validate')
->with($message)
->willReturn($this->createMock(ConstraintViolationListInterface::class))
;
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
$next
->expects($this->once())
->method('__invoke')
->with($envelope)
->willReturn('Hello')
;
$result = (new ValidationMiddleware($validator))->handle($envelope, $next);
$this->assertSame('Hello', $result);
}
public function testValidateWithConfigurationAndNextMiddleware()
{
$envelope = Envelope::wrap($message = new DummyMessage('Hey'))->with(new ValidationConfiguration($groups = array('Default', 'Extra')));
$validator = $this->createMock(ValidatorInterface::class);
$validator
->expects($this->once())
->method('validate')
->with($message, null, $groups)
->willReturn($this->createMock(ConstraintViolationListInterface::class))
;
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
$next
->expects($this->once())
->method('__invoke')
->with($envelope)
->willReturn('Hello')
;
$result = (new ValidationMiddleware($validator))->handle($envelope, $next);
$this->assertSame('Hello', $result);
}
/**
* @expectedException \Symfony\Component\Messenger\Exception\ValidationFailedException
* @expectedExceptionMessage Message of type "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage" failed validation.
*/
public function testValidationFailedException()
{
$message = new DummyMessage('Hey');
$envelope = Envelope::wrap($message);
$violationList = $this->createMock(ConstraintViolationListInterface::class);
$violationList
->expects($this->once())
->method('count')
->willReturn(1)
;
$validator = $this->createMock(ValidatorInterface::class);
$validator
->expects($this->once())
->method('validate')
->with($message)
->willReturn($violationList)
;
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
$next
->expects($this->never())
->method('__invoke')
;
(new ValidationMiddleware($validator))->handle($envelope, $next);
}
}