PageRenderTime 21ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/test/all.php

https://github.com/Yannix/ImprovedZipArchive
PHP | 271 lines | 219 code | 34 blank | 18 comment | 15 complexity | 4b25102e7c6203ba48d0c72d796cfb51 MD5 | raw file
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Encoding: UTF-8
  5. * Requirements:
  6. * - PHP 5 with extensions: spl, zip, pcre, iconv, mbstring
  7. * - SimpleTest (not bundled)
  8. **/
  9. mb_internal_encoding('UTF-8');
  10. if (!defined('__DIR__')) {
  11. define('__DIR__', dirname(__FILE__));
  12. }
  13. if (PHP_SAPI != 'cli') {
  14. die("It is a very bad idea to run these tests with a non CLI sapi.");
  15. }
  16. if (strpos(PHP_OS, 'WIN') === 0) {
  17. set_include_path('C:/AMP/;.'); // To find external SimpleTest
  18. define('FS_ENCODING', 'CP1252');
  19. // Use slashes not backslashes and trailing slash required
  20. define('TMP_DIR', !empty($_SERVER['TMP']) ? str_replace('\\', '/', $_SERVER['TMP'] . '/') : 'C:/WINDOWS/Temp/');
  21. define('WIN', TRUE);
  22. } else {
  23. set_include_path($_SERVER['HOME'] . ':.'); // To find external SimpleTest
  24. define('FS_ENCODING', 'UTF-8');
  25. define('TMP_DIR', !empty($_SERVER['TMP']) ? $_SERVER['TMP'] . '/' : '/tmp/'); // Trailing slash required
  26. define('WIN', FALSE);
  27. }
  28. chdir(TMP_DIR);
  29. $inputs = array();
  30. $inputs['RELATIVE_INPUT_DIR'] = 'premier/deuxième/troisième/'; // Trailing slash required
  31. $inputs['ABSOLUTE_INPUT_DIR'] = TMP_DIR . $inputs['RELATIVE_INPUT_DIR'];
  32. $outputs = array();
  33. $outputs['RELATIVE_OUTPUT_DIR'] = 'où' . uniqid() . '/'; // Trailing slash required
  34. $outputs['ABSOLUTE_OUTPUT_DIR'] = TMP_DIR . $outputs['RELATIVE_OUTPUT_DIR'];
  35. if (WIN) {
  36. foreach (array_keys($inputs) as $k) {
  37. $inputs['WIN_' . $k] = str_replace('/', '\\', $inputs[$k]);
  38. }
  39. foreach (array_keys($outputs) as $k) {
  40. $outputs['WIN_' . $k] = str_replace('/', '\\', $outputs[$k]);
  41. }
  42. }
  43. foreach (array_merge($inputs, $outputs) as $k => $v) {
  44. define($k, $v);
  45. }
  46. require_once('simpletest/unit_tester.php');
  47. require_once('simpletest/reporter.php');
  48. require_once('simpletest/simpletest/colortext_reporter.php');
  49. require_once(__DIR__ . '/../ImprovedZipArchive.php');
  50. class TestOfImprovedZipArchive extends UnitTestCase {
  51. protected $archives = array();
  52. /* File system helpers */
  53. protected static function php2fs($string) {
  54. return iconv('UTF-8', FS_ENCODING, $string);
  55. }
  56. // From ImprovedZipArchive.php
  57. protected static function mkdir($path) {
  58. $path = self::php2fs($path);
  59. $parts = preg_split('#/|' . preg_quote(DIRECTORY_SEPARATOR) . '#', $path, -1, PREG_SPLIT_NO_EMPTY);
  60. $base = (iconv_substr($path, 0, 1, FS_ENCODING) == '/' ? '/' : '');
  61. foreach ($parts as $p) {
  62. if (!file_exists($base . $p)) {
  63. if (!mkdir($base . $p)) {
  64. return FALSE;
  65. }
  66. } else if (!is_dir($base . $p)) {
  67. return FALSE;
  68. }
  69. $base .= $p . DIRECTORY_SEPARATOR;
  70. }
  71. return TRUE;
  72. }
  73. protected static function createFile($path, $content) {
  74. self::mkdir(dirname($path));
  75. return file_put_contents(self::php2fs($path), $content);
  76. }
  77. protected function makePath($methodname) {
  78. $key = TMP_DIR . $methodname . '-' . date('Ymd') . '-' . uniqid() . '.zip';
  79. $this->archives[$key] = NULL;
  80. return $key;
  81. }
  82. protected static function is_file($path) {
  83. return is_file(self::php2fs($path));
  84. }
  85. protected static function rm_fr($path) {
  86. $fspath = self::php2fs($path);
  87. if (is_dir($fspath)) {
  88. $iter = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fspath), RecursiveIteratorIterator::CHILD_FIRST);
  89. foreach ($iter as $f) {
  90. if (!$iter->isDot()) {
  91. call_user_func(array(__CLASS__, __FUNCTION__), $f->getRealPath());
  92. }
  93. }
  94. rmdir($fspath);
  95. } else {
  96. unlink($fspath);
  97. }
  98. }
  99. /* Assertion helpers */
  100. public function assertIsInt($value, $message = '%s') {
  101. return $this->assertIsA($value, 'int', $message);
  102. }
  103. public function assertZipEntryExistsByName(ZipArchive $zip, $name, $message = '%s') {
  104. return $this->assertIsInt($zip->locateName($name), $message);
  105. }
  106. public function assertZipEntryNameByIndex(ZipArchive $zip, $name, $index = 0, $message = '%s') {
  107. return $this->assertIdentical($zip->getNameIndex($index), $name, $message);
  108. }
  109. /* Tests */
  110. public function __construct() {
  111. parent::__construct();
  112. self::mkdir(ABSOLUTE_INPUT_DIR);
  113. self::createFile(ABSOLUTE_INPUT_DIR . '/quatrième.txt', uniqid());
  114. self::mkdir(ABSOLUTE_OUTPUT_DIR);
  115. }
  116. public function __destruct() {
  117. $dirs = explode('/', RELATIVE_INPUT_DIR);
  118. self::rm_fr(array_shift($dirs));
  119. self::rm_fr(ABSOLUTE_OUTPUT_DIR);
  120. }
  121. public function setUp() {
  122. }
  123. public function tearDown() {
  124. $files = array_keys($this->archives);
  125. array_walk(
  126. $files,
  127. array(__CLASS__, 'rm_fr')
  128. );
  129. $this->archives = array();
  130. }
  131. public function testAddFile() {
  132. global $inputs;
  133. foreach ($inputs as $k => $v) {
  134. $archivepath = $this->makePath(__FUNCTION__);
  135. $zip = ImprovedZipArchive::create($archivepath, FS_ENCODING, 'UTF-8', 'CP850');
  136. $this->assertTrue($zip->addFile($v . 'quatrième.txt'));
  137. //$this->assertIdentical($zip->getNameIndex(0), $v . 'quatrième.txt', __FUNCTION__ . ' ' . $k . ': %s');
  138. $this->assertZipEntryNameByIndex($zip, $v . 'quatrième.txt');
  139. $zip->close();
  140. }
  141. }
  142. public function testExtractTo() {
  143. global $outputs;
  144. $archivepath = $this->makePath(__FUNCTION__);
  145. $zip = ImprovedZipArchive::create($archivepath, FS_ENCODING, 'UTF-8', 'CP850');
  146. // On windows, absolute path gives an invalid path when extracting
  147. $this->assertTrue($zip->addFile(RELATIVE_INPUT_DIR . 'quatrième.txt'));
  148. $zip->close();
  149. $zip = ImprovedZipArchive::read($archivepath, FS_ENCODING, 'UTF-8', 'CP850');
  150. foreach ($outputs as $k => $v) {
  151. $this->assertTrue($zip->extractTo($v));
  152. $this->assertTrue(self::is_file($v . (strpos($k, 'WIN_') === 0 ? WIN_RELATIVE_INPUT_DIR : RELATIVE_INPUT_DIR) . 'quatrième.txt'), __FUNCTION__ . ' ' . $k . ': %s'); // assertFileExists
  153. }
  154. $zip->close();
  155. }
  156. public function testAddFromString() {
  157. $archivepath = $this->makePath(__FUNCTION__);
  158. $zip = ImprovedZipArchive::create($archivepath, FS_ENCODING, 'UTF-8', 'CP850');
  159. $this->assertTrue($zip->addFromString('fuß.txt', uniqid()));
  160. //$this->assertIdentical($zip->getNameIndex(0), 'fuß.txt');
  161. $this->assertZipEntryNameByIndex($zip, 'fuß.txt');
  162. $zip->close();
  163. }
  164. public function testAddEmptyDir() {
  165. $archivepath = $this->makePath(__FUNCTION__);
  166. $zip = ImprovedZipArchive::create($archivepath, FS_ENCODING, 'UTF-8', 'CP850');
  167. $this->assertTrue($zip->addEmptyDir('encyclopædia', uniqid()));
  168. //$this->assertIdentical($zip->getNameIndex(0), 'encyclopædia/');
  169. $this->assertZipEntryNameByIndex($zip, 'encyclopædia/');
  170. $zip->close();
  171. }
  172. public function testAddGlob() {
  173. $subdir = 'cinquième/';
  174. self::mkdir(ABSOLUTE_INPUT_DIR . $subdir);
  175. self::createFile(ABSOLUTE_INPUT_DIR . $subdir . 'foo.txt', uniqid());
  176. self::createFile(ABSOLUTE_INPUT_DIR . $subdir . 'aïe.txt', uniqid());
  177. $archivepath = $this->makePath(__FUNCTION__);
  178. // TODO: iterate on $inputs? (at least WIN_RELATIVE_INPUT_DIR should tested too)
  179. $zip = ImprovedZipArchive::create($archivepath, FS_ENCODING, 'UTF-8', 'CP850');
  180. $zip->addGlob(RELATIVE_INPUT_DIR . '*/*.txt');
  181. $this->assertZipEntryExistsByName($zip, RELATIVE_INPUT_DIR . $subdir . 'foo.txt');
  182. $this->assertZipEntryExistsByName($zip, RELATIVE_INPUT_DIR . $subdir . 'aïe.txt');
  183. $zip->close();
  184. }
  185. public function testAddPattern() {
  186. self::createFile(ABSOLUTE_INPUT_DIR . 'aaa.txt', uniqid());
  187. self::createFile(ABSOLUTE_INPUT_DIR . 'xçx.txt', uniqid());
  188. $archivepath = $this->makePath(__FUNCTION__);
  189. // TODO: iterate on $inputs? (at least WIN_RELATIVE_INPUT_DIR should tested too)
  190. $zip = ImprovedZipArchive::create($archivepath, FS_ENCODING, 'UTF-8', 'CP850');
  191. $zip->addPattern(FS_ENCODING == 'UTF-8' ? '~^\p{L}{3}\.txt$~u' : '~^\w{3}\.txt$~', RELATIVE_INPUT_DIR/*, array('add_path' => '')*/);
  192. $this->assertZipEntryExistsByName($zip, RELATIVE_INPUT_DIR . 'aaa.txt');
  193. $this->assertZipEntryExistsByName($zip, RELATIVE_INPUT_DIR . 'xçx.txt');
  194. $zip->close();
  195. }
  196. public function testStat() {
  197. $entry = 'Noël.txt';
  198. $archivepath = $this->makePath(__FUNCTION__);
  199. $zip = ImprovedZipArchive::create($archivepath, FS_ENCODING, 'UTF-8', 'CP850');
  200. $this->assertTrue($zip->addFromString($entry, uniqid()));
  201. $ret = $zip->statName($entry);
  202. $this->assertTrue(is_array($ret) && isset($ret['name']));
  203. $this->assertIdentical($ret['name'], $entry);
  204. $ret = $zip->statIndex(0);
  205. $this->assertTrue(is_array($ret) && isset($ret['name']));
  206. $this->assertIdentical($ret['name'], $entry);
  207. $zip->close();
  208. }
  209. public function testTranslit() {
  210. $real_entry = 'Le nœud éléphant';
  211. //$transliterate_entry = 'Le noeud éléphant'; // œ doesn't exist in CP850, but é does
  212. $transliterate_entry = iconv('CP850', 'UTF-8', iconv('UTF-8', 'CP850//TRANSLIT', $real_entry));
  213. $archivepath = $this->makePath(__FUNCTION__);
  214. $zip = ImprovedZipArchive::create($archivepath, FS_ENCODING, 'UTF-8', 'CP850', TRUE);
  215. $this->assertTrue($zip->addFromString($real_entry, uniqid()));
  216. $this->assertZipEntryExistsByName($zip, $transliterate_entry);
  217. $this->assertZipEntryExistsByName($zip, $real_entry);
  218. self::createFile(RELATIVE_INPUT_DIR . 'nœud.txt', uniqid());
  219. $this->assertTrue($zip->addFile(RELATIVE_INPUT_DIR . 'nœud.txt'));
  220. $this->assertZipEntryExistsByName($zip, RELATIVE_INPUT_DIR . 'nœud.txt');
  221. // TODO: extract it? and does it is_file?
  222. $zip->close();
  223. }
  224. }
  225. $t = new TestOfImprovedZipArchive();
  226. $t->run(WIN ? new TextReporter() : new ColorTextReporter());