Как бы вы реорганизовали такой код с помощью нескольких операторов if-else if?

У меня есть класс ParentIdResolver который возвращает родительский идентификатор продукта в зависимости от типа.
Класс выглядит так:

<?php

namespace AppModel;

use AppModelProductBundle;
use AppModelProductConfigurable;
use AppModelProductDownloadable;

class ParentIdResolver
{
    /**
     * @var Bundle
     */
    private $bundle;
    /**
     * @var Configurable
     */
    private $configurable;
    /**
     * @var Downloadable
     */
    private $downloadable;

    public function __construct(
        Bundle $bundle,
        Configurable $configurable,
        Downloadable $downloadable
    ) {
        $this->bundle = $bundle;
        $this->configurable = $configurable;
        $this->downloadable = $downloadable;
    }

    public function getParentId($productId, $productType)
    {
        $parentIds = [];
        if ($productType == 'bundle') {
            $parentIds = $this->bundle->getParentIdsByChild($productId);
        } elseif ($productType == 'configurable') {
            $parentIds = $this->configurable->getParentIdsByChild($productId);
        } elseif ($productType == '') {
            $parentIds = $this->downloadable->getParentIdsByChild($productId);
        }
        return $parentIds[0] ?? null;
    }
}

И я пытаюсь проверить getParentId() в качестве:

<?php

namespace AppTestUnit;

use PHPUnitFrameworkTestCase;
use AppModelParentIdResolver;
use AppModelProductBundle;
use AppModelProductConfigurable;
use AppModelProductDownloadable;

class ParentIdResolverTest extends TestCase
{
    protected $model;
    protected $bundleMock;
    protected $configurableMock;
    protected $downloadableMock;

    public function setUp(): void
    {
        $this->bundleMock = $this->createPartialMock(
            Bundle::class,
            ['getParentIdsByChild']
        );
        $this->configurableMock = $this->createPartialMock(
            Configurable::class,
            ['getParentIdsByChild']
        );
        $this->downloadableMock = $this->createPartialMock(
            Downloadable::class,
            ['getParentIdsByChild']
        );

        $this->model = new ParentIdResolver(
            $this->bundleMock,
            $this->configurableMock,
            $this->downloadableMock
        );
    }

    /**
     * @dataProvider getParentIdDataProvider
     */
    public function testGetParentId($productId, $productType, $parentId)
    {
        if ($productType == 'bundle') {
            $this->bundleMock->expects($this->any())
                ->method('getParentIdsByChild')
                ->willReturn([$parentId]);
        }
        if ($productType == 'configurable') {
            $this->configurableMock->expects($this->any())
                ->method('getParentIdsByChild')
                ->willReturn([$parentId]);
        }
        if ($productType == 'downloadable') {
            $this->downloadableMock->expects($this->any())
                ->method('getParentIdsByChild')
                ->willReturn([$parentId]);
        }

        $this->assertEquals($parentId, $this->model->getParentId($productId, $productType));
    }

    public function getParentIdDataProvider()
    {
        return [
            [1, 'bundle', 11],
            [2, 'configurable', 22],
            [3, 'downloadable', 33],
        ];
    }
}

И я не чувствую, что делаю это правильно, может, мне нужно реорганизовать основной класс или модульный тест?
Подскажите, пожалуйста, как бы вы реорганизовали или написали модульный тест в этом случае.

0

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *