0

I am new in writing unit testing in Magento 2. I want to write a test case for getting an order by the order_id.

Below is my model function:

/**
 * Initialize dependencies
 * @param \Magento\Framework\Serialize\Serializer\Json $json
 * @param  Data $helper
 * @param \Magento\Framework\HTTP\Client\Curl $curl
 * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
 * @param Helper $websiteCheck
 * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
 */
public function __construct(
    Json $json,
    Helper $helper,
    Curl $curl,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    Helper $websiteCheck,
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
) {
    $this->json = $json;
    $this->helper = $helper;
    $this->curl = $curl;
    $this->_scopeConfig = $scopeConfig;
    $this->_websiteCheck = $websiteCheck;
    $this->orderRepository = $orderRepository;
    $this->_storeId = $this->_websiteCheck->getStoreId();
}

public function updateOrderStatus($orderId)
    {    
       if ($orderId) {
            $order = $this->orderRepository->get($orderId);
            $order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING);
            $order->setStatus(self::SENT_TO_WAREHOUSE);
            $comment = "Order Status Changed to Sent to Warehouse";
            try {
                $order->addStatusToHistory(
                    self::SENT_TO_WAREHOUSE,
                    $comment
                );
                $this->orderRepository->save($order);
} catch (\Exception $e) {
            }
}

I have used \Magento\Sales\Api\OrderRepositoryInterface $orderRepository repository,

Below is my test case functions:

use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;

protected function setup(): void
    {
           $this->_orderRepositoryInterface = $this->getMockBuilder(OrderRepositoryInterface::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->orderMock = $this->getMockBuilder(OrderInterface::class)
            ->disableOriginalConstructor()
            ->getMockForAbstractClass();

          $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);

          $this->model = $this->objectManager->getObject(
            \ModelClassName::class,
            [
                'helper' => $this->helper,
                'json' => $this->json,
                'orderRepositoryInterface' => $this->_orderRepositoryInterface,
                'orderInterface' => $this->_orderInterface,
                'orderFactory' => $this->orderFactory
            ]
        );
}

public function testUpdateOrderStatus()
{
    $orderID = '57956';
    $this->_orderRepositoryInterface->expects($this->once())
        ->method('get')
        ->willReturn($this->orderMock);

    $this->orderMock->expects($this->once())
        ->method('setState')->with("processing")
        ->willReturnSelf();

    $this->orderMock->expects($this->once())
        ->method('setStatus')->with("sent_to_warehouse")
        ->willReturnSelf();

    $this->_orderRepositoryInterface->expects($this->once())
        ->method('save')
        ->with($this->orderMock)
        ->willReturnSelf();

    $this->assertEquals(true, $this->model->updateOrderStatus($orderID));

$this->model is my original model class for which i am writing Test Unit. I am receiving error like below :

Error: Call to a member function setState() on null

As i said i am new in writing unit tests, so if anyone can get idea from above code then it will helpful for me.

Thanks in advance.

1 Answer 1

0

You have to add $this->orderMock as you added orderRepositoryInterface' => $this->_orderRepositoryInterface in setup:void funntion constructor.

3
  • I have already tried that $this->orderMock passed into Constructor in Test class's Setup but it didn't worked for me.
    – Rik Patel
    Jan 2, 2023 at 8:54
  • Can you please send original class constructor?
    – Ankit
    Jan 2, 2023 at 9:12
  • I have added constructor in the original question, can you please check that and let me know what i am doing wrong.
    – Rik Patel
    Jan 2, 2023 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.