Skip to content

Commit dcaf30a

Browse files
committed
Add test case for handler callback metadata
1 parent 3c02462 commit dcaf30a

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2013 Johannes M. Schmitt <[email protected]>
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace JMS\Serializer\Tests\Fixtures;
20+
21+
use JMS\Serializer\Annotation\HandlerCallback;
22+
use JMS\Serializer\Annotation\Type;
23+
24+
class ObjectWithHandlerCallbacks
25+
{
26+
/**
27+
* @Type("string")
28+
*/
29+
public $name;
30+
31+
/**
32+
* @HandlerCallback(direction="serialization", format="json")
33+
*/
34+
public function toJson()
35+
{
36+
return $this->name;
37+
}
38+
39+
/**
40+
* @HandlerCallback(direction="serialization", format="xml")
41+
*/
42+
public function toXml()
43+
{
44+
return $this->name;
45+
}
46+
}

tests/JMS/Serializer/Tests/Metadata/Driver/BaseDriverTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace JMS\Serializer\Tests\Metadata\Driver;
2020

21+
use JMS\Serializer\GraphNavigator;
2122
use JMS\Serializer\Metadata\ClassMetadata;
2223
use JMS\Serializer\Metadata\PropertyMetadata;
2324
use JMS\Serializer\Metadata\VirtualPropertyMetadata;
@@ -321,6 +322,13 @@ public function testXmlNamespaceInheritanceMetadata()
321322
$this->assertEquals($p, $m->propertyMetadata['qux']);
322323
}
323324

325+
public function testHandlerCallbacks()
326+
{
327+
$m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ObjectWithHandlerCallbacks'));
328+
329+
$this->assertEquals('toJson', $m->handlerCallbacks[GraphNavigator::DIRECTION_SERIALIZATION]['json']);
330+
$this->assertEquals('toXml', $m->handlerCallbacks[GraphNavigator::DIRECTION_SERIALIZATION]['xml']);
331+
}
324332

325333
/**
326334
* @return DriverInterface
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use JMS\Serializer\GraphNavigator;
4+
use JMS\Serializer\Metadata\ClassMetadata;
5+
use JMS\Serializer\Metadata\PropertyMetadata;
6+
7+
$metadata = new ClassMetadata('JMS\Serializer\Tests\Fixtures\ObjectWithHandlerCallbacks');
8+
9+
$pMetadata = new PropertyMetadata('JMS\Serializer\Tests\Fixtures\ObjectWithHandlerCallbacks', 'name');
10+
$pMetadata->type = 'string';
11+
$metadata->addPropertyMetadata($pMetadata);
12+
13+
$metadata->addHandlerCallback(GraphNavigator::DIRECTION_SERIALIZATION, 'json', 'toJson');
14+
$metadata->addHandlerCallback(GraphNavigator::DIRECTION_SERIALIZATION, 'xml', 'toXml');
15+
16+
return $metadata;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<serializer>
3+
<class name="JMS\Serializer\Tests\Fixtures\ObjectWithHandlerCallbacks">
4+
<property name="name" type="string"/>
5+
<callback-method name="toJson" type="handler" direction="serialization" format="json" />
6+
<callback-method name="toXml" type="handler" direction="serialization" format="xml" />
7+
</class>
8+
</serializer>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
JMS\Serializer\Tests\Fixtures\ObjectWithHandlerCallbacks:
2+
properties:
3+
name:
4+
type: string
5+
handler_callbacks:
6+
serialization:
7+
xml: toXml
8+
json: toJson

0 commit comments

Comments
 (0)