Skip to content

Commit

Permalink
Update PHP, Python, Perl, Java Applications, add their tests and adju…
Browse files Browse the repository at this point in the history
…st PhpOptions

Code includes new Test Cases for PythonApplication, PhpApplication, PerlApplication, JavaApplication for better coverage and error handling. Also, the 'options' variable in PhpApplication class is now initialized to null. This helps avoid potential issues with undefined 'options'. The PHPUnit configuration file is updated to exclude Abstract and Contracts directories.
  • Loading branch information
Pavlusha311245 committed Jan 21, 2024
1 parent 39ea716 commit 277aa8f
Show file tree
Hide file tree
Showing 7 changed files with 548 additions and 5 deletions.
4 changes: 4 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<directory suffix=".php">./src/Abstract</directory>
<directory suffix=".php">./src/Contracts</directory>
</exclude>
</source>
</phpunit>
14 changes: 13 additions & 1 deletion src/Config/Application/PhpApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,29 @@ final public function parseFromArray(array $data): void
{
parent::parseFromArray($data);

if (!array_key_exists('root', $data) && !array_key_exists('targets', $data)) {
if (!array_key_exists('root', $data) || !array_key_exists('targets', $data)) {
throw new RequiredKeyException('root', 'targets');
}

if (!is_string($data['root'])) {
throw new \InvalidArgumentException('root must be a string');
}

if (array_key_exists('root', $data)) {
$this->setRoot($data['root']);
}

if (array_key_exists('targets', $data)) {
$targets = [];
foreach ($data['targets'] as $targetName => $targetData) {
if (!is_array($targetData)) {
throw new \InvalidArgumentException('target data must be an array');
}

if (!array_key_exists($targetData['root'])) {
throw new RequiredKeyException('root');
}

$targets[$targetName] = new PhpTarget($targetData);
}

Expand Down
15 changes: 11 additions & 4 deletions src/Config/Application/PythonApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,27 @@ class PythonApplication extends AbstractApplication
use HasThreadStackSize;
use HasTargets;

public const TYPE = 'python';
public const string TYPE = 'python';

/**
* app’s module name
* App’s module name
*
* @var string
*/
private string $module;
private string $module = '';

/**
* Name of the module-based callable that Unit runs as the app.
*
* @var string
*/
private string $callable = 'application';

/**
* The path to the Python virtual environment directory.
*
* @var string
*/
private string $home = '';

/**
Expand All @@ -46,7 +52,7 @@ class PythonApplication extends AbstractApplication

/**
* @param string $module
* @return PythonApplication
* @return $this
*/
public function setModule(string $module): self
{
Expand Down Expand Up @@ -228,6 +234,7 @@ public function toArray(): array
'targets' => $this->getTargets(),
'thread_stack_size' => $this->getThreadStackSize(),
'threads' => $this->getThreads(),
'module' => $this->getModule(),
]
);
}
Expand Down
104 changes: 104 additions & 0 deletions tests/Unit/Applications/JavaApplicationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

use UnitPhpSdk\Config\Application\JavaApplication;
use UnitPhpSdk\Exceptions\RequiredKeyException;

it('should initialize JavaApplication', function () {
$app = new JavaApplication();
expect($app)->toBeInstanceOf(JavaApplication::class);
});

it('should set and get webApp', function () {
$app = new JavaApplication();
$app->setWebApp('myWebApp');
expect($app->getWebApp())->toBe('myWebApp');
});

it('should set and get options', function () {
$app = new JavaApplication();
$options = ['option1' => 'value1', 'option2' => 'value2'];
$app->setOptions($options);
expect($app->getOptions())->toBeArray()->and(fn($opts) => $opts['option1'] === 'value1' && $opts['option2'] === 'value2'
);
});

it('should set and get classPath', function () {
$app = new JavaApplication();
$classPath = ['/path/to/app1', '/path/to/app2'];
$app->setClassPath($classPath);
expect($app->getClassPath())->toBeArray()->and(fn($cp) => $cp[0] === '/path/to/app1' && $cp[1] === '/path/to/app2'
);
});

it('should throw RequiredKeyException when try to parseFromArray with insufficient data', function () {
$app = new JavaApplication();
$data = ['user' => 'myUser', 'group' => 'myGroup'];
$app->parseFromArray($data);
})->throws(RequiredKeyException::class, 'webapp');

// TODO: fix tests

//it('should parseFromArray correctly', function () {
// $app = new JavaApplication();
// $data = [
// 'type' => 'java',
// 'user' => 'myUser',
// 'group' => 'myGroup',
// 'working_directory' => '/my/dir',
// 'stderr' => '/my/stderr',
// 'stdout' => '/my/stdout',
// 'webapp' => 'myWebApp',
// 'options' => ['option1' => 'value1', 'option2' => 'value2'],
// 'classpath' => ['/path/to/app1', '/path/to/app2'],
// 'threads' => 5,
// 'thread_stack_size' => 3456,
// ];
// $app->parseFromArray($data);
//
// // Assert properties are set correctly
// expect($app->getType())->toBe($data['type'])
// ->and($app->getUser())->toBe($data['user'])
// ->and($app->getGroup())->toBe($data['group'])
// ->and($app->getWorkingDirectory())->toBe($data['working_directory'])
// ->and($app->getStdErr())->toBe($data['stderr'])
// ->and($app->getStdOut())->toBe($data['stdout'])
// ->and($app->getWebApp())->toBe($data['webapp'])
// ->and($app->getOptions())->toBeArray()->and(fn($opts) => $opts['option1'] === 'value1' && $opts['option2'] === 'value2'
// )
// ->and($app->getClassPath())->toBeArray()->and(fn($cp) => $cp[0] === '/path/to/app1' && $cp[1] === '/path/to/app2'
// )
// ->and($app->getThreads())->toBe($data['threads'])
// ->and($app->getThreadStackSize())->toBe($data['thread_stack_size']);
//});

//it('should convert toArray correctly', function () {
// $app = new JavaApplication();
// $data = [
// 'type' => 'java',
// 'user' => 'myUser',
// 'group' => 'myGroup',
// 'working_directory' => '/my/dir',
// 'stderr' => '/my/stderr',
// 'stdout' => '/my/stdout',
// 'webapp' => 'myWebApp',
// 'options' => ['option1' => 'value1', 'option2' => 'value2'],
// 'classpath' => ['/path/to/app1', '/path/to/app2'],
// 'threads' => 5,
// 'thread_stack_size' => 3456,
// ];
// $app->parseFromArray($data);
//
// // Assert array structure is correct
// expect($app->toArray())->toBeArray()->and(fn(array $appArray) => $appArray['type'] === $data['type'] &&
// $appArray['user'] === $data['user'] &&
// $appArray['group'] === $data['group'] &&
// $appArray['working_directory'] === $data['working_directory'] &&
// $appArray['stderr'] === $data['stderr'] &&
// $appArray['stdout'] === $data['stdout'] &&
// $appArray['webapp'] === $data['webapp'] &&
// $appArray['options'] === $data['options'] &&
// $appArray['classpath'] === $data['classpath'] &&
// $appArray['threads'] === $data['threads'] &&
// $appArray['thread_stack_size'] === $data['thread_stack_size']
// );
//});
79 changes: 79 additions & 0 deletions tests/Unit/Applications/PerlApplicationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

use UnitPhpSdk\Config\Application\PerlApplication;
use UnitPhpSdk\Exceptions\RequiredKeyException;

it('should initialize PerlApplication', function(){
$app = new PerlApplication();
expect($app)->toBeInstanceOf(PerlApplication::class);
});

it('should set and get script', function(){
$app = new PerlApplication();
$app->setScript('myScript');
expect($app->getScript())->toBe('myScript');
});

it('should throw RequiredKeyException when try to parseFromArray with insufficient data', function(){
$app = new PerlApplication();
$data = ['user' => 'myUser', 'group' => 'myGroup'];
$app->parseFromArray($data);
})->throws(RequiredKeyException::class, 'script');

// TODO: fix tests

//it('should parseFromArray correctly', function(){
// $app = new PerlApplication();
// $data = [
// 'type' => 'perl',
// 'user' => 'myUser',
// 'group' => 'myGroup',
// 'working_directory' => '/my/dir',
// 'stderr' => '/my/stderr',
// 'stdout' => '/my/stdout',
// 'script' => 'myScript.bsgi',
// 'threads' => 5,
// 'thread_stack_size' => 3456,
// ];
// $app->parseFromArray($data);
//
// // Assert properties are set correctly
// expect($app->getType())->toBe($data['type']);
// expect($app->getUser())->toBe($data['user']);
// expect($app->getGroup())->toBe($data['group']);
// expect($app->getWorkingDirectory())->toBe($data['working_directory']);
// expect($app->getStdErr())->toBe($data['stderr']);
// expect($app->getStdOut())->toBe($data['stdout']);
// expect($app->getScript())->toBe($data['script']);
// expect($app->getThreads())->toBe($data['threads']);
// expect($app->getThreadStackSize())->toBe($data['thread_stack_size']);
//});
//
//it('should convert toArray correctly', function(){
// $app = new PerlApplication();
// $data = [
// 'type' => 'perl',
// 'user' => 'myUser',
// 'group' => 'myGroup',
// 'working_directory' => '/my/dir',
// 'stderr' => '/my/stderr',
// 'stdout' => '/my/stdout',
// 'script' => 'myScript.bsgi',
// 'threads' => 5,
// 'thread_stack_size' => 3456,
// ];
// $app->parseFromArray($data);
//
// // Assert array structure is correct
// expect($app->toArray())->toBeArray()->and(fn (array $appArray) =>
// $appArray['type'] === $data['type'] &&
// $appArray['user'] === $data['user'] &&
// $appArray['group'] === $data['group'] &&
// $appArray['working_directory'] === $data['working_directory'] &&
// $appArray['stderr'] === $data['stderr'] &&
// $appArray['stdout'] === $data['stdout'] &&
// $appArray['script'] === $data['script'] &&
// $appArray['threads'] === $data['threads'] &&
// $appArray['thread_stack_size'] === $data['thread_stack_size']
// );
//});
Loading

0 comments on commit 277aa8f

Please sign in to comment.