Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ArchiveTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Framework\Archive;
10 
11 class ArchiveTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $archive;
17 
21  protected $sourceFilePath;
22 
26  protected $destinationDir;
27 
31  protected $packed;
32 
36  protected $unpacked;
37 
38  protected function setUp()
39  {
40  $this->archive = new Archive();
41  $this->sourceFilePath = __DIR__ . '/_files/source.txt';
42  $this->destinationDir = __DIR__ . '/_files/archives/';
43  }
44 
45  protected function tearDown()
46  {
47  if (!empty($this->packed) && file_exists($this->packed)) {
48  unlink($this->packed);
49  $this->packed = null;
50  }
51  if (!empty($this->unpacked) && file_exists($this->unpacked)) {
52  unlink($this->unpacked);
53  $this->unpacked = null;
54  }
55  }
56 
62  public function testIsArchive($file, $isArchive)
63  {
64  $this->assertEquals($isArchive, $this->archive->isArchive($file));
65  }
66 
70  public function isArchiveProvider()
71  {
72  return [
73  ['archive.tar', true],
74  ['archive.gz', true],
75  ['archive.gzip', true],
76  ['archive.tgz', true],
77  ['archive.tgzip', true],
78  ['archive.bz', true],
79  ['archive.bzip', true],
80  ['archive.bzip2', true],
81  ['archive.bz2', true],
82  ['archive.tbz', true],
83  ['archive.tbzip', true],
84  ['archive.tbz2', true],
85  ['archive.tbzip2', true],
86  ['archive.txt', false],
87  ['archive.php', false],
88  ['archive.phtml', false],
89  ['archive.js', false],
90  ['archive.log', false],
91  ];
92  }
93 
99  public function testIsTar($file, $isArchive)
100  {
101  $this->assertEquals($isArchive, $this->archive->isTar($file));
102  }
103 
107  public function isTarProvider()
108  {
109  return [
110  ['archive.tar', true],
111  ['archive.gz', false],
112  ['archive.gzip', false],
113  ['archive.tgz', false],
114  ['archive.tgzip', false],
115  ['archive.bz', false],
116  ['archive.bzip', false],
117  ['archive.bzip2', false],
118  ['archive.bz2', false],
119  ['archive.tbz', false],
120  ['archive.tbzip', false],
121  ['archive.tbz2', false],
122  ['archive.tbzip2', false],
123  ['archive.txt', false],
124  ['archive.php', false],
125  ['archive.phtml', false],
126  ['archive.js', false],
127  ['archive.log', false],
128  ];
129  }
130 
136  public function testPackUnpackGzBz($destinationFile, $extensionRequired)
137  {
138  if ($extensionRequired && !extension_loaded($extensionRequired)) {
139  $this->markTestSkipped("The extension '{$extensionRequired}' is not enabled.");
140  }
141  $this->packed = $this->archive->pack($this->sourceFilePath, $this->destinationDir . $destinationFile);
142 
143  $this->assertFileExists($this->packed);
144  $this->assertEquals($this->destinationDir . $destinationFile, $this->packed);
145 
146  $this->unpacked = $this->archive->unpack($this->packed, $this->destinationDir);
147 
148  $this->assertFileExists($this->unpacked);
149  $this->assertStringStartsWith($this->destinationDir, $this->unpacked);
150  }
151 
155  public function destinationProvider()
156  {
157  return [
158  ['archive.gz', 'zlib'],
159  ['archive.gzip', 'zlib'],
160  ['archive.bz', 'bz2'],
161  ['archive.bzip', 'bz2'],
162  ['archive.bzip2', 'bz2'],
163  ['archive.bz2', 'bz2']
164  ];
165  }
166 
172  public function testPackUnpackTar($destinationFile, $extensionRequired)
173  {
174  if ($extensionRequired && !extension_loaded($extensionRequired)) {
175  $this->markTestSkipped("The extension '{$extensionRequired}' is not enabled.");
176  }
177  $this->packed = $this->archive->pack($this->sourceFilePath, $this->destinationDir . $destinationFile);
178 
179  $this->assertFileExists($this->packed);
180  $this->assertEquals($this->destinationDir . $destinationFile, $this->packed);
181 
182  $unpacked = $this->archive->unpack($this->packed, $this->destinationDir);
183 
184  $this->unpacked = $unpacked . pathinfo($this->sourceFilePath, PATHINFO_BASENAME);
185 
186  $this->assertFileExists($this->unpacked);
187  $this->assertStringStartsWith($this->destinationDir, $this->unpacked);
188  }
189 
195  public function testExtract($destinationFile, $extensionRequired)
196  {
197  if ($extensionRequired && !extension_loaded($extensionRequired)) {
198  $this->markTestSkipped("The extension '{$extensionRequired}' is not enabled.");
199  }
200  $this->packed = $this->archive->pack($this->sourceFilePath, $this->destinationDir . $destinationFile);
201 
202  $this->assertFileExists($this->packed);
203  $this->assertEquals($this->destinationDir . $destinationFile, $this->packed);
204 
205  $filename = pathinfo($this->sourceFilePath, PATHINFO_BASENAME);
206  $this->unpacked = $this->archive->extract($filename, $this->packed, $this->destinationDir);
207 
208  $this->assertFileExists($this->unpacked);
209  $this->assertStringStartsWith($this->destinationDir, $this->unpacked);
210  }
211 
215  public function tarProvider()
216  {
217  return [
218  ['archive.tar', ''],
219  ['archive.tgz', 'zlib'],
220  ['archive.tgzip', 'zlib'],
221  ['archive.tbz', 'bz2'],
222  ['archive.tbzip', 'bz2'],
223  ['archive.tbz2', 'bz2'],
224  ['archive.tbzip2', 'bz2']
225  ];
226  }
227 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
testExtract($destinationFile, $extensionRequired)
testPackUnpackTar($destinationFile, $extensionRequired)
testPackUnpackGzBz($destinationFile, $extensionRequired)