File tree Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Original file line number Diff line number Diff line change @@ -119,4 +119,27 @@ private function checkDir(): void
119
119
}
120
120
}
121
121
}
122
+
123
+ /**
124
+ * Determines if the given path is a symbolic link
125
+ *
126
+ * @return bool True if the path is a symbolic link, false otherwise.
127
+ */
128
+ public function isLink (): bool
129
+ {
130
+ return is_link ($ this ->path );
131
+ }
132
+
133
+ /**
134
+ * Resolves and returns the target of a symbolic link
135
+ *
136
+ * @return string The resolved target of the symbolic link.
137
+ */
138
+ public function linkTarget (): string
139
+ {
140
+ if (!$ this ->isLink ()) {
141
+ throw new RuntimeException ('Not a symbolic link: ' . $ this ->path );
142
+ }
143
+ return (string )readlink ($ this ->path );
144
+ }
122
145
}
Original file line number Diff line number Diff line change 14
14
use Exception ;
15
15
use org \bovigo \vfs \vfsStream ;
16
16
use PHPUnit \Framework \TestCase ;
17
+ use RuntimeException ;
17
18
18
19
class FileTest extends TestCase
19
20
{
@@ -63,6 +64,36 @@ public function testWrite(): void
63
64
$ this ->assertTrue (unlink ($ path ));
64
65
}
65
66
67
+ /**
68
+ * Tests File::isLink
69
+ */
70
+ public function testIsLink (): void
71
+ {
72
+ $ tmpDir = sys_get_temp_dir ();
73
+ $ link = $ tmpDir . '/fooLink ' ;
74
+ $ target = tempnam ($ tmpDir , 'bar ' );
75
+
76
+ symlink ($ target , $ link );
77
+
78
+ $ file = new File ($ link );
79
+ $ this ->assertTrue ($ file ->isLink ());
80
+ $ this ->assertEquals ($ target , $ file ->linkTarget ());
81
+
82
+ $ this ->assertTrue (unlink ($ link ));
83
+ $ this ->assertTrue (unlink ($ target ));
84
+ }
85
+
86
+ /**
87
+ * Tests File::linkTarget
88
+ */
89
+ public function testLinkTarget (): void
90
+ {
91
+ $ this ->expectException (RuntimeException::class);
92
+
93
+ $ file = new File (__FILE__ );
94
+ $ file ->linkTarget ();
95
+ }
96
+
66
97
/**
67
98
* Tests File::write
68
99
*/
You can’t perform that action at this time.
0 commit comments