Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FileRecorderTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Analytics\Model\FileInfoFactory;
17 
18 class FileRecorderTest extends \PHPUnit\Framework\TestCase
19 {
23  private $fileInfoManagerMock;
24 
28  private $fileInfoFactoryMock;
29 
33  private $filesystemMock;
34 
38  private $fileInfoMock;
39 
43  private $directoryMock;
44 
48  private $encodedContextMock;
49 
53  private $objectManagerHelper;
54 
58  private $fileRecorder;
59 
63  private $fileSubdirectoryPath = 'analytics_subdir/';
64 
68  private $encodedFileName = 'filename.tgz';
69 
73  protected function setUp()
74  {
75  $this->fileInfoManagerMock = $this->getMockBuilder(FileInfoManager::class)
76  ->disableOriginalConstructor()
77  ->getMock();
78 
79  $this->fileInfoFactoryMock = $this->getMockBuilder(FileInfoFactory::class)
80  ->setMethods(['create'])
81  ->disableOriginalConstructor()
82  ->getMock();
83 
84  $this->filesystemMock = $this->getMockBuilder(Filesystem::class)
85  ->disableOriginalConstructor()
86  ->getMock();
87 
88  $this->fileInfoMock = $this->getMockBuilder(FileInfo::class)
89  ->disableOriginalConstructor()
90  ->getMock();
91 
92  $this->directoryMock = $this->getMockBuilder(WriteInterface::class)
93  ->disableOriginalConstructor()
94  ->getMock();
95 
96  $this->encodedContextMock = $this->getMockBuilder(EncodedContext::class)
97  ->disableOriginalConstructor()
98  ->getMock();
99 
100  $this->objectManagerHelper = new ObjectManagerHelper($this);
101 
102  $this->fileRecorder = $this->objectManagerHelper->getObject(
103  FileRecorder::class,
104  [
105  'fileInfoManager' => $this->fileInfoManagerMock,
106  'fileInfoFactory' => $this->fileInfoFactoryMock,
107  'filesystem' => $this->filesystemMock,
108  'fileSubdirectoryPath' => $this->fileSubdirectoryPath,
109  'encodedFileName' => $this->encodedFileName,
110  ]
111  );
112  }
113 
118  public function testRecordNewFile($pathToExistingFile)
119  {
120  $content = openssl_random_pseudo_bytes(200);
121 
122  $this->filesystemMock
123  ->expects($this->once())
124  ->method('getDirectoryWrite')
125  ->with(DirectoryList::MEDIA)
126  ->willReturn($this->directoryMock);
127 
128  $this->encodedContextMock
129  ->expects($this->once())
130  ->method('getContent')
131  ->with()
132  ->willReturn($content);
133 
134  $hashLength = 64;
135  $fileRelativePathPattern = '#' . preg_quote($this->fileSubdirectoryPath, '#')
136  . '.{' . $hashLength . '}/' . preg_quote($this->encodedFileName, '#') . '#';
137  $this->directoryMock
138  ->expects($this->once())
139  ->method('writeFile')
140  ->with($this->matchesRegularExpression($fileRelativePathPattern), $content)
141  ->willReturn($this->directoryMock);
142 
143  $this->fileInfoManagerMock
144  ->expects($this->once())
145  ->method('load')
146  ->with()
147  ->willReturn($this->fileInfoMock);
148 
149  $this->encodedContextMock
150  ->expects($this->once())
151  ->method('getInitializationVector')
152  ->with()
153  ->willReturn('init_vector***');
154 
156  $this->fileInfoFactoryMock
157  ->expects($this->once())
158  ->method('create')
159  ->with($this->callback(
160  function ($parameters) {
161  return !empty($parameters['path']) && ('init_vector***' === $parameters['initializationVector']);
162  }
163  ))
164  ->willReturn($this->fileInfoMock);
165  $this->fileInfoManagerMock
166  ->expects($this->once())
167  ->method('save')
168  ->with($this->fileInfoMock);
169 
171  $this->fileInfoMock
172  ->expects($this->exactly($pathToExistingFile ? 3 : 1))
173  ->method('getPath')
174  ->with()
175  ->willReturn($pathToExistingFile);
176  $directoryName = dirname($pathToExistingFile);
177  if ($directoryName === '.') {
178  $this->directoryMock
179  ->expects($this->once())
180  ->method('delete')
181  ->with($pathToExistingFile);
182  } elseif ($directoryName) {
183  $this->directoryMock
184  ->expects($this->exactly(2))
185  ->method('delete')
186  ->withConsecutive(
187  [$pathToExistingFile],
189  );
190  }
191 
192  $this->assertTrue($this->fileRecorder->recordNewFile($this->encodedContextMock));
193  }
194 
198  public function recordNewFileDataProvider()
199  {
200  return [
201  'File doesn\'t exist' => [''],
202  'Existing file into subdirectory' => ['dir_name/file.txt'],
203  'Existing file doesn\'t into subdirectory' => ['file.txt'],
204  ];
205  }
206 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$directoryName
Definition: linked_media.php:9