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
119119 }
120120 }
121121 }
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+ }
122145}
Original file line number Diff line number Diff line change 1414use Exception ;
1515use org \bovigo \vfs \vfsStream ;
1616use PHPUnit \Framework \TestCase ;
17+ use RuntimeException ;
1718
1819class FileTest extends TestCase
1920{
@@ -63,6 +64,36 @@ public function testWrite(): void
6364 $ this ->assertTrue (unlink ($ path ));
6465 }
6566
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+
6697 /**
6798 * Tests File::write
6899 */
You can’t perform that action at this time.
0 commit comments