|
| 1 | +<?php |
| 2 | +/*** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Variable\Test\Unit\Model\Variable; |
| 10 | + |
| 11 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use Magento\Variable\Model\Variable\Data as VariableDataModel; |
| 14 | +use Magento\Variable\Model\ResourceModel\Variable\CollectionFactory as VariableCollectionFactory; |
| 15 | +use Magento\Variable\Model\ResourceModel\Variable\Collection as VariableCollection; |
| 16 | +use Magento\Variable\Model\Source\Variables as StoreVariables; |
| 17 | + |
| 18 | +class DataTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var VariableDataModel |
| 22 | + */ |
| 23 | + private $model; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var ObjectManagerHelper |
| 27 | + */ |
| 28 | + private $objectManagerHelper; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var StoreVariables|PHPUnit_Framework_MockObject_MockObject |
| 32 | + */ |
| 33 | + private $storesVariablesMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var VariableCollectionFactory|PHPUnit_Framework_MockObject_MockObject |
| 37 | + */ |
| 38 | + private $variableCollectionFactoryMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * Set up before tests |
| 42 | + */ |
| 43 | + protected function setUp() |
| 44 | + { |
| 45 | + $this->storesVariablesMock = $this->createMock(StoreVariables::class); |
| 46 | + $this->variableCollectionFactoryMock = $this->getMockBuilder( |
| 47 | + VariableCollectionFactory::class |
| 48 | + )->disableOriginalConstructor()->setMethods(['create'])->getMock(); |
| 49 | + |
| 50 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 51 | + $this->model = $this->objectManagerHelper->getObject( |
| 52 | + VariableDataModel::class, |
| 53 | + [ |
| 54 | + 'collectionFactory' => $this->variableCollectionFactoryMock, |
| 55 | + 'storesVariables' => $this->storesVariablesMock |
| 56 | + ] |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Test getDefaultVariables() function |
| 62 | + */ |
| 63 | + public function testGetDefaultVariables() |
| 64 | + { |
| 65 | + $storesVariablesData = [ |
| 66 | + [ |
| 67 | + 'value' => 'test 1', |
| 68 | + 'label' => 'Test Label 1', |
| 69 | + 'group_label' => 'Group Label 1' |
| 70 | + ], |
| 71 | + [ |
| 72 | + 'value' => 'test 2', |
| 73 | + 'label' => 'Test Label 2', |
| 74 | + 'group_label' => 'Group Label 2' |
| 75 | + ] |
| 76 | + ]; |
| 77 | + $expectedResult = [ |
| 78 | + [ |
| 79 | + 'code' => 'test 1', |
| 80 | + 'variable_name' => 'Group Label 1 / Test Label 1', |
| 81 | + 'variable_type' => StoreVariables::DEFAULT_VARIABLE_TYPE |
| 82 | + ], |
| 83 | + [ |
| 84 | + 'code' => 'test 2', |
| 85 | + 'variable_name' => 'Group Label 2 / Test Label 2', |
| 86 | + 'variable_type' => StoreVariables::DEFAULT_VARIABLE_TYPE |
| 87 | + ] |
| 88 | + ]; |
| 89 | + $this->storesVariablesMock->expects($this->any())->method('getData')->willReturn($storesVariablesData); |
| 90 | + |
| 91 | + $this->assertEquals($expectedResult, $this->model->getDefaultVariables()); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Test getCustomVariables() function |
| 96 | + */ |
| 97 | + public function testGetCustomVariables() |
| 98 | + { |
| 99 | + $customVariables = [ |
| 100 | + [ |
| 101 | + 'code' => 'test 1', |
| 102 | + 'name' => 'Test 1' |
| 103 | + ], |
| 104 | + [ |
| 105 | + 'code' => 'test 2', |
| 106 | + 'name' => 'Test 2' |
| 107 | + ] |
| 108 | + ]; |
| 109 | + $expectedResult = [ |
| 110 | + [ |
| 111 | + 'code' => 'test 1', |
| 112 | + 'variable_name' => 'Custom Variable / Test 1', |
| 113 | + 'variable_type' => StoreVariables::CUSTOM_VARIABLE_TYPE |
| 114 | + ], |
| 115 | + [ |
| 116 | + 'code' => 'test 2', |
| 117 | + 'variable_name' => 'Custom Variable / Test 2', |
| 118 | + 'variable_type' => StoreVariables::CUSTOM_VARIABLE_TYPE |
| 119 | + ] |
| 120 | + ]; |
| 121 | + $variableCollectionMock = $this->createMock(VariableCollection::class); |
| 122 | + $this->variableCollectionFactoryMock->expects($this->once())->method('create') |
| 123 | + ->willReturn($variableCollectionMock); |
| 124 | + $variableCollectionMock->expects($this->any())->method('getData')->willReturn($customVariables); |
| 125 | + |
| 126 | + $this->assertEquals($expectedResult, $this->model->getCustomVariables()); |
| 127 | + } |
| 128 | +} |
0 commit comments