diff --git a/lib/PayPal/Rest/ApiContext.php b/lib/PayPal/Rest/ApiContext.php index dd68fe8..25bca9b 100644 --- a/lib/PayPal/Rest/ApiContext.php +++ b/lib/PayPal/Rest/ApiContext.php @@ -85,13 +85,19 @@ class ApiContext */ public function getRequestId() { - if ($this->requestId == null) { - $this->requestId = $this->generateRequestId(); - } - return $this->requestId; } + /** + * Sets the request ID + * + * @param string $requestId the PayPal-Request-Id value to use + */ + public function setRequestId($requestId) + { + $this->requestId = $requestId; + } + /** * Resets the requestId that can be used to set the PayPal-request-id * header used for idempotency. In cases where you need to make multiple create calls diff --git a/tests/PayPal/Test/Rest/ApiContextTest.php b/tests/PayPal/Test/Rest/ApiContextTest.php index 32c711c..9869b9d 100644 --- a/tests/PayPal/Test/Rest/ApiContextTest.php +++ b/tests/PayPal/Test/Rest/ApiContextTest.php @@ -22,13 +22,27 @@ class ApiContextTest extends PHPUnit_Framework_TestCase public function testGetRequestId() { $requestId = $this->apiContext->getRequestId(); - $this->assertNotNull($requestId); - $this->assertEquals($requestId, $this->apiContext->getRequestId()); + $this->assertNull($requestId); + } + + public function testSetRequestId() + { + $this->assertNull($this->apiContext->getRequestId()); + + $expectedRequestId = 'random-value'; + $this->apiContext->setRequestId($expectedRequestId); + $requestId = $this->apiContext->getRequestId(); + $this->assertEquals($expectedRequestId, $requestId); } public function testResetRequestId() { - $requestId = $this->apiContext->getRequestId(); + $this->assertNull($this->apiContext->getRequestId()); + + $requestId = $this->apiContext->resetRequestId(); + $this->assertNotNull($requestId); + + // Tests that another resetRequestId call will generate a new ID $newRequestId = $this->apiContext->resetRequestId(); $this->assertNotNull($newRequestId); $this->assertNotEquals($newRequestId, $requestId);