22
33declare (strict_types=1 );
44
5- namespace RoadRunner \VersionChecker \Tests \Unit ;
5+ namespace RoadRunner \VersionChecker \Tests \Unit \ Version ;
66
77use PHPUnit \Framework \TestCase ;
8+ use RoadRunner \VersionChecker \Environment \EnvironmentInterface ;
89use RoadRunner \VersionChecker \Exception \RoadrunnerNotInstalledException ;
910use RoadRunner \VersionChecker \Process \ProcessInterface ;
1011use RoadRunner \VersionChecker \Version \Installed ;
1112use Symfony \Component \Process \Exception \ProcessFailedException ;
1213
1314final class InstalledTest extends TestCase
1415{
16+ protected function tearDown (): void
17+ {
18+ // clean the cache
19+ (new \ReflectionProperty (Installed::class, 'cachedVersion ' ))->setValue (null );
20+ }
21+
1522 /**
1623 * @dataProvider outputDataProvider
1724 */
@@ -29,6 +36,63 @@ public function testGetInstalledVersion(string $version, string $output): void
2936 $ this ->assertSame ($ version , $ installed ->getInstalledVersion ());
3037 }
3138
39+ public function testCachedVersion (): void
40+ {
41+ $ env = $ this ->createMock (EnvironmentInterface::class);
42+ $ env
43+ // $this->once() is important for this test!
44+ ->expects ($ this ->once ())
45+ ->method ('get ' )
46+ ->with ('RR_VERSION ' )
47+ ->willReturn ('2023.1.0 ' );
48+
49+ $ installed = new Installed (environment: $ env );
50+
51+ $ version = $ installed ->getInstalledVersion ();
52+ $ version2 = $ installed ->getInstalledVersion ();
53+
54+ $ this ->assertSame ('2023.1.0 ' , $ version );
55+ $ this ->assertSame ('2023.1.0 ' , $ version2 );
56+ }
57+
58+ public function getVersionFromEnv (): void
59+ {
60+ $ env = $ this ->createMock (EnvironmentInterface::class);
61+ $ env
62+ ->expects ($ this ->once ())
63+ ->method ('get ' )
64+ ->with ('RR_VERSION ' )
65+ ->willReturn ('2023.1.0 ' );
66+
67+ $ process = $ this ->createMock (ProcessInterface::class);
68+ $ process ->expects ($ this ->never ());
69+
70+ $ installed = new Installed ($ process , $ env );
71+
72+ $ this ->assertSame ('2023.1.0 ' , $ installed ->getInstalledVersion ());
73+ }
74+
75+ public function getVersionFromConsoleCommand (): void
76+ {
77+ $ env = $ this ->createMock (EnvironmentInterface::class);
78+ $ env
79+ ->expects ($ this ->once ())
80+ ->method ('get ' )
81+ ->with ('RR_VERSION ' )
82+ ->willReturn (null );
83+
84+ $ process = $ this ->createMock (ProcessInterface::class);
85+ $ process
86+ ->expects ($ this ->once ())
87+ ->method ('exec ' )
88+ ->with (['./rr ' , '--version ' ])
89+ ->willReturn ('version 2023.1.0 ' );
90+
91+ $ installed = new Installed ($ process , $ env );
92+
93+ $ this ->assertSame ('2023.1.0 ' , $ installed ->getInstalledVersion ());
94+ }
95+
3296 public function testGetInstalledVersionRoadRunnerIsNotInstalled (): void
3397 {
3498 $ process = $ this ->createMock (ProcessInterface::class);
0 commit comments