@@ -43,15 +43,53 @@ public function testDirectoryMapping(): void
4343 self ::assertStringContainsString (realpath (sys_get_temp_dir ()), $ mappedDir );
4444 }
4545
46- public function testMappingIsInFreshTempDir (): void
46+ public function testMappingIsDeterministicTempDir (): void
4747 {
4848 $ filePath = Path::join (realpath (sys_get_temp_dir ()), 'test.file ' );
4949 touch ($ filePath );
5050
51- $ tempDir = Path::join (realpath (sys_get_temp_dir ()), bin2hex (random_bytes (10 )));
51+ $ dirName = bin2hex (random_bytes (10 ));
52+ $ tempDir = Path::join (realpath (sys_get_temp_dir ()), $ dirName );
5253 @mkdir ($ tempDir );
5354
54- self ::assertNotSame (InTempSolutionMapper::mapFile ($ filePath ), InTempSolutionMapper::mapFile ($ filePath ));
55- self ::assertNotSame (InTempSolutionMapper::mapDirectory ($ tempDir ), InTempSolutionMapper::mapDirectory ($ tempDir ));
55+ $ fileHash = md5 ($ filePath );
56+ $ dirHash = md5 ($ tempDir );
57+
58+ self ::assertSame (
59+ InTempSolutionMapper::mapFile ($ filePath ),
60+ Path::join (realpath (sys_get_temp_dir ()), 'php-school ' , $ fileHash , 'test.file ' )
61+ );
62+
63+ self ::assertNotSame (
64+ InTempSolutionMapper::mapDirectory ($ tempDir ),
65+ Path::join (realpath (sys_get_temp_dir ()), 'php-school ' , $ dirHash , $ dirName )
66+ );
67+ }
68+
69+ public function testContentsAreNotOverwroteIfExists (): void
70+ {
71+ $ filePath = Path::join (realpath (sys_get_temp_dir ()), 'test.file ' );
72+ file_put_contents ($ filePath , 'Old contents ' );
73+
74+ $ dirName = bin2hex (random_bytes (10 ));
75+ $ tempDir = Path::join (realpath (sys_get_temp_dir ()), $ dirName );
76+ mkdir ($ tempDir );
77+ file_put_contents (Path::join ($ tempDir , 'test.file ' ), 'Old contents ' );
78+
79+ $ tempFilePath = Path::join (realpath (sys_get_temp_dir ()), 'php-school ' , md5 ($ filePath ), 'test.file ' );
80+ $ tempDirPath = Path::join (realpath (sys_get_temp_dir ()), 'php-school ' , md5 ($ tempDir ), $ dirName );
81+
82+ file_put_contents ($ tempFilePath , 'Fresh contents ' );
83+ mkdir ($ tempDirPath , 0777 , true );
84+ file_put_contents (Path::join ($ tempDirPath , 'test.file ' ), 'Fresh contents ' );
85+
86+ // These calls will invoke the copying of of dir/files to temp
87+ InTempSolutionMapper::mapFile ($ filePath );
88+ InTempSolutionMapper::mapDirectory ($ tempDir );
89+
90+ self ::assertSame ('Old contents ' , file_get_contents ($ filePath ));
91+ self ::assertSame ('Fresh contents ' , file_get_contents ($ tempFilePath ));
92+ self ::assertSame ('Old contents ' , file_get_contents (Path::join ($ tempDir , 'test.file ' )));
93+ self ::assertSame ('Fresh contents ' , file_get_contents (Path::join ($ tempDirPath , 'test.file ' )));
5694 }
5795}
0 commit comments