1<?php 2namespace GoetasWebservices\Xsd\XsdToPhpRuntime\Tests\Jms\Handler; 3 4use Doctrine\Common\Annotations\AnnotationReader; 5use GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler\XmlSchemaDateHandler; 6use JMS\Serializer\Accessor\DefaultAccessorStrategy; 7use JMS\Serializer\Construction\UnserializeObjectConstructor; 8use JMS\Serializer\EventDispatcher\EventDispatcher; 9use JMS\Serializer\GraphNavigator; 10use JMS\Serializer\Handler\HandlerRegistry; 11use JMS\Serializer\Metadata\Driver\AnnotationDriver; 12use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; 13use JMS\Serializer\SerializationContext; 14use JMS\Serializer\XmlSerializationVisitor; 15use Metadata\MetadataFactory; 16 17class XmlSchemaDateHandlerSerializationTest extends \PHPUnit_Framework_TestCase 18{ 19 /** 20 * @var XmlSchemaDateHandler 21 */ 22 protected $handler; 23 /** 24 * @var SerializationContext 25 */ 26 protected $context; 27 28 /** 29 * @var XmlSerializationVisitor 30 */ 31 protected $visitor; 32 33 public function setUp() 34 { 35 $this->handler = new XmlSchemaDateHandler(); 36 $this->context = SerializationContext::create(); 37 38 $naming = new IdenticalPropertyNamingStrategy(); 39 $cons = new UnserializeObjectConstructor(); 40 41 $dispatcher = new EventDispatcher(); 42 $handlerRegistry= new HandlerRegistry(); 43 44 $navigator = class_exists('JMS\Serializer\GraphNavigator\DeserializationGraphNavigator') 45 ? $this->initJmsv2($naming, $handlerRegistry, $cons, $dispatcher) 46 : $this->initJmsv1($naming, $handlerRegistry, $cons, $dispatcher) 47 ; 48 $this->visitor->setNavigator($navigator); 49 } 50 51 private function initJmsv2($naming, $handlerRegistry, $cons, $dispatcher) 52 { 53 $accessor = new DefaultAccessorStrategy(); 54 $this->visitor = new XmlSerializationVisitor(); 55 $metadataFactory = new MetadataFactory(new AnnotationDriver(new AnnotationReader(), $naming)); 56 return new GraphNavigator\SerializationGraphNavigator($metadataFactory, $handlerRegistry, $accessor, $dispatcher); 57 } 58 59 private function initJmsv1($naming, $handlerRegistry, $cons, $dispatcher) 60 { 61 $this->visitor = new XmlSerializationVisitor($naming); 62 $metadataFactory = new MetadataFactory(new AnnotationDriver(new AnnotationReader())); 63 return new GraphNavigator($metadataFactory, $handlerRegistry, $cons, $dispatcher); 64 } 65 66 /** 67 * @dataProvider getSerializeDateTime 68 * @param \DateTime $date 69 */ 70 public function testSerializeDateTime(\DateTime $date, $expected) 71 { 72 $ret = $this->handler->serializeDateTime($this->visitor, $date, [], $this->context); 73 $actual = $ret ? $ret->nodeValue : $this->visitor->getCurrentNode()->nodeValue; 74 $this->assertEquals($expected, $actual); 75 } 76 77 public function getSerializeDateTime() 78 { 79 return [ 80 [new \DateTime('2015-01-01 12:00+00:00'), '2015-01-01T12:00:00+00:00'], 81 [new \DateTime('2015-01-01 12:00:56+00:00'), '2015-01-01T12:00:56+00:00'], 82 [new \DateTime('2015-01-01 12:00:56+00:00'), '2015-01-01T12:00:56+00:00'], 83 [new \DateTime('2015-01-01 12:00:56+20:00'), '2015-01-01T12:00:56+20:00'], 84 [new \DateTime('2015-01-01 12:00:56', new \DateTimeZone("Europe/London")), '2015-01-01T12:00:56+00:00'], 85 [new \DateTime('2015-01-01 12:00:56+00:00', new \DateTimeZone("Europe/London")), '2015-01-01T12:00:56+00:00'], 86 [new \DateTime('2015-01-01 12:00:56', new \DateTimeZone("Europe/Rome")), '2015-01-01T12:00:56+01:00'], 87 ]; 88 } 89 90 /** 91 * @dataProvider getSerializeDate 92 * @param \DateTime $date 93 * @param string $expected 94 */ 95 public function testSerializeDate(\DateTime $date, $expected) 96 { 97 $ret = $this->handler->serializeDate($this->visitor, $date, [], $this->context); 98 99 $actual = $ret ? $ret->nodeValue : $this->visitor->getCurrentNode()->nodeValue; 100 $this->assertEquals($expected, $actual); 101 } 102 103 public function getSerializeDate() 104 { 105 return [ 106 [new \DateTime('2015-01-01 12:00'), '2015-01-01'], 107 [new \DateTime('2015-01-01 12:00:56'), '2015-01-01'], 108 [new \DateTime('2015-01-01 12:00:56+00:00'), '2015-01-01'], 109 [new \DateTime('2015-01-01 12:00:56+20:00'), '2015-01-01'], 110 [new \DateTime('2015-01-01 12:00:56', new \DateTimeZone("Europe/London")), '2015-01-01'], 111 ]; 112 } 113} 114