diff --git a/src/Helper/EntityHelper.php b/src/Helper/EntityHelper.php index 5cfad30..5ffed7c 100644 --- a/src/Helper/EntityHelper.php +++ b/src/Helper/EntityHelper.php @@ -35,13 +35,25 @@ public static function createSerializer(): Serializer return self::$serializer; } + public static function formatDateCreated(string $dateCreated): string + { + return (new \DateTime($dateCreated))->format('Y-m-d\TH:i:s'); + } + /** * @return mixed */ public static function deserializeXml(string $class, string $xml) { $serializer = self::createSerializer()->getSerializer(); - return $serializer->deserialize($xml, $class, 'xml'); + + $simpleXml = new \SimpleXMLElement($xml); + + if (property_exists($simpleXml, 'Header')) { + $simpleXml->Header->DateCreated = self::formatDateCreated((string) $simpleXml->Header->DateCreated); + } + + return $serializer->deserialize((string) $simpleXml->saveXML(), $class, 'xml'); } /** diff --git a/src/Helper/XmlHelper.php b/src/Helper/XmlHelper.php index 3c8f2b2..8483f64 100644 --- a/src/Helper/XmlHelper.php +++ b/src/Helper/XmlHelper.php @@ -101,7 +101,7 @@ public static function fetchChildNodes(string $node, \SimpleXMLElement $xml): ar if (property_exists($xml, 'Header')) { $data['Header'] = (array) $xml->Header; - $data['Header']['DateCreated'] = (new \DateTime($data['Header']['DateCreated']))->format('Y-m-d\TH:i:s'); + $data['Header']['DateCreated'] = EntityHelper::formatDateCreated($data['Header']['DateCreated']); } return $data; diff --git a/tests/Integration/Component/CloudLicenseOrderTest.php b/tests/Integration/Component/CloudLicenseOrderTest.php index 41a8515..20845d1 100644 --- a/tests/Integration/Component/CloudLicenseOrderTest.php +++ b/tests/Integration/Component/CloudLicenseOrderTest.php @@ -30,11 +30,12 @@ public function create(): void 'Test', 'john@doe.com' ); + $contact = $officeClient->contact->agreement->create( - 'john', - 'doe', - 'john@doe.com', - '12345', + 'Sandwave', + 'Test', + 'willem@sandwave.io', + '0622029880', new \DateTime() ); @@ -42,7 +43,7 @@ public function create(): void $tenant, $contact, '507201', - '120A00064B', + '120A00385B', 1 ); diff --git a/tests/Integration/Component/OrderModifyQuantityTest.php b/tests/Integration/Component/OrderModifyQuantityTest.php index 916781a..64e5920 100644 --- a/tests/Integration/Component/OrderModifyQuantityTest.php +++ b/tests/Integration/Component/OrderModifyQuantityTest.php @@ -25,8 +25,8 @@ public function create(): void $officeClient = new OfficeClient('example.com', 'test', 'test', ['handler' => $stack]); $response = $officeClient->order->modify( - 10035367, - 0, + 10333119, + 2, false, '123' ); diff --git a/tests/Integration/Component/PreferenceHeaderDateTest.php b/tests/Integration/Component/PreferenceHeaderDateTest.php new file mode 100644 index 0000000..b98866c --- /dev/null +++ b/tests/Integration/Component/PreferenceHeaderDateTest.php @@ -0,0 +1,71 @@ + $stack]); + + $client->webhook->addEventSubscriber(Event::CUSTOMER_CREATE, new class() implements CustomerObserverInterface { + public function execute(Customer $customer, ?Status $status): void + { + if ($customer->getHeader() !== null) { + Assert::assertSame('2022-02-15 15:14:09', $customer->getHeader()->getDateCreated()->format('Y-m-d H:i:s')); + } + } + }); + + $client->webhook->process( + (string) file_get_contents(__DIR__ . '/../Data/Response/NewCustomerResponse_V3.xml') + ); + } + + /** + * @test + */ + public function deserializeXmlPrefHeaderDate(): void + { + $mockHandler = new MockHandler( + [new Response(200, [], (string) file_get_contents(__DIR__ . '/../Data/Request/NewCustomerRequest.xml'))] + ); + + $stack = HandlerStack::create($mockHandler); + $client = new OfficeClient('example.com', 'test', 'test', ['handler' => $stack]); + + $client->webhook->addEventSubscriber(Event::ORDER_MODIFY_QUANTITY, new class() implements OrderModifyQuantityObserverInterface { + public function execute(OrderModifyQuantity $order, ?Status $status): void + { + if ($order->getHeader() !== null) { + Assert::assertSame('2022-03-07 12:09:56', $order->getHeader()->getDateCreated()->format('Y-m-d H:i:s')); + } + } + }); + + $client->webhook->process( + (string) file_get_contents(__DIR__ . '/../Data/Response/ModifyOrderQuantityResponse.xml') + ); + } +} diff --git a/tests/Integration/Webhook/DateTest.php b/tests/Integration/Webhook/DateTest.php new file mode 100644 index 0000000..bb1d21a --- /dev/null +++ b/tests/Integration/Webhook/DateTest.php @@ -0,0 +1,71 @@ + $stack]); + + $client->webhook->addEventSubscriber(Event::CUSTOMER_CREATE, new class() implements CustomerObserverInterface { + public function execute(Customer $customer, ?Status $status): void + { + if ($customer->getHeader() !== null) { + Assert::assertSame('2022-02-15 15:14:09', $customer->getHeader()->getDateCreated()->format('Y-m-d H:i:s')); + } + } + }); + + $client->webhook->process( + (string) file_get_contents(__DIR__ . '/../Data/Response/NewCustomerResponse_V3.xml') + ); + } + + /** + * @test + */ + public function deserializeXmlPrefHeaderDate(): void + { + $mockHandler = new MockHandler( + [new Response(200, [], (string) file_get_contents(__DIR__ . '/../Data/Request/NewCustomerRequest.xml'))] + ); + + $stack = HandlerStack::create($mockHandler); + $client = new OfficeClient('example.com', 'test', 'test', ['handler' => $stack]); + + $client->webhook->addEventSubscriber(Event::ORDER_MODIFY_QUANTITY, new class() implements OrderModifyQuantityObserverInterface { + public function execute(OrderModifyQuantity $order, ?Status $status): void + { + if ($order->getHeader() !== null) { + Assert::assertSame('2022-03-07 12:09:56', $order->getHeader()->getDateCreated()->format('Y-m-d H:i:s')); + } + } + }); + + $client->webhook->process( + (string) file_get_contents(__DIR__ . '/../Data/Response/ModifyOrderQuantityResponse.xml') + ); + } +}