-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from xavierleune/feature/more-serializer
Feature: Serializer for dateTimeZone and Uuid
- Loading branch information
Showing
7 changed files
with
234 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace CCMBenchmark\Ting\Serializer; | ||
|
||
|
||
class DateTimeZone implements SerializerInterface | ||
{ | ||
public function serialize($toSerialize, array $options = []) | ||
{ | ||
if ($toSerialize === null) { | ||
return null; | ||
} | ||
|
||
if (!($toSerialize instanceof \DateTimeZone)) { | ||
throw new RuntimeException('datetimezone has to be an instance of \DateTimeZone'); | ||
} | ||
|
||
return $toSerialize->getName(); | ||
} | ||
|
||
public function unserialize($serialized, array $options = []) | ||
{ | ||
if ($serialized === null) { | ||
return null; | ||
} | ||
try { | ||
return new \DateTimeZone($serialized); | ||
} catch (\Exception $e) { | ||
throw new RuntimeException('Cannot convert ' . $serialized . ' to DateTimeZone.'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace CCMBenchmark\Ting\Serializer; | ||
|
||
|
||
class Uuid implements SerializerInterface | ||
{ | ||
public function serialize($toSerialize, array $options = []) | ||
{ | ||
if ($toSerialize === null) { | ||
return null; | ||
} | ||
if (!($toSerialize instanceof \Symfony\Component\Uid\Uuid)) { | ||
throw new RuntimeException('UUID has to be an instance of \Symfony\Component\Uid\Uuid . ' . gettype($toSerialize) . ' given.'); | ||
} | ||
|
||
return $toSerialize->toRfc4122(); | ||
} | ||
|
||
public function unserialize($serialized, array $options = []) | ||
{ | ||
if ($serialized === null) { | ||
return null; | ||
} | ||
try { | ||
return \Symfony\Component\Uid\Uuid::fromString($serialized); | ||
} catch (\InvalidArgumentException $e) { | ||
throw new RuntimeException('Cannot convert ' . $serialized . ' to UUID.'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
/*********************************************************************** | ||
* | ||
* Ting - PHP Datamapper | ||
* ========================================== | ||
* | ||
* Copyright (C) 2014 CCM Benchmark Group. (http://www.ccmbenchmark.com) | ||
* | ||
*********************************************************************** | ||
* | ||
* 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 tests\units\CCMBenchmark\Ting\Serializer; | ||
|
||
use atoum; | ||
|
||
class DateTimeZone extends atoum | ||
{ | ||
public function testSerializeThenUnSerializeShouldReturnOriginalValue(): void | ||
{ | ||
$datetime = new \DateTimeZone('Europe/Paris'); | ||
$this | ||
->if($serializer = new \CCMBenchmark\Ting\Serializer\DateTimeZone()) | ||
->object($serializer->unserialize($serializer->serialize($datetime))) | ||
->isInstanceOf(\DateTimeZone::class) | ||
->string($serializer->unserialize($serializer->serialize($datetime))->getName()) | ||
->isEqualTo('Europe/Paris') | ||
; | ||
} | ||
|
||
public function testUnserializeInvalidValueShouldRaiseException(): void | ||
{ | ||
$this | ||
->if($serializer = new \CCMBenchmark\Ting\Serializer\DateTimeZone()) | ||
->exception(function () use ($serializer) { | ||
$serializer->unserialize('Not a timezone'); | ||
}) | ||
->isInstanceOf('CCMBenchmark\Ting\Serializer\RuntimeException') | ||
; | ||
} | ||
|
||
public function testSerializeInvalidValueShouldRaiseException(): void | ||
{ | ||
$this | ||
->if($serializer = new \CCMBenchmark\Ting\Serializer\DateTimeZone()) | ||
->exception(function () use ($serializer) { | ||
$serializer->serialize(new \StdClass()); | ||
}) | ||
->isInstanceOf('CCMBenchmark\Ting\Serializer\RuntimeException') | ||
; | ||
} | ||
|
||
public function testNullValueShouldBeReturned(): void | ||
{ | ||
$this | ||
->if($serializer = new \CCMBenchmark\Ting\Serializer\DateTimeZone()) | ||
->variable($serializer->serialize(null)) | ||
->isNull() | ||
->variable($serializer->unserialize(null)) | ||
->isNull() | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
/*********************************************************************** | ||
* | ||
* Ting - PHP Datamapper | ||
* ========================================== | ||
* | ||
* Copyright (C) 2014 CCM Benchmark Group. (http://www.ccmbenchmark.com) | ||
* | ||
*********************************************************************** | ||
* | ||
* 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 tests\units\CCMBenchmark\Ting\Serializer; | ||
|
||
use atoum; | ||
use Symfony\Component\Uid\UuidV4; | ||
|
||
class Uuid extends atoum | ||
{ | ||
public function testSerializeThenUnSerializeShouldReturnOriginalValue(): void | ||
{ | ||
$uuid = new UuidV4(); | ||
$this | ||
->if($serializer = new \CCMBenchmark\Ting\Serializer\Uuid()) | ||
->dump($serializer->serialize($uuid)) | ||
->object($serializer->unserialize($serializer->serialize($uuid))) | ||
->isInstanceOf(Uuidv4::class) | ||
->string($serializer->unserialize($serializer->serialize($uuid))->toRfc4122()) | ||
->isEqualTo($uuid->toRfc4122()) | ||
; | ||
} | ||
|
||
public function testUnserializeInvalidValueShouldRaiseException(): void | ||
{ | ||
$this | ||
->if($serializer = new \CCMBenchmark\Ting\Serializer\Uuid()) | ||
->exception(function () use ($serializer) { | ||
$serializer->unserialize('Invalid uuid'); | ||
}) | ||
->isInstanceOf('CCMBenchmark\Ting\Serializer\RuntimeException') | ||
; | ||
} | ||
|
||
public function testSerializeInvalidValueShouldRaiseException(): void | ||
{ | ||
$this | ||
->if($serializer = new \CCMBenchmark\Ting\Serializer\Uuid()) | ||
->exception(function () use ($serializer) { | ||
$serializer->serialize(new \StdClass()); | ||
}) | ||
->isInstanceOf('CCMBenchmark\Ting\Serializer\RuntimeException') | ||
; | ||
} | ||
|
||
public function testNullValueShouldBeReturned(): void | ||
{ | ||
$this | ||
->if($serializer = new \CCMBenchmark\Ting\Serializer\Uuid()) | ||
->variable($serializer->serialize(null)) | ||
->isNull() | ||
->variable($serializer->unserialize(null)) | ||
->isNull() | ||
; | ||
} | ||
} |