0

Am finding difficult to unit test my custom functions. Below is my custom module's model function which i used for fetching cart informations.

<?php
namespace Namespace\MiniCart\Model;

use Namespace\MiniCart\Api\CartInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Quote\Model\Quote;

class Cartupdate implements CartInterface
{

/**
 * @var \Magento\Customer\Model\Session
 */
protected $_customerSession;

/**
 * @var \Magento\Quote\Api\CartRepositoryInterface
 */
protected $quoteRepository;

/**
 * @var \Magento\Checkout\Model\Cart
 */
protected $_cart;
protected $_product;
protected $_formkey;
protected $_storemanager;

public function __construct(
     \Magento\Customer\Model\Session $customerSession,
     \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
     \Magento\Checkout\Model\Cart $cart,
     \Magento\Catalog\Model\Product $product,
     \Magento\Framework\Data\Form\FormKey $formkey,
     \Magento\Store\Model\StoreManagerInterface $storemanager,
     \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
)
{
    $this->_customerSession = $customerSession;
    $this->_quoteRepository = $quoteRepository;
    $this->_cart = $cart;
    $this->_product = $product;
    $this->_formkey = $formkey;
    $this->_storemanager = $storemanager;
    $this->priceCurrency = $priceCurrency;
}

public function object() {

    $itemsVisible = $this->_cart->getQuote()->getAllVisibleItems();
    $items        = $this->_cart->getQuote()->getAllItems();
    $subtotal     = $this->priceCurrency->format($this->_cart->getQuote()->getSubtotal(), true, 0);
    $totalItems   = round($this->_cart->getQuote()->getItemsQty());
    $cart_details = array();
    $cartList     = array();
    foreach($itemsVisible as $item) {
        $proid     = $item->getProductId();
        $item_id   = $item->getItemId();
        $item_sku  = $item->getSku();
        $item_name = $item->getName();
        $item_qty  = $item->getQty();
        $item_rate = $item->getPrice() * $item_qty;
        $item_price= $item_price= $this->priceCurrency->format($item_rate, true, 0);
        $image_url = $this->getProductImageUrl($proid);
        $url_key   = $this->_storemanager->getStore()->getBaseUrl() . $this->getProductCollection($proid)->getUrlKey() . ".html";
        $cart_details[$proid] = array
              (
              "pro_id"     => $proid,
              "item_id"    => $item_id,
              "item_sku"   => $item_sku,
              "item_name"  => $item_name,
              "item_qty"   => $item_qty,
              "item_price" => $item_price,
              "imageUrl"   => $image_url,
              "url_key"    => $url_key
              );
    }
    $cartList['products']   = $cart_details;
    $cartList['subtotal']   = $subtotal;
    $cartList['totalItems'] = $totalItems;

    return json_encode($cartList);

}
}

And my Unit test function is

<?php


namespace Namespace\MiniCart\Test\Unit\Model;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class MinicartObjectTest extends  \PHPUnit\Framework\TestCase {


protected $_desiredResult;
protected $_actulResult;


/**
 * unset the variables and objects after use
 *
 * @return void
 */
public function tearDown() {

}

/**
 * used to set the values to variables or objects.
 *
 * @return void
 */
public function setUp() {

    $this->customerSessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
        ->disableOriginalConstructor()
        ->getMock();
    $this->cartRepositoryInterface = $this->getMockBuilder(\Magento\Quote\Api\CartRepositoryInterface::class)
        ->disableOriginalConstructor()
        ->getMock();
    $this->cart = $this->getMockBuilder(\Magento\Checkout\Model\Cart::class)
        ->disableOriginalConstructor()
        ->getMock();
    $this->product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
        ->disableOriginalConstructor()
        ->getMock();
    $this->formKey = $this->getMockBuilder(\Magento\Framework\Data\Form\FormKey::class)
        ->disableOriginalConstructor()
        ->getMock();
    $this->storeManagerInterface = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
        ->disableOriginalConstructor()
        ->getMock();
    $this->priceCurrencyInterface = $this->getMockBuilder(\Magento\Framework\Pricing\PriceCurrencyInterface::class)
        ->disableOriginalConstructor()
        ->getMock();
    $this->sourceModel = new \Namespace\MiniCart\Model\Cartupdate(
            $this->customerSessionMock,
            $this->cartRepositoryInterface,
            $this->cart,
            $this->product,
            $this->formKey,
            $this->storeManagerInterface,
            $this->priceCurrencyInterface
        );
}


public function testCartObject() {

    $this->_actulResult = $this->sourceModel->object();
    $this->_desiredResult = "";
    $this->assertEquals($this->_desiredResult, $this->_actulResult);

}

}

It throws me

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

And also help me how can i assert my test function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.