From 532dfd43a19af0e0f7497ac616ff5a9a19f4c416 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sun, 31 May 2020 01:02:21 +0100 Subject: [PATCH] Add order and order request classes References #3 --- src/Glassboxx/Enum/InteractionType.php | 9 +++ src/Glassboxx/Request/OrderRequest.php | 62 +++++++++++++++++ .../Request/OrderRequestInterface.php | 16 +++++ src/Glassboxx/Traits/UsesCreatedAtTrait.php | 25 +++++++ src/Glassboxx/ValueObject/Order.php | 62 +++++++++++++++++ src/Glassboxx/ValueObject/OrderInterface.php | 24 +++++++ .../Glassboxx/Request/CustomerRequestTest.php | 10 +-- tests/Glassboxx/Request/OrderRequestTest.php | 67 +++++++++++++++++++ tests/Glassboxx/TestCase.php | 41 ++++++++++++ tests/Glassboxx/ValueObject/OrderTest.php | 34 ++++++++++ 10 files changed, 342 insertions(+), 8 deletions(-) create mode 100644 src/Glassboxx/Enum/InteractionType.php create mode 100644 src/Glassboxx/Request/OrderRequest.php create mode 100644 src/Glassboxx/Request/OrderRequestInterface.php create mode 100644 src/Glassboxx/Traits/UsesCreatedAtTrait.php create mode 100644 src/Glassboxx/ValueObject/Order.php create mode 100644 src/Glassboxx/ValueObject/OrderInterface.php create mode 100644 tests/Glassboxx/Request/OrderRequestTest.php create mode 100644 tests/Glassboxx/ValueObject/OrderTest.php diff --git a/src/Glassboxx/Enum/InteractionType.php b/src/Glassboxx/Enum/InteractionType.php new file mode 100644 index 0000000..38996eb --- /dev/null +++ b/src/Glassboxx/Enum/InteractionType.php @@ -0,0 +1,9 @@ +order = $order; + + return $this; + } + + public function execute(): string + { + $body = [ + 'items' => [ + [ + 'created_at' => $this->getCreatedAtDate(), + 'currency_code' => $this->order->getCurrencyCode(), + 'customer_email' => $this->order->getCustomer()->getEmailAddress(), + 'customer_firstname' => $this->order->getCustomer()->getFirstName(), + 'customer_lastname' => $this->order->getCustomer()->getLastName(), + 'discount_amount' => 0, + 'duration_for_loan' => 0, + 'hostname' => $this->config->getVendorId(), + 'original_order_number' => $this->order->getOrderNumber(), + 'price_incl_tax' => $this->order->getPrice(), + 'sku' => $this->order->getSku(), + 'type_of_interaction' => InteractionType::PURCHASE, + ], + ], + ]; + + $response = $this->client->request( + 'POST', + self::BASE_URL . self::ENDPOINT, + [ + 'auth_bearer' => $this->authToken, + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'body' => json_encode($body), + ] + ); + + return $response->getContent(false); + } +} diff --git a/src/Glassboxx/Request/OrderRequestInterface.php b/src/Glassboxx/Request/OrderRequestInterface.php new file mode 100644 index 0000000..1bb3628 --- /dev/null +++ b/src/Glassboxx/Request/OrderRequestInterface.php @@ -0,0 +1,16 @@ +time))->format('Y-m-d H:i:s'); + } + + public function setCreatedDate(string $dateString): self + { + $this->time = $dateString; + + return $this; + } +} diff --git a/src/Glassboxx/ValueObject/Order.php b/src/Glassboxx/ValueObject/Order.php new file mode 100644 index 0000000..c8e821d --- /dev/null +++ b/src/Glassboxx/ValueObject/Order.php @@ -0,0 +1,62 @@ +customer = $customer; + $this->sku = $sku; + $this->orderNumber = $orderNumber; + $this->currencyCode = $currencyCode; + $this->price = $price; + } + + public function getCurrencyCode(): string + { + return $this->currencyCode; + } + + public function getCustomer(): CustomerInterface + { + return $this->customer; + } + + public function getOrderNumber(): string + { + return $this->orderNumber; + } + + public function getPrice(): float + { + return $this->price; + } + + public function getSku(): string + { + return $this->sku; + } +} diff --git a/src/Glassboxx/ValueObject/OrderInterface.php b/src/Glassboxx/ValueObject/OrderInterface.php new file mode 100644 index 0000000..8fa4d85 --- /dev/null +++ b/src/Glassboxx/ValueObject/OrderInterface.php @@ -0,0 +1,24 @@ +willReturn($response); - $customer = $this->getMockBuilder(CustomerInterface::class) - ->disableOriginalConstructor() - ->getMock(); - $customer->method('getFirstName')->willReturn('Oliver'); - $customer->method('getLastName')->willReturn('Davies'); - $customer->method('getEmailAddress')->willReturn('oliver@oliverdavies.uk'); - $request = (new CustomerRequest($client)) - ->forCustomer($customer) + ->forCustomer($this->getMockCustomer()) ->withAuthToken($authTokenRequest->getToken()) ->withConfig($this->config); diff --git a/tests/Glassboxx/Request/OrderRequestTest.php b/tests/Glassboxx/Request/OrderRequestTest.php new file mode 100644 index 0000000..4993266 --- /dev/null +++ b/tests/Glassboxx/Request/OrderRequestTest.php @@ -0,0 +1,67 @@ + [ + [ + 'created_at' => '2020-06-04 12:00:00', + 'currency_code' => 'GBP', + 'customer_email' => 'oliver@oliverdavies.uk', + 'customer_firstname' => 'Oliver', + 'customer_lastname' => 'Davies', + 'discount_amount' => 0, + 'duration_for_loan' => 0, + 'hostname' => 123, + 'original_order_number' => 'abc123', + 'price_incl_tax' => 100, + 'sku' => 'this-is-the-first-sku', + 'type_of_interaction' => 'purchase', + ], + ], + ]; + + $response = $this->getMockBuilder(ResponseInterface::class)->getMock(); + $response->method('getContent')->willReturn(json_encode($body)); + + $authTokenRequest = $this->getMockAuthTokenRequest(); + + $client = $this->getMockBuilder(MockHttpClient::class)->getMock(); + $client->expects($this->once()) + ->method('request') + ->with( + 'POST', + OrderRequest::BASE_URL + .OrderRequest::ENDPOINT, + [ + 'auth_bearer' => $authTokenRequest->getToken(), + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'body' => json_encode($body), + ] + ) + ->willReturn($response); + + $request = (new OrderRequest($client)) + ->forOrder($this->getMockOrder()) + ->withAuthToken($authTokenRequest->getToken()) + ->withConfig($this->config) + ->setCreatedDate('2020-06-04 12:00:00'); + + // A successful response returns the original body. + $this->assertSame(json_encode($body), $request->execute()); + } +} diff --git a/tests/Glassboxx/TestCase.php b/tests/Glassboxx/TestCase.php index 37f4d31..ee4f12e 100644 --- a/tests/Glassboxx/TestCase.php +++ b/tests/Glassboxx/TestCase.php @@ -3,6 +3,9 @@ namespace Opdavies\Glassboxx\Tests\Glassboxx; use Opdavies\Glassboxx\Config; +use Opdavies\Glassboxx\Request\AuthTokenRequestInterface; +use Opdavies\Glassboxx\ValueObject\CustomerInterface; +use Opdavies\Glassboxx\ValueObject\OrderInterface; class TestCase extends \PHPUnit\Framework\TestCase { @@ -24,4 +27,42 @@ class TestCase extends \PHPUnit\Framework\TestCase ) ->getMock(); } + + protected function getMockAuthTokenRequest(): AuthTokenRequestInterface + { + $authTokenRequest = $this->getMockBuilder(AuthTokenRequestInterface::class) + ->getMock(); + + $authTokenRequest->expects($this->any()) + ->method('getToken') + ->willReturn('testtoken'); + + return $authTokenRequest; + } + + protected function getMockCustomer(): CustomerInterface + { + $customer = $this->getMockBuilder(CustomerInterface::class) + ->disableOriginalConstructor() + ->getMock(); + $customer->method('getFirstName')->willReturn('Oliver'); + $customer->method('getLastName')->willReturn('Davies'); + $customer->method('getEmailAddress')->willReturn('oliver@oliverdavies.uk'); + + return $customer; + } + + protected function getMockOrder(): OrderInterface + { + $order = $this->getMockBuilder(OrderInterface::class) + ->disableOriginalConstructor() + ->getMock(); + $order->method('getCurrencyCode')->willReturn('GBP'); + $order->method('getCustomer')->willReturn($this->getMockCustomer()); + $order->method('getOrderNumber')->willReturn('abc123'); + $order->method('getPrice')->willReturn((float) 100); + $order->method('getSku')->willReturn('this-is-the-first-sku'); + + return $order; + } } diff --git a/tests/Glassboxx/ValueObject/OrderTest.php b/tests/Glassboxx/ValueObject/OrderTest.php new file mode 100644 index 0000000..bf561e5 --- /dev/null +++ b/tests/Glassboxx/ValueObject/OrderTest.php @@ -0,0 +1,34 @@ +getMockBuilder(CustomerInterface::class) + ->disableOriginalConstructor() + ->getMock(); + + $order = new Order( + $customer, + 'this-is-the-sku', + '123', + 'GBP', + 10.00 + ); + + $this->assertSame('GBP', $order->getCurrencyCode()); + $this->assertSame($customer, $order->getCustomer()); + $this->assertSame('123', $order->getOrderNumber()); + $this->assertSame(10.0, $order->getPrice()); + $this->assertSame('this-is-the-sku', $order->getSku()); + } +}