Skip to content

Commit

Permalink
Feature/date parser (#64)
Browse files Browse the repository at this point in the history
* update test data
* fix rendering date in pref header
* php cs fix
* update helper methods
* php stan fix
  • Loading branch information
willemdaems authored Apr 6, 2022
1 parent 4083c8a commit 3793bd0
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 9 deletions.
14 changes: 13 additions & 1 deletion src/Helper/EntityHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Helper/XmlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 6 additions & 5 deletions tests/Integration/Component/CloudLicenseOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,20 @@ 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()
);

$customerResponse = $officeClient->order->cloudLicense->create(
$tenant,
$contact,
'507201',
'120A00064B',
'120A00385B',
1
);

Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Component/OrderModifyQuantityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
Expand Down
71 changes: 71 additions & 0 deletions tests/Integration/Component/PreferenceHeaderDateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php declare(strict_types = 1);

namespace Integration\Webhook;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use SandwaveIo\Office365\Entity\Customer;
use SandwaveIo\Office365\Entity\OrderModifyQuantity;
use SandwaveIo\Office365\Enum\Event;
use SandwaveIo\Office365\Library\Observer\Customer\CustomerObserverInterface;
use SandwaveIo\Office365\Library\Observer\Order\OrderModifyQuantityObserverInterface;
use SandwaveIo\Office365\Library\Observer\Status\Status;
use SandwaveIo\Office365\Office\OfficeClient;

final class PreferenceHeaderDateTest extends TestCase
{
/**
* @test
*/
public function deserializeArrayPrefHeaderDate(): 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::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')
);
}
}
71 changes: 71 additions & 0 deletions tests/Integration/Webhook/DateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php declare(strict_types = 1);

namespace Integration\Webhook;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use SandwaveIo\Office365\Entity\Customer;
use SandwaveIo\Office365\Entity\OrderModifyQuantity;
use SandwaveIo\Office365\Enum\Event;
use SandwaveIo\Office365\Library\Observer\Customer\CustomerObserverInterface;
use SandwaveIo\Office365\Library\Observer\Order\OrderModifyQuantityObserverInterface;
use SandwaveIo\Office365\Library\Observer\Status\Status;
use SandwaveIo\Office365\Office\OfficeClient;

final class DateTest extends TestCase
{
/**
* @test
*/
public function deserializeArrayPrefHeaderDate(): 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::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')
);
}
}

0 comments on commit 3793bd0

Please sign in to comment.