1+ <?php
2+
3+ namespace Tests ;
4+
5+ use PHPUnit \Framework \TestCase ;
6+ use VoltTest \Platform ;
7+
8+ Class BinaryTest extends TestCase
9+ {
10+ private string $ binaryPath ;
11+
12+ protected function setUp (): void
13+ {
14+ $ this ->binaryPath = Platform::getBinaryPath ();
15+ $ this ->assertTrue (file_exists ($ this ->binaryPath ), "Binary does not exist at: {$ this ->binaryPath }" );
16+ $ this ->assertTrue (is_executable ($ this ->binaryPath ), "Binary is not executable " );
17+ }
18+
19+ private function runCommand (string $ cmd ): array
20+ {
21+ $ output = [];
22+ $ returnVar = -1 ;
23+ exec ($ cmd . " 2>&1 " , $ output , $ returnVar );
24+ return ['output ' => $ output , 'code ' => $ returnVar ];
25+ }
26+
27+ public function testBinaryBasicExecution (): void
28+ {
29+ $ result = $ this ->runCommand (escapeshellarg ($ this ->binaryPath ));
30+ $ this ->assertNotEmpty ($ result ['output ' ], "Binary should produce some output " );
31+ }
32+
33+ public function testBinaryHelp (): void
34+ {
35+ $ result = $ this ->runCommand (escapeshellarg ($ this ->binaryPath ) . " -h " );
36+ $ this ->assertNotEmpty ($ result ['output ' ], "Help command should produce output " );
37+ $ this ->assertStringContainsString ("Usage " , implode ("\n" , $ result ['output ' ]), "Help output should contain usage information " );
38+ }
39+
40+ public function testBinaryVersion (): void
41+ {
42+ $ result = $ this ->runCommand (escapeshellarg ($ this ->binaryPath ) . " -v " );
43+ $ this ->assertNotEmpty ($ result ['output ' ], "Version command should produce output " );
44+ }
45+
46+ public function testBinaryWithConfig (): void
47+ {
48+ // Create test configuration
49+ $ config = [
50+ 'name ' => 'test ' ,
51+ 'description ' => 'test ' ,
52+ 'virtual_users ' => 1 ,
53+ 'duration ' => '5s ' ,
54+ 'target ' => [
55+ 'url ' => 'http://example.com ' ,
56+ 'idle_timeout ' => '30s '
57+ ],
58+ 'scenarios ' => [[
59+ 'name ' => 'test ' ,
60+ 'steps ' => [[
61+ 'name ' => 'test ' ,
62+ 'request ' => [
63+ 'method ' => 'GET ' ,
64+ 'url ' => 'http://example.com '
65+ ]
66+ ]]
67+ ]]
68+ ];
69+
70+ // Create temporary config file
71+ $ configFile = tempnam (sys_get_temp_dir (), 'volt_test_ ' );
72+ $ this ->assertNotFalse ($ configFile , "Failed to create temporary config file " );
73+
74+ file_put_contents ($ configFile , json_encode ($ config , JSON_PRETTY_PRINT ));
75+
76+ try {
77+ // Test different ways of passing config
78+ $ testCommands = [
79+ 'standard ' => escapeshellarg ($ this ->binaryPath ) . " -config " . escapeshellarg ($ configFile ),
80+ 'short ' => escapeshellarg ($ this ->binaryPath ) . " -f " . escapeshellarg ($ configFile ),
81+ 'input ' => escapeshellarg ($ this ->binaryPath ) . " -input " . escapeshellarg ($ configFile ),
82+ 'stdin ' => "type " . escapeshellarg ($ configFile ) . " | " . escapeshellarg ($ this ->binaryPath )
83+ ];
84+
85+ foreach ($ testCommands as $ type => $ cmd ) {
86+ $ result = $ this ->runCommand ($ cmd );
87+ $ output = implode ("\n" , $ result ['output ' ]);
88+
89+ $ this ->assertNotEmpty ($ output , "Command $ type should produce output " );
90+ $ this ->assertStringNotContainsString ("panic: " , $ output , "Command $ type should not panic " );
91+ $ this ->assertStringNotContainsString ("fatal error: " , $ output , "Command $ type should not have fatal errors " );
92+ }
93+ } finally {
94+ // Cleanup
95+ if (file_exists ($ configFile )) {
96+ unlink ($ configFile );
97+ }
98+ }
99+ }
100+
101+ public function testBinaryEnvironment (): void
102+ {
103+ // Get environment information
104+ $ this ->assertNotFalse (getenv ('PATH ' ), "PATH environment variable should be set " );
105+ $ this ->assertNotFalse (getenv ('TEMP ' ), "TEMP environment variable should be set " );
106+ $ this ->assertDirectoryIsWritable (sys_get_temp_dir (), "Temp directory should be writable " );
107+
108+ // Check binary metadata
109+ $ this ->assertGreaterThan (0 , filesize ($ this ->binaryPath ), "Binary file should not be empty " );
110+
111+ // Verify working directory permissions
112+ $ cwd = getcwd ();
113+ $ this ->assertNotFalse ($ cwd , "Should be able to get current working directory " );
114+ $ this ->assertDirectoryIsWritable ($ cwd , "Working directory should be writable " );
115+ }
116+
117+ public function testBinaryProcessHandling (): void
118+ {
119+ // Start binary with minimal config
120+ $ config = [
121+ 'name ' => 'process_test ' ,
122+ 'virtual_users ' => 1 ,
123+ 'duration ' => '1s ' ,
124+ 'target ' => ['url ' => 'http://example.com ' , 'idle_timeout ' => '5s ' ],
125+ 'scenarios ' => [[
126+ 'name ' => 'test ' ,
127+ 'steps ' => [[
128+ 'name ' => 'test ' ,
129+ 'request ' => ['method ' => 'GET ' , 'url ' => 'http://example.com ' ]
130+ ]]
131+ ]]
132+ ];
133+
134+ $ configFile = tempnam (sys_get_temp_dir (), 'volt_proc_ ' );
135+ $ this ->assertNotFalse ($ configFile , "Failed to create temporary config file " );
136+
137+ file_put_contents ($ configFile , json_encode ($ config , JSON_PRETTY_PRINT ));
138+
139+ try {
140+ $ cmd = escapeshellarg ($ this ->binaryPath ) . " -config " . escapeshellarg ($ configFile );
141+ $ result = $ this ->runCommand ($ cmd );
142+
143+ $ output = implode ("\n" , $ result ['output ' ]);
144+ $ this ->assertNotEmpty ($ output , "Process should produce output " );
145+ $ this ->assertNotEquals (-1 , $ result ['code ' ], "Process should exit normally " );
146+ } finally {
147+ if (file_exists ($ configFile )) {
148+ unlink ($ configFile );
149+ }
150+ }
151+ }
152+ }
0 commit comments