You need to sign in or sign up before continuing.
OrderSaveAfterObserverTest.php 15.9 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
<?php

namespace Vertex\Tax\Test\Unit\Observer;

use Magento\Framework\DataObject;
use Magento\Framework\Event;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Sales\Api\Data\OrderExtension;
use Magento\Sales\Model\Order;
use Vertex\Services\Invoice\Request;
use Vertex\Services\Invoice\Response;
use Vertex\Tax\Model\Api\Data\InvoiceRequestBuilder;
use Vertex\Tax\Model\Config;
use Vertex\Tax\Model\ConfigurationValidator;
use Vertex\Tax\Model\CountryGuard;
use Vertex\Tax\Model\Data\OrderInvoiceStatus;
use Vertex\Tax\Model\Data\OrderInvoiceStatusFactory;
use Vertex\Tax\Model\ModuleManager;
use Vertex\Tax\Model\Repository\OrderInvoiceStatusRepository;
use Vertex\Tax\Model\TaxInvoice;
use Vertex\Tax\Observer\GiftwrapExtensionLoader;
use Vertex\Tax\Observer\OrderSavedAfterObserver;
use Vertex\Tax\Observer\ShippingAssignmentExtensionLoader;
use Vertex\Tax\Test\Unit\TestCase;

/**
 * Tests for {@see OrderSaveAfterObserver}
 *
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects) At limit - doesn't make sense to move success case to drop 1
 */
class OrderSaveAfterObserverTest extends TestCase
{
    /** @var \PHPUnit_Framework_MockObject_MockObject|Config */
    private $configMock;

    /** @var \PHPUnit_Framework_MockObject_MockObject|ConfigurationValidator */
    private $configValidatorMock;

    /** @var \PHPUnit_Framework_MockObject_MockObject|CountryGuard */
    private $countryGuardMock;

    /** @var \PHPUnit_Framework_MockObject_MockObject|OrderInvoiceStatusFactory */
    private $factoryMock;

    /** @var \PHPUnit_Framework_MockObject_MockObject|GiftwrapExtensionLoader */
    private $giftwrapExtensionMock;

    /** @var \PHPUnit_Framework_MockObject_MockObject|ManagerInterface */
    private $managerInterfaceMock;

    /** @var int */
    private $orderId;

    /** @var \PHPUnit_Framework_MockObject_MockObject|OrderInvoiceStatus */
    private $orderInvoiceStatusMock;

    /** @var \PHPUnit_Framework_MockObject_MockObject|Order */
    private $orderMock;

    /** @var OrderSavedAfterObserver */
    private $orderSavedAfterObserver;

    /** @var \PHPUnit_Framework_MockObject_MockObject|OrderInvoiceStatusRepository */
    private $repositoryMock;

    /** @var \PHPUnit_Framework_MockObject_MockObject|ShippingAssignmentExtensionLoader */
    private $shipmentExtensionLoader;

    /** @var \PHPUnit_Framework_MockObject_MockObject|TaxInvoice */
    private $taxInvoiceMock;

    /** @var \PHPUnit_Framework_MockObject_MockObject|OrderExtension */
    private $orderExtensionMock;

    /** @var \PHPUnit_Framework_MockObject_MockObject|InvoiceRequestBuilder */
    private $invoiceRequestBuilderMock;

