1313
1414namespace CodeIgniter \HotReloader ;
1515
16+ use CodeIgniter \Config \Factories ;
1617use CodeIgniter \Exceptions \FrameworkException ;
1718use CodeIgniter \Test \CIUnitTestCase ;
19+ use Config \Toolbar ;
1820use PHPUnit \Framework \Attributes \Group ;
1921
2022/**
2426final class DirectoryHasherTest extends CIUnitTestCase
2527{
2628 private DirectoryHasher $ hasher ;
29+ private string $ fixtureDirectory ;
30+ private string $ fixturePath ;
31+ private string $ fixturePathAlt ;
2732
2833 protected function setUp (): void
2934 {
3035 parent ::setUp ();
3136
37+ $ suffix = str_replace ('. ' , '' , uniqid ('' , true ));
38+ $ this ->fixtureDirectory = 'writable/hot-reloader-test- ' . $ suffix ;
39+ $ fixtureDirectoryAlt = 'writable/hot-reloader-test-alt- ' . $ suffix ;
40+ $ this ->fixturePath = ROOTPATH . $ this ->fixtureDirectory . '/ ' ;
41+ $ this ->fixturePathAlt = ROOTPATH . $ fixtureDirectoryAlt . '/ ' ;
42+
43+ $ this ->createFixtureDirectory ($ this ->fixturePath , 'test ' );
44+ $ this ->createFixtureDirectory ($ this ->fixturePathAlt , 'test-alt ' );
45+
3246 $ this ->hasher = new DirectoryHasher ();
3347 }
3448
49+ protected function tearDown (): void
50+ {
51+ $ this ->removeFixtureDirectory ($ this ->fixturePath );
52+ $ this ->removeFixtureDirectory ($ this ->fixturePathAlt );
53+
54+ parent ::tearDown ();
55+ }
56+
3557 public function testHashApp (): void
3658 {
59+ $ config = new Toolbar ();
60+ $ config ->watchedDirectories = [$ this ->fixtureDirectory ];
61+ Factories::injectMock ('config ' , Toolbar::class, $ config );
62+
3763 $ results = $ this ->hasher ->hashApp ();
3864
3965 $ this ->assertIsArray ($ results );
40- $ this ->assertArrayHasKey (' app ' , $ results );
66+ $ this ->assertArrayHasKey ($ this -> fixtureDirectory , $ results );
4167 }
4268
4369 public function testHashDirectoryInvalid (): void
@@ -50,24 +76,48 @@ public function testHashDirectoryInvalid(): void
5076
5177 public function testUniqueHashes (): void
5278 {
53- $ hash1 = $ this ->hasher ->hashDirectory (APPPATH );
54- $ hash2 = $ this ->hasher ->hashDirectory (SYSTEMPATH );
79+ $ hash1 = $ this ->hasher ->hashDirectory ($ this -> fixturePath );
80+ $ hash2 = $ this ->hasher ->hashDirectory ($ this -> fixturePathAlt );
5581
5682 $ this ->assertNotSame ($ hash1 , $ hash2 );
5783 }
5884
5985 public function testRepeatableHashes (): void
6086 {
61- $ hash1 = $ this ->hasher ->hashDirectory (APPPATH );
62- $ hash2 = $ this ->hasher ->hashDirectory (APPPATH );
87+ $ hash1 = $ this ->hasher ->hashDirectory ($ this -> fixturePath );
88+ $ hash2 = $ this ->hasher ->hashDirectory ($ this -> fixturePath );
6389
6490 $ this ->assertSame ($ hash1 , $ hash2 );
6591 }
6692
6793 public function testHash (): void
6894 {
95+ $ config = new Toolbar ();
96+ $ config ->watchedDirectories = [$ this ->fixtureDirectory ];
97+ Factories::injectMock ('config ' , Toolbar::class, $ config );
98+
6999 $ expected = md5 (implode ('' , $ this ->hasher ->hashApp ()));
70100
71101 $ this ->assertSame ($ expected , $ this ->hasher ->hash ());
72102 }
103+
104+ private function createFixtureDirectory (string $ path , string $ contents ): void
105+ {
106+ if (! is_dir ($ path )) {
107+ mkdir ($ path , 0777 , true );
108+ }
109+
110+ file_put_contents ($ path . 'index.php ' , $ contents );
111+ }
112+
113+ private function removeFixtureDirectory (string $ path ): void
114+ {
115+ if (is_file ($ path . 'index.php ' )) {
116+ unlink ($ path . 'index.php ' );
117+ }
118+
119+ if (is_dir ($ path )) {
120+ rmdir ($ path );
121+ }
122+ }
73123}
0 commit comments