xref: /PHP-7.2/ext/phar/tests/phar_extract.phpt (revision 72281f29)
1--TEST--
2Phar: Phar::extractTo()
3--SKIPIF--
4<?php if (!extension_loaded("phar")) die("skip"); ?>
5--INI--
6phar.readonly=0
7--FILE--
8<?php
9
10$fname = dirname(__FILE__) . '/tempmanifest1.phar.php';
11$pname = 'phar://' . $fname;
12
13$a = new Phar($fname);
14$a['file1.txt'] = 'hi';
15$a['file2.txt'] = 'hi2';
16$a['subdir/ectory/file.txt'] = 'hi3';
17$a->mount($pname . '/mount', __FILE__);
18$a->addEmptyDir('one/level');
19
20$a->extractTo(dirname(__FILE__) . '/extract', 'mount');
21$a->extractTo(dirname(__FILE__) . '/extract');
22
23$out = array();
24
25foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(dirname(__FILE__) . '/extract', 0x00003000), RecursiveIteratorIterator::CHILD_FIRST) as $p => $b) {
26	$out[] = $p;
27}
28
29sort($out);
30
31foreach ($out as $b) {
32	echo "$b\n";
33}
34
35$a->extractTo(dirname(__FILE__) . '/extract1', 'file1.txt');
36var_dump(file_get_contents(dirname(__FILE__) . '/extract1/file1.txt'));
37
38$a->extractTo(dirname(__FILE__) . '/extract1', 'subdir/ectory/file.txt');
39var_dump(file_get_contents(dirname(__FILE__) . '/extract1/subdir/ectory/file.txt'));
40
41$a->extractTo(dirname(__FILE__) . '/extract1-2', array('file2.txt', 'one/level'));
42var_dump(file_get_contents(dirname(__FILE__) . '/extract1-2/file2.txt'));
43var_dump(is_dir(dirname(__FILE__) . '/extract1-2/one/level'));
44
45try {
46	$a->extractTo(dirname(__FILE__) . '/whatever', 134);
47} catch (Exception $e) {
48	echo $e->getMessage(), "\n";
49}
50
51$a->extractTo(array());
52
53try {
54	$a->extractTo('');
55} catch (Exception $e) {
56	echo $e->getMessage(), "\n";
57}
58
59file_put_contents(dirname(__FILE__) . '/oops', 'I is file');
60
61try {
62	$a->extractTo(dirname(__FILE__) . '/oops', 'file1.txt');
63} catch (Exception $e) {
64	echo $e->getMessage(), "\n";
65}
66
67try {
68	$a->extractTo(dirname(__FILE__) . '/oops1', array(array(), 'file1.txt'));
69} catch (Exception $e) {
70	echo $e->getMessage(), "\n";
71}
72
73try {
74	$a->extractTo(dirname(__FILE__) . '/extract', 'file1.txt');
75} catch (Exception $e) {
76	echo $e->getMessage(), "\n";
77}
78
79file_put_contents(dirname(__FILE__) . '/extract/file1.txt', 'first');
80var_dump(file_get_contents(dirname(__FILE__) . '/extract/file1.txt'));
81
82$a->extractTo(dirname(__FILE__) . '/extract', 'file1.txt', true);
83var_dump(file_get_contents(dirname(__FILE__) . '/extract/file1.txt'));
84
85try {
86	$a->extractTo(str_repeat('a', 20000), 'file1.txt');
87} catch (Exception $e) {
88	echo $e->getMessage(), "\n";
89}
90
91$a[str_repeat('a', 20000)] = 'long';
92
93try {
94	$a->extractTo(dirname(__FILE__) . '/extract', str_repeat('a', 20000));
95} catch (Exception $e) {
96	echo $e->getMessage(), "\n";
97}
98
99?>
100===DONE===
101--CLEAN--
102<?php
103@rmdir(dirname(__FILE__) . '/whatever');
104@unlink(dirname(__FILE__) . '/oops');
105@rmdir(dirname(__FILE__) . '/oops1');
106@unlink(dirname(__FILE__) . '/tempmanifest1.phar.php');
107$e = dirname(__FILE__) . '/extract/';
108@unlink($e . 'file1.txt');
109@unlink($e . 'file2.txt');
110@unlink($e . 'subdir/ectory/file.txt');
111@rmdir($e . 'subdir/ectory');
112@rmdir($e . 'subdir');
113@rmdir($e . 'one/level');
114@rmdir($e . 'one');
115@rmdir($e);
116$e = dirname(__FILE__) . '/extract1/';
117@unlink($e . 'file1.txt');
118@unlink($e . 'subdir/ectory/file.txt');
119@rmdir($e . 'subdir/ectory');
120@rmdir($e . 'subdir');
121@rmdir($e);
122$e = dirname(__FILE__) . '/extract1-2/';
123@unlink($e . 'file2.txt');
124@rmdir($e . 'one/level');
125@rmdir($e . 'one');
126@rmdir($e);
127?>
128--EXPECTF--
129%sextract%cfile1.txt
130%sextract%cfile2.txt
131%sextract%cone
132%sextract%csubdir
133%sextract%csubdir%cectory
134%sextract%csubdir%cectory%cfile.txt
135string(2) "hi"
136string(3) "hi3"
137string(3) "hi2"
138bool(false)
139Invalid argument, expected a filename (string) or array of filenames
140
141Warning: Phar::extractTo() expects parameter 1 to be a valid path, array given in %sphar_extract.php on line %d
142Invalid argument, extraction path must be non-zero length
143Unable to use path "%soops" for extraction, it is a file, must be a directory
144Invalid argument, array of filenames to extract contains non-string value
145Extraction from phar "%stempmanifest1.phar.php" failed: Cannot extract "file1.txt" to "%sextract/file1.txt", path already exists
146string(5) "first"
147string(2) "hi"
148Cannot extract to "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...", destination directory is too long for filesystem
149Extraction from phar "%stempmanifest1.phar.php" failed: Cannot extract "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..." to "%s...", extracted filename is too long for filesystem
150===DONE===
151