CalculatorTest.php 6.47 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
<?php
/**
 * @author    Mediotype Developement <diveinto@mediotype.com>
 * @copyright 2018 Mediotype. All rights reserved.
 */

namespace Vertex\Tax\Test\Integration\Model;

use Magento\Checkout\Api\Data\TotalsInformationInterface;
use Magento\Checkout\Api\TotalsInformationManagementInterface;
use Magento\Framework\Message\Collection;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Message\MessageInterface;
use Vertex\Tax\Test\Integration\Scenarios\QuoteWithPaymentAndInvalidAddress;
use Vertex\Tax\Test\Integration\TestCase;

/**
 * Ensure that Calculator works properly
 */
class CalculatorTest extends TestCase
{
    /** @var QuoteWithPaymentAndInvalidAddress */
    private $quoteWithPaymentAndInvalidAddress;

    /** @inheritdoc */
    protected function setUp()
    {
        parent::setUp();
        $this->quoteWithPaymentAndInvalidAddress = $this->getObject(
            QuoteWithPaymentAndInvalidAddress::class
        );
    }

    /**
     * Test if vertex error message is shown on frontend
     *
     * @magentoAppArea frontend
     * @magentoDbIsolation enabled
     * @magentoAppIsolation enabled
     * @magentoCache all disabled
     * @magentoConfigFixture default_store tax/vertex_settings/enable_vertex 1
     * @magentoConfigFixture default_store tax/vertex_settings/api_url https://example.org/CalculateTax70
     */
    public function testShowErrorMessageToCustomer()
    {
        $soapClient = $this->createPartialMock(\SoapClient::class, ['CalculateTax70']);
        $soapClient->expects($this->atLeastOnce())
            ->method('CalculateTax70')
            ->with(
                $this->callback(
                    function (\stdClass $request) {
                        $destination = $request->QuotationRequest->Customer->Destination;
                        if ($destination->PostalCode !== QuoteWithPaymentAndInvalidAddress::INVALID_POSTAL_CODE) {
                            $this->fail(
                                'Post code is valid, please use \''
                                . QuoteWithPaymentAndInvalidAddress::INVALID_POSTAL_CODE . '\''
                            );

                            return false;
                        }
                        $this->assertEquals(
                            QuoteWithPaymentAndInvalidAddress::INVALID_POSTAL_CODE,
                            $destination->PostalCode
                        );

                        return true;
                    }
                )
            )
            ->willReturn(new \stdClass());
        $this->getSoapFactory()->setSoapClient($soapClient);

        $cart = $this->quoteWithPaymentAndInvalidAddress->create('vertex_cart_with_invalid_address');

        /** @var TotalsInformationInterface $totalsInfo */
        $totalsInfo = $this->createObject(TotalsInformationInterface::class);
        $totalsInfo->setAddress($cart->getBillingAddress());
        $totalsInfo->setShippingCarrierCode('flatrate');
        $totalsInfo->setShippingMethodCode('flatrate');

        /** @var TotalsInformationManagementInterface $totalsManagement */
        $totalsManagement = $this->getObject(TotalsInformationManagementInterface::class);
        $totals = $totalsManagement->calculate($cart->getId(), $totalsInfo);

        $messages = $totals->getTotalSegments()['tax']->getExtensionAttributes()->getVertexTaxCalculationMessages();

        $this->assertInternalType('array', $messages);
        $this->assertContains(
            'Unable to calculate taxes. This could be caused by an invalid address provided in checkout.',
            $messages
        );
    }

    /**
     * Test if vertex error message is shown on admin
     *
     * @magentoAppArea adminhtml
     * @magentoDbIsolation enabled
     * @magentoAppIsolation enabled
     * @magentoCache all disabled
     * @magentoConfigFixture default_store tax/vertex_settings/enable_vertex 1
     * @magentoConfigFixture default_store tax/vertex_settings/api_url https://example.org/CalculateTax70
     */
    public function testShowErrorMessageToAdminUser()
    {
        $soapClient = $this->createPartialMock(\SoapClient::class, ['CalculateTax70']);
        $soapClient->expects($this->atLeastOnce())
            ->method('CalculateTax70')
            ->with(
                $this->callback(
                    function (\stdClass $request) {
                        $destination = $request->QuotationRequest->Customer->Destination;
                        if ($destination->PostalCode !== QuoteWithPaymentAndInvalidAddress::INVALID_POSTAL_CODE) {
                            $this->fail(
                                'Post code is valid, please use \''
                                . QuoteWithPaymentAndInvalidAddress::INVALID_POSTAL_CODE . '\''
                            );

                            return false;
                        }
                        $this->assertEquals(
                            QuoteWithPaymentAndInvalidAddress::INVALID_POSTAL_CODE,
                            $destination->PostalCode
                        );

                        return true;
                    }
                )
            )
            ->willReturn(new \stdClass());
        $this->getSoapFactory()->setSoapClient($soapClient);

        $cart = $this->quoteWithPaymentAndInvalidAddress->create('vertex_cart_with_invalid_address');

        /** @var TotalsInformationInterface $totalsInfo */
        $totalsInfo = $this->createObject(TotalsInformationInterface::class);
        $totalsInfo->setAddress($cart->getBillingAddress());
        $totalsInfo->setShippingCarrierCode('flatrate');
        $totalsInfo->setShippingMethodCode('flatrate');

        /** @var TotalsInformationManagementInterface $totalsManagement */
        $totalsManagement = $this->getObject(TotalsInformationManagementInterface::class);
        $totalsManagement->calculate($cart->getId(), $totalsInfo);

        /** @var ManagerInterface $messageManager */
        $messageManager = $this->getObject(ManagerInterface::class);

        $messages = $messageManager->getMessages(true);

        $this->assertInstanceOf(Collection::class, $messages);

        $this->assertContainsOnlyInstancesOf(MessageInterface::class, $messages->getItems());
        $this->assertEquals(1, $messages->getCount());

        /** @var MessageInterface $message */
        $message = current($messages->getItems());

        $this->assertContains(
            'Unable to calculate taxes. This could be caused by an invalid address provided in checkout.',
            $message->getText()
        );
    }
}