Skip to content

Commit

Permalink
Merge pull request #746 from simonsolutions/patch-2
Browse files Browse the repository at this point in the history
Update Deserializes.php Timezone problems
  • Loading branch information
jlevers authored Jul 9, 2024
2 parents 40d5bf2 + 5986baa commit 286be85
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Traits/Deserializes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace SellingPartnerApi\Traits;

use DateTime;
use DateTimeZone;
use DateTimeInterface;
use ReflectionClass;
use SellingPartnerApi\Exceptions\InvalidAttributeTypeException;
Expand Down Expand Up @@ -121,7 +122,11 @@ protected static function convertValueToDateTime(string $value): DateTime
{
foreach (static::$validDatetimeFormats as $validDatetimeFormat) {
try {
$returnValue = DateTime::createFromFormat($validDatetimeFormat, $value);
$returnValue = DateTime::createFromFormat(
$validDatetimeFormat,
$value,
new DateTimeZone('UTC')
);
// Only return a valid object, else try again until failure
if ($returnValue instanceof DateTimeInterface) {
return $returnValue;
Expand Down
24 changes: 24 additions & 0 deletions tests/SerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

declare(strict_types=1);

use DateTime;
use DateTimeZone;
use DateTimeInterface;
use PHPUnit\Framework\TestCase;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
Expand All @@ -14,6 +17,7 @@
use SellingPartnerApi\Seller\ProductPricingV0\Dto\GetItemOffersBatchRequest;
use SellingPartnerApi\Seller\ProductPricingV0\Dto\ItemOffersRequest;
use SellingPartnerApi\Seller\ProductPricingV0\Requests\GetItemOffersBatch;
use SellingPartnerApi\Seller\ProductFeesV0\Responses\GetMyFeesEstimateResponse;
use SellingPartnerApi\Seller\SellerConnector;
use SellingPartnerApi\SellingPartnerApi;

Expand Down Expand Up @@ -164,4 +168,24 @@ public function testDatesInBodyParametersAreSerializedInZuluFormat(): void

$this->assertEquals('2024-01-01T00:00:00Z', $body['packageDetail']['shipDate']);
}

public function testDeserializeDateTimeWithTimezone(): void
{
$now = new DateTime();
$now->setTimeZone(new DateTimeZone('+02:00'));

$result = GetMyFeesEstimateResponse::deserialize([
'payload' => [
'FeesEstimateResult' => [
'status' => 'Success',
'FeesEstimate' => [
'TimeOfFeesEstimation' => $now->format(DateTimeInterface::ATOM),
],
],
],
]);
$this->assertNotNull($result);
$this->assertInstanceOf(DateTimeInterface::class, $result->payload->feesEstimateResult->feesEstimate->timeOfFeesEstimation);
$this->assertEquals( (new DateTimeZone('+02:00')), $result->payload->feesEstimateResult->feesEstimate->timeOfFeesEstimation->getTimeZone() );
}
}

0 comments on commit 286be85

Please sign in to comment.