Skip to content

Commit

Permalink
Merge pull request #2 from haw-trezo/feature/trezo_unit_tests
Browse files Browse the repository at this point in the history
Feature/trezo unit tests
  • Loading branch information
brunoroeder authored Aug 16, 2019
2 parents c37b3c5 + 36f9a34 commit c85911a
Show file tree
Hide file tree
Showing 9 changed files with 457 additions and 27 deletions.
7 changes: 5 additions & 2 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Vindi\Payment\Helper;

use \Magento\Framework\App\Helper\AbstractHelper;
use Magento\Sales\Model\Order;
use Vindi\Payment\Model\Config\Source\Mode;

class Data extends AbstractHelper
Expand Down Expand Up @@ -63,9 +64,11 @@ public function getMode()
return $this->getModuleGeneralConfig('mode');
}

public function getOrderStatus()
public function getStatusToOrderComplete()
{
return $this->getCreditCardConfig('order_status');
$status = $this->getModuleGeneralConfig('order_status');

return $status ? : Order::STATE_PROCESSING;
}

public function getBaseUrl()
Expand Down
2 changes: 1 addition & 1 deletion Helper/WebHookHandlers/BillPaid.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function createInvoice(\Magento\Sales\Model\Order $order)

$order->addStatusHistoryComment(
__('The payment was confirmed and the order is beeing processed')->getText(),
$order->getConfig()->getStateDefaultStatus(\Magento\Sales\Model\Order::STATE_PROCESSING)
$this->helperData->getStatusToOrderComplete()
);
$this->orderRepository->save($order);

Expand Down
72 changes: 62 additions & 10 deletions Model/Payment/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,28 @@ public function __construct(

public function findOrCreateProducts($order)
{
$list = [];
foreach ($order->getItems() as $item) {
$productType = $item->getProduct()->getTypeId();
$vindiProductId = $this->findOrCreateProduct($item->getSku(), $item->getName(), $productType);

for ($i = 0; $i < $item->getQtyOrdered(); $i++) {
$itemPrice = $this->getItemPrice($item, $productType);

if (false === $itemPrice)
if (! $itemPrice)
continue;

$list[] = [
array_push($list, [
'product_id' => $vindiProductId,
'amount' => $itemPrice
];
]);
}
}

if ($order->getShippingAmount() > 0) {
$shippingId = $this->findOrCreateProduct('frete', 'frete');
$list[] = [
'product_id' => $shippingId,
'amount' => $order->getShippingAmount()
];
}
$list = $this->buildTax($list, $order);
$list = $this->buildDiscount($list, $order);
$list = $this->buildShipping($list, $order);

return $list;
}

Expand All @@ -52,6 +50,60 @@ private function getItemPrice($item, $productType)
return $item->getPrice();
}

/**
* @param array $list
* @param $order
* @return array
*/
private function buildTax(array $list, $order)
{
if ($order->getTaxAmount() > 0) {
$productId = $this->findOrCreateProduct('taxa', 'Taxa');
array_push($list, [
'product_id' => $productId,
'amount' => $order->getTaxAmount()
]);
}

return $list;
}

/**
* @param $list
* @param $order
* @return array
*/
private function buildDiscount(array $list, $order)
{
if ($order->getDiscountAmount() < 0) {
$productId = $this->findOrCreateProduct('cupom', 'Cupom de Desconto');
array_push($list, [
'product_id' => $productId,
'amount' => $order->getDiscountAmount()
]);
}

return $list;
}

/**
* @param array $list
* @param $order
* @return array
*/
private function buildShipping(array $list, $order)
{
if ($order->getShippingAmount() > 0) {
$productId = $this->findOrCreateProduct('frete', 'frete');
array_push($list, [
'product_id' => $productId,
'amount' => $order->getShippingAmount()
]);
}

return $list;
}

