From bc2e0fd920a21d7bae45ebc200a8a0c76ba16af7 Mon Sep 17 00:00:00 2001 From: Yurii Torbyk Date: Fri, 9 Sep 2016 18:32:36 +0300 Subject: [PATCH] Update webhook service Add files requires_hipping, platform_specific_fields fields to subscription --- src/SubscribePro/Http.php | 11 +++ .../Service/Subscription/Subscription.php | 57 ++++++++++-- .../Subscription/SubscriptionInterface.php | 24 +++++ src/SubscribePro/Service/Webhook/Event.php | 32 +++---- .../Service/Webhook/EventFactory.php | 43 ++++----- .../Service/Webhook/EventInterface.php | 12 +-- .../Service/Webhook/WebhookService.php | 14 +++ .../Service/Webhook/WebhookServiceFactory.php | 4 - .../Service/Subscription/SubscriptionTest.php | 88 ++++++++++++++++--- tests/Service/Webhook/EventTest.php | 81 ++++++++++++++--- tests/Service/Webhook/WebhookServiceTest.php | 52 +++++++++++ 11 files changed, 329 insertions(+), 89 deletions(-) diff --git a/src/SubscribePro/Http.php b/src/SubscribePro/Http.php index bdfaaf8..52f2647 100644 --- a/src/SubscribePro/Http.php +++ b/src/SubscribePro/Http.php @@ -230,4 +230,15 @@ protected function processResponse($response) throw new HttpException($response); } + + /** + * @codeCoverageIgnore + * + * @return array|bool + */ + public function getRawRequest() + { + $rawRequest = file_get_contents('php://input'); + return $rawRequest ? json_decode($rawRequest, true) : false; + } } diff --git a/src/SubscribePro/Service/Subscription/Subscription.php b/src/SubscribePro/Service/Subscription/Subscription.php index f16f0d8..2939b11 100644 --- a/src/SubscribePro/Service/Subscription/Subscription.php +++ b/src/SubscribePro/Service/Subscription/Subscription.php @@ -22,6 +22,7 @@ class Subscription extends DataObject implements SubscriptionInterface self::SHIPPING_ADDRESS_ID => false, self::SHIPPING_ADDRESS => false, self::PRODUCT_SKU => true, + self::REQUIRES_SHIPPING => true, self::QTY => true, self::USE_FIXED_PRICE => true, self::FIXED_PRICE => false, @@ -32,8 +33,9 @@ class Subscription extends DataObject implements SubscriptionInterface self::EXPIRATION_DATE => false, self::COUPON_CODE => false, self::MAGENTO_STORE_CODE => false, - self::MAGENTO_SHIPPING_METHOD_CODE => true, - self::USER_DEFINED_FIELDS => false + self::MAGENTO_SHIPPING_METHOD_CODE => false, + self::USER_DEFINED_FIELDS => false, + self::PLATFORM_SPECIFIC_FIELDS => false ]; /** @@ -44,6 +46,7 @@ class Subscription extends DataObject implements SubscriptionInterface self::SHIPPING_ADDRESS_ID => false, self::SHIPPING_ADDRESS => false, self::PRODUCT_SKU => true, + self::REQUIRES_SHIPPING => true, self::QTY => true, self::USE_FIXED_PRICE => true, self::FIXED_PRICE => false, @@ -53,8 +56,9 @@ class Subscription extends DataObject implements SubscriptionInterface self::EXPIRATION_DATE => false, self::COUPON_CODE => false, self::MAGENTO_STORE_CODE => false, - self::MAGENTO_SHIPPING_METHOD_CODE => true, - self::USER_DEFINED_FIELDS => false + self::MAGENTO_SHIPPING_METHOD_CODE => false, + self::USER_DEFINED_FIELDS => false, + self::PLATFORM_SPECIFIC_FIELDS => false ]; /** @@ -126,10 +130,19 @@ public function getFormData() */ public function isValid() { - return ($this->getData(self::SHIPPING_ADDRESS_ID, false) || $this->getShippingAddress()->isAsChildValid($this->isNew())) + return (!$this->getData(self::REQUIRES_SHIPPING) || $this->isShippingValid()) && $this->checkRequiredFields($this->getFormFields()); } + /** + * @return bool + */ + protected function isShippingValid() + { + return ($this->getData(self::SHIPPING_ADDRESS_ID, false) || $this->getShippingAddress()->isAsChildValid($this->isNew())) + && $this->getData(self::MAGENTO_SHIPPING_METHOD_CODE, false); + } + //@codeCoverageIgnoreStart /** @@ -186,6 +199,23 @@ public function setProductSku($productSku) return $this->setData(self::PRODUCT_SKU, $productSku); } + /** + * @return bool + */ + public function getRequiresShipping() + { + return $this->getData(self::REQUIRES_SHIPPING, false); + } + + /** + * @param bool $useShipping + * @return $this + */ + public function setRequiresShipping($useShipping) + { + return $this->setData(self::REQUIRES_SHIPPING, $useShipping); + } + /** * @return array */ @@ -508,6 +538,23 @@ public function setUserDefinedFields(array $userDefinedFields) return $this->setData(self::USER_DEFINED_FIELDS, $userDefinedFields); } + /** + * @return array + */ + public function getPlatformSpecificFields() + { + return $this->getData(self::PLATFORM_SPECIFIC_FIELDS, []); + } + + /** + * @param array $platformSpecificFields + * @return $this + */ + public function setPlatformSpecificFields(array $platformSpecificFields) + { + return $this->setData(self::PLATFORM_SPECIFIC_FIELDS, $platformSpecificFields); + } + /** * @param string|null $format * @return string|null diff --git a/src/SubscribePro/Service/Subscription/SubscriptionInterface.php b/src/SubscribePro/Service/Subscription/SubscriptionInterface.php index e4dbab8..bf58804 100644 --- a/src/SubscribePro/Service/Subscription/SubscriptionInterface.php +++ b/src/SubscribePro/Service/Subscription/SubscriptionInterface.php @@ -14,6 +14,7 @@ interface SubscriptionInterface extends DataInterface const CUSTOMER_ID = 'customer_id'; const STATUS = 'status'; const PRODUCT_SKU = 'product_sku'; + const REQUIRES_SHIPPING = 'requires_shipping'; const SUBSCRIPTION_PRODUCTS = 'subscription_products'; const QTY = 'qty'; const USE_FIXED_PRICE = 'use_fixed_price'; @@ -36,6 +37,7 @@ interface SubscriptionInterface extends DataInterface const EXPIRATION_DATE = 'expiration_date'; const COUPON_CODE = 'coupon_code'; const USER_DEFINED_FIELDS = 'user_defined_fields'; + const PLATFORM_SPECIFIC_FIELDS = 'platform_specific_fields'; const RECURRING_ORDER_COUNT = 'recurring_order_count'; const ERROR_TIME = 'error_time'; const ERROR_CLASS = 'error_class'; @@ -98,6 +100,17 @@ public function getStatus(); */ public function getProductSku(); + /** + * @return bool + */ + public function getRequiresShipping(); + + /** + * @param bool $useShipping + * @return $this + */ + public function setRequiresShipping($useShipping); + /** * @param string $productSku * @return $this @@ -307,6 +320,17 @@ public function getUserDefinedFields(); */ public function setUserDefinedFields(array $userDefinedFields); + /** + * @return array + */ + public function getPlatformSpecificFields(); + + /** + * @param array $platformSpecificFields + * @return $this + */ + public function setPlatformSpecificFields(array $platformSpecificFields); + /** * @param string|null $format * @return string|null diff --git a/src/SubscribePro/Service/Webhook/Event.php b/src/SubscribePro/Service/Webhook/Event.php index 3716f82..784f672 100644 --- a/src/SubscribePro/Service/Webhook/Event.php +++ b/src/SubscribePro/Service/Webhook/Event.php @@ -9,22 +9,6 @@ class Event extends DataObject implements EventInterface { //@codeCoverageIgnoreStart - /** - * @return \SubscribePro\Service\Customer\CustomerInterface - */ - public function getCustomer() - { - return $this->getData(self::CUSTOMER); - } - - /** - * @return \SubscribePro\Service\Subscription\SubscriptionInterface - */ - public function getSubscription() - { - return $this->getData(self::SUBSCRIPTION); - } - /** * @return string */ @@ -61,14 +45,26 @@ public function getUpdated($format = null) //@codeCoverageIgnoreEnd + /** + * @param string|null $field + * @return mixed|null + */ + public function getEventData($field = null) + { + $data = $this->getData(self::DATA); + if ($field && $data !== null) { + return isset($data[$field]) ? $data[$field] : null; + } + + return $data; + } + /** * @return array */ public function toArray() { $data = parent::toArray(); - $data[self::CUSTOMER] = $this->getCustomer()->toArray(); - $data[self::SUBSCRIPTION] = $this->getSubscription()->toArray(); $data[self::DESTINATIONS] = array_map(function (DestinationInterface $destination) { return $destination->toArray(); }, $this->getDestinations()); diff --git a/src/SubscribePro/Service/Webhook/EventFactory.php b/src/SubscribePro/Service/Webhook/EventFactory.php index e1d26bd..bdb15e8 100644 --- a/src/SubscribePro/Service/Webhook/EventFactory.php +++ b/src/SubscribePro/Service/Webhook/EventFactory.php @@ -21,24 +21,10 @@ class EventFactory implements DataFactoryInterface protected $destinationFactory; /** - * @var \SubscribePro\Service\DataFactoryInterface - */ - protected $customerFactory; - - /** - * @var \SubscribePro\Service\DataFactoryInterface - */ - protected $subscriptionFactory; - - /** - * @param \SubscribePro\Service\DataFactoryInterface $customerFactory - * @param \SubscribePro\Service\DataFactoryInterface $subscriptionFactory * @param \SubscribePro\Service\DataFactoryInterface $destinationFactory * @param string $instanceName */ public function __construct( - \SubscribePro\Service\DataFactoryInterface $customerFactory, - \SubscribePro\Service\DataFactoryInterface $subscriptionFactory, \SubscribePro\Service\DataFactoryInterface $destinationFactory, $instanceName = '\SubscribePro\Service\Webhook\Event' ) { @@ -48,8 +34,6 @@ public function __construct( $this->instanceName = $instanceName; $this->destinationFactory = $destinationFactory; - $this->customerFactory = $customerFactory; - $this->subscriptionFactory = $subscriptionFactory; } /** @@ -58,19 +42,12 @@ public function __construct( */ public function create(array $data = []) { - $jsonData = $this->getJsonData($data, EventInterface::DATA); - $customerData = $this->getFieldData($jsonData, EventInterface::CUSTOMER); - $subscriptionData = $this->getFieldData($jsonData, EventInterface::SUBSCRIPTION); + $eventData = $this->getEventData($data); $destinationsData = $this->getFieldData($data, EventInterface::DESTINATIONS); $data[EventInterface::DESTINATIONS] = $this->createDestinationItems($destinationsData); - $data[EventInterface::CUSTOMER] = $this->customerFactory->create($customerData); - $data[EventInterface::SUBSCRIPTION] = $this->subscriptionFactory->create($subscriptionData); - - if (isset($data[EventInterface::DATA])) { - unset($data[EventInterface::DATA]); - } - + $data[EventInterface::DATA] = $eventData; + return new $this->instanceName($data); } @@ -86,10 +63,20 @@ protected function getFieldData($data, $field) /** * @param array $data - * @param string $field * @return array */ - protected function getJsonData($data, $field) + protected function getEventData($data) + { + $eventData = $this->getJsonData($data, EventInterface::DATA); + return is_array($eventData) ? $eventData : []; + } + + /** + * @param array $data + * @param string $field + * @return mixed + */ + private function getJsonData($data, $field) { return isset($data[$field]) && is_string($data[$field]) ? json_decode($data[$field], true) : []; } diff --git a/src/SubscribePro/Service/Webhook/EventInterface.php b/src/SubscribePro/Service/Webhook/EventInterface.php index dd8e983..42b49dd 100644 --- a/src/SubscribePro/Service/Webhook/EventInterface.php +++ b/src/SubscribePro/Service/Webhook/EventInterface.php @@ -12,21 +12,15 @@ interface EventInterface extends DataInterface const ID = 'id'; const TYPE = 'type'; const DATA = 'data'; - const CUSTOMER = 'customer'; - const SUBSCRIPTION = 'subscription'; const DESTINATIONS = 'destinations'; const CREATED = 'created'; const UPDATED = 'updated'; /** - * @return \SubscribePro\Service\Customer\CustomerInterface + * @param string|null $field + * @return mixed|null */ - public function getCustomer(); - - /** - * @return \SubscribePro\Service\Subscription\SubscriptionInterface - */ - public function getSubscription(); + public function getEventData($field = null); /** * @return string diff --git a/src/SubscribePro/Service/Webhook/WebhookService.php b/src/SubscribePro/Service/Webhook/WebhookService.php index 615835e..bcdc63a 100644 --- a/src/SubscribePro/Service/Webhook/WebhookService.php +++ b/src/SubscribePro/Service/Webhook/WebhookService.php @@ -45,6 +45,20 @@ public function ping() return true; } + /** + * Read webhook event from request + * + * @return \SubscribePro\Service\Webhook\EventInterface|bool + */ + public function readEvent() + { + $rawRequestBody = $this->httpClient->getRawRequest(); + $webhookEvent = !empty($rawRequestBody['webhook_event']) + ? json_decode($rawRequestBody['webhook_event'], true) + : false; + return $webhookEvent ? $this->dataFactory->create($webhookEvent) : false; + } + /** * @param int $eventId * @return \SubscribePro\Service\Webhook\EventInterface diff --git a/src/SubscribePro/Service/Webhook/WebhookServiceFactory.php b/src/SubscribePro/Service/Webhook/WebhookServiceFactory.php index e6d0aa9..786b29e 100644 --- a/src/SubscribePro/Service/Webhook/WebhookServiceFactory.php +++ b/src/SubscribePro/Service/Webhook/WebhookServiceFactory.php @@ -3,8 +3,6 @@ namespace SubscribePro\Service\Webhook; use SubscribePro\Service\AbstractServiceFactory; -use SubscribePro\Service\Customer\CustomerService; -use SubscribePro\Service\Subscription\SubscriptionService; use SubscribePro\Service\Webhook\Event\DestinationFactory; /** @@ -35,8 +33,6 @@ protected function createDataFactory() ); return new EventFactory( - $this->serviceFactoryResolver->getServiceFactory(CustomerService::NAME)->createDataFactory(), - $this->serviceFactoryResolver->getServiceFactory(SubscriptionService::NAME)->createDataFactory(), $destinationFactory, $this->getConfigValue(WebhookService::CONFIG_INSTANCE_NAME, '\SubscribePro\Service\Webhook\Event') ); diff --git a/tests/Service/Subscription/SubscriptionTest.php b/tests/Service/Subscription/SubscriptionTest.php index 4761aa5..e9b7df6 100644 --- a/tests/Service/Subscription/SubscriptionTest.php +++ b/tests/Service/Subscription/SubscriptionTest.php @@ -211,6 +211,7 @@ public function testIsNotValidIfNotValidShippingAddress() $data = [ SubscriptionInterface::CUSTOMER_ID => 11, SubscriptionInterface::NEXT_ORDER_DATE => '2016-12-12', + SubscriptionInterface::REQUIRES_SHIPPING => true, SubscriptionInterface::MAGENTO_SHIPPING_METHOD_CODE => 'tablerate', SubscriptionInterface::SHIPPING_ADDRESS => $addressData ]; @@ -250,10 +251,14 @@ public function testIsValidWithValidAddress($data, $addressData, $isNew, $isVali ->with($addressData) ->willReturnSelf(); $this->shippingAddressMock->expects($this->once())->method('getId')->willReturn(null); - $this->shippingAddressMock->expects($this->once()) - ->method('isAsChildValid') - ->with($isNew) - ->willReturn(true); + + if (!empty($data[SubscriptionInterface::REQUIRES_SHIPPING]) && $data[SubscriptionInterface::REQUIRES_SHIPPING]) { + $this->shippingAddressMock->expects($this->once()) + ->method('isAsChildValid') + ->with($isNew) + ->willReturn(true); + } + $this->subscription->importData($data); $this->assertEquals($isValid, $this->subscription->isValid()); @@ -279,7 +284,7 @@ public function isValidWithValidAddressDataProvider() SubscriptionInterface::QTY => '123', SubscriptionInterface::INTERVAL => 'weekly', SubscriptionInterface::NEXT_ORDER_DATE => '2016-12-12', - SubscriptionInterface::NEXT_ORDER_DATE => '2016-12-12', + SubscriptionInterface::REQUIRES_SHIPPING => true, SubscriptionInterface::MAGENTO_SHIPPING_METHOD_CODE => 'tablerate', SubscriptionInterface::SHIPPING_ADDRESS => ['key' => 'value'] ], @@ -295,7 +300,7 @@ public function isValidWithValidAddressDataProvider() SubscriptionInterface::QTY => '123', SubscriptionInterface::USE_FIXED_PRICE => true, SubscriptionInterface::NEXT_ORDER_DATE => '2016-12-12', - SubscriptionInterface::NEXT_ORDER_DATE => '2016-12-12', + SubscriptionInterface::REQUIRES_SHIPPING => true, SubscriptionInterface::MAGENTO_SHIPPING_METHOD_CODE => 'tablerate', SubscriptionInterface::SHIPPING_ADDRESS => ['key' => 'value'] ], @@ -311,7 +316,7 @@ public function isValidWithValidAddressDataProvider() SubscriptionInterface::USE_FIXED_PRICE => true, SubscriptionInterface::INTERVAL => 'weekly', SubscriptionInterface::NEXT_ORDER_DATE => '2016-12-12', - SubscriptionInterface::NEXT_ORDER_DATE => '2016-12-12', + SubscriptionInterface::REQUIRES_SHIPPING => true, SubscriptionInterface::MAGENTO_SHIPPING_METHOD_CODE => 'tablerate', SubscriptionInterface::SHIPPING_ADDRESS => ['key' => 'value'] ], @@ -328,7 +333,7 @@ public function isValidWithValidAddressDataProvider() SubscriptionInterface::USE_FIXED_PRICE => true, SubscriptionInterface::INTERVAL => 'weekly', SubscriptionInterface::NEXT_ORDER_DATE => '2016-12-12', - SubscriptionInterface::NEXT_ORDER_DATE => '2016-12-12', + SubscriptionInterface::REQUIRES_SHIPPING => true, SubscriptionInterface::MAGENTO_SHIPPING_METHOD_CODE => 'tablerate', SubscriptionInterface::SHIPPING_ADDRESS => ['key' => 'value'] ], @@ -345,7 +350,7 @@ public function isValidWithValidAddressDataProvider() SubscriptionInterface::USE_FIXED_PRICE => true, SubscriptionInterface::INTERVAL => 'weekly', SubscriptionInterface::NEXT_ORDER_DATE => '2016-12-12', - SubscriptionInterface::NEXT_ORDER_DATE => '2016-12-12', + SubscriptionInterface::REQUIRES_SHIPPING => true, SubscriptionInterface::MAGENTO_SHIPPING_METHOD_CODE => 'tablerate', SubscriptionInterface::SHIPPING_ADDRESS => ['key' => 'value'] ], @@ -393,7 +398,7 @@ public function isValidWithShippingAddressIdDataProvider() SubscriptionInterface::USE_FIXED_PRICE => true, SubscriptionInterface::INTERVAL => 'weekly', SubscriptionInterface::NEXT_ORDER_DATE => '2016-12-12', - SubscriptionInterface::NEXT_ORDER_DATE => '2016-12-12', + SubscriptionInterface::REQUIRES_SHIPPING => true, SubscriptionInterface::MAGENTO_SHIPPING_METHOD_CODE => 'tablerate', SubscriptionInterface::SHIPPING_ADDRESS_ID => 111, ], @@ -407,7 +412,7 @@ public function isValidWithShippingAddressIdDataProvider() SubscriptionInterface::USE_FIXED_PRICE => true, SubscriptionInterface::INTERVAL => 'weekly', SubscriptionInterface::NEXT_ORDER_DATE => '2016-12-12', - SubscriptionInterface::NEXT_ORDER_DATE => '2016-12-12', + SubscriptionInterface::REQUIRES_SHIPPING => true, SubscriptionInterface::MAGENTO_SHIPPING_METHOD_CODE => 'tablerate', SubscriptionInterface::SHIPPING_ADDRESS_ID => 111, ], @@ -415,6 +420,61 @@ public function isValidWithShippingAddressIdDataProvider() ]; } + /** + * @param array $data + * @dataProvider isValidIfNotUseShipping + */ + public function testIsValidIfNotUseShipping($data) + { + $this->paymentProfileMock->expects($this->once()) + ->method('importData') + ->with([]) + ->willReturnSelf(); + + $this->shippingAddressMock->expects($this->once()) + ->method('importData') + ->with([]) + ->willReturnSelf(); + $this->shippingAddressMock->expects($this->once())->method('getId')->willReturn(null); + $this->shippingAddressMock->expects($this->never())->method('isAsChildValid'); + + $this->subscription->importData($data); + $this->assertTrue($this->subscription->isValid()); + } + + /** + * @return array + */ + public function isValidIfNotUseShipping() + { + return [ + 'Valid: new: with use_shipping false' => [ + 'data' => [ + SubscriptionInterface::CUSTOMER_ID => 11, + SubscriptionInterface::PAYMENT_PROFILE_ID => '04', + SubscriptionInterface::PRODUCT_SKU => 'sku', + SubscriptionInterface::QTY => '123', + SubscriptionInterface::USE_FIXED_PRICE => true, + SubscriptionInterface::INTERVAL => 'weekly', + SubscriptionInterface::NEXT_ORDER_DATE => '2016-02-10', + SubscriptionInterface::REQUIRES_SHIPPING => false + ], + ], + 'Valid: not new: with use_shipping false' => [ + 'data' => [ + SubscriptionInterface::ID => 11, + SubscriptionInterface::PAYMENT_PROFILE_ID => '04', + SubscriptionInterface::PRODUCT_SKU => 'sku', + SubscriptionInterface::QTY => '123', + SubscriptionInterface::USE_FIXED_PRICE => true, + SubscriptionInterface::INTERVAL => 'weekly', + SubscriptionInterface::NEXT_ORDER_DATE => '2016-03-05', + SubscriptionInterface::REQUIRES_SHIPPING => false + ], + ], + ]; + } + /** * @param array $data * @param array $addressData @@ -465,6 +525,7 @@ public function getFormDataProvider() SubscriptionInterface::AUTHORIZE_NET_PAYMENT_PROFILE_ID => '313', SubscriptionInterface::CREDITCARD_LAST_DIGITS => '311', SubscriptionInterface::MAGENTO_BILLING_ADDRESS_ID => '5242', + SubscriptionInterface::REQUIRES_SHIPPING => true, SubscriptionInterface::SHIPPING_ADDRESS_ID => null, SubscriptionInterface::MAGENTO_SHIPPING_ADDRESS_ID => '123', SubscriptionInterface::MAGENTO_SHIPPING_METHOD_CODE => 'tablerate', @@ -500,6 +561,7 @@ public function getFormDataProvider() SubscriptionInterface::INTERVAL => 'monthly', SubscriptionInterface::MAGENTO_STORE_CODE => 'code', SubscriptionInterface::PAYMENT_PROFILE_ID => '333', + SubscriptionInterface::REQUIRES_SHIPPING => true, SubscriptionInterface::MAGENTO_SHIPPING_METHOD_CODE => 'tablerate', SubscriptionInterface::SEND_CUSTOMER_NOTIFICATION_EMAIL => true, SubscriptionInterface::FIRST_ORDER_ALREADY_CREATED => true, @@ -527,6 +589,7 @@ public function getFormDataProvider() SubscriptionInterface::CREDITCARD_LAST_DIGITS => '311', SubscriptionInterface::MAGENTO_BILLING_ADDRESS_ID => '5242', SubscriptionInterface::SHIPPING_ADDRESS_ID => null, + SubscriptionInterface::REQUIRES_SHIPPING => true, SubscriptionInterface::MAGENTO_SHIPPING_ADDRESS_ID => '123', SubscriptionInterface::MAGENTO_SHIPPING_METHOD_CODE => 'tablerate', SubscriptionInterface::SEND_CUSTOMER_NOTIFICATION_EMAIL => true, @@ -562,6 +625,7 @@ public function getFormDataProvider() SubscriptionInterface::INTERVAL => 'monthly', SubscriptionInterface::MAGENTO_STORE_CODE => 'code', SubscriptionInterface::PAYMENT_PROFILE_ID => '333', + SubscriptionInterface::REQUIRES_SHIPPING => true, SubscriptionInterface::MAGENTO_SHIPPING_METHOD_CODE => 'tablerate', SubscriptionInterface::SEND_CUSTOMER_NOTIFICATION_EMAIL => true, SubscriptionInterface::FIRST_ORDER_ALREADY_CREATED => true, @@ -590,6 +654,7 @@ public function getFormDataProvider() SubscriptionInterface::CREDITCARD_LAST_DIGITS => '311', SubscriptionInterface::MAGENTO_BILLING_ADDRESS_ID => '5242', SubscriptionInterface::SHIPPING_ADDRESS_ID => null, + SubscriptionInterface::REQUIRES_SHIPPING => true, SubscriptionInterface::MAGENTO_SHIPPING_ADDRESS_ID => '123', SubscriptionInterface::MAGENTO_SHIPPING_METHOD_CODE => 'tablerate', SubscriptionInterface::SEND_CUSTOMER_NOTIFICATION_EMAIL => true, @@ -623,6 +688,7 @@ public function getFormDataProvider() SubscriptionInterface::INTERVAL => 'monthly', SubscriptionInterface::MAGENTO_STORE_CODE => 'code', SubscriptionInterface::PAYMENT_PROFILE_ID => '333', + SubscriptionInterface::REQUIRES_SHIPPING => true, SubscriptionInterface::MAGENTO_SHIPPING_METHOD_CODE => 'tablerate', SubscriptionInterface::SEND_CUSTOMER_NOTIFICATION_EMAIL => true, SubscriptionInterface::COUPON_CODE => 'code', diff --git a/tests/Service/Webhook/EventTest.php b/tests/Service/Webhook/EventTest.php index 2dee787..120c8de 100644 --- a/tests/Service/Webhook/EventTest.php +++ b/tests/Service/Webhook/EventTest.php @@ -19,27 +19,15 @@ public function testToArray() ->method('toArray') ->willReturn(['second destination data']); - $customerMock = $this->getMockBuilder('SubscribePro\Service\Customer\CustomerInterface')->getMock(); - $customerMock->expects($this->once()) - ->method('toArray') - ->willReturn(['customer data']); - - $subscriptionMock = $this->getMockBuilder('SubscribePro\Service\Subscription\SubscriptionInterface')->getMock(); - $subscriptionMock->expects($this->once()) - ->method('toArray') - ->willReturn(['subscription data']); - $event = new Event([ EventInterface::ID => 111, - EventInterface::CUSTOMER => $customerMock, - EventInterface::SUBSCRIPTION => $subscriptionMock, + EventInterface::DATA => ['data'], EventInterface::DESTINATIONS => [$destination1Mock, $destination2Mock], ]); $expectedData = [ EventInterface::ID => 111, - EventInterface::CUSTOMER => ['customer data'], - EventInterface::SUBSCRIPTION => ['subscription data'], + EventInterface::DATA => ['data'], EventInterface::DESTINATIONS => [ ['first destination data'], ['second destination data'] @@ -48,4 +36,69 @@ public function testToArray() $this->assertEquals($expectedData, $event->toArray()); } + + /** + * @param array $data + * @param string|null $field + * @param mixed|null $result + * @dataProvider getEventDataDataProvider + */ + public function testGetEventData($data, $field, $result) + { + $event = new Event($data); + + $this->assertEquals($result, $event->getEventData($field)); + } + + /** + * @return array + */ + public function getEventDataDataProvider() + { + return [ + 'Event data not set:without field' => [ + 'data' => [ + EventInterface::ID => 2312, + EventInterface::TYPE => 'type', + ], + 'field' => null, + 'result' => null + ], + 'Event data not set:with field' => [ + 'data' => [ + EventInterface::ID => 54, + EventInterface::TYPE => 'subscription', + ], + 'field' => 'field_value', + 'result' => null + ], + 'With event data:without field' => [ + 'data' => [ + EventInterface::ID => 897, + EventInterface::CREATED => '2020-12-12', + EventInterface::DATA => ['data'], + ], + 'field' => null, + 'result' => ['data'] + ], + 'With event data:with field:not found in data' => [ + 'data' => [ + EventInterface::UPDATED => '2018-08-08', + EventInterface::TYPE => 'payment', + EventInterface::DATA => ['field' => 'data'], + ], + 'field' => 'another_field', + 'result' => null + ], + 'With event data:with field:field found' => [ + 'data' => [ + EventInterface::ID => 65455, + EventInterface::TYPE => 'some_type', + EventInterface::DATA => ['field' => 'value'], + ], + 'field' => 'field', + 'result' => 'value' + ], + ]; + } } diff --git a/tests/Service/Webhook/WebhookServiceTest.php b/tests/Service/Webhook/WebhookServiceTest.php index f9ba927..f451d33 100644 --- a/tests/Service/Webhook/WebhookServiceTest.php +++ b/tests/Service/Webhook/WebhookServiceTest.php @@ -55,6 +55,58 @@ public function testPing() $this->assertTrue($this->webhookService->ping()); } + /** + * @param string $request + * @dataProvider readEventDataProvider + */ + public function testReadEventIfWrongRequest($request) + { + $this->httpClientMock->expects($this->once()) + ->method('getRawRequest') + ->willReturn($request); + + $this->assertFalse($this->webhookService->readEvent()); + } + + /** + * @return array + */ + public function readEventDataProvider() + { + return [ + 'not array' => [ + 'request' => 'some text' + ], + 'wrong array' => [ + 'request' => ['test' => 'value'] + ], + 'empty webhook_event' => [ + 'request' => ['webhook_event' => ''] + ], + 'not json webhook_event' => [ + 'request' => ['webhook_event' => 'text'] + ] + ]; + } + + public function testReadEvent() + { + $webhookEvent = ['key' => 'value']; + + $this->httpClientMock->expects($this->once()) + ->method('getRawRequest') + ->willReturn(['webhook_event' => json_encode($webhookEvent)]); + + $eventMock = $this->getMockBuilder(EventInterface::class)->getMock(); + + $this->eventFactoryMock->expects($this->once()) + ->method('create') + ->with($webhookEvent) + ->willReturn($eventMock); + + $this->assertSame($eventMock, $this->webhookService->readEvent()); + } + public function testLoadEvent() { $eventId = 52231;