Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit bc863c7

Browse files
committed
feat(ClientLogging): support setting X-External-User-ID header
1 parent b64d199 commit bc863c7

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/Core/Request/AbstractApiRequest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828
abstract class AbstractApiRequest implements ClientRequestInterface, ContextAwareInterface
2929
{
30+
const EXTERNAL_USER_HEADER = 'X-External-User-ID';
3031
use ContextTrait;
3132

3233
/**
@@ -43,6 +44,8 @@ abstract class AbstractApiRequest implements ClientRequestInterface, ContextAwar
4344

4445
protected $resultClass = JsonObject::class;
4546

47+
protected $headers = [];
48+
4649
/**
4750
* @param JsonEndpoint $endpoint
4851
* @param Context $context
@@ -93,6 +96,17 @@ public function setIdentifier($identifier)
9396
return $this;
9497
}
9598

99+
/**
100+
* @param string $externalUserId
101+
* @return $this
102+
*/
103+
public function setExternalUserId($externalUserId)
104+
{
105+
$this->headers[self::EXTERNAL_USER_HEADER] = $externalUserId;
106+
107+
return $this;
108+
}
109+
96110
/**
97111
* @param JsonEndpoint $endpoint
98112
* @return $this
@@ -241,6 +255,9 @@ public function map(array $data, Context $context = null, MapperInterface $mappe
241255
*/
242256
public function executeWithClient(Client $client, array $headers = null)
243257
{
244-
return $client->execute($this, $headers);
258+
if (!is_null($headers)) {
259+
$this->headers = array_merge($headers, $this->headers);
260+
}
261+
return $client->execute($this, $this->headers);
245262
}
246263
}

tests/integration/Customer/CustomerUpdateRequestTest.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Commercetools\Core\ApiTestCase;
1010
use Commercetools\Core\Model\Common\Address;
1111
use Commercetools\Core\Model\Common\AddressCollection;
12+
use Commercetools\Core\Model\Common\CreatedBy;
13+
use Commercetools\Core\Model\Common\LastModifiedBy;
1214
use Commercetools\Core\Model\Customer\Customer;
1315
use Commercetools\Core\Model\Customer\CustomerDraft;
1416
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft;
@@ -547,7 +549,6 @@ public function testCompanyName()
547549

548550
$this->assertSame($customer->getVersion(), $result->getVersion());
549551
$this->assertSame($customer->getCompanyName(), $result->getCompanyName());
550-
551552
}
552553

553554
public function testDateOfBirth()
@@ -653,6 +654,48 @@ public function testCustomField()
653654
$this->assertSame($this->getTestRun(), $customer->getCustom()->getFields()->getTestField());
654655
}
655656

657+
658+
public function testSetExternalUserOnCustomerUpdate()
659+
{
660+
$draft = $this->getDraft('name');
661+
662+
$request = CustomerCreateRequest::ofDraft($draft);
663+
$request->setExternalUserId('custom-external-user-id');
664+
665+
$response = $request->executeWithClient($this->getClient());
666+
$result = $request->mapResponse($response);
667+
668+
$this->cleanupRequests[] = $this->deleteRequest = CustomerDeleteRequest::ofIdAndVersion(
669+
$result->getCustomer()->getId(),
670+
$result->getCustomer()->getVersion()
671+
);
672+
$customer = $result->getCustomer();
673+
674+
$this->assertInstanceOf(Customer::class, $customer);
675+
$this->assertInstanceOf(CreatedBy::class, $customer->getCreatedBy());
676+
$this->assertInstanceOf(LastModifiedBy::class, $customer->getLastModifiedBy());
677+
$this->assertSame('custom-external-user-id', $customer->getCreatedBy()->getExternalUserId());
678+
$this->assertSame('custom-external-user-id', $customer->getLastModifiedBy()->getExternalUserId());
679+
680+
$key = 'new-' . $this->getTestRun();
681+
$request = CustomerUpdateRequest::ofIdAndVersion($customer->getId(), $customer->getVersion())
682+
->addAction(
683+
CustomerSetKeyAction::of()->setKey($key)
684+
)
685+
;
686+
$request->setExternalUserId('another-user');
687+
688+
$response = $request->executeWithClient($this->getClient());
689+
$result = $request->mapResponse($response);
690+
$this->deleteRequest->setVersion($result->getVersion());
691+
692+
$this->assertInstanceOf(Customer::class, $result);
693+
$this->assertInstanceOf(CreatedBy::class, $result->getCreatedBy());
694+
$this->assertInstanceOf(LastModifiedBy::class, $result->getLastModifiedBy());
695+
$this->assertSame('custom-external-user-id', $result->getCreatedBy()->getExternalUserId());
696+
$this->assertSame('another-user', $result->getLastModifiedBy()->getExternalUserId());
697+
}
698+
656699
public function localeProvider()
657700
{
658701
return [

0 commit comments

Comments
 (0)