forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 2
/
IgnoredAttributesTestTrait.php
87 lines (71 loc) · 2.7 KB
/
IgnoredAttributesTestTrait.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
<?php
namespace Symfony\Component\Serializer\Tests\Normalizer\Features;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Test AbstractNormalizer::IGNORED_ATTRIBUTES.
*/
trait IgnoredAttributesTestTrait
{
abstract protected function getNormalizerForIgnoredAttributes(): NormalizerInterface;
abstract protected function getDenormalizerForIgnoredAttributes(): DenormalizerInterface;
public function testIgnoredAttributesNormalize()
{
$normalizer = $this->getNormalizerForIgnoredAttributes();
$objectInner = new ObjectInner();
$objectInner->foo = 'innerFoo';
$objectInner->bar = 'innerBar';
$objectOuter = new ObjectOuter();
$objectOuter->foo = 'foo';
$objectOuter->bar = 'bar';
$objectOuter->setInner($objectInner);
$context = ['ignored_attributes' => ['bar']];
$this->assertEquals(
[
'foo' => 'foo',
'inner' => ['foo' => 'innerFoo'],
'date' => null,
'inners' => null,
],
$normalizer->normalize($objectOuter, null, $context)
);
$context = ['ignored_attributes' => ['foo', 'inner']];
$this->assertEquals(
[
'bar' => 'bar',
'date' => null,
'inners' => null,
],
$normalizer->normalize($objectOuter, null, $context)
);
}
public function testIgnoredAttributesContextDenormalize()
{
$normalizer = $this->getDenormalizerForIgnoredAttributes();
$objectOuter = new ObjectOuter();
$objectOuter->bar = 'bar';
$context = ['ignored_attributes' => ['foo', 'inner']];
$this->assertEquals($objectOuter, $normalizer->denormalize(
[
'foo' => 'foo',
'bar' => 'bar',
'inner' => ['foo' => 'innerFoo', 'bar' => 'innerBar'],
], ObjectOuter::class, null, $context));
}
public function testIgnoredAttributesContextDenormalizeInherit()
{
$normalizer = $this->getDenormalizerForIgnoredAttributes();
$objectInner = new ObjectInner();
$objectInner->foo = 'innerFoo';
$objectOuter = new ObjectOuter();
$objectOuter->foo = 'foo';
$objectOuter->setInner($objectInner);
$context = ['ignored_attributes' => ['bar']];
$this->assertEquals($objectOuter, $normalizer->denormalize(
[
'foo' => 'foo',
'bar' => 'bar',
'inner' => ['foo' => 'innerFoo', 'bar' => 'innerBar'],
], ObjectOuter::class, null, $context));
}
}