Skip to content

Commit

Permalink
test: added WebhookManagement recipient unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateus Picoloto committed Apr 23, 2024
1 parent 1bcda4c commit 70d3573
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Model/WebhookManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -112,7 +119,7 @@ private function getMetadata($data)
}
return $metadata;
}

private function hasMagentoOrder($data)
{
$code = 0;
Expand Down
69 changes: 69 additions & 0 deletions Test/Unit/Model/WebhookManagementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Pagarme\Pagarme\Test\Unit\Model;

use Mockery;
use Magento\Sales\Model\Order;
use Pagarme\Pagarme\Model\Account;
use Magento\Sales\Model\OrderFactory;
use Pagarme\Pagarme\Test\Unit\BaseTest;
use Pagarme\Pagarme\Model\WebhookManagement;
use Pagarme\Core\Webhook\Services\WebhookReceiverService;

class WebhookManagementTest extends BaseTest
{
public function testeSaveWithRecipientWebhook()
{
$moduleCoreSetupMock = Mockery::mock('alias:Pagarme\Core\Kernel\Abstractions\AbstractModuleCoreSetup');
$moduleCoreSetupMock->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);
}
}

0 comments on commit 70d3573

Please sign in to comment.