diff --git a/Model/WebhookManagement.php b/Model/WebhookManagement.php index c59b5230..04da88c5 100644 --- a/Model/WebhookManagement.php +++ b/Model/WebhookManagement.php @@ -27,13 +27,21 @@ class WebhookManagement implements WebhookManagementInterface */ protected $account; + /** + * @var WebhookReceiverService + */ + protected $webhookReceiverService; + public function __construct( OrderFactory $orderFactory, - Account $account + Account $account, + WebhookReceiverService $webhookReceiverService ) { $this->orderFactory = $orderFactory; $this->account = $account; + $this->webhookReceiverService = $webhookReceiverService; } + /** * @api * @param mixed $id @@ -67,8 +75,7 @@ public function save($id, $type, $data, $account) $this->account->saveAccountIdFromWebhook($account); } - $webhookReceiverService = new WebhookReceiverService(); - return $webhookReceiverService->handle($postData); + return $this->webhookReceiverService->handle($postData); } catch (WebhookHandlerNotFoundException | WebhookAlreadyHandledException $e) { return [ "message" => $e->getMessage(), @@ -112,7 +119,7 @@ private function getMetadata($data) } return $metadata; } - + private function hasMagentoOrder($data) { $code = 0; diff --git a/Test/Unit/Model/WebhookManagementTest.php b/Test/Unit/Model/WebhookManagementTest.php new file mode 100644 index 00000000..73023f73 --- /dev/null +++ b/Test/Unit/Model/WebhookManagementTest.php @@ -0,0 +1,69 @@ +shouldReceive('bootstrap') + ->andReturnSelf(); + + + $orderMock = Mockery::mock(Order::class); + $orderMock->shouldReceive('loadByIncrementId') + ->andReturnSelf(); + $orderMock->shouldReceive('getId') + ->andReturnFalse(); + + $orderFactoryMock = Mockery::mock(OrderFactory::class); + $orderFactoryMock->shouldReceive('create') + ->andReturn($orderMock); + $accountMock = Mockery::mock(Account::class); + + $webhookReceiverServiceMock = Mockery::mock(WebhookReceiverService::class); + $webhookRecipientResponse = [ + 'message' => 'Recipient updated', + 'code' => 200 + ]; + $webhookReceiverServiceMock->shouldReceive('handle') + ->once() + ->andReturn($webhookRecipientResponse); + + $webhookManagement = new WebhookManagement($orderFactoryMock, $accountMock, $webhookReceiverServiceMock); + + $id = "hook_aaaaaaaaaaaaaaaa"; + $type = "recipient.updated"; + $data = [ + "id" => 'rp_xxxxxxxxxxxxxxxx', + "name" => "Test recipient", + "email" => "test@recipient.test", + "document" => "11111111111", + "description" => "Test description", + "type" => "individual", + "payment_mode" => "bank_transfer", + "status" => "active", + "kyc_details" => + [ + "status" => "approved" + ], + ]; + + $account = [ + "id" => "acc_pBLvRR1HAAhXvD34", + "name" => "Account Test" + ]; + $result = $webhookManagement->save($id, $type, $data, $account); + + $this->assertSame($webhookRecipientResponse, $result); + } +}