FileInfoManagerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Test\Unit\Model;
  7. use Magento\Analytics\Model\FileInfo;
  8. use Magento\Analytics\Model\FileInfoFactory;
  9. use Magento\Analytics\Model\FileInfoManager;
  10. use Magento\Framework\FlagManager;
  11. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  12. class FileInfoManagerTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var FlagManager|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $flagManagerMock;
  18. /**
  19. * @var FileInfoFactory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $fileInfoFactoryMock;
  22. /**
  23. * @var FileInfo|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $fileInfoMock;
  26. /**
  27. * @var ObjectManagerHelper
  28. */
  29. private $objectManagerHelper;
  30. /**
  31. * @var FileInfoManager
  32. */
  33. private $fileInfoManager;
  34. /**
  35. * @var string
  36. */
  37. private $flagCode = 'analytics_file_info';
  38. /**
  39. * @var array
  40. */
  41. private $encodedParameters = [
  42. 'initializationVector'
  43. ];
  44. /**
  45. * @return void
  46. */
  47. protected function setUp()
  48. {
  49. $this->flagManagerMock = $this->getMockBuilder(FlagManager::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->fileInfoFactoryMock = $this->getMockBuilder(FileInfoFactory::class)
  53. ->setMethods(['create'])
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->fileInfoMock = $this->getMockBuilder(FileInfo::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $this->objectManagerHelper = new ObjectManagerHelper($this);
  60. $this->fileInfoManager = $this->objectManagerHelper->getObject(
  61. FileInfoManager::class,
  62. [
  63. 'flagManager' => $this->flagManagerMock,
  64. 'fileInfoFactory' => $this->fileInfoFactoryMock,
  65. 'flagCode' => $this->flagCode,
  66. 'encodedParameters' => $this->encodedParameters,
  67. ]
  68. );
  69. }
  70. /**
  71. * @return void
  72. */
  73. public function testSave()
  74. {
  75. $path = 'path/to/file';
  76. $initializationVector = openssl_random_pseudo_bytes(16);
  77. $parameters = [
  78. 'path' => $path,
  79. 'initializationVector' => $initializationVector,
  80. ];
  81. $this->fileInfoMock
  82. ->expects($this->once())
  83. ->method('getPath')
  84. ->with()
  85. ->willReturn($path);
  86. $this->fileInfoMock
  87. ->expects($this->once())
  88. ->method('getInitializationVector')
  89. ->with()
  90. ->willReturn($initializationVector);
  91. foreach ($this->encodedParameters as $encodedParameter) {
  92. $parameters[$encodedParameter] = base64_encode($parameters[$encodedParameter]);
  93. }
  94. $this->flagManagerMock
  95. ->expects($this->once())
  96. ->method('saveFlag')
  97. ->with($this->flagCode, $parameters);
  98. $this->assertTrue($this->fileInfoManager->save($this->fileInfoMock));
  99. }
  100. /**
  101. * @param string|null $path
  102. * @param string|null $initializationVector
  103. * @dataProvider saveWithLocalizedExceptionDataProvider
  104. * @expectedException \Magento\Framework\Exception\LocalizedException
  105. */
  106. public function testSaveWithLocalizedException($path, $initializationVector)
  107. {
  108. $this->fileInfoMock
  109. ->expects($this->once())
  110. ->method('getPath')
  111. ->with()
  112. ->willReturn($path);
  113. $this->fileInfoMock
  114. ->expects($this->once())
  115. ->method('getInitializationVector')
  116. ->with()
  117. ->willReturn($initializationVector);
  118. $this->fileInfoManager->save($this->fileInfoMock);
  119. }
  120. /**
  121. * @return array
  122. */
  123. public function saveWithLocalizedExceptionDataProvider()
  124. {
  125. return [
  126. 'Empty FileInfo' => [null, null],
  127. 'FileInfo without IV' => ['path/to/file', null],
  128. ];
  129. }
  130. /**
  131. * @dataProvider loadDataProvider
  132. * @param array|null $parameters
  133. */
  134. public function testLoad($parameters)
  135. {
  136. $this->flagManagerMock
  137. ->expects($this->once())
  138. ->method('getFlagData')
  139. ->with($this->flagCode)
  140. ->willReturn($parameters);
  141. $processedParameters = $parameters ?: [];
  142. $encodedParameters = array_intersect($this->encodedParameters, array_keys($processedParameters));
  143. foreach ($encodedParameters as $encodedParameter) {
  144. $processedParameters[$encodedParameter] = base64_decode($processedParameters[$encodedParameter]);
  145. }
  146. $this->fileInfoFactoryMock
  147. ->expects($this->once())
  148. ->method('create')
  149. ->with($processedParameters)
  150. ->willReturn($this->fileInfoMock);
  151. $this->assertSame($this->fileInfoMock, $this->fileInfoManager->load());
  152. }
  153. /**
  154. * @return array
  155. */
  156. public function loadDataProvider()
  157. {
  158. return [
  159. 'Empty flag data' => [null],
  160. 'Correct flag data' => [[
  161. 'path' => 'path/to/file',
  162. 'initializationVector' => 'xUJjl54MVke+FvMFSBpRSA==',
  163. ]],
  164. ];
  165. }
  166. }