    /**
     * @inheritdoc
     */
    protected function setUp()
    {
        parent::setUp();

        $this->configMock = $this->getMockBuilder(Config::class)
            ->setMethods(['isVertexActive', 'requestByOrderStatus', 'invoiceOrderStatus', 'isTaxCalculationEnabled'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->countryGuardMock = $this->getMockBuilder(CountryGuard::class)
            ->setMethods(['isOrderServiceableByVertex'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->taxInvoiceMock = $this->getMockBuilder(TaxInvoice::class)
            ->setMethods(['prepareInvoiceData', 'sendInvoiceRequest'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->managerInterfaceMock = $this->getMockBuilder(ManagerInterface::class)
            ->setMethods(['addSuccessMessage'])
            ->disableOriginalConstructor()
            ->getMockForAbstractClass();

        $this->repositoryMock = $this->getMockBuilder(OrderInvoiceStatusRepository::class)
            ->setMethods(['getByOrderId', 'save'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->factoryMock = $this->getMockBuilder(OrderInvoiceStatusFactory::class)
            ->setMethods(['create'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->invoiceRequestBuilderMock = $this->getMockBuilder(InvoiceRequestBuilder::class)
            ->setMethods(['buildFromOrder'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->orderInvoiceStatusMock = $this->getMockBuilder(OrderInvoiceStatus::class)
            ->setMethods(['setId', 'setIsSent'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->factoryMock->method('create')
            ->willReturn($this->orderInvoiceStatusMock);

        $this->orderMock = $this->getMockBuilder(Order::class)
            ->setMethods(['getStore', 'getId', 'getStatus', 'getExtensionAttributes'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->configValidatorMock = $this->getMockBuilder(ConfigurationValidator::class)
            ->setMethods(['execute'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->orderExtensionMock = $this->getMockBuilder(OrderExtension::class)
            ->setMethods(['getShippingAssignments'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->giftwrapExtensionMock = $this->getMockBuilder(GiftwrapExtensionLoader::class)
            ->setMethods(['loadOnOrder'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->shipmentExtensionLoader = $this->getMockBuilder(ShippingAssignmentExtensionLoader::class)
            ->setMethods(['loadOnOrder'])
            ->disableOriginalConstructor()
            ->getMock();

        $this->orderId = rand();

        $this->orderMock->method('getId')
            ->willReturn($this->orderId);

        $this->orderMock
            ->method('getExtensionAttributes')
            ->willReturn($this->orderExtensionMock);
        $this->orderExtensionMock->method('getShippingAssignments')->willReturn(true);

        $this->orderSavedAfterObserver = $this->getObject(
            OrderSavedAfterObserver::class,
            [
                'config' => $this->configMock,
                'countryGuard' => $this->countryGuardMock,
                'taxInvoice' => $this->taxInvoiceMock,
                'messageManager' => $this->managerInterfaceMock,
                'repository' => $this->repositoryMock,
                'factory' => $this->factoryMock,
                'configValidator' => $this->configValidatorMock,
                'invoiceRequestBuilder' => $this->invoiceRequestBuilderMock,
                'giftwrapExtensionLoader' => $this->giftwrapExtensionMock,
                'shipmentExtensionLoader' => $this->shipmentExtensionLoader,
                'showSuccessMessage' => true
            ]
        );
    }

    /**
     * Test that invoice is not sent when configuration is not valid
     *
     * @covers \Vertex\Tax\Observer\OrderSavedAfterObserver
     * @return void
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function testInvoiceNotSentIfConfigurationInvalid()
    {
        $this->assertInvoiceNeverSent();

        $this->setVertexActive();
        $this->setCanService();
        $this->setHasNoInvoice();
        $this->prepareEmptyInvoiceData();
        $this->setupRequestByOrderFlag();
        $this->setConfigValid(false);

        $observer = $this->createObserver();
        $this->orderSavedAfterObserver->execute($observer);
    }

    /**
     * Test that invoice is not sent when order has been invoiced
     *
     * @covers \Vertex\Tax\Observer\OrderSavedAfterObserver
     * @return void
     */
    public function testInvoiceNotSentIfOrderAlreadyInvoiced()
    {
        $this->assertInvoiceNeverSent();

        $this->setVertexActive();
        $this->setConfigValid();
        $this->setupRequestByOrderFlag();
        $this->setCanService();
        $this->prepareEmptyInvoiceData();

        // An Invoice is already stored.  Existence is based on non-exception return
        $orderInvoiceStatus = new DataObject();
        $this->repositoryMock->method('getByOrderId')
            ->willReturn($orderInvoiceStatus);

        $observer = $this->createObserver();
        $this->orderSavedAfterObserver->execute($observer);
    }

    /**
     * Test that invoice is not sent when order is not serviceable
     *
     * @covers \Vertex\Tax\Observer\OrderSavedAfterObserver
     * @return void
     */
    public function testInvoiceNotSentIfOrderNotServiceable()
    {
        $this->assertInvoiceNeverSent();
        $this->setConfigValid();
        $this->setVertexActive();

        $this->countryGuardMock->expects($this->atLeastOnce())
            ->method('isOrderServiceableByVertex')
            ->willReturn(false);

        $this->setHasNoInvoice();
        $this->prepareEmptyInvoiceData();
        $this->setupRequestByOrderFlag();

        $observer = $this->createObserver();
        $this->orderSavedAfterObserver->execute($observer);
    }

    /**
     * Test that invoice is not sent when order is not right status
     *
     * @covers \Vertex\Tax\Observer\OrderSavedAfterObserver
     * @return void
     */
    public function testInvoiceNotSentIfOrderStatusWrong()
    {
        $this->assertInvoiceNeverSent();
        $this->setConfigValid();
        $this->setVertexActive();
        $this->setCanService();
        $this->setHasNoInvoice();
        $this->prepareEmptyInvoiceData();

        $this->configMock->method('requestByOrderStatus')
            ->willReturn(true);

        $status1 = uniqid('order-status-', false);
        $status2 = uniqid('order-status-', false);

        $this->configMock->expects($this->atLeastOnce())
            ->method('invoiceOrderStatus')
            ->willReturn($status1);

        $this->orderMock->expects($this->atLeastOnce())
            ->method('getStatus')
            ->willReturn($status2);

        $observer = $this->createObserver();
        $this->orderSavedAfterObserver->execute($observer);
    }

    /**
     * Test that invoice is not sent when Vertex is not active
     *
     * @covers \Vertex\Tax\Observer\OrderSavedAfterObserver
     * @return void
     */
    public function testInvoiceNotSentIfVertexIsNotActive()
    {
        $this->assertInvoiceNeverSent();
        $this->setConfigValid();
        $this->configMock->expects($this->atLeastOnce())
            ->method('isVertexActive')
            ->willReturn(false);

        $this->setCanService();
        $this->setHasNoInvoice();
        $this->prepareEmptyInvoiceData();
        $this->setupRequestByOrderFlag();

        $observer = $this->createObserver();
        $this->orderSavedAfterObserver->execute($observer);
    }

    /**
     * Test that invoice is sent when conditions align
     *
     * @covers \Vertex\Tax\Observer\OrderSavedAfterObserver
     * @return void
     */
    public function testInvoiceSentAndActionsOccur()
    {
        $this->setVertexActive();
        $this->setCanService();
        $this->setHasNoInvoice();
        $this->prepareEmptyInvoiceData();
        $this->setupRequestByOrderFlag();
        $this->setConfigValid();

        $response = new Response();

        // Assert Invoice is Sent
        $this->taxInvoiceMock->expects($this->once())
            ->method('sendInvoiceRequest')
            ->willReturn($response);

        $this->shipmentExtensionLoader->expects($this->once())
            ->method('loadOnOrder')
            ->with(
                $this->callback(
                    function ($order) {
                        return $order->getId() === $this->orderId;
                    }
                )
            )
            ->willReturn($this->orderMock);

        $this->giftwrapExtensionMock->expects($this->once())
            ->method('loadOnOrder')
            ->with(
                $this->callback(
                    function ($order) {
                        return $order->getId() === $this->orderId;
                    }
                )
            )
            ->willReturn($this->orderMock);

        // Assert success message is added
        $this->managerInterfaceMock->expects($this->once())
            ->method('addSuccessMessage')
            ->with(
                'The Vertex invoice has been sent.'
            );

        // Assert OrderInvoiceStatus given correct data
        $this->orderInvoiceStatusMock->expects($this->once())
            ->method('setId')
            ->with($this->orderId);
        $this->orderInvoiceStatusMock->expects($this->once())
            ->method('setIsSent')
            ->with(true);

        // Assert OrderInvoiceStatus record saved
        $this->repositoryMock->expects($this->once())
            ->method('save')
            ->with($this->orderInvoiceStatusMock);

        $request = new Request();
        $this->invoiceRequestBuilderMock->expects($this->once())
            ->method('buildFromOrder')
            ->with(
                $this->callback(
                    function ($order) {
                        return $order->getId() === $this->orderId;
                    }
                )
            )
            ->willReturn($request);

        $observer = $this->createObserver();
        $this->orderSavedAfterObserver->execute($observer);
    }

    /**
     * Assert that a Vertex Invoice is never sent
     *
     * @return void
     */
    private function assertInvoiceNeverSent()
    {
        $this->taxInvoiceMock->expects($this->never())
            ->method('sendInvoiceRequest');
    }

    /**
     * Generate the instance of {@see OrderSavedAfterObserver}
     *
     * @return Event\Observer
     */
    private function createObserver()
    {
        $observer = $this->getObject(Event\Observer::class);
        $observer->setData('event', $observer);
        $observer->setData('order', $this->orderMock);
        return $observer;
    }

    /**
     * Ensure that taxInvoice->prepareInvoiceData always returns an array for check before sendInvoiceRequest
     *
     * @return void
     */
    private function prepareEmptyInvoiceData()
    {
        $this->taxInvoiceMock->method('prepareInvoiceData')
            ->willReturn(new Request());
    }

    /**
     * Register that Vertex can/can't service the order
     *
     * @param bool $canService
     * @return void
     */
    private function setCanService($canService = true)
    {
        $this->countryGuardMock->method('isOrderServiceableByVertex')
            ->willReturn($canService);
    }

    /**
     * Register that the Configuration is OK
     *
     * @param bool $configValid
     * @return void
     */
    private function setConfigValid($configValid = true)
    {
        $validatorResult = new ConfigurationValidator\Result();
        $validatorResult->setValid($configValid);

        $this->configValidatorMock->method('execute')
            ->willReturn($validatorResult);
    }

    /**
     * Register that an Order does not have a corresponding Vertex Invoice sent for it
     *
     * @return void
     */
    private function setHasNoInvoice()
    {
        $this->repositoryMock->method('getByOrderId')
            ->willThrowException(new NoSuchEntityException(__('No Such Entity')));
    }

    /**
     * Register that the Vertex module and tax calculation are enabled
     *
     * @return void
     */
    private function setVertexActive()
    {
        $this->configMock->method('isVertexActive')
            ->willReturn(true);

        $this->configMock->method('isTaxCalculationEnabled')
            ->willReturn(true);
    }

    /**
     * Perform setup so that the `$requestByOrder` variable is true
     *
     * Here we create an order status - give it to the order, ensure that the invoiceOrderStatus method on the config
     * will return that status, and set that the requestByOrderStatus config is true.
     *
     * @return void
     */
    private function setupRequestByOrderFlag()
    {
        $status = uniqid('order-status-', false);

        $this->configMock->method('requestByOrderStatus')
            ->willReturn(true);
        $this->configMock->method('invoiceOrderStatus')
            ->willReturn($status);

        $this->orderMock->method('getStatus')
            ->willReturn($status);
    }
}