Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bc48707

Browse files
committed
extract tests for callbacks
1 parent c3b05a7 commit bc48707

File tree

5 files changed

+174
-265
lines changed

5 files changed

+174
-265
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,19 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
5555
public const ALLOW_EXTRA_ATTRIBUTES = 'allow_extra_attributes';
5656

5757
const DEFAULT_CONSTRUCTOR_ARGUMENTS = 'default_constructor_arguments';
58-
const CALLBACKS = 'callbacks';
58+
59+
/**
60+
* Hashmap of field name => callable to normalize this field.
61+
*
62+
* The callable is called if the field is encountered with the arguments:
63+
*
64+
* - mixed $attributeValue value of this field
65+
* - object $object the whole object being normalized
66+
* - string $attributeName name of the attribute being normalized
67+
* - string $format the requested format
68+
* - array $context the serialization context
69+
*/
70+
public const CALLBACKS = 'callbacks';
5971
const CIRCULAR_REFERENCE_HANDLER = 'circular_reference_handler';
6072

6173
/**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Tests\Normalizer\Features;
4+
5+
class CallbacksObject
6+
{
7+
public $bar;
8+
9+
public function __construct($bar = null)
10+
{
11+
$this->bar = $bar;
12+
}
13+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Tests\Normalizer\Features;
4+
5+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
6+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
7+
8+
/**
9+
* Test AbstractNormalizer::CALLBACKS.
10+
*/
11+
trait CallbacksTestTrait
12+
{
13+
abstract protected function getNormalizerForCallbacks(): NormalizerInterface;
14+
15+
/**
16+
* @dataProvider provideCallbacks
17+
*/
18+
public function testCallbacks($callbacks, $valueBar, $result)
19+
{
20+
$normalizer = $this->getNormalizerForCallbacks();
21+
22+
$obj = new CallbacksObject();
23+
$obj->bar = $valueBar;
24+
25+
$this->assertEquals(
26+
$result,
27+
$normalizer->normalize($obj, 'any', ['callbacks' => $callbacks])
28+
);
29+
}
30+
31+
/**
32+
* @dataProvider provideInvalidCallbacks()
33+
*/
34+
public function testUncallableCallbacks($callbacks)
35+
{
36+
$normalizer = $this->getNormalizerForCallbacks();
37+
38+
$obj = new CallbacksObject();
39+
40+
$this->markTestSkipped('Callback validation for callbacks in the context has been forgotten. See https://github.com/symfony/symfony/pull/30950');
41+
$this->expectException(InvalidArgumentException::class);
42+
$normalizer->normalize($obj, null, ['callbacks' => $callbacks]);
43+
}
44+
45+
public function provideCallbacks()
46+
{
47+
return [
48+
'Change a string' => [
49+
[
50+
'bar' => function ($bar) {
51+
$this->assertEquals('baz', $bar);
52+
53+
return 'baz';
54+
},
55+
],
56+
'baz',
57+
['bar' => 'baz'],
58+
],
59+
'Null an item' => [
60+
[
61+
'bar' => function ($value, $object, $attributeName, $format, $context) {
62+
$this->assertSame('baz', $value);
63+
$this->assertInstanceOf(CallbacksObject::class, $object);
64+
$this->assertSame('bar', $attributeName);
65+
$this->assertSame('any', $format);
66+
$this->assertArrayHasKey('circular_reference_limit_counters', $context);
67+
},
68+
],
69+
'baz',
70+
['bar' => null],
71+
],
72+
'Format a date' => [
73+
[
74+
'bar' => function ($bar) {
75+
$this->assertInstanceOf(\DateTime::class, $bar);
76+
return $bar->format('d-m-Y H:i:s');
77+
},
78+
],
79+
new \DateTime('2011-09-10 06:30:00'),
80+
['bar' => '10-09-2011 06:30:00'],
81+
],
82+
'Collect a property' => [
83+
[
84+
'bar' => function (array $bars) {
85+
$result = '';
86+
foreach ($bars as $bar) {
87+
$result .= $bar->bar;
88+
}
89+
90+
return $result;
91+
},
92+
],
93+
[new CallbacksObject('baz'), new CallbacksObject('quux')],
94+
['bar' => 'bazquux'],
95+
],
96+
'Count a property' => [
97+
[
98+
'bar' => function (array $bars) {
99+
return \count($bars);
100+
},
101+
],
102+
[new CallbacksObject(), new CallbacksObject()],
103+
['bar' => 2],
104+
],
105+
];
106+
}
107+
108+
public function provideInvalidCallbacks()
109+
{
110+
return [
111+
[['bar' => null]],
112+
[['bar' => 'thisisnotavalidfunction']],
113+
];
114+
}
115+
}

0 commit comments

Comments
 (0)