|
1 | 1 | <?php |
2 | 2 |
|
3 | | -use PHPUnit\Framework\TestCase; |
4 | | -use Omaralalwi\PhpPy\PhpPy; |
5 | 3 | use Omaralalwi\PhpPy\Managers\ConfigManager; |
| 4 | +use Omaralalwi\PhpPy\PhpPy; |
| 5 | + |
| 6 | +test('PhpPy runs the script correctly', function () { |
| 7 | + |
| 8 | + $configManager = new ConfigManager([ |
| 9 | + 'scripts_directory' => __DIR__ . '/../example-scripts', |
| 10 | + 'python_executable' => '/usr/bin/python3', |
| 11 | + 'max_timeout' => 30, |
| 12 | + ]); |
| 13 | + |
| 14 | + $result = PhpPy::build() |
| 15 | + ->setConfig($configManager) |
| 16 | + ->loadScript('sum_calculator.py') |
| 17 | + ->withArguments([10, 20, 30]) |
| 18 | + ->run(); |
| 19 | + |
| 20 | + expect(json_decode($result))->toBe(60.0); |
| 21 | +}); |
| 22 | + |
| 23 | +test('PhpPy throws exception when try to run script outside allowed path', function () { |
| 24 | + |
| 25 | + $configManager = new ConfigManager([ |
| 26 | + 'scripts_directory' => __DIR__ . '/../example-scripts', |
| 27 | + 'python_executable' => '/usr/bin/python3', |
| 28 | + 'max_timeout' => 30, |
| 29 | + ]); |
| 30 | + |
| 31 | + $this->expectException(\InvalidArgumentException::class); |
| 32 | + $this->expectExceptionMessage('Script path does not exist.'); |
| 33 | + |
| 34 | + $result = PhpPy::build() |
| 35 | + ->setConfig($configManager) |
| 36 | + ->loadScript('/fake-path/sum_calculator.py') |
| 37 | + ->withArguments([10, 20, 30]) |
| 38 | + ->run(); |
| 39 | + |
| 40 | + expect(json_decode($result))->toBe('Script path does not exist.'); |
| 41 | +}); |
| 42 | + |
| 43 | +test('withEnvironment throws exception for blacklisted environment variables', function () { |
| 44 | + |
| 45 | + $configManager = new ConfigManager([ |
| 46 | + 'scripts_directory' => __DIR__ . '/../example-scripts', |
| 47 | + 'python_executable' => '/usr/bin/python3', |
| 48 | + 'max_timeout' => 30, |
| 49 | + ]); |
| 50 | + |
| 51 | + $env = [ |
| 52 | + 'LD_LIBRARY_PATH' => 'fake value', |
| 53 | + ]; |
| 54 | + |
| 55 | + $this->expectException(\InvalidArgumentException::class); |
| 56 | + $this->expectExceptionMessage("Environment variable 'LD_LIBRARY_PATH' is not allowed."); |
| 57 | + |
| 58 | + $result = PhpPy::build() |
| 59 | + ->setConfig($configManager) |
| 60 | + ->loadScript('sum_calculator.py') |
| 61 | + ->withArguments([10, 20, 30]) |
| 62 | + ->withEnvironment($env) |
| 63 | + ->run(); |
6 | 64 |
|
7 | | -class PhpPyTest extends TestCase |
8 | | -{ |
9 | | - // will be ready in coming version |
10 | | -} |
| 65 | + expect(json_decode($result))->toBe("Environment variable 'LD_LIBRARY_PATH' is not allowed."); |
| 66 | +}); |
0 commit comments