-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathTraceableNormalizerTest.php
115 lines (94 loc) · 5.38 KB
/
TraceableNormalizerTest.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
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Tests\Debug;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\DataCollector\SerializerDataCollector;
use Symfony\Component\Serializer\Debug\TraceableNormalizer;
use Symfony\Component\Serializer\Debug\TraceableSerializer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class TraceableNormalizerTest extends TestCase
{
public function testForwardsToNormalizer()
{
$normalizer = $this->createMock(NormalizerInterface::class);
$normalizer->method('getSupportedTypes')->willReturn(['*' => false]);
$normalizer
->expects($this->once())
->method('normalize')
->with('data', 'format', $this->isType('array'))
->willReturn('normalized');
$denormalizer = $this->createMock(DenormalizerInterface::class);
$denormalizer->method('getSupportedTypes')->willReturn(['*' => false]);
$denormalizer
->expects($this->once())
->method('denormalize')
->with('data', 'type', 'format', $this->isType('array'))
->willReturn('denormalized');
$this->assertSame('normalized', (new TraceableNormalizer($normalizer, new SerializerDataCollector(), 'default'))->normalize('data', 'format'));
$this->assertSame('denormalized', (new TraceableNormalizer($denormalizer, new SerializerDataCollector(), 'default'))->denormalize('data', 'type', 'format'));
}
public function testCollectNormalizationData()
{
$serializerName = uniqid('name', true);
$normalizer = $this->createMock(NormalizerInterface::class);
$normalizer->method('getSupportedTypes')->willReturn(['*' => false]);
$denormalizer = $this->createMock(DenormalizerInterface::class);
$denormalizer->method('getSupportedTypes')->willReturn(['*' => false]);
$dataCollector = $this->createMock(SerializerDataCollector::class);
$dataCollector
->expects($this->once())
->method('collectNormalization')
->with($this->isType('string'), $normalizer::class, $this->isType('float'), $serializerName);
$dataCollector
->expects($this->once())
->method('collectDenormalization')
->with($this->isType('string'), $denormalizer::class, $this->isType('float'), $serializerName);
(new TraceableNormalizer($normalizer, $dataCollector, $serializerName))->normalize('data', 'format', [TraceableSerializer::DEBUG_TRACE_ID => 'debug']);
(new TraceableNormalizer($denormalizer, $dataCollector, $serializerName))->denormalize('data', 'type', 'format', [TraceableSerializer::DEBUG_TRACE_ID => 'debug']);
}
public function testNotCollectNormalizationDataIfNoDebugTraceId()
{
$normalizer = $this->createMock(NormalizerInterface::class);
$normalizer->method('getSupportedTypes')->willReturn(['*' => false]);
$denormalizer = $this->createMock(DenormalizerInterface::class);
$denormalizer->method('getSupportedTypes')->willReturn(['*' => false]);
$dataCollector = $this->createMock(SerializerDataCollector::class);
$dataCollector->expects($this->never())->method('collectNormalization');
$dataCollector->expects($this->never())->method('collectDenormalization');
(new TraceableNormalizer($normalizer, $dataCollector, 'default'))->normalize('data', 'format');
(new TraceableNormalizer($denormalizer, $dataCollector, 'default'))->denormalize('data', 'type', 'format');
}
public function testCannotNormalizeIfNotNormalizer()
{
$this->expectException(\BadMethodCallException::class);
(new TraceableNormalizer($this->createMock(DenormalizerInterface::class), new SerializerDataCollector(), 'default'))->normalize('data');
}
public function testCannotDenormalizeIfNotDenormalizer()
{
$this->expectException(\BadMethodCallException::class);
(new TraceableNormalizer($this->createMock(NormalizerInterface::class), new SerializerDataCollector(), 'default'))->denormalize('data', 'type');
}
public function testSupports()
{
$normalizer = $this->createMock(NormalizerInterface::class);
$normalizer->method('getSupportedTypes')->willReturn(['*' => false]);
$normalizer->method('supportsNormalization')->willReturn(true);
$denormalizer = $this->createMock(DenormalizerInterface::class);
$denormalizer->method('getSupportedTypes')->willReturn(['*' => false]);
$denormalizer->method('supportsDenormalization')->willReturn(true);
$traceableNormalizer = new TraceableNormalizer($normalizer, new SerializerDataCollector(), 'default');
$traceableDenormalizer = new TraceableNormalizer($denormalizer, new SerializerDataCollector(), 'default');
$this->assertTrue($traceableNormalizer->supportsNormalization('data'));
$this->assertTrue($traceableDenormalizer->supportsDenormalization('data', 'type'));
$this->assertFalse($traceableNormalizer->supportsDenormalization('data', 'type'));
$this->assertFalse($traceableDenormalizer->supportsNormalization('data'));
}
}