<?php
/**
 * Copyright 2015 Adobe
 * All Rights Reserved.
 */
declare(strict_types=1);

namespace Magento\Catalog\Test\Unit\Block\Category\Rss;

use PHPUnit\Framework\Attributes\DataProvider;
use Magento\Catalog\Block\Category\Rss\Link;
use Magento\Catalog\Model\Category;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Rss\UrlBuilderInterface;
use Magento\Framework\Registry;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Store\Model\StoreManagerInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class LinkTest extends TestCase
{
    /**
     * @var Link
     */
    protected $link;

    /**
     * @var ObjectManagerHelper
     */
    protected $objectManagerHelper;

    /**
     * @var UrlBuilderInterface|MockObject
     */
    protected $urlBuilderInterface;

    /**
     * @var ScopeConfigInterface|MockObject
     */
    protected $scopeConfigInterface;

    /**
     * @var StoreManagerInterface|MockObject
     */
    protected $storeManagerInterface;

    /**
     * @var Registry|MockObject
     */
    protected $registry;

    protected function setUp(): void
    {
        $this->urlBuilderInterface = $this->createMock(UrlBuilderInterface::class);
        $this->scopeConfigInterface = $this->createMock(ScopeConfigInterface::class);
        $this->storeManagerInterface = $this->createMock(StoreManagerInterface::class);
        $this->registry = $this->createMock(Registry::class);

        $this->objectManagerHelper = new ObjectManagerHelper($this);
        $this->link = $this->objectManagerHelper->getObject(
            Link::class,
            [
                'rssUrlBuilder' => $this->urlBuilderInterface,
                'registry' => $this->registry,
                'scopeConfig' => $this->scopeConfigInterface,
                'storeManager' => $this->storeManagerInterface
            ]
        );
    }

    /**
     * @param bool $isAllowed
     */
    #[DataProvider('isRssAllowedDataProvider')]
    public function testIsRssAllowed($isAllowed)
    {
        $this->scopeConfigInterface->expects($this->once())->method('getValue')->willReturn($isAllowed);
        $this->assertEquals($isAllowed, $this->link->isRssAllowed());
    }

    /**
     * @return array
     */
    public static function isRssAllowedDataProvider()
    {
        return [
            [true],
            [false]
        ];
    }

    public function testGetLabel()
    {
        $this->assertEquals('Subscribe to RSS Feed', $this->link->getLabel());
    }

    /**
     * @param bool $isTop
     * @param string $categoryLevel
     */
    #[DataProvider('isTopCategoryDataProvider')]
    public function testIsTopCategory($isTop, $categoryLevel)
    {
        $categoryModel = $this->createPartialMock(Category::class, [ 'getLevel']);
        $this->registry->expects($this->once())->method('registry')->willReturn($categoryModel);
        $categoryModel->method('getLevel')->willReturn($categoryLevel);
        $this->assertEquals($isTop, $this->link->isTopCategory());
    }

    /**
     * @return array
     */
    public static function isTopCategoryDataProvider()
    {
        return [
            [true, '2'],
            [false, '1']
        ];
    }

    public function testGetLink()
    {
        $rssUrl = 'http://rss.magento.com';
        $this->urlBuilderInterface->expects($this->once())->method('getUrl')->willReturn($rssUrl);

        $categoryModel = $this->createPartialMock(Category::class, [ 'getId']);
        $this->registry->expects($this->once())->method('registry')->willReturn($categoryModel);
        $categoryModel->method('getId')->willReturn('1');

        $storeModel = $this->createPartialMock(Category::class, [ 'getId']);
        $this->storeManagerInterface->method('getStore')->willReturn($storeModel);
        $storeModel->method('getId')->willReturn('1');

        $this->assertEquals($rssUrl, $this->link->getLink());
    }
}
