Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Deserializes.php Timezone problems #746

Merged
merged 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -123,7 +124,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);
jlevers marked this conversation as resolved.
Show resolved Hide resolved
$this->assertEquals( (new DateTimeZone('+02:00'), $result->payload->feesEstimateResult->feesEstimate->timeOfFeesEstimation->getTimeZone() );
jlevers marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading