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