77use Kocal \BiomeJsBundle \BiomeJsBinary ;
88use PHPUnit \Framework \Attributes \DataProvider ;
99use PHPUnit \Framework \TestCase ;
10+ use Symfony \Component \Cache \Adapter \ArrayAdapter ;
1011use Symfony \Component \Filesystem \Filesystem ;
1112use Symfony \Component \HttpClient \MockHttpClient ;
1213use Symfony \Component \HttpClient \Response \JsonMockResponse ;
1314use Symfony \Component \HttpClient \Response \MockResponse ;
14- use Symfony \Contracts \HttpClient \HttpClientInterface ;
1515
1616final class BiomeJsBinaryTest extends TestCase
1717{
1818 private const BINARY_DOWNLOAD_DIR = __DIR__ . '/fixtures/var/download ' ;
1919
20- private HttpClientInterface $ httpClient ;
20+ private MockHttpClient $ httpClient ;
21+
22+ private ArrayAdapter $ cache ;
2123
2224 protected function setUp (): void
2325 {
@@ -51,6 +53,8 @@ protected function setUp(): void
5153
5254 return new MockResponse ('Not Found ' , ['http_code ' => 404 ]);
5355 });
56+
57+ $ this ->cache = new ArrayAdapter ();
5458 }
5559
5660 /**
@@ -71,6 +75,7 @@ public function testBinaryIsDownloadedIfNotExists(?string $passedVersion, string
7175 __DIR__ ,
7276 self ::BINARY_DOWNLOAD_DIR ,
7377 $ passedVersion ,
78+ $ this ->cache ,
7479 $ this ->httpClient ,
7580 );
7681 $ process = $ binary ->createProcess (['check ' , '--apply ' , '*.{js,ts} ' ]);
@@ -83,5 +88,34 @@ public function testBinaryIsDownloadedIfNotExists(?string $passedVersion, string
8388 sprintf ($ expectedTemplate , self ::BINARY_DOWNLOAD_DIR . '/ ' . $ expectedVersion . '/ ' . BiomeJsBinary::getBinaryName ()),
8489 $ process ->getCommandLine ()
8590 );
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 ;
86120 }
87121}
0 commit comments