1414use Behat \Gherkin \Exception \CacheException ;
1515use Behat \Gherkin \Node \FeatureNode ;
1616use Behat \Gherkin \Node \ScenarioNode ;
17+ use org \bovigo \vfs \vfsStream ;
18+ use org \bovigo \vfs \vfsStreamDirectory ;
1719use PHPUnit \Framework \TestCase ;
1820use Tests \Behat \Gherkin \Filesystem ;
1921
20- /**
21- * @todo Use VFS
22- */
2322class FileCacheTest extends TestCase
2423{
25- private string $ path ;
26- private FileCache $ cache ;
27-
28- protected function setUp (): void
29- {
30- $ this ->cache = new FileCache ($ this ->path = sys_get_temp_dir () . uniqid ('/gherkin-test ' ));
31- }
32-
3324 public function testIsFreshWhenThereIsNoFile (): void
3425 {
35- $ this ->assertFalse ($ this ->cache ->isFresh ('unexisting ' , time () + 100 ));
26+ $ cache = $ this ->createCache ();
27+
28+ $ this ->assertFalse ($ cache ->isFresh ('unexisting ' , time () + 100 ));
3629 }
3730
3831 public function testIsFreshOnFreshFile (): void
3932 {
33+ $ cache = $ this ->createCache ();
4034 $ feature = new FeatureNode (null , null , [], null , [], '' , '' , null , 1 );
4135
42- $ this -> cache ->write ('some_path ' , $ feature );
36+ $ cache ->write ('some_path ' , $ feature );
4337
44- $ this ->assertFalse ($ this -> cache ->isFresh ('some_path ' , time () + 100 ));
38+ $ this ->assertFalse ($ cache ->isFresh ('some_path ' , time () + 100 ));
4539 }
4640
4741 public function testIsFreshOnOutdated (): void
4842 {
43+ $ cache = $ this ->createCache ();
4944 $ feature = new FeatureNode (null , null , [], null , [], '' , '' , null , 1 );
5045
51- $ this -> cache ->write ('some_path ' , $ feature );
46+ $ cache ->write ('some_path ' , $ feature );
5247
53- $ this ->assertTrue ($ this -> cache ->isFresh ('some_path ' , time () - 100 ));
48+ $ this ->assertTrue ($ cache ->isFresh ('some_path ' , time () - 100 ));
5449 }
5550
5651 public function testCacheAndRead (): void
5752 {
53+ $ cache = $ this ->createCache ();
5854 $ scenarios = [new ScenarioNode ('Some scenario ' , [], [], '' , 1 )];
5955 $ feature = new FeatureNode ('Some feature ' , 'some description ' , [], null , $ scenarios , '' , '' , null , 1 );
6056
61- $ this -> cache ->write ('some_feature ' , $ feature );
62- $ featureRead = $ this -> cache ->read ('some_feature ' );
57+ $ cache ->write ('some_feature ' , $ feature );
58+ $ featureRead = $ cache ->read ('some_feature ' );
6359
6460 $ this ->assertEquals ($ feature , $ featureRead );
6561 }
6662
6763 public function testBrokenCacheRead (): void
6864 {
65+ $ root = $ this ->createRoot ();
66+ $ cache = $ this ->createCache ($ root );
6967 // First, write a valid cache and find the file that was written
70- $ this -> cache ->write (
68+ $ cache ->write (
7169 'broken_feature ' ,
7270 new FeatureNode (null , null , [], null , [], '' , '' , null , 1 ),
7371 );
74- $ files = Filesystem::find ( " $ this -> path /**/* .feature.cache" );
72+ $ files = Filesystem::findFilesRecursively ( $ root -> url (), ' * .feature.cache' );
7573
7674 $ this ->assertCount (1 , $ files , 'Cache should have written a single file ' );
7775
@@ -80,17 +78,28 @@ public function testBrokenCacheRead(): void
8078
8179 $ this ->expectException (CacheException::class);
8280
83- $ this -> cache ->read ('broken_feature ' );
81+ $ cache ->read ('broken_feature ' );
8482 }
8583
8684 public function testUnwriteableCacheDir (): void
8785 {
86+ $ root = vfsStream::setup ();
87+ $ root ->chmod (0 );
88+
8889 $ this ->expectException (CacheException::class);
8990
90- if (PHP_OS_FAMILY === 'Windows ' ) {
91- new FileCache ('C: \\Windows \\System32 \\drivers \\etc ' );
92- } else {
93- new FileCache ('/dev/null/gherkin-test ' );
94- }
91+ new FileCache ($ root ->url ());
92+ }
93+
94+ private function createRoot (): vfsStreamDirectory
95+ {
96+ return vfsStream::setup ();
97+ }
98+
99+ private function createCache (?vfsStreamDirectory $ root = null ): FileCache
100+ {
101+ return new FileCache (
102+ ($ root ?? $ this ->createRoot ())->url ()
103+ );
95104 }
96105}
0 commit comments