Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
Merge efa898e into c2e0eff
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Feb 28, 2019
2 parents c2e0eff + efa898e commit cc9503c
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 53 deletions.
5 changes: 5 additions & 0 deletions src/Requests/PvzListRequest.php
Expand Up @@ -109,6 +109,11 @@ public function setPostCode(string $code)
return $this;
}

public function setCityPostCode(string $code)
{
return $this->setPostCode($code);
}

public function setCashless(bool $cashless)
{
$this->cashless = $cashless;
Expand Down
44 changes: 19 additions & 25 deletions src/Serialization/NullableDateTimeHandler.php
Expand Up @@ -37,6 +37,8 @@

final class NullableDateTimeHandler extends DateHandler
{
const PLAIN_DATE_FORMAT = 'Y-m-d';

public static function getSubscribingMethods()
{
$methods = parent::getSubscribingMethods();
Expand All @@ -60,49 +62,41 @@ public function deserializeDateTimeImmutableFromXml(XmlDeserializationVisitor $v
try {
return parent::deserializeDateTimeImmutableFromXml($visitor, $data, $type);
} catch (RuntimeException $e) {
if (!isset($type['params'][3])) {
// Fallback date format; specify like so:
// @JMS\Type("DateTimeImmutable<'Y-m-d\TH:i:sP', '', 'Y-m-d\TH:i:sP', 'Y-m-d'>")
throw $e;
}

return $this->parseDateTimeFallback($data, $type);
return $this->parseDateTimeFallback($visitor, $data, $type, $e);
}
}

/**
* @param mixed $data
* @param array $type
* Fallback date format handler; for usage see below.
*
* @throws RuntimeException
* @JMS\Type("DateTimeImmutable<'Y-m-d\TH:i:sP', '', 'Y-m-d\TH:i:sP', 'Y-m-d'>")
*
* @return \DateTimeImmutable
* @param XmlDeserializationVisitor $visitor
* @param mixed $data
*
* @return \DateTimeInterface|null
*
* @psalm-suppress MixedAssignment
* @psalm-suppress PossiblyUndefinedArrayOffset
* @psalm-suppress MixedInferredReturnType
* @psalm-suppress MixedArrayAccess
* @psalm-suppress MixedArrayAssignment
* @psalm-suppress MixedArgument
* @psalm-suppress MixedReturnStatement
*/
private function parseDateTimeFallback($data, array $type)
private function parseDateTimeFallback(XmlDeserializationVisitor $visitor, $data, array $type, \Throwable $e)
{
$timezone = !empty($type['params'][1]) ? $type['params'][1] : 'UTC';
$format = $type['params'][3];
if (!array_key_exists(3, $type['params'])) {
throw $e;
}

/** @var \DateTimeImmutable|false $datetime */
$datetime = \DateTimeImmutable::createFromFormat($format, (string) $data, new \DateTimeZone($timezone));
$format = $type['params'][2] = $type['params'][3];

if (!$datetime instanceof \DateTimeImmutable) {
throw new RuntimeException(sprintf('Invalid datetime "%s", expected format %s.', $data, $format));
}
$datetime = parent::deserializeDateTimeImmutableFromXml($visitor, $data, $type);

if ($format === 'Y-m-d') {
if ($datetime instanceof \DateTimeImmutable && self::PLAIN_DATE_FORMAT === $format) {
$datetime = $datetime->setTime(0, 0, 0);
}

if ($format === 'U') {
$datetime = $datetime->setTimezone($timezone);
}

return $datetime;
}

Expand Down
36 changes: 36 additions & 0 deletions tests/Deserialization/NullableDateTimeHandlerTest.php
Expand Up @@ -28,7 +28,9 @@

namespace Tests\CdekSDK\Deserialization;

use CdekSDK\Common\Order;
use CdekSDK\Responses\StatusReportResponse;
use JMS\Serializer\Exception\RuntimeException;
use Tests\CdekSDK\Fixtures\FixtureLoader;

/**
Expand All @@ -44,4 +46,38 @@ public function test_unserialize_normal_date()
$this->assertInstanceOf(StatusReportResponse::class, $result);
$this->assertNull($result->getOrders()[0]->getDate());
}

public function test_it_reads_response_with_delivery_date_without_time()
{
$response = $this->getSerializer()->deserialize(FixtureLoader::load('StatusReportResponseDateOnly.xml'), StatusReportResponse::class, 'xml');

/** @var $response StatusReportResponse */
$this->assertInstanceOf(StatusReportResponse::class, $response);

$this->assertCount(2, $response->getOrders());

$order = $response->getOrders()[0];
$this->assertInstanceOf(Order::class, $order);

$this->assertSame('2018-04-06 13:33:27', $order->getDeliveryDate()->format('Y-m-d H:i:s'));

$order = $response->getOrders()[1];
$this->assertInstanceOf(Order::class, $order);

$this->assertSame('2011-04-07 00:00:00', $order->getDeliveryDate()->format('Y-m-d H:i:s'));
}

public function test_fails_on_invalid_date()
{
$this->expectException(RuntimeException::class);

$this->getSerializer()->deserialize('<Order DeliveryDate="00:00:00" />', Order::class, 'xml');
}

public function test_fails_on_unexpected_date_format()
{
$this->expectException(RuntimeException::class);

$this->getSerializer()->deserialize('<Order Date="2000-01-01 00:00:00" />', Order::class, 'xml');
}
}
8 changes: 0 additions & 8 deletions tests/Deserialization/OrderTest.php
Expand Up @@ -33,7 +33,6 @@
use CdekSDK\Common\State;
use CdekSDK\Common\Status;
use CdekSDK\Responses\StatusReportResponse;
use JMS\Serializer\Exception\RuntimeException;
use Tests\CdekSDK\Fixtures\FixtureLoader;

/**
Expand Down Expand Up @@ -168,11 +167,4 @@ public function test_reads_pvz_code()
/** @var $order Order */
$this->assertSame('ABC123', $order->getPvzCode());
}

public function test_fails_on_invalid_date()
{
$this->expectException(RuntimeException::class);

$this->getSerializer()->deserialize('<Order DeliveryDate="00:00:00" />', Order::class, 'xml');
}
}
20 changes: 0 additions & 20 deletions tests/Deserialization/StatusReportResponseTest.php
Expand Up @@ -210,26 +210,6 @@ public function test_it_reads_response_with_return_order()
$this->assertSame(12, $order->getReturnOrder()->getStatus()->getCode());
}

