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\DeserializationContext;
9use JMS\Serializer\EventDispatcher\EventDispatcher;
10use JMS\Serializer\GraphNavigator;
11use JMS\Serializer\Handler\HandlerRegistry;
12use JMS\Serializer\Metadata\Driver\AnnotationDriver;
13use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
14use JMS\Serializer\XmlDeserializationVisitor;
15use JMS\Serializer\XmlSerializationVisitor;
16use Metadata\MetadataFactory;
17
18class XmlSchemaDateHandlerDeserializationTest extends \PHPUnit_Framework_TestCase
19{
20    /**
21     * @var XmlSchemaDateHandler
22     */
23    protected $handler;
24    /**
25     * @var DeserializationContext
26     */
27    protected $context;
28
29    /**
30     * @var XmlDeserializationVisitor
31     */
32    protected $visitor;
33
34    public function setUp()
35    {
36        $this->handler = new XmlSchemaDateHandler();
37        $this->context = DeserializationContext::create();
38        $naming = new IdenticalPropertyNamingStrategy();
39
40
41
42        $dispatcher = new EventDispatcher();
43        $handlerRegistry= new HandlerRegistry();
44        $cons = new UnserializeObjectConstructor();
45
46        $navigator = class_exists('JMS\Serializer\GraphNavigator\DeserializationGraphNavigator')
47            ? $this->initJmsv2($naming, $handlerRegistry, $cons, $dispatcher)
48            : $this->initJmsv1($naming, $handlerRegistry, $cons, $dispatcher)
49        ;
50        $this->visitor->setNavigator($navigator);
51    }
52
53    private function initJmsv2($naming, $handlerRegistry, $cons, $dispatcher)
54    {
55        $accessor = new DefaultAccessorStrategy();
56        $this->visitor = new XmlDeserializationVisitor();
57        $metadataFactory = new MetadataFactory(new AnnotationDriver(new AnnotationReader(), $naming));
58        return new GraphNavigator\DeserializationGraphNavigator($metadataFactory, $handlerRegistry, $cons, $accessor, $dispatcher);
59    }
60
61    private function initJmsv1($naming, $handlerRegistry, $cons, $dispatcher)
62    {
63        $this->visitor = new XmlDeserializationVisitor($naming);
64        $metadataFactory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
65        return new GraphNavigator($metadataFactory, $handlerRegistry, $cons, $dispatcher);
66    }
67
68    /**
69     * @dataProvider getDeserializeDate
70     * @param string    $date
71     * @param \DateTime $expected
72     */
73    public function testDeserializeDate($date, \DateTime $expected)
74    {
75        $element = new \SimpleXMLElement("<Date>$date</Date>");
76        $deserialized = $this->handler->deserializeDate($this->visitor, $element, [], $this->context);
77        $this->assertEquals($expected, $deserialized);
78    }
79
80    public function getDeserializeDate()
81    {
82        return [
83            ['2015-01-01', new \DateTime('2015-01-01')],
84            ['2015-01-01Z', new \DateTime('2015-01-01', new \DateTimeZone("UTC"))],
85            ['2015-01-01+06:00', new \DateTime('2015-01-01', new \DateTimeZone("+06:00"))],
86            ['2015-01-01-20:00', new \DateTime('2015-01-01', new \DateTimeZone("-20:00"))],
87        ];
88    }
89
90    /**
91     * @expectedException \RuntimeException
92     */
93    public function testDeserializeInvalidDate()
94    {
95        $element = new \SimpleXMLElement("<Date>2015-01-01T</Date>");
96        $this->handler->deserializeDate($this->visitor, $element, [], $this->context);
97    }
98}
99