Skip to content

Commit 66bbe61

Browse files
authored
Merge pull request #2 from omaralalwi/add-tests
add tests
2 parents 3d962e5 + cd37de1 commit 66bbe61

File tree

4 files changed

+83
-17
lines changed

4 files changed

+83
-17
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"role": "Developer"
3030
}
3131
],
32-
"version": "1.0.1",
32+
"version": "1.0.2",
3333
"require": {
3434
"php": "^8.1",
3535
"omaralalwi/php-builders": "^1.0"

tests/PhpPyTest.php

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,66 @@
11
<?php
22

3-
use PHPUnit\Framework\TestCase;
4-
use Omaralalwi\PhpPy\PhpPy;
53
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();
664

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+
});

tests/TestCase.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/fake-path/sum_calculator.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
import json
4+
import random
5+
6+
def main():
7+
# Parse command-line arguments into numbers
8+
try:
9+
numbers = [float(arg) for arg in sys.argv[1:]]
10+
except ValueError:
11+
print(json.dumps({'error': 'All arguments must be numeric.'}))
12+
sys.exit(1)
13+
14+
# Calculate the sum of the numbers
15+
result = sum(numbers)
16+
# Output the result as JSON
17+
print(json.dumps(result))
18+
19+
if __name__ == '__main__':
20+
main()

0 commit comments

Comments
 (0)