Skip to content

Commit

Permalink
Merge pull request #8 from graze/hotfix/time-format
Browse files Browse the repository at this point in the history
custom gigya DateTimeFormat
  • Loading branch information
Harry Bragg committed Oct 5, 2015
2 parents b3ffff4 + 5d8b309 commit 4d1b7bd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Graze\Gigya\Response;

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use GuzzleHttp\Message\ResponseInterface as GuzzleResponseInterface;
Expand All @@ -12,6 +11,8 @@

class Response implements ResponseInterface
{
const DATE_TIME_FORMAT = 'Y-m-d\TH:i:s.uP';

/**
* @var array
*/
Expand Down Expand Up @@ -70,7 +71,7 @@ public function __construct(GuzzleResponseInterface $response)
$this->statusCode = (int)$this->popField('statusCode');
$this->statusReason = $this->popField('statusReason');
$this->callId = $this->popField('callId');
$this->time = DateTimeImmutable::createFromFormat(DateTime::ATOM, $this->popField('time'));
$this->time = DateTimeImmutable::createFromFormat(static::DATE_TIME_FORMAT, $this->popField('time'));
}

/**
Expand Down
15 changes: 12 additions & 3 deletions tests/unit/Response/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Graze\Gigya\Test\Unit\Response;

use DateTime;
use DateTimeImmutable;
use DateTimeZone;
use Graze\Gigya\Exceptions\UnknownResponseException;
use Graze\Gigya\Response\Response;
use Graze\Gigya\Response\ResponseCollectionInterface;
use Graze\Gigya\Response\ResponseFactory;
use Graze\Gigya\Test\TestCase;
Expand All @@ -18,6 +17,7 @@

class ResponseFactoryTest extends TestCase
{

/**
* @var ResponseFactory
*/
Expand All @@ -28,6 +28,11 @@ class ResponseFactoryTest extends TestCase
*/
private $validator;

public static function setUpBeforeClass()
{
date_default_timezone_set('UTC');
}

public function setUp()
{
$this->validator = m::mock('Graze\Gigya\Validation\GigyaResponseValidatorInterface');
Expand Down Expand Up @@ -57,7 +62,11 @@ public function testAccountModel()
static::assertEquals(0, $gigyaResponse->getErrorCode());
static::assertEquals("OK", $gigyaResponse->getStatusReason());
static::assertEquals("e6f891ac17f24810bee6eb533524a152", $gigyaResponse->getCallId());
static::assertEquals(DateTimeImmutable::createFromFormat(DateTime::ATOM, "2015-03-22T11:42:25.943Z"), $gigyaResponse->getTime());
static::assertInstanceOf('DateTimeInterface', $gigyaResponse->getTime());
static::assertEquals(
DateTimeImmutable::createFromFormat(Response::DATE_TIME_FORMAT, "2015-03-22T11:42:25.943Z"),
$gigyaResponse->getTime()
);
$data = $gigyaResponse->getData();
static::assertEquals("_gid_30A3XVJciH95WEEnoRmfZS7ee3MY+lUAtpVxvUWNseU=", $data->get('UID'));
static::assertSame($response, $gigyaResponse->getOriginalResponse());
Expand Down
45 changes: 45 additions & 0 deletions tests/unit/Response/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Graze\Gigya\Test\Unit\Response;

use DateTime;
use Graze\Gigya\Response\Response;
use Graze\Gigya\Test\TestCase;

class ResponseTest extends TestCase
{
public static function setUpBeforeClass()
{
date_default_timezone_set('UTC');
}

public function testDateFormat()
{
$time = DateTime::createFromFormat(Response::DATE_TIME_FORMAT, '2015-03-22T11:42:25.943Z');

static::assertInstanceOf('DateTimeInterface', $time);
static::assertEquals("2015", $time->format('Y'));
static::assertEquals("03", $time->format('m'));
static::assertEquals("22", $time->format('d'));
static::assertEquals("11", $time->format('H'));
static::assertEquals("42", $time->format('i'));
static::assertEquals("25", $time->format('s'));
static::assertEquals("943000", $time->format('u'));
static::assertEquals("Z", $time->getTimezone()->getName());
}

public function testOtherTimeZoneFormat()
{
$time = DateTime::createFromFormat(Response::DATE_TIME_FORMAT, '2015-03-22T11:42:25.943+02:00');

static::assertInstanceOf('DateTimeInterface', $time);
static::assertEquals("2015", $time->format('Y'));
static::assertEquals("03", $time->format('m'));
static::assertEquals("22", $time->format('d'));
static::assertEquals("11", $time->format('H'));
static::assertEquals("42", $time->format('i'));
static::assertEquals("25", $time->format('s'));
static::assertEquals("943000", $time->format('u'));
static::assertEquals("+02:00", $time->getTimezone()->getName());
}
}

0 comments on commit 4d1b7bd

Please sign in to comment.