7
7
use Kocal \BiomeJsBundle \BiomeJsBinary ;
8
8
use PHPUnit \Framework \Attributes \DataProvider ;
9
9
use PHPUnit \Framework \TestCase ;
10
+ use Symfony \Component \Cache \Adapter \ArrayAdapter ;
10
11
use Symfony \Component \Filesystem \Filesystem ;
11
12
use Symfony \Component \HttpClient \MockHttpClient ;
12
13
use Symfony \Component \HttpClient \Response \JsonMockResponse ;
13
14
use Symfony \Component \HttpClient \Response \MockResponse ;
14
- use Symfony \Contracts \HttpClient \HttpClientInterface ;
15
15
16
16
final class BiomeJsBinaryTest extends TestCase
17
17
{
18
18
private const BINARY_DOWNLOAD_DIR = __DIR__ . '/fixtures/var/download ' ;
19
19
20
- private HttpClientInterface $ httpClient ;
20
+ private MockHttpClient $ httpClient ;
21
+
22
+ private ArrayAdapter $ cache ;
21
23
22
24
protected function setUp (): void
23
25
{
@@ -51,6 +53,8 @@ protected function setUp(): void
51
53
52
54
return new MockResponse ('Not Found ' , ['http_code ' => 404 ]);
53
55
});
56
+
57
+ $ this ->cache = new ArrayAdapter ();
54
58
}
55
59
56
60
/**
@@ -71,6 +75,7 @@ public function testBinaryIsDownloadedIfNotExists(?string $passedVersion, string
71
75
__DIR__ ,
72
76
self ::BINARY_DOWNLOAD_DIR ,
73
77
$ passedVersion ,
78
+ $ this ->cache ,
74
79
$ this ->httpClient ,
75
80
);
76
81
$ process = $ binary ->createProcess (['check ' , '--apply ' , '*.{js,ts} ' ]);
@@ -83,5 +88,34 @@ public function testBinaryIsDownloadedIfNotExists(?string $passedVersion, string
83
88
sprintf ($ expectedTemplate , self ::BINARY_DOWNLOAD_DIR . '/ ' . $ expectedVersion . '/ ' . BiomeJsBinary::getBinaryName ()),
84
89
$ process ->getCommandLine ()
85
90
);
91
+
92
+ $ cacheValues = $ this ->cache ->getValues ();
93
+ if (null !== $ passedVersion && str_starts_with ($ passedVersion , 'v ' )) {
94
+ // A specific version was passed, so no HTTP call expected and no cache call
95
+ self ::assertCount (0 , $ cacheValues );
96
+ } else {
97
+ // No specific version was passed, so an HTTP call expected and cache should be set
98
+ self ::assertCount (1 , $ cacheValues );
99
+ self ::assertSame ($ expectedVersion , unserialize ($ cacheValues [array_key_first ($ cacheValues )]));
100
+
101
+ // Check that the binary is not downloaded again, but the cache is used
102
+ $ binary = $ this ->cloneAndResetBinary ($ binary );
103
+ $ this ->httpClient ->setResponseFactory (fn () => throw new \LogicException ('No HTTP request should be made ' ));
104
+ $ binary ->createProcess (['check ' , '--apply ' , '*.{js,ts} ' ]);
105
+
106
+ $ cacheValues = $ this ->cache ->getValues ();
107
+ self ::assertCount (1 , $ cacheValues );
108
+ self ::assertSame ($ expectedVersion , unserialize ($ cacheValues [array_key_first ($ cacheValues )]));
109
+ }
110
+ }
111
+
112
+ private function cloneAndResetBinary (BiomeJsBinary $ binary ): BiomeJsBinary
113
+ {
114
+ $ binary = clone $ binary ;
115
+
116
+ $ reflProperty = new \ReflectionProperty ($ binary , 'cachedVersion ' );
117
+ $ reflProperty ->setValue ($ binary , null );
118
+
119
+ return clone $ binary ;
86
120
}
87
121
}
0 commit comments