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
<?php
namespace JMS\Serializer\Tests\Serializer\EventDispatcher\Subscriber;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\EventDispatcher\EventDispatcher;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use JMS\Serializer\EventDispatcher\Subscriber\SymfonyValidatorSubscriber;
use JMS\Serializer\SerializerBuilder;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
class SymfonyValidatorSubscriberTest extends \PHPUnit_Framework_TestCase
{
private $validator;
/** @var SymfonyValidatorSubscriber */
private $subscriber;
public function testValidate()
{
$obj = new \stdClass;
$this->validator->expects($this->once())
->method('validate')
->with($obj, array('foo'))
->will($this->returnValue(new ConstraintViolationList()));
$context = DeserializationContext::create()->setAttribute('validation_groups', array('foo'));
$this->subscriber->onPostDeserialize(new ObjectEvent($context, $obj, array()));
}
/**
* @expectedException JMS\Serializer\Exception\ValidationFailedException
* @expectedExceptionMessage Validation failed with 1 error(s).
*/
public function testValidateThrowsExceptionWhenListIsNotEmpty()
{
$obj = new \stdClass;
$this->validator->expects($this->once())
->method('validate')
->with($obj, array('foo'))
->will($this->returnValue(new ConstraintViolationList(array(new ConstraintViolation('foo', 'foo', array(), 'a', 'b', 'c')))));
$context = DeserializationContext::create()->setAttribute('validation_groups', array('foo'));
$this->subscriber->onPostDeserialize(new ObjectEvent($context, $obj, array()));
}
public function testValidatorIsNotCalledWhenNoGroupsAreSet()
{
$this->validator->expects($this->never())
->method('validate');
$this->subscriber->onPostDeserialize(new ObjectEvent(DeserializationContext::create(), new \stdClass, array()));
}
public function testValidationIsOnlyPerformedOnRootObject()
{
$this->validator->expects($this->once())
->method('validate')
->with($this->isInstanceOf('JMS\Serializer\Tests\Fixtures\AuthorList'), array('Foo'))
->will($this->returnValue(new ConstraintViolationList()));
$subscriber = $this->subscriber;
$list = SerializerBuilder::create()
->configureListeners(function (EventDispatcher $dispatcher) use ($subscriber) {
$dispatcher->addSubscriber($subscriber);
})
->build()
->deserialize(
'{"authors":[{"full_name":"foo"},{"full_name":"bar"}]}',
'JMS\Serializer\Tests\Fixtures\AuthorList',
'json',
DeserializationContext::create()->setAttribute('validation_groups', array('Foo'))
);
$this->assertCount(2, $list);
}
protected function setUp()
{
if (!interface_exists('Symfony\Component\Validator\ValidatorInterface')) {
$this->markTestSkipped('Symfony\Component\Validator\ValidatorInterface is not available');
}
$this->validator = $this->getMockBuilder('Symfony\Component\Validator\ValidatorInterface')->getMock();
$this->subscriber = new SymfonyValidatorSubscriber($this->validator);
}
}