1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* (c) itmedia.by <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Itmedia\CommandBusBundle\Tests\Middleware; |
8
|
|
|
|
9
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
10
|
|
|
use Doctrine\Common\Annotations\DocParser; |
11
|
|
|
use Itmedia\CommandBusBundle\Exception\ValidationException; |
12
|
|
|
use Itmedia\CommandBusBundle\Middleware\ValidationMiddleware; |
13
|
|
|
use Itmedia\CommandBusBundle\Tests\Stub\TestCommand; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Symfony\Component\Validator\Constraints\Email; |
16
|
|
|
use Symfony\Component\Validator\Validation; |
17
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank; |
18
|
|
|
|
19
|
|
|
class ValidationMiddlewareTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
public function testHandleFailed() |
22
|
|
|
{ |
23
|
|
|
$this->loadConstraintsClass(); |
24
|
|
|
|
25
|
|
|
$command = new TestCommand(); |
26
|
|
|
$command->handle('Test', 'aaa'); |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
$validator = Validation::createValidatorBuilder() |
30
|
|
|
->enableAnnotationMapping() |
31
|
|
|
->getValidator(); |
32
|
|
|
|
33
|
|
|
$middleware = new ValidationMiddleware($validator); |
34
|
|
|
|
35
|
|
|
try { |
36
|
|
|
$middleware->handle($command); |
37
|
|
|
$this->fail('Must throw ' . ValidationException::class); |
38
|
|
|
} catch (ValidationException $exception) { |
39
|
|
|
$this->assertArrayNotHasKey('username', $exception->getMessages()); |
40
|
|
|
$this->assertArrayHasKey('email', $exception->getMessages()); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
public function testHandleSuccess() |
46
|
|
|
{ |
47
|
|
|
$this->loadConstraintsClass(); |
48
|
|
|
|
49
|
|
|
$command = new TestCommand(); |
50
|
|
|
$command->handle('Test', '[email protected]'); |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
$validator = Validation::createValidatorBuilder() |
54
|
|
|
->enableAnnotationMapping() |
55
|
|
|
->getValidator(); |
56
|
|
|
|
57
|
|
|
$middleware = new ValidationMiddleware($validator); |
58
|
|
|
|
59
|
|
|
$middleware->handle($command); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
private function loadConstraintsClass() |
64
|
|
|
{ |
65
|
|
|
new NotBlank(); |
66
|
|
|
new Email(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|