xref: /php-src/ext/phar/tests/phar_stub.phpt (revision 840d6655)
1--TEST--
2Phar::setStub()
3--EXTENSIONS--
4phar
5--INI--
6phar.require_hash=0
7phar.readonly=0
8--FILE--
9<?php
10$fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.phar.php';
11$pname = 'phar://' . $fname;
12$file = '<?php echo "first stub\n"; __HALT_COMPILER(); ?>';
13
14$files = array();
15$files['a'] = 'a';
16$files['b'] = 'b';
17$files['c'] = 'c';
18
19include 'files/phar_test.inc';
20
21$file = '<?php echo "first stub\n"; __HALT_COMPILER(); ?>';
22$fp = fopen($fname, 'rb');
23//// 1
24echo fread($fp, strlen($file)) . "\n";
25fclose($fp);
26$phar = new Phar($fname);
27$file = '<?php echo "second stub\n"; __HALT_COMPILER(); ?>';
28
29//// 2
30$phar->setStub($file);
31$fp = fopen($fname, 'rb');
32echo fread($fp, strlen($file)) . "\n";
33fclose($fp);
34
35$fname2 = __DIR__ . '/' . basename(__FILE__, '.php') . '.phartmp.php';
36$file = '<?php echo "third stub\n"; __HALT_COMPILER(); ?>';
37$fp = fopen($fname2, 'wb');
38fwrite($fp, $file);
39fclose($fp);
40$fp = fopen($fname2, 'rb');
41
42//// 3
43$phar->setStub($fp);
44fclose($fp);
45
46$fp = fopen($fname, 'rb');
47echo fread($fp, strlen($file)) . "\n";
48fclose($fp);
49
50$fp = fopen($fname2, 'ab');
51fwrite($fp, 'booya');
52fclose($fp);
53echo file_get_contents($fname2) . "\n";
54
55$fp = fopen($fname2, 'rb');
56
57//// 4
58set_error_handler(function ($severity, $message, $file, $line) {
59    throw new Exception($message);
60});
61try {
62    $phar->setStub($fp);
63} catch (Exception $e) {
64    echo $e->getMessage() . "\n";
65}
66set_error_handler(null);
67fclose($fp);
68
69$fp = fopen($fname, 'rb');
70echo fread($fp, strlen($file)) . "\n";
71fclose($fp);
72
73$fp = fopen($fname2, 'rb');
74
75//// 5
76$phar->setStub($fp, strlen($file));
77fclose($fp);
78
79$fp = fopen($fname, 'rb');
80echo fread($fp, strlen($file)) . "\n";
81if (fread($fp, strlen('booya')) == 'booya') {
82    echo 'failed - copied booya';
83}
84fclose($fp);
85$phar['testing'] = 'hi';
86
87// ensure stub is not overwritten
88$fp = fopen($fname, 'rb');
89echo fread($fp, strlen($file)) . "\n";
90if (fread($fp, strlen('booya')) == 'booya') {
91    echo 'failed - copied booya';
92}
93fclose($fp);
94
95?>
96--CLEAN--
97<?php
98unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar.php');
99unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phartmp.php');
100__HALT_COMPILER();
101?>
102--EXPECTF--
103<?php echo "first stub\n"; __HALT_COMPILER(); ?>
104<?php echo "second stub\n"; __HALT_COMPILER(); ?>
105
106Deprecated: Calling Phar::setStub(resource $stub, int $length) is deprecated in %s on line %d
107<?php echo "third stub\n"; __HALT_COMPILER(); ?>
108<?php echo "third stub\n"; __HALT_COMPILER(); ?>booya
109Calling Phar::setStub(resource $stub, int $length) is deprecated
110<?php echo "third stub\n"; __HALT_COMPILER(); ?>
111
112Deprecated: Calling Phar::setStub(resource $stub, int $length) is deprecated in %s on line %d
113<?php echo "third stub\n"; __HALT_COMPILER(); ?>
114<?php echo "third stub\n"; __HALT_COMPILER(); ?>
115