Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UseConfigSettingsTest.php
Go to the documentation of this file.
1 <?php
7 
13 
14 class UseConfigSettingsTest extends \PHPUnit\Framework\TestCase
15 {
19  private $objectManagerHelper;
20 
24  private $contextMock;
25 
29  private $serializerMock;
30 
34  private $useConfigSettings;
35 
39  private $jsonValidatorMock;
40 
41  protected function setUp()
42  {
43  $this->objectManagerHelper = new ObjectManagerHelper($this);
44  $this->contextMock = $this->createMock(\Magento\Framework\View\Element\UiComponent\ContextInterface::class);
45  $this->serializerMock = $this->createMock(Json::class);
46  $this->jsonValidatorMock = $this->getMockBuilder(\Magento\Framework\Serialize\JsonValidator::class)
47  ->disableOriginalConstructor()
48  ->getMock();
49  $this->useConfigSettings = $this->objectManagerHelper->getObject(
50  UseConfigSettings::class,
51  [
52  'context' => $this->contextMock,
53  'serializer' => $this->serializerMock,
54  'jsonValidator' => $this->jsonValidatorMock
55  ]
56  );
57  }
58 
59  public function testPrepare()
60  {
61  $processorMock = $this->createMock(\Magento\Framework\View\Element\UiComponent\Processor::class);
62  $processorMock->expects($this->atLeastOnce())->method('register');
63  $this->contextMock->expects($this->atLeastOnce())->method('getProcessor')->willReturn($processorMock);
64  $config = ['valueFromConfig' => 123];
65  $this->useConfigSettings->setData('config', $config);
66  $this->useConfigSettings->prepare();
67  $this->assertEquals($config, $this->useConfigSettings->getData('config'));
68  }
69 
77  public function testPrepareSource(
78  array $expectedResult,
79  $sourceValue,
80  $serializedCalledNum = 0,
81  $isValidCalledNum = 0
82  ) {
83  $processorMock = $this->createMock(\Magento\Framework\View\Element\UiComponent\Processor::class);
84  $processorMock->expects($this->atLeastOnce())->method('register');
85  $this->contextMock->expects($this->atLeastOnce())->method('getProcessor')->willReturn($processorMock);
87  $source = $this->createMock(ValueSourceInterface::class);
88  $source->expects($this->once())
89  ->method('getValue')
90  ->with($expectedResult['keyInConfiguration'])
91  ->willReturn($sourceValue);
92 
93  $this->serializerMock->expects($this->exactly($serializedCalledNum))
94  ->method('unserialize')
95  ->with($sourceValue)
96  ->willReturn($expectedResult['valueFromConfig']);
97 
98  $this->jsonValidatorMock->expects($this->exactly($isValidCalledNum))
99  ->method('isValid')
100  ->willReturn(true);
101 
102  $config = array_replace($expectedResult, ['valueFromConfig' => $source]);
103  $this->useConfigSettings->setData('config', $config);
104  $this->useConfigSettings->prepare();
105 
106  $this->assertEquals($expectedResult, $this->useConfigSettings->getData('config'));
107  }
108 
112  public function prepareSourceDataProvider()
113  {
114  return [
115  'valid' => [
116  'expectedResult' => [
117  'valueFromConfig' => 2,
118  'keyInConfiguration' => 'validKey'
119  ],
120  'sourceValue' => 2
121  ],
122  'serialized' => [
123  'expectedResult' => [
124  'valueFromConfig' => ['32000' => 3],
125  'keyInConfiguration' => 'serializedKey',
126  'unserialized' => true
127  ],
128  'sourceValue' => '{"32000":3}',
129  'serialziedCalledNum' => 1,
130  'isValidCalledNum' => 1
131  ]
132  ];
133  }
134 }
$config
Definition: fraud_order.php:17
$source
Definition: source.php:23