Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FileInfoManagerTest.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Analytics\Model\FileInfoFactory;
13 
14 class FileInfoManagerTest extends \PHPUnit\Framework\TestCase
15 {
19  private $flagManagerMock;
20 
24  private $fileInfoFactoryMock;
25 
29  private $fileInfoMock;
30 
34  private $objectManagerHelper;
35 
39  private $fileInfoManager;
40 
44  private $flagCode = 'analytics_file_info';
45 
49  private $encodedParameters = [
50  'initializationVector'
51  ];
52 
56  protected function setUp()
57  {
58  $this->flagManagerMock = $this->getMockBuilder(FlagManager::class)
59  ->disableOriginalConstructor()
60  ->getMock();
61 
62  $this->fileInfoFactoryMock = $this->getMockBuilder(FileInfoFactory::class)
63  ->setMethods(['create'])
64  ->disableOriginalConstructor()
65  ->getMock();
66 
67  $this->fileInfoMock = $this->getMockBuilder(FileInfo::class)
68  ->disableOriginalConstructor()
69  ->getMock();
70 
71  $this->objectManagerHelper = new ObjectManagerHelper($this);
72 
73  $this->fileInfoManager = $this->objectManagerHelper->getObject(
74  FileInfoManager::class,
75  [
76  'flagManager' => $this->flagManagerMock,
77  'fileInfoFactory' => $this->fileInfoFactoryMock,
78  'flagCode' => $this->flagCode,
79  'encodedParameters' => $this->encodedParameters,
80  ]
81  );
82  }
83 
87  public function testSave()
88  {
89  $path = 'path/to/file';
90  $initializationVector = openssl_random_pseudo_bytes(16);
91  $parameters = [
92  'path' => $path,
93  'initializationVector' => $initializationVector,
94  ];
95 
96  $this->fileInfoMock
97  ->expects($this->once())
98  ->method('getPath')
99  ->with()
100  ->willReturn($path);
101  $this->fileInfoMock
102  ->expects($this->once())
103  ->method('getInitializationVector')
104  ->with()
105  ->willReturn($initializationVector);
106 
107  foreach ($this->encodedParameters as $encodedParameter) {
108  $parameters[$encodedParameter] = base64_encode($parameters[$encodedParameter]);
109  }
110  $this->flagManagerMock
111  ->expects($this->once())
112  ->method('saveFlag')
113  ->with($this->flagCode, $parameters);
114 
115  $this->assertTrue($this->fileInfoManager->save($this->fileInfoMock));
116  }
117 
124  public function testSaveWithLocalizedException($path, $initializationVector)
125  {
126  $this->fileInfoMock
127  ->expects($this->once())
128  ->method('getPath')
129  ->with()
130  ->willReturn($path);
131  $this->fileInfoMock
132  ->expects($this->once())
133  ->method('getInitializationVector')
134  ->with()
135  ->willReturn($initializationVector);
136 
137  $this->fileInfoManager->save($this->fileInfoMock);
138  }
139 
144  {
145  return [
146  'Empty FileInfo' => [null, null],
147  'FileInfo without IV' => ['path/to/file', null],
148  ];
149  }
150 
155  public function testLoad($parameters)
156  {
157  $this->flagManagerMock
158  ->expects($this->once())
159  ->method('getFlagData')
160  ->with($this->flagCode)
161  ->willReturn($parameters);
162 
163  $processedParameters = $parameters ?: [];
164  $encodedParameters = array_intersect($this->encodedParameters, array_keys($processedParameters));
165  foreach ($encodedParameters as $encodedParameter) {
166  $processedParameters[$encodedParameter] = base64_decode($processedParameters[$encodedParameter]);
167  }
168 
169  $this->fileInfoFactoryMock
170  ->expects($this->once())
171  ->method('create')
172  ->with($processedParameters)
173  ->willReturn($this->fileInfoMock);
174 
175  $this->assertSame($this->fileInfoMock, $this->fileInfoManager->load());
176  }
177 
181  public function loadDataProvider()
182  {
183  return [
184  'Empty flag data' => [null],
185  'Correct flag data' => [[
186  'path' => 'path/to/file',
187  'initializationVector' => 'xUJjl54MVke+FvMFSBpRSA==',
188  ]],
189  ];
190  }
191 }