ArchiveTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Test\Unit;
  7. use \Magento\Framework\Archive;
  8. class ArchiveTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var Archive
  12. */
  13. protected $archive;
  14. /**
  15. * @var string
  16. */
  17. protected $sourceFilePath;
  18. /**
  19. * @var string
  20. */
  21. protected $destinationDir;
  22. /**
  23. * @var string
  24. */
  25. protected $packed;
  26. /**
  27. * @var string
  28. */
  29. protected $unpacked;
  30. protected function setUp()
  31. {
  32. $this->archive = new Archive();
  33. $this->sourceFilePath = __DIR__ . '/_files/source.txt';
  34. $this->destinationDir = __DIR__ . '/_files/archives/';
  35. }
  36. protected function tearDown()
  37. {
  38. if (!empty($this->packed) && file_exists($this->packed)) {
  39. unlink($this->packed);
  40. $this->packed = null;
  41. }
  42. if (!empty($this->unpacked) && file_exists($this->unpacked)) {
  43. unlink($this->unpacked);
  44. $this->unpacked = null;
  45. }
  46. }
  47. /**
  48. * @dataProvider isArchiveProvider
  49. * @param string $file
  50. * @param bool $isArchive
  51. */
  52. public function testIsArchive($file, $isArchive)
  53. {
  54. $this->assertEquals($isArchive, $this->archive->isArchive($file));
  55. }
  56. /**
  57. * @return array
  58. */
  59. public function isArchiveProvider()
  60. {
  61. return [
  62. ['archive.tar', true],
  63. ['archive.gz', true],
  64. ['archive.gzip', true],
  65. ['archive.tgz', true],
  66. ['archive.tgzip', true],
  67. ['archive.bz', true],
  68. ['archive.bzip', true],
  69. ['archive.bzip2', true],
  70. ['archive.bz2', true],
  71. ['archive.tbz', true],
  72. ['archive.tbzip', true],
  73. ['archive.tbz2', true],
  74. ['archive.tbzip2', true],
  75. ['archive.txt', false],
  76. ['archive.php', false],
  77. ['archive.phtml', false],
  78. ['archive.js', false],
  79. ['archive.log', false],
  80. ];
  81. }
  82. /**
  83. * @dataProvider isTarProvider
  84. * @param string $file
  85. * @param bool $isArchive
  86. */
  87. public function testIsTar($file, $isArchive)
  88. {
  89. $this->assertEquals($isArchive, $this->archive->isTar($file));
  90. }
  91. /**
  92. * @return array
  93. */
  94. public function isTarProvider()
  95. {
  96. return [
  97. ['archive.tar', true],
  98. ['archive.gz', false],
  99. ['archive.gzip', false],
  100. ['archive.tgz', false],
  101. ['archive.tgzip', false],
  102. ['archive.bz', false],
  103. ['archive.bzip', false],
  104. ['archive.bzip2', false],
  105. ['archive.bz2', false],
  106. ['archive.tbz', false],
  107. ['archive.tbzip', false],
  108. ['archive.tbz2', false],
  109. ['archive.tbzip2', false],
  110. ['archive.txt', false],
  111. ['archive.php', false],
  112. ['archive.phtml', false],
  113. ['archive.js', false],
  114. ['archive.log', false],
  115. ];
  116. }
  117. /**
  118. * @param string $destinationFile
  119. * @param string $extensionRequired
  120. * @dataProvider destinationProvider
  121. */
  122. public function testPackUnpackGzBz($destinationFile, $extensionRequired)
  123. {
  124. if ($extensionRequired && !extension_loaded($extensionRequired)) {
  125. $this->markTestSkipped("The extension '{$extensionRequired}' is not enabled.");
  126. }
  127. $this->packed = $this->archive->pack($this->sourceFilePath, $this->destinationDir . $destinationFile);
  128. $this->assertFileExists($this->packed);
  129. $this->assertEquals($this->destinationDir . $destinationFile, $this->packed);
  130. $this->unpacked = $this->archive->unpack($this->packed, $this->destinationDir);
  131. $this->assertFileExists($this->unpacked);
  132. $this->assertStringStartsWith($this->destinationDir, $this->unpacked);
  133. }
  134. /**
  135. * @return array
  136. */
  137. public function destinationProvider()
  138. {
  139. return [
  140. ['archive.gz', 'zlib'],
  141. ['archive.gzip', 'zlib'],
  142. ['archive.bz', 'bz2'],
  143. ['archive.bzip', 'bz2'],
  144. ['archive.bzip2', 'bz2'],
  145. ['archive.bz2', 'bz2']
  146. ];
  147. }
  148. /**
  149. * @param string $destinationFile
  150. * @param string $extensionRequired
  151. * @dataProvider tarProvider
  152. */
  153. public function testPackUnpackTar($destinationFile, $extensionRequired)
  154. {
  155. if ($extensionRequired && !extension_loaded($extensionRequired)) {
  156. $this->markTestSkipped("The extension '{$extensionRequired}' is not enabled.");
  157. }
  158. $this->packed = $this->archive->pack($this->sourceFilePath, $this->destinationDir . $destinationFile);
  159. $this->assertFileExists($this->packed);
  160. $this->assertEquals($this->destinationDir . $destinationFile, $this->packed);
  161. $unpacked = $this->archive->unpack($this->packed, $this->destinationDir);
  162. $this->unpacked = $unpacked . pathinfo($this->sourceFilePath, PATHINFO_BASENAME);
  163. $this->assertFileExists($this->unpacked);
  164. $this->assertStringStartsWith($this->destinationDir, $this->unpacked);
  165. }
  166. /**
  167. * @param string $destinationFile
  168. * @param string $extensionRequired
  169. * @dataProvider tarProvider
  170. */
  171. public function testExtract($destinationFile, $extensionRequired)
  172. {
  173. if ($extensionRequired && !extension_loaded($extensionRequired)) {
  174. $this->markTestSkipped("The extension '{$extensionRequired}' is not enabled.");
  175. }
  176. $this->packed = $this->archive->pack($this->sourceFilePath, $this->destinationDir . $destinationFile);
  177. $this->assertFileExists($this->packed);
  178. $this->assertEquals($this->destinationDir . $destinationFile, $this->packed);
  179. $filename = pathinfo($this->sourceFilePath, PATHINFO_BASENAME);
  180. $this->unpacked = $this->archive->extract($filename, $this->packed, $this->destinationDir);
  181. $this->assertFileExists($this->unpacked);
  182. $this->assertStringStartsWith($this->destinationDir, $this->unpacked);
  183. }
  184. /**
  185. * @return array
  186. */
  187. public function tarProvider()
  188. {
  189. return [
  190. ['archive.tar', ''],
  191. ['archive.tgz', 'zlib'],
  192. ['archive.tgzip', 'zlib'],
  193. ['archive.tbz', 'bz2'],
  194. ['archive.tbzip', 'bz2'],
  195. ['archive.tbz2', 'bz2'],
  196. ['archive.tbzip2', 'bz2']
  197. ];
  198. }
  199. }