|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Adapter; |
|
6
|
|
|
|
|
7
|
|
|
use Gorynych\Adapter\ValidatorAdapter; |
|
8
|
|
|
use Gorynych\Resource\Exception\InvalidEntityException; |
|
9
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
use Symfony\Component\Validator\ConstraintViolationInterface; |
|
12
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
|
13
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
14
|
|
|
|
|
15
|
|
|
class ValidatorAdapterTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var ValidatorInterface|MockObject */ |
|
18
|
|
|
private $validatorMock; |
|
19
|
|
|
|
|
20
|
|
|
public function setUp(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$this->validatorMock = $this->createMock(ValidatorInterface::class); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testThrowsInvalidEntityOnFailure(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$this->expectException(InvalidEntityException::class); |
|
28
|
|
|
|
|
29
|
|
|
$constraintViolation = $this->createMock(ConstraintViolationInterface::class); |
|
30
|
|
|
$constraintViolation->method('getPropertyPath')->willReturn('foo'); |
|
31
|
|
|
$constraintViolation->method('getMessage')->willReturn('bar'); |
|
32
|
|
|
|
|
33
|
|
|
$constraintViolationList = new ConstraintViolationList([$constraintViolation]); |
|
34
|
|
|
|
|
35
|
|
|
$this->validatorMock |
|
36
|
|
|
->expects($this->once()) |
|
37
|
|
|
->method('validate') |
|
38
|
|
|
->with(new \stdClass()) |
|
39
|
|
|
->willReturn($constraintViolationList); |
|
40
|
|
|
|
|
41
|
|
|
$this->setUpAdapter()->validate(new \stdClass()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function setUpAdapter(): ValidatorAdapter |
|
45
|
|
|
{ |
|
46
|
|
|
$reflection = new \ReflectionClass(ValidatorAdapter::class); |
|
47
|
|
|
$validatorAdapter = $reflection->newInstanceWithoutConstructor(); |
|
48
|
|
|
|
|
49
|
|
|
$reflectionProperty = $reflection->getProperty('validator'); |
|
50
|
|
|
$reflectionProperty->setAccessible(true); |
|
51
|
|
|
$reflectionProperty->setValue($validatorAdapter, $this->validatorMock); |
|
52
|
|
|
|
|
53
|
|
|
return $validatorAdapter; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|