AttributeMergerTest.php 3.13 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\Checkout\Test\Unit\Block\Checkout;

use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
use Magento\Customer\Helper\Address as AddressHelper;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Directory\Helper\Data as DirectoryHelper;
use Magento\Checkout\Block\Checkout\AttributeMerger;
use PHPUnit\Framework\TestCase;

class AttributeMergerTest extends TestCase
{
    /**
     * @var CustomerRepository
     */
    private $customerRepository;

    /**
     * @var CustomerSession
     */
    private $customerSession;

    /**
     * @var AddressHelper
     */
    private $addressHelper;

    /**
     * @var DirectoryHelper
     */
    private $directoryHelper;

    /**
     * @var AttributeMerger
     */
    private $attributeMerger;

    /**
     * @inheritdoc
     */
    protected function setUp()
    {

        $this->customerRepository = $this->createMock(CustomerRepository::class);
        $this->customerSession = $this->createMock(CustomerSession::class);
        $this->addressHelper = $this->createMock(AddressHelper::class);
        $this->directoryHelper = $this->createMock(DirectoryHelper::class);

        $this->attributeMerger = new AttributeMerger(
            $this->addressHelper,
            $this->customerSession,
            $this->customerRepository,
            $this->directoryHelper
        );
    }

    /**
     * Tests of element attributes merging.
     *
     * @param String $validationRule - validation rule.
     * @param String $expectedValidation - expected mapped validation.
     * @dataProvider validationRulesDataProvider
     */
    public function testMerge(String $validationRule, String $expectedValidation): void
    {
        $elements = [
            'field' => [
                'visible' => true,
                'formElement' => 'input',
                'label' => __('City'),
                'value' =>  null,
                'sortOrder' => 1,
                'validation' => [
                    'input_validation' => $validationRule
                ],
            ]
        ];

        $actualResult = $this->attributeMerger->merge(
            $elements,
            'provider',
            'dataScope',
            ['field' =>
                [
                    'validation' => ['length' => true]
                ]
            ]
        );

        $expectedResult = [
            $expectedValidation => true,
            'length' => true
        ];

        self::assertEquals($expectedResult, $actualResult['field']['validation']);
    }

    /**
     * Provides possible validation types.
     *
     * @return array
     */
    public function validationRulesDataProvider(): array
    {
        return [
            ['alpha', 'validate-alpha'],
            ['numeric', 'validate-number'],
            ['alphanumeric', 'validate-alphanum'],
            ['alphanum-with-spaces', 'validate-alphanum-with-spaces'],
            ['url', 'validate-url'],
            ['email', 'email2'],
            ['length', 'validate-length']
        ];
    }
}