You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							37 lines
						
					
					
						
							1.3 KiB
						
					
					
				
			
		
		
	
	
							37 lines
						
					
					
						
							1.3 KiB
						
					
					
				| <?php
 | |
| namespace Tests\ModbusMaster;
 | |
| 
 | |
| use PHPUnit\Framework\TestCase;
 | |
| use React\ChildProcess\Process;
 | |
| use React\EventLoop\Factory;
 | |
| use React\EventLoop\Timer\Timer;
 | |
| 
 | |
| abstract class MockServerTestCase extends TestCase
 | |
| {
 | |
|     public static function executeWithMockServer($packetToRespond, \Closure $closure, $protocol = 'TCP', $answerTimeout = 0, $port = 0)
 | |
|     {
 | |
|         $loop = Factory::create();
 | |
| 
 | |
|         $port = $port ?: mt_rand(40000, 50000);
 | |
|         $process = new Process(PHP_BINARY . ' ' . __DIR__ . DIRECTORY_SEPARATOR . "MockResponseServer.php {$protocol} {$port} {$answerTimeout} {$packetToRespond}");
 | |
| 
 | |
|         $clientData = [];
 | |
|         $loop->addTimer(0.001, function (Timer $timer) use ($process, $closure, $port, &$clientData) {
 | |
|             $process->start($timer->getLoop());
 | |
| 
 | |
|             $process->stdout->on('data', function ($output) use (&$clientData) {
 | |
|                 $clientData[] = $output;
 | |
|             });
 | |
| 
 | |
|             if (strpos(PHP_OS, 'WIN') === false || getenv('MOCKSERVER_TIMEOUT_USEC') !== false) {
 | |
|                 // wait to spin up. needed for linux. unnessecary on Windows 10.
 | |
|                 // Ugly but even with 150ms sleep test run faster on Linux
 | |
|                 usleep(getenv('MOCKSERVER_TIMEOUT_USEC') ?: 150000);
 | |
|             }
 | |
|             $closure($port);
 | |
|         });
 | |
| 
 | |
|         $loop->run();
 | |
|         return $clientData;
 | |
|     }
 | |
| } |