1

I have a Helper class and a Model class, which looks like

<?php
# File: app/code/MagePsycho/Catalog/Helper/Data.php

namespace MagePsycho\Catalog\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\StoreManagerInterface;

class Data extends AbstractHelper
{
    /**
     * @var StoreManagerInterface
     */
    protected $storeManager;

    public function __construct(
        Context $context,
        StoreManagerInterface $storeManager
    ) {
        $this->storeManager = $storeManager;

        parent::__construct($context);
    }

    public function getUrl($route, $params = [])
    {
        return $this->_getUrl($route, $params);
    }

    // Other helper methods...
}

and

<?php
# File: app/code/MagePsycho/Catalog/Model/UrlResolver.php

namespace MagePsycho\Catalog\Model;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Helper\Product as ProductHelper;
use MagePsycho\Catalog\Helper\Data as Helper;

class UrlResolver
{
    /**
     * @var Helper
     */
    private $helper;

    /**
     * @var ProductHelper
     */
    private $productHelper;

    /**
     * @var ProductRepositoryInterface
     */
    private $productRepository;

    public function __construct(
        Helper $helper,
        ProductHelper $productHelper,
        ProductRepositoryInterface $productRepository
    ) {
        $this->helper = $helper;
        $this->productHelper = $productHelper;
        $this->productRepository = $productRepository;
    }

    public function getDefaultUrl($keyword)
    {
        return $this->helper->getUrl('catalogsearch/result', ['_secure' => true]) . '?q=' . $keyword;
    }

    public function getUrlBySku($sku)
    {
        $product = $this->productRepository->get($sku);
        if (!$product || !$product->getId()) {
            return false;
        }

        if (!$this->productHelper->canShow($product)) {
            return false;
        }

        return $product->getProductUrl();
    }
}

Now I want to test the getDefaultUrl() and getUrlBySku() methods of UrlResolver.
And I tried to test getDefaultUrl() as

<?php
# File: app/code/MagePsycho/Catalog/Test/Unit/Model/UrlResolverTest.php

namespace MagePsycho\Catalog\Test\Unit\Model;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Helper\Product as ProductHelper;
use MagePsycho\Catalog\Helper\Data as Helper;
use MagePsycho\Catalog\Model\UrlResolver;
use PHPUnit\Framework\TestCase;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;

class UrlResolverTest extends TestCase
{
    /**
     * @var UrlResolver
     */
    protected $model;

    public function setUp(): void
    {
        $objectManager = new ObjectManagerHelper($this);
        $productRepository = $this->getMockBuilder(ProductRepositoryInterface::class)
            ->disableOriginalConstructor()
            ->getMockForAbstractClass();
        $productHelper = $this->getMockBuilder(ProductHelper::class)
            ->disableOriginalConstructor()
            ->getMock();

        $arguments = $objectManager->getConstructArguments(Helper::class);
        $helper = $objectManager->getObject(Helper::class, $arguments);
        //$helper = $this->createMock(Helper::class);

        $this->model = $objectManager->getObject(
            UrlResolver::class,
            [
                'helper' => $helper,
                'productHelper' => $productHelper,
                'productRepository' => $productRepository,
            ]
        );
    }

    /**
     * @dataProvider defaultUrlDataProvider
     */
    public function testGetDefaultUrl($sku, $url)
    {
        $this->assertStringContainsString($url, $this->model->getDefaultUrl($sku));
    }

    public function defaultUrlDataProvider(): array
    {
        return [
            ['sku1', 'catalogsearch/result?q=sku1'],
            ['sku2', 'catalogsearch/result?q=sku2'],
        ];
    }
}

But $this->model->getDefaultUrl($sku) always results in ?q=$sku without the URL part.
I feel that Helper dependency injection is not working properly.

Can you please let me know how should I test getDefaultUrl() and getUrlBySku() methods of UrlResolver?

2

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.