public function test_it_reads_response_with_delivery_date_without_time()
{
$response = $this->getSerializer()->deserialize(FixtureLoader::load('StatusReportResponseDateOnly.xml'), StatusReportResponse::class, 'xml');

/** @var $response StatusReportResponse */
$this->assertInstanceOf(StatusReportResponse::class, $response);

$this->assertCount(2, $response->getOrders());

$order = $response->getOrders()[0];
$this->assertInstanceOf(Order::class, $order);

$this->assertSame('2018-04-06 13:33:27', $order->getDeliveryDate()->format('Y-m-d H:i:s'));

$order = $response->getOrders()[1];
$this->assertInstanceOf(Order::class, $order);

$this->assertSame('2011-04-07 00:00:00', $order->getDeliveryDate()->format('Y-m-d H:i:s'));
}

public function test_it_serializes_to_empty_json()
{
$response = new StatusReportResponse();
Expand Down
5 changes: 5 additions & 0 deletions tests/Serialization/CallCourierRequestTest.php
Expand Up @@ -57,11 +57,16 @@ public function test_can_serialize()
'Street' => 'Тестовая',
'House' => '8',
'Flat' => '32',
]))->setSendAddress(Address::create([
'Street' => 'Проверочная',
'House' => '10',
'Flat' => '22',
])));

$this->assertSameAsXML('<?xml version="1.0" encoding="UTF-8"?>
<CallCourier CallCount="1">
<Call Date="2017-03-14" TimeBeg="10:00" TimeEnd="17:00" LunchBeg="14:00" LunchEnd="14:30" SendCityCode="44" SendPhone="+79138739944" SenderName="Testing" Weight="20" Comment="foo" DispatchNumber="1039547805" IgnoreTime="true">
<SendAddress Street="Проверочная" House="10" Flat="22"/>
<Address Street="Тестовая" House="8" Flat="32"/>
</Call>
</CallCourier>
Expand Down
15 changes: 15 additions & 0 deletions tests/Serialization/PvzListRequestTest.php
Expand Up @@ -74,5 +74,20 @@ public function test_can_get_params()
'allowedcod' => true,
'isdressingroom' => false,
], $request->getParams());

$request = (new PvzListRequest())
->setCityPostCode('800900');

$this->assertEquals([
'type' => PvzListRequest::TYPE_PVZ,
'cityid' => null,
'regionid' => null,
'countryid' => null,
'citypostcode' => '800900',
'havecashles' => null,
'weightmax' => null,
'allowedcod' => null,
'isdressingroom' => null,
], $request->getParams());
}
}

0 comments on commit cc9503c

Please sign in to comment.