|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Filesystem\Tests;
|
13 | 13 |
|
| 14 | +use Phar; |
14 | 15 | /**
|
15 | 16 | * Test class for Filesystem.
|
16 | 17 | */
|
@@ -946,6 +947,116 @@ public function providePathsForIsAbsolutePath()
|
946 | 947 | );
|
947 | 948 | }
|
948 | 949 |
|
| 950 | + public function testTempnam() |
| 951 | + { |
| 952 | + $dirname = $this->workspace; |
| 953 | + |
| 954 | + $filename = $this->filesystem->tempnam($dirname, 'foo'); |
| 955 | + |
| 956 | + $this->assertNotFalse($filename); |
| 957 | + $this->assertFileExists($filename); |
| 958 | + } |
| 959 | + |
| 960 | + public function testTempnamWithFileScheme() |
| 961 | + { |
| 962 | + $scheme = 'file://'; |
| 963 | + $dirname = $scheme.$this->workspace; |
| 964 | + |
| 965 | + $filename = $this->filesystem->tempnam($dirname, 'foo'); |
| 966 | + |
| 967 | + $this->assertNotFalse($filename); |
| 968 | + $this->assertStringStartsWith($scheme, $filename); |
| 969 | + $this->assertFileExists($filename); |
| 970 | + } |
| 971 | + |
| 972 | + public function testTempnamWithMockScheme() |
| 973 | + { |
| 974 | + // We avoid autoloading via ClassLoader as stream_wrapper_register creates the object |
| 975 | + if (!@include __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'MockStream'.DIRECTORY_SEPARATOR.'MockStream.php') { |
| 976 | + $this->markTestSkipped('Unable to load mock:// stream.'); |
| 977 | + } |
| 978 | + |
| 979 | + stream_wrapper_register('mock', 'MockStream\MockStream'); |
| 980 | + |
| 981 | + $scheme = 'mock://'; |
| 982 | + $dirname = $scheme.$this->workspace; |
| 983 | + |
| 984 | + $filename = $this->filesystem->tempnam($dirname, 'foo'); |
| 985 | + |
| 986 | + $this->assertNotFalse($filename); |
| 987 | + $this->assertStringStartsWith($scheme, $filename); |
| 988 | + $this->assertFileExists($filename); |
| 989 | + } |
| 990 | + |
| 991 | + public function testTempnamWithZlibSchemeFails() |
| 992 | + { |
| 993 | + $scheme = 'compress.zlib://'; |
| 994 | + $dirname = $scheme.$this->workspace; |
| 995 | + |
| 996 | + $filename = $this->filesystem->tempnam($dirname, 'bar'); |
| 997 | + |
| 998 | + // The compress.zlib:// stream does not support mode x: creates the file, errors "failed to open stream: operation failed" and returns false |
| 999 | + $this->assertFalse($filename); |
| 1000 | + } |
| 1001 | + |
| 1002 | + public function testTempnamWithPHPTempSchemeFails() |
| 1003 | + { |
| 1004 | + $scheme = 'php://temp'; |
| 1005 | + $dirname = $scheme; |
| 1006 | + |
| 1007 | + $filename = $this->filesystem->tempnam($dirname, 'bar'); |
| 1008 | + |
| 1009 | + $this->assertNotFalse($filename); |
| 1010 | + $this->assertStringStartsWith($scheme, $filename); |
| 1011 | + |
| 1012 | + // The php://temp stream deletes the file after close |
| 1013 | + $this->assertFileNotExists($filename); |
| 1014 | + } |
| 1015 | + |
| 1016 | + public function testTempnamWithPharSchemeFails() |
| 1017 | + { |
| 1018 | + // Skip test if Phar disabled phar.readonly must be 0 in php.ini |
| 1019 | + if (!Phar::canWrite()) { |
| 1020 | + $this->markTestSkipped('This test cannot run when phar.readonly is 1.'); |
| 1021 | + } |
| 1022 | + |
| 1023 | + $scheme = 'phar://'; |
| 1024 | + $dirname = $scheme.$this->workspace; |
| 1025 | + $pharname = 'foo.phar'; |
| 1026 | + |
| 1027 | + $p = new Phar($this->workspace.'/'.$pharname, 0, $pharname); |
| 1028 | + $filename = $this->filesystem->tempnam($dirname, $pharname.'/bar'); |
| 1029 | + |
| 1030 | + // The phar:// stream does not support mode x: fails to create file, errors "failed to open stream: phar error: "$filename" is not a file in phar "$pharname"" and returns false |
| 1031 | + $this->assertFalse($filename); |
| 1032 | + } |
| 1033 | + |
| 1034 | + public function testTempnamWithHTTPSchemeFails() |
| 1035 | + { |
| 1036 | + $scheme = 'http://'; |
| 1037 | + $dirname = $scheme.$this->workspace; |
| 1038 | + |
| 1039 | + $filename = $this->filesystem->tempnam($dirname, 'bar'); |
| 1040 | + |
| 1041 | + // The http:// scheme is read-only |
| 1042 | + $this->assertFalse($filename); |
| 1043 | + } |
| 1044 | + |
| 1045 | + public function testTempnamOnUnwritableFallsBackToSysTmp() |
| 1046 | + { |
| 1047 | + $scheme = 'file://'; |
| 1048 | + $dirname = $scheme.$this->workspace.DIRECTORY_SEPARATOR.'does_not_exist'; |
| 1049 | + |
| 1050 | + $filename = $this->filesystem->tempnam($dirname, 'bar'); |
| 1051 | + |
| 1052 | + $this->assertNotFalse($filename); |
| 1053 | + $this->assertStringStartsWith(rtrim($scheme.sys_get_temp_dir(), DIRECTORY_SEPARATOR), $filename); |
| 1054 | + $this->assertFileExists($filename); |
| 1055 | + |
| 1056 | + // Tear down |
| 1057 | + unlink($filename); |
| 1058 | + } |
| 1059 | + |
949 | 1060 | public function testDumpFile()
|
950 | 1061 | {
|
951 | 1062 | $filename = $this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'baz.txt';
|
@@ -1000,6 +1111,29 @@ public function testDumpFileOverwritesAnExistingFile()
|
1000 | 1111 | $this->assertSame('bar', file_get_contents($filename));
|
1001 | 1112 | }
|
1002 | 1113 |
|
| 1114 | + public function testDumpFileWithFileScheme() |
| 1115 | + { |
| 1116 | + $scheme = 'file://'; |
| 1117 | + $filename = $scheme.$this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'baz.txt'; |
| 1118 | + |
| 1119 | + $this->filesystem->dumpFile($filename, 'bar', null); |
| 1120 | + |
| 1121 | + $this->assertFileExists($filename); |
| 1122 | + $this->assertSame('bar', file_get_contents($filename)); |
| 1123 | + } |
| 1124 | + |
| 1125 | + public function testDumpFileWithZlibScheme() |
| 1126 | + { |
| 1127 | + $scheme = 'compress.zlib://'; |
| 1128 | + $filename = $this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'baz.txt'; |
| 1129 | + |
| 1130 | + $this->filesystem->dumpFile($filename, 'bar', null); |
| 1131 | + |
| 1132 | + // Zlib stat uses file:// wrapper so remove scheme |
| 1133 | + $this->assertFileExists(str_replace($scheme, '', $filename)); |
| 1134 | + $this->assertSame('bar', file_get_contents($filename)); |
| 1135 | + } |
| 1136 | + |
1003 | 1137 | public function testCopyShouldKeepExecutionPermission()
|
1004 | 1138 | {
|
1005 | 1139 | $this->markAsSkippedIfChmodIsMissing();
|
|
0 commit comments