LogRequestTest.php 4.93 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
<?php
/**
 * @copyright  Vertex. All rights reserved.  https://www.vertexinc.com/
 * @author     Mediotype                     https://www.mediotype.com/
 */

namespace Vertex\Tax\Test\Unit\Model\Vertex;

use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Framework\Stdlib\DateTime\DateTimeFactory;
use Vertex\Tax\Api\Data\LogEntryInterface;
use Vertex\Tax\Api\Data\LogEntryInterfaceFactory;
use Vertex\Tax\Api\LogEntryRepositoryInterface;
use Vertex\Tax\Model\ApiClient;
use Vertex\Tax\Model\DomDocumentFactory;
use Vertex\Tax\Model\RequestLogger;
use Vertex\Tax\Test\Unit\TestCase;

class LogRequestTest extends TestCase
{
    const DATE = '1991-08-06 12:00:00';
    const OBJECT_ID = '42';
    const REQUEST_XML = '<request/>';
    const REQUEST_XML_FORMATTED = "<?xml version=\"1.0\"?>\n<request/>\n";
    const RESPONSE_EXCEPTIONTYPE = 'SOME EXCEPTION';
    const RESPONSE_LOOKUP_RESULT = 'RESULT OK';
    const RESPONSE_SUBTOTAL = '4.20';
    const RESPONSE_TOTAL = '5.25';
    const RESPONSE_TOTAL_TAX = '1.05';
    const RESPONSE_XML = '<QuotationResponse><TotalTax>1.05</TotalTax><Total>5.25</Total><SubTotal>4.20</SubTotal>'
    . '<Status lookupResult="RESULT OK"/></QuotationResponse>';
    const RESPONSE_XML_EXCEPTIONTYPE = '<response><exceptionType>SOME EXCEPTION</exceptionType></response>';
    const RESPONSE_XML_FORMATTED = <<<XML
<?xml version="1.0"?>
<QuotationResponse>
  <TotalTax>1.05</TotalTax>
  <Total>5.25</Total>
  <SubTotal>4.20</SubTotal>
  <Status lookupResult="RESULT OK"/>
</QuotationResponse>\n
XML;
    const SOURCE_PATH = 'source_path';
    const TAX_AREA_ID = '3.14';
    private $dateTimeFactory;

    public function setUp()
    {
        parent::setUp();

        $dateTimeMock = $this->createPartialMock(DateTime::class, ['date']);
        $dateTimeMock->expects($this->any())
            ->method('date')
            ->willReturn(static::DATE);

        $this->dateTimeFactory = $this->createMock(DateTimeFactory::class);
        $this->dateTimeFactory->expects($this->any())
            ->method('create')
            ->willReturn($dateTimeMock);
    }

    public function testHappyLogRequest()
    {
        $type = 'third_type';

        $logEntry = $this->createMock(LogEntryInterface::class);

        // Test that factory is used to generate LogEntry
        $logEntryFactory = $this->createMock(LogEntryInterfaceFactory::class);
        $logEntryFactory->expects($this->once())
            ->method('create')
            ->willReturn($logEntry);

        // Test that repository is used to save LogEntry
        $logEntryRepository = $this->createMock(LogEntryRepositoryInterface::class);
        $logEntryRepository->expects($this->once())
            ->method('save')
            ->with($logEntry);

        // Test that Type is properly set as passed in parameter
        $logEntry->expects($this->once())
            ->method('setType')
            ->with($type)
            ->willReturnSelf();

        // Test that Date is properly set as the date from the DateTimeFactory's DateTime
        $logEntry->expects($this->once())
            ->method('setDate')
            ->with(static::DATE)
            ->willReturnSelf();

        // Test that Request XML is formatted
        $logEntry->expects($this->once())
            ->method('setRequestXml')
            ->with(static::REQUEST_XML_FORMATTED);

        // Test that Response XML is formatted
        $logEntry->expects($this->once())
            ->method('setResponseXml')
            ->with(static::RESPONSE_XML_FORMATTED);

        // Test that Total Tax is set from XML
        $logEntry->expects($this->once())
            ->method('setTotalTax')
            ->with(static::RESPONSE_TOTAL_TAX);

        // Test that total is set from XML
        $logEntry->expects($this->once())
            ->method('setTotal')
            ->with(static::RESPONSE_TOTAL);

        // Test that subtotal is set from XML
        $logEntry->expects($this->once())
            ->method('setSubTotal')
            ->with(static::RESPONSE_SUBTOTAL);

        // Test that lookup result is set from XML
        $logEntry->expects($this->once())
            ->method('setLookupResult')
            ->with(static::RESPONSE_LOOKUP_RESULT);

        $requestLogger = $this->getObject(
            RequestLogger::class,
            [
                'repository' => $logEntryRepository,
                'logEntryFactory' => $logEntryFactory,
                'dateTime' => $this->dateTimeFactory->create(),
                'documentFactory' => $this->getObject(DomDocumentFactory::class),
            ]
        );

        $vertex = $this->getObject(
            ApiClient::class,
            [
                'requestLogger' => $requestLogger,
            ]
        );

        $this->invokeInaccessibleMethod(
            $vertex,
            'logRequest',
            $type,
            static::REQUEST_XML,
            static::RESPONSE_XML,
            ['_' => static::RESPONSE_TOTAL_TAX],
            static::TAX_AREA_ID
        );
    }
}