Skip to content

Unit test, Failed asserting

I am trying to write a unit testing, but i am not sure why the actual result is not getting generated correctly. Below are my unit testing code,

<?php
declare(strict_types=1);

namespace VendorProductCustomizerTestUnitModelComponentsPricing;

use PHPUnitFrameworkTestCase;
use PHPUnitFrameworkMockObjectMockObject;
use VendorProductCustomizerModelComponentsPricingAddons;
use MagentoFrameworkAppConfigScopeConfigInterface;
use MagentoStoreModelScopeInterface;

class AddonTest extends TestCase
{
    /**
     * @var Addons
     */
    protected $addon;

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

    protected function setUp(): void
    {
        $this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
        $this->scopeConfigMock->expects($this->once())
            ->method('getValue')
            ->willReturnMap(
                [
                    [
                        'custom_frame/component_price/addon_plunge_price',
                        ScopeInterface::SCOPE_STORE,
                        8,
                        35,
                    ]
                ]
            );
        $this->addons = new Addons(
            $this->scopeConfigMock
        );
    }

    /**
     * @covers VendorProductCustomizerModelComponentsPricingAddons::getPrice
     * @return void
     */
    public function testGetPrice()
    {
        $selection = [
            'form_data' => [
                '0' => [
                    'name' => 'plunge_lock',
                    'value' => 'include'
                ],
                '1' => [
                    'name' => 'hinge_position',
                    'value' => 'left'
                ]
        ]

        ];
        if (isset($selection['form_data'])) {
            foreach ($selection['form_data'] as $formData) {
                if ($formData['name'] !== 'plunge_lock') {
                    continue;
                }
                if ($formData['value'] === 'include') {
                    $resultExpected = 35;
                }
            }
        } else {
            $resultExpected = 0;
        }
        $this->scopeConfigMock->expects($this->once())
            ->method('getValue')
            ->willReturn($resultExpected);

        $this->assertEquals($resultExpected, $this->addons->getPrice($selection));
    }
}

Below is the actual code for which i am trying to write unit testing.

<?php
declare(strict_types=1);

namespace VendorProductCustomizerModelComponentsPricing;

use MagentoCatalogApiDataProductInterface;

class Addons
{
    const ADDON_PLUNGE_CONFIG_PATH = 'custom_frame/component_price/addon_plunge_price';

    /**
     * @var MagentoFrameworkAppConfigScopeConfigInterface
     */
    protected $scopeConfig;
    /**
     * @param  MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig
     */
    public function __construct(
        MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig
    ) {
        $this->scopeConfig = $scopeConfig;
    }

    /**
     * @return float
     */
    public function getConfigPlungePrice()
    {
        return $this->scopeConfig->getValue(
            self::ADDON_PLUNGE_CONFIG_PATH,
            MagentoStoreModelScopeInterface::SCOPE_STORE
        );
    }

    /**
     * @param  []$selection of current product.
     *
     * @return float
     */
    public function getPrice($selection)
    {
        if (isset($selection['form_data'])) {
            foreach ($selection['form_data'] as $formData) {
                if ($formData['name'] !== 'plunge_lock') {
                    continue;
                }
                if ($formData['value'] === 'include') {
                    return (float)$this->getConfigPlungePrice();
                }
            }
        }
        return 0;
    }
}

But i am getting the below error.

Failed asserting that 0.0 matches expected 35.

Can anyone please explain why the $this->addons->getPrice($selection) piece of code under test files is returning 0 instead of 35.