xref: /PHP-8.0/ext/spl/tests/fileobject_003.phpt (revision f8d79582)
1--TEST--
2SPL: SplFileInfo cloning
3--FILE--
4<?php
5
6function test($name, $lc, $lp)
7{
8    static $i = 0;
9    echo "===$i===\n";
10    $i++;
11
12    $o = new SplFileInfo($name);
13
14    var_dump($o);
15    $c = clone $o;
16    var_dump($c);
17    var_dump($o === $c);
18    var_dump($o == $c);
19    var_dump($o->getPathname() == $c->getPathname());
20
21    try {
22        $f = new SplFileObject($name);
23        var_dump($name);
24        var_dump($f->getPathName());
25        $l = substr($f->getPathName(), -1);
26        var_dump($l != '/' && $l != '\\' && $l == $lc);
27        var_dump($f->getFileName());
28        $l = substr($f->getFileName(), -1);
29        var_dump($l != '/' && $l != '\\' && $l == $lc);
30        var_dump($f->getPath());
31        $l = substr($f->getPath(), -1);
32        var_dump($l != '/' && $l != '\\' && $l == $lp);
33    } catch (LogicException $e) {
34        echo "LogicException: ".$e->getMessage()."\n";
35    }
36    try {
37        $fo = $o->openFile();
38        var_dump($fo->getPathName(), $fo->getFileName(), $fo->getPath());
39    } catch (LogicException $e) {
40        echo "LogicException: ".$e->getMessage()."\n";
41    }
42}
43
44test(__DIR__ . '/' . 'fileobject_001a.txt', 't', substr(__DIR__,-1));
45test(__DIR__ . '/', substr(__DIR__,-1), 'l');
46test(__DIR__,       substr(__DIR__,-1), 'l');
47
48?>
49--EXPECTF--
50===0===
51object(SplFileInfo)#%d (2) {
52  ["pathName":"SplFileInfo":private]=>
53  string(%d) "%s"
54  ["fileName":"SplFileInfo":private]=>
55  string(%d) "fileobject_001a.txt"
56}
57object(SplFileInfo)#%d (2) {
58  ["pathName":"SplFileInfo":private]=>
59  string(%d) "%s"
60  ["fileName":"SplFileInfo":private]=>
61  string(%d) "fileobject_001a.txt"
62}
63bool(false)
64bool(true)
65bool(true)
66%s(%d) "%sfileobject_001a.txt"
67string(%d) "%sfileobject_001a.txt"
68bool(true)
69string(19) "fileobject_001a.txt"
70bool(true)
71string(%d) "%stests"
72bool(true)
73string(%d) "%sfileobject_001a.txt"
74string(19) "fileobject_001a.txt"
75string(%d) "%stests"
76===1===
77object(SplFileInfo)#%d (2) {
78  ["pathName":"SplFileInfo":private]=>
79  string(%d) "%s"
80  ["fileName":"SplFileInfo":private]=>
81  string(%d) "%s"
82}
83object(SplFileInfo)#%d (2) {
84  ["pathName":"SplFileInfo":private]=>
85  string(%d) "%s"
86  ["fileName":"SplFileInfo":private]=>
87  string(%d) "%s"
88}
89bool(false)
90bool(true)
91bool(true)
92LogicException: Cannot use SplFileObject with directories
93LogicException: Cannot use SplFileObject with directories
94===2===
95object(SplFileInfo)#%d (2) {
96  ["pathName":"SplFileInfo":private]=>
97  string(%d) "%s"
98  ["fileName":"SplFileInfo":private]=>
99  string(%d) "%s"
100}
101object(SplFileInfo)#%d (2) {
102  ["pathName":"SplFileInfo":private]=>
103  string(%d) "%s"
104  ["fileName":"SplFileInfo":private]=>
105  string(%d) "%s"
106}
107bool(false)
108bool(true)
109bool(true)
110LogicException: Cannot use SplFileObject with directories
111LogicException: Cannot use SplFileObject with directories
112