StatusResolverTest.php 3.09 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Sales\Test\Unit\Model\Order;

use Magento\Payment\Model\MethodInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderPaymentInterface;
use Magento\Sales\Model\Order\Config;
use Magento\Sales\Model\Order\StatusResolver;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

class StatusResolverTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @param OrderInterface|MockObject $order
     * @param string $expectedReturn
     *
     * @dataProvider statesDataProvider
     */
    public function testGetOrderStatusByState($order, $expectedReturn)
    {
        $actualReturn = (new StatusResolver())->getOrderStatusByState($order, 'new');

        self::assertEquals($expectedReturn, $actualReturn);
    }

    /**
     * @return array
     */
    public function statesDataProvider()
    {
        return [
            [
                $this->getOrder('pending', ['pending' => 'pending']),
                'pending'
            ],
            [
                $this->getOrder('processing', ['pending' => 'pending']),
                'processing'
            ],
        ];
    }

    /**
     * @param string $newOrderStatus
     * @param array $stateStatuses
     * @return OrderInterface|MockObject
     */
    private function getOrder($newOrderStatus, $stateStatuses)
    {
        $order = $this->getMockBuilder(OrderInterface::class)
            ->setMethods(['getConfig'])
            ->getMockForAbstractClass();
        $order->method('getPayment')
            ->willReturn($this->getPayment($newOrderStatus));
        $order->method('getConfig')
            ->willReturn($this->getConfig($stateStatuses));

        return $order;
    }

    /**
     * @param string $newOrderStatus
     * @return MockObject
     */
    private function getPayment($newOrderStatus)
    {
        $payment = $this->getMockBuilder(OrderPaymentInterface::class)
            ->setMethods(['getMethodInstance'])
            ->getMockForAbstractClass();
        $payment->method('getMethodInstance')
            ->willReturn($this->getMethodInstance($newOrderStatus));

        return $payment;
    }

    /**
     * @param string $newOrderStatus
     * @return MethodInterface|MockObject
     */
    private function getMethodInstance($newOrderStatus)
    {
        $methodInstance = $this->getMockBuilder(MethodInterface::class)
            ->getMockForAbstractClass();
        $methodInstance->method('getConfigData')
            ->with('order_status')
            ->willReturn($newOrderStatus);

        return $methodInstance;
    }

    /**
     * @param array $stateStatuses
     * @return Config|MockObject
     */
    private function getConfig($stateStatuses)
    {
        $config = $this->getMockBuilder(Config::class)
            ->disableOriginalConstructor()
            ->getMock();
        $config->method('getStateStatuses')
            ->willReturn($stateStatuses);
        $config->method('getStateDefaultStatus')
            ->willReturn('processing');

        return $config;
    }
}