Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1388449

Browse filesBrowse files
committed
extract tests for callbacks
1 parent 2ae86bb commit 1388449
Copy full SHA for 1388449

File tree

5 files changed

+166
-265
lines changed
Filter options

5 files changed

+166
-265
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ 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+
*
61+
*/
62+
public const CALLBACKS = 'callbacks';
5963
const CIRCULAR_REFERENCE_HANDLER = 'circular_reference_handler';
6064

6165
/**
+13Lines 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+
}
+115Lines 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 is broken');
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.