From 19a4e2c8a627f11f577ab37c5a34fdeebb4a6ad2 Mon Sep 17 00:00:00 2001 From: Harry Bragg Date: Mon, 5 Oct 2015 09:38:08 +0100 Subject: [PATCH 1/2] custom gigya DateTimeFormat --- src/Response/Response.php | 10 ++++- tests/unit/Response/ResponseFactoryTest.php | 11 ++++- tests/unit/Response/ResponseTest.php | 49 +++++++++++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 tests/unit/Response/ResponseTest.php diff --git a/src/Response/Response.php b/src/Response/Response.php index 2971cd7..6f1d2b0 100644 --- a/src/Response/Response.php +++ b/src/Response/Response.php @@ -2,9 +2,9 @@ namespace Graze\Gigya\Response; -use DateTime; use DateTimeImmutable; use DateTimeInterface; +use DateTimeZone; use GuzzleHttp\Message\ResponseInterface as GuzzleResponseInterface; use Illuminate\Support\Collection; @@ -12,6 +12,8 @@ class Response implements ResponseInterface { + const DATE_TIME_FORMAT = 'Y-m-d\TH:i:s.uP'; + /** * @var array */ @@ -70,7 +72,11 @@ 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'), + new DateTimeZone('UTC') + ); } /** diff --git a/tests/unit/Response/ResponseFactoryTest.php b/tests/unit/Response/ResponseFactoryTest.php index 7efa00b..b878844 100644 --- a/tests/unit/Response/ResponseFactoryTest.php +++ b/tests/unit/Response/ResponseFactoryTest.php @@ -6,6 +6,7 @@ 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; @@ -57,7 +58,15 @@ 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", + new DateTimeZone('UTC') + ), + $gigyaResponse->getTime() + ); $data = $gigyaResponse->getData(); static::assertEquals("_gid_30A3XVJciH95WEEnoRmfZS7ee3MY+lUAtpVxvUWNseU=", $data->get('UID')); static::assertSame($response, $gigyaResponse->getOriginalResponse()); diff --git a/tests/unit/Response/ResponseTest.php b/tests/unit/Response/ResponseTest.php new file mode 100644 index 0000000..dc59f3c --- /dev/null +++ b/tests/unit/Response/ResponseTest.php @@ -0,0 +1,49 @@ +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', + new DateTimeZone('UTC') + ); + + 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()); + } +} From 5d8b3090f7f29b0b8e4f452f5cd130bf6d7c02e0 Mon Sep 17 00:00:00 2001 From: Harry Bragg Date: Mon, 5 Oct 2015 09:47:02 +0100 Subject: [PATCH 2/2] get timezone from string, but set locally to avoid NOTICEs --- src/Response/Response.php | 7 +------ tests/unit/Response/ResponseFactoryTest.php | 14 +++++++------- tests/unit/Response/ResponseTest.php | 18 +++++++----------- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/Response/Response.php b/src/Response/Response.php index 6f1d2b0..4bed777 100644 --- a/src/Response/Response.php +++ b/src/Response/Response.php @@ -4,7 +4,6 @@ use DateTimeImmutable; use DateTimeInterface; -use DateTimeZone; use GuzzleHttp\Message\ResponseInterface as GuzzleResponseInterface; use Illuminate\Support\Collection; @@ -72,11 +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( - static::DATE_TIME_FORMAT, - $this->popField('time'), - new DateTimeZone('UTC') - ); + $this->time = DateTimeImmutable::createFromFormat(static::DATE_TIME_FORMAT, $this->popField('time')); } /** diff --git a/tests/unit/Response/ResponseFactoryTest.php b/tests/unit/Response/ResponseFactoryTest.php index b878844..75f2384 100644 --- a/tests/unit/Response/ResponseFactoryTest.php +++ b/tests/unit/Response/ResponseFactoryTest.php @@ -2,9 +2,7 @@ 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; @@ -19,6 +17,7 @@ class ResponseFactoryTest extends TestCase { + /** * @var ResponseFactory */ @@ -29,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'); @@ -60,11 +64,7 @@ public function testAccountModel() static::assertEquals("e6f891ac17f24810bee6eb533524a152", $gigyaResponse->getCallId()); static::assertInstanceOf('DateTimeInterface', $gigyaResponse->getTime()); static::assertEquals( - DateTimeImmutable::createFromFormat( - Response::DATE_TIME_FORMAT, - "2015-03-22T11:42:25.943Z", - new DateTimeZone('UTC') - ), + DateTimeImmutable::createFromFormat(Response::DATE_TIME_FORMAT, "2015-03-22T11:42:25.943Z"), $gigyaResponse->getTime() ); $data = $gigyaResponse->getData(); diff --git a/tests/unit/Response/ResponseTest.php b/tests/unit/Response/ResponseTest.php index dc59f3c..9b8656c 100644 --- a/tests/unit/Response/ResponseTest.php +++ b/tests/unit/Response/ResponseTest.php @@ -3,19 +3,19 @@ namespace Graze\Gigya\Test\Unit\Response; use DateTime; -use DateTimeZone; 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', - new DateTimeZone('UTC') - ); + $time = DateTime::createFromFormat(Response::DATE_TIME_FORMAT, '2015-03-22T11:42:25.943Z'); static::assertInstanceOf('DateTimeInterface', $time); static::assertEquals("2015", $time->format('Y')); @@ -30,11 +30,7 @@ public function testDateFormat() public function testOtherTimeZoneFormat() { - $time = DateTime::createFromFormat( - Response::DATE_TIME_FORMAT, - '2015-03-22T11:42:25.943+02:00', - new DateTimeZone('UTC') - ); + $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'));