Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
HashTest.php
Go to the documentation of this file.
1 <?php
8 
16 
17 class HashTest extends \PHPUnit\Framework\TestCase
18 {
22  private $configHashGeneratorMock;
23 
27  private $dataConfigCollectorMock;
28 
32  private $flagFactoryMock;
33 
37  private $flagResourceMock;
38 
42  private $flagMock;
43 
47  private $hash;
48 
52  protected function setUp()
53  {
54  $this->flagResourceMock = $this->getMockBuilder(FlagResource::class)
55  ->disableOriginalConstructor()
56  ->getMock();
57  $this->flagFactoryMock = $this->getMockBuilder(FlagFactory::class)
58  ->disableOriginalConstructor()
59  ->getMock();
60  $this->flagMock = $this->getMockBuilder(Flag::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63  $this->deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class)
64  ->disableOriginalConstructor()
65  ->getMock();
66  $this->configHashGeneratorMock = $this->getMockBuilder(Generator::class)
67  ->disableOriginalConstructor()
68  ->getMock();
69  $this->dataConfigCollectorMock = $this->getMockBuilder(DataCollector::class)
70  ->disableOriginalConstructor()
71  ->getMock();
72 
73  $this->hash = new Hash(
74  $this->configHashGeneratorMock,
75  $this->dataConfigCollectorMock,
76  $this->flagResourceMock,
77  $this->flagFactoryMock
78  );
79  }
80 
87  public function testGet($dataFromStorage, $expectedResult)
88  {
89  $this->flagMock->expects($this->once())
90  ->method('getFlagData')
91  ->willReturn($dataFromStorage);
92  $this->flagFactoryMock->expects($this->once())
93  ->method('create')
94  ->with(['data' => ['flag_code' => Hash::CONFIG_KEY]])
95  ->willReturn($this->flagMock);
96  $this->flagResourceMock->expects($this->once())
97  ->method('load')
98  ->with($this->flagMock, Hash::CONFIG_KEY, 'flag_code')
99  ->willReturnSelf();
100 
101  $this->assertSame($expectedResult, $this->hash->get());
102  }
103 
107  public function getDataProvider()
108  {
109  return [
110  [['section' => 'hash'], ['section' => 'hash']],
111  ['hash', ['hash']],
112  ['', []],
113  [null, []],
114  ];
115  }
116 
120  public function testRegenerate()
121  {
122  $section = 'section';
123  $config = 'some config';
124  $fullConfig = ['section' => $config];
125  $hash = 'some hash';
126  $hashes = [$section => $hash];
127 
128  $this->generalRegenerateMocks($fullConfig, $config, $hash, $hashes);
129 
130  $this->flagResourceMock->expects($this->once())
131  ->method('save')
132  ->with($this->flagMock)
133  ->willReturnSelf();
134 
135  $this->hash->regenerate();
136  }
137 
143  public function testRegenerateWithException()
144  {
145  $section = 'section';
146  $config = 'some config';
147  $fullConfig = ['section' => $config];
148  $hash = 'some hash';
149  $hashes = [$section => $hash];
150 
151  $this->generalRegenerateMocks($fullConfig, $config, $hash, $hashes, $section);
152 
153  $this->flagResourceMock->expects($this->once())
154  ->method('save')
155  ->with($this->flagMock)
156  ->willThrowException(new \Exception('Some error'));
157  $this->hash->regenerate($section);
158  }
159 
168  private function generalRegenerateMocks($fullConfig, $config, $hash, $hashes, $sectionName = null)
169  {
170  $this->dataConfigCollectorMock->expects($this->once())
171  ->method('getConfig')
172  ->with($sectionName)
173  ->willReturn($fullConfig);
174  $this->configHashGeneratorMock->expects($this->once())
175  ->method('generate')
176  ->with($config)
177  ->willReturn($hash);
178  $this->flagMock->expects($this->once())
179  ->method('setFlagData')
180  ->willReturn($hashes);
181  $this->flagFactoryMock->expects($this->exactly(2))
182  ->method('create')
183  ->with(['data' => ['flag_code' => Hash::CONFIG_KEY]])
184  ->willReturn($this->flagMock);
185  $this->flagResourceMock->expects($this->exactly(2))
186  ->method('load')
187  ->with($this->flagMock, Hash::CONFIG_KEY, 'flag_code');
188  }
189 }
$config
Definition: fraud_order.php:17
testGet($dataFromStorage, $expectedResult)
Definition: HashTest.php:87