private function findOrCreateProduct($itemSku, $itemName, $itemType = 'simple')
{
$itemName = $itemType == 'configurable' ? $itemSku : $itemName;
Expand Down
4 changes: 3 additions & 1 deletion Model/Payment/Vindi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

class Vindi extends \Vindi\Payment\Model\Payment\AbstractMethod
{
protected $_code = 'vindi';
const CODE = 'vindi';

protected $_code = self::CODE;
protected $_isOffline = true;
protected $_infoBlockType = Cc::class;

Expand Down
58 changes: 52 additions & 6 deletions Plugin/SetOrderStatusOnPlace.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,61 @@

namespace Vindi\Payment\Plugin;

use Magento\Sales\Model\Order\Payment;
use Vindi\Payment\Helper\Data;
use Vindi\Payment\Model\Payment\BankSlip;
use Vindi\Payment\Model\Payment\Vindi;

class SetOrderStatusOnPlace
{
public function afterPlace(\Magento\Sales\Model\Order\Payment $subject, $result)
/**
* @var Data
*/
private $helperData;

/**
* SetOrderStatusOnPlace constructor.
* @param Data $helperData
*/
public function __construct(
Data $helperData
) {
$this->helperData = $helperData;
}

public function afterPlace(Payment $subject, $result)
{
if ($subject->getMethod() == \Vindi\Payment\Model\Payment\BankSlip::CODE) {
$order = $subject->getOrder();
$order->setState('new')
->setStatus('pending');
switch ($subject->getMethod()) {
case BankSlip::CODE:
$this->pendingStatus($subject);
break;
case Vindi::CODE:
$this->completeStatus($subject);
break;
}

return $result;
}
}

/**
* @param Payment $subject
*/
private function pendingStatus(Payment $subject)
{
$order = $subject->getOrder();
$order->setState('new')
->setStatus('pending');
}

/**
* @param Payment $subject
*/
private function completeStatus(Payment $subject)
{
$order = $subject->getOrder();
$order->setState('new')
->setStatus($this->helperData->getStatusToOrderComplete())
->addCommentToStatusHistory(__('The payment was confirmed and the order is beeing processed'))
;
}
}
166 changes: 166 additions & 0 deletions Test/Unit/OrderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php

namespace Vindi\Payment\Test\Unit;

class OrderTest extends \PHPUnit\Framework\TestCase
{

protected $objectManager;
protected $customerRepositoryInterface;
protected $managerInterface;

public function setUp()
{
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->customerRepositoryInterface = $this->getMockBuilder(\Magento\Customer\Api\CustomerRepositoryInterface::class)->disableOriginalConstructor()->getMock();
$this->managerInterface = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)->disableOriginalConstructor()->getMock();
}

public function testOrderWithTax()
{
$vindiProductId = 'taxa';
$amount = 1.00;

$order = $this->createOrderMock($amount, 0.00, 0.00);
$list = $this->createVindiProductMock($vindiProductId)->findOrCreateProducts($order);

$this->makeAssertions($list, $vindiProductId, $amount);
}

public function testOrderWithDiscount()
{
$vindiProductId = 'cupom';
$amount = -5.00;

$order = $this->createOrderMock(0.00, $amount, 0.00);
$list = $this->createVindiProductMock($vindiProductId)->findOrCreateProducts($order);

$this->makeAssertions($list, $vindiProductId, $amount);
}

public function testOrderWithShipping()
{
$vindiProductId = 'frete';
$amount = 10.00;

$order = $this->createOrderMock(0.00, 0.00, $amount);
$list = $this->createVindiProductMock($vindiProductId)->findOrCreateProducts($order);

$this->makeAssertions($list, $vindiProductId, $amount);
}

private function makeAssertions($list, $vindiProductId, $amount)
{
$this->assertContains('fake_sku', $list[0]['product_id'], '', true);
$this->assertEquals('9.99', $list[0]['amount']);

$this->assertEquals($vindiProductId, $list[1]['product_id']);
$this->assertEquals($amount, $list[1]['amount']);

return true;
}

private function createApiMock($desiredTestResponse = null)
{
$apiMock = $this->getMockBuilder(\Vindi\Payment\Model\Payment\Api::class)
->disableOriginalConstructor()
->getMock();

$requestResponses = [];

$requestResponses[] = [
'product' => [
'id' => 'fake_sku'
]
];

$requestResponses[] = [
'products' => [
0 => [
'id' => $desiredTestResponse
]
]
];

$apiMock->method('request')
->willReturnOnConsecutiveCalls(false, $requestResponses[0], $requestResponses[1]);

return $apiMock;
}

private function createVindiProductMock($desiredTestResponse)
{
$product = $this->objectManager->getObject(\Vindi\Payment\Model\Payment\Product::class, [
'customerRepository' => $this->customerRepositoryInterface,
'api' => $this->createApiMock($desiredTestResponse),
'messageManager' => $this->managerInterface
]);

return $product;
}

private function createOrderMock(
$orderTaxAmount = 0.00, $orderDiscountAmount = 0.00, $orderShippingAmount = 0.00,
$itemQty = 1, $itemType = 'simple', $itemPrice = 9.99
)
{
$orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
->disableOriginalConstructor()
->getMock();

$items = [
$this->createItemMock($itemQty, $itemType, $itemPrice)
];

$orderMock->method('getItems')
->willReturn($items);

$orderMock->method('getTaxAmount')
->willReturn($orderTaxAmount);

$orderMock->method('getDiscountAmount')
->willReturn($orderDiscountAmount);

$orderMock->method('getShippingAmount')
->willReturn($orderShippingAmount);

return $orderMock;
}

private function createItemMock($qty = 1, $type = 'simple', $price = 10.99)
{
$itemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
->disableOriginalConstructor()
->getMock();

$itemMock->method('getQtyOrdered')
->willReturn($qty);

$itemMock->method('getSku')
->willReturn('FAKE_SKU');

$itemMock->method('getName')
->willReturn('FAKE_NAME');

$itemMock->method('getPrice')
->willReturn($price);

$itemMock->method('getProduct')
->willReturn($this->createProductMock($type));

return $itemMock;
}

private function createProductMock($type)
{
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->disableOriginalConstructor()
->getMock();

$productMock->method('getTypeId')
->willReturn($type);

return $productMock;
}

}
Loading

0 comments on commit c85911a

Please sign in to comment.