HashTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Test\Unit\Model\DeploymentConfig;
  7. use Magento\Deploy\Model\DeploymentConfig\DataCollector;
  8. use Magento\Deploy\Model\DeploymentConfig\Hash;
  9. use Magento\Deploy\Model\DeploymentConfig\Hash\Generator;
  10. use Magento\Framework\App\DeploymentConfig;
  11. use Magento\Framework\Flag;
  12. use Magento\Framework\Flag\FlagResource;
  13. use Magento\Framework\FlagFactory;
  14. class HashTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var Generator|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $configHashGeneratorMock;
  20. /**
  21. * @var DataCollector|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $dataConfigCollectorMock;
  24. /**
  25. * @var FlagFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $flagFactoryMock;
  28. /**
  29. * @var FlagResource|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $flagResourceMock;
  32. /**
  33. * @var Flag|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $flagMock;
  36. /**
  37. * @var Hash
  38. */
  39. private $hash;
  40. /**
  41. * @return void
  42. */
  43. protected function setUp()
  44. {
  45. $this->flagResourceMock = $this->getMockBuilder(FlagResource::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->flagFactoryMock = $this->getMockBuilder(FlagFactory::class)
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->flagMock = $this->getMockBuilder(Flag::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->deploymentConfigMock = $this->getMockBuilder(DeploymentConfig::class)
  55. ->disableOriginalConstructor()
  56. ->getMock();
  57. $this->configHashGeneratorMock = $this->getMockBuilder(Generator::class)
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $this->dataConfigCollectorMock = $this->getMockBuilder(DataCollector::class)
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $this->hash = new Hash(
  64. $this->configHashGeneratorMock,
  65. $this->dataConfigCollectorMock,
  66. $this->flagResourceMock,
  67. $this->flagFactoryMock
  68. );
  69. }
  70. /**
  71. * @param string|array|null $dataFromStorage
  72. * @param array $expectedResult
  73. * @return void
  74. * @dataProvider getDataProvider
  75. */
  76. public function testGet($dataFromStorage, $expectedResult)
  77. {
  78. $this->flagMock->expects($this->once())
  79. ->method('getFlagData')
  80. ->willReturn($dataFromStorage);
  81. $this->flagFactoryMock->expects($this->once())
  82. ->method('create')
  83. ->with(['data' => ['flag_code' => Hash::CONFIG_KEY]])
  84. ->willReturn($this->flagMock);
  85. $this->flagResourceMock->expects($this->once())
  86. ->method('load')
  87. ->with($this->flagMock, Hash::CONFIG_KEY, 'flag_code')
  88. ->willReturnSelf();
  89. $this->assertSame($expectedResult, $this->hash->get());
  90. }
  91. /**
  92. * @return array
  93. */
  94. public function getDataProvider()
  95. {
  96. return [
  97. [['section' => 'hash'], ['section' => 'hash']],
  98. ['hash', ['hash']],
  99. ['', []],
  100. [null, []],
  101. ];
  102. }
  103. /**
  104. * @return void
  105. */
  106. public function testRegenerate()
  107. {
  108. $section = 'section';
  109. $config = 'some config';
  110. $fullConfig = ['section' => $config];
  111. $hash = 'some hash';
  112. $hashes = [$section => $hash];
  113. $this->generalRegenerateMocks($fullConfig, $config, $hash, $hashes);
  114. $this->flagResourceMock->expects($this->once())
  115. ->method('save')
  116. ->with($this->flagMock)
  117. ->willReturnSelf();
  118. $this->hash->regenerate();
  119. }
  120. /**
  121. * @return void
  122. * @expectedException \Magento\Framework\Exception\LocalizedException
  123. * @expectedExceptionMessage The hash isn't saved.
  124. */
  125. public function testRegenerateWithException()
  126. {
  127. $section = 'section';
  128. $config = 'some config';
  129. $fullConfig = ['section' => $config];
  130. $hash = 'some hash';
  131. $hashes = [$section => $hash];
  132. $this->generalRegenerateMocks($fullConfig, $config, $hash, $hashes, $section);
  133. $this->flagResourceMock->expects($this->once())
  134. ->method('save')
  135. ->with($this->flagMock)
  136. ->willThrowException(new \Exception('Some error'));
  137. $this->hash->regenerate($section);
  138. }
  139. /**
  140. * @param array $fullConfig
  141. * @param string $config
  142. * @param string $hash
  143. * @param array $hashes
  144. * @param string|null $sectionName
  145. * @return void
  146. */
  147. private function generalRegenerateMocks($fullConfig, $config, $hash, $hashes, $sectionName = null)
  148. {
  149. $this->dataConfigCollectorMock->expects($this->once())
  150. ->method('getConfig')
  151. ->with($sectionName)
  152. ->willReturn($fullConfig);
  153. $this->configHashGeneratorMock->expects($this->once())
  154. ->method('generate')
  155. ->with($config)
  156. ->willReturn($hash);
  157. $this->flagMock->expects($this->once())
  158. ->method('setFlagData')
  159. ->willReturn($hashes);
  160. $this->flagFactoryMock->expects($this->exactly(2))
  161. ->method('create')
  162. ->with(['data' => ['flag_code' => Hash::CONFIG_KEY]])
  163. ->willReturn($this->flagMock);
  164. $this->flagResourceMock->expects($this->exactly(2))
  165. ->method('load')
  166. ->with($this->flagMock, Hash::CONFIG_KEY, 'flag_code');
  167. }
  168. }