Skip to content

Commit

Permalink
Fix bug schmittjoh#343 return integer when the column is datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
Bukashk0zzz committed Mar 10, 2016
1 parent fe13a1f commit 5461933
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/JMS/Serializer/Handler/DateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public function serializeDateTime(VisitorInterface $visitor, \DateTime $date, ar
if ($visitor instanceof XmlSerializationVisitor && false === $this->xmlCData) {
return $visitor->visitSimpleString($date->format($this->getFormat($type)), $type, $context);
}

$format = $this->getFormat($type);
if ('U' === $format) {
return $visitor->visitInteger($date->format($format), $type, $context);
}
return $visitor->visitString($date->format($this->getFormat($type)), $type, $context);
}

Expand Down
34 changes: 34 additions & 0 deletions tests/JMS/Serializer/Tests/Fixtures/Timestamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* Copyright 2013 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\Serializer\Tests\Fixtures;

use JMS\Serializer\Annotation\Type;

class Timestamp
{
/**
* @Type("DateTime<'U'>")
*/
private $timestamp;

public function __construct($timestamp)
{
$this->timestamp = $timestamp;
}
}
17 changes: 17 additions & 0 deletions tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use JMS\Serializer\Tests\Fixtures\Garage;
use JMS\Serializer\Tests\Fixtures\InlineChildEmpty;
use JMS\Serializer\Tests\Fixtures\NamedDateTimeArraysObject;
use JMS\Serializer\Tests\Fixtures\Timestamp;
use JMS\Serializer\Tests\Fixtures\Tree;
use JMS\Serializer\Tests\Fixtures\VehicleInterfaceGarage;
use PhpCollection\Sequence;
Expand Down Expand Up @@ -365,6 +366,22 @@ public function getDateTime()
);
}

/**
* @dataProvider getTimestamp
* @group datetime
*/
public function testTimestamp($key, $value)
{
$this->assertEquals($this->getContent($key), $this->serialize($value));
}

public function getTimestamp()
{
return array(
array('timestamp', new Timestamp(new \DateTime('2016-02-11 00:00:00', new \DateTimeZone('UTC')))),
);
}

public function testDateInterval()
{
$duration = new \DateInterval('PT45M');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ protected function getContent($key)
$outputs['object_when_null'] = '{"text":"foo"}';
$outputs['object_when_null_and_serialized'] = '{"author":null,"text":"foo"}';
$outputs['date_time'] = '"2011-08-30T00:00:00+0000"';
$outputs['timestamp'] = '{"timestamp":1455148800}';
$outputs['date_interval'] = '"PT45M"';
$outputs['car'] = '{"km":5,"type":"car"}';
$outputs['car_without_type'] = '{"km":5}';
Expand Down
4 changes: 4 additions & 0 deletions tests/JMS/Serializer/Tests/Serializer/xml/timestamp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<timestamp>1455148800</timestamp>
</result>
1 change: 1 addition & 0 deletions tests/JMS/Serializer/Tests/Serializer/yml/timestamp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
timestamp: 1455148800

0 comments on commit 5461933

Please sign in to comment.