Skip to content

Commit

Permalink
New tests for multi-proc mode
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Nov 4, 2021
1 parent e394782 commit 5726837
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 17 deletions.
15 changes: 13 additions & 2 deletions src/CliHelper.php
Expand Up @@ -17,6 +17,7 @@

namespace JBZoo\Cli;

use JBZoo\Utils\Env;
use JBZoo\Utils\Sys;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -107,15 +108,25 @@ public function getErrOutput(): OutputInterface
*/
public static function getRootPath(): string
{
return defined('JBZOO_PATH_ROOT') ? (string)JBZOO_PATH_ROOT : '';
$rootPath = defined('JBZOO_PATH_ROOT') ? (string)JBZOO_PATH_ROOT : null;
if (!$rootPath) {
return Env::string('JBZOO_PATH_ROOT');
}

return $rootPath;
}

/**
* @return string
*/
public static function getBinPath(): string
{
return defined('JBZOO_PATH_BIN') ? (string)JBZOO_PATH_BIN : '';
$binPath = defined('JBZOO_PATH_BIN') ? (string)JBZOO_PATH_BIN : null;
if (!$binPath) {
return Env::string('JBZOO_PATH_BIN');
}

return $binPath;
}

/**
Expand Down
56 changes: 53 additions & 3 deletions tests/CliMultiProcess.php
Expand Up @@ -17,19 +17,69 @@

namespace JBZoo\PHPUnit;

use JBZoo\Utils\Env;

use function JBZoo\Data\json;

/**
* Class CliMultiProcess
* @package JBZoo\PHPUnit
*/
class CliMultiProcess extends PHPUnit
{
public function testNormal()
public function testAsRealExecution()
{
$start = microtime(true);
$output = Helper::executeReal('test:sleep-multi 123 " qwerty " --sleep=1 --profile');
$result = Helper::executeReal('test:sleep-multi 123 " qwerty "', [
'sleep' => 1
], 'JBZOO_TEST_VAR=123456');

$time = microtime(true) - $start;

echo $output;
$outputAsArray = json($result[1])->getArrayCopy();

$expectecContent = implode("\n", [
'Sleep : 1',
'Arg #1: 123',
'Arg #2: qwerty ',
'Arg #3: QWERTY-3',
'Env Var: 123456',
]);

isSame([
"Started: 1\n{$expectecContent}\nFinished: 1",
"Started: 2\n{$expectecContent}\nFinished: 2",
"Started: 3\n{$expectecContent}\nFinished: 3",
"Started: 4\n{$expectecContent}\nFinished: 4",
"Started: 5\n{$expectecContent}\nFinished: 5",
], $outputAsArray);

isTrue($time < 5);
}

public function testAsVirtalExecution()
{
$start = microtime(true);
$result = Helper::executeVirtaul('test:sleep-multi', ['sleep' => 1]);
$time = microtime(true) - $start;

$outputAsArray = json($result)->getArrayCopy();

$expectecContent = implode("\n", [
'Sleep : 1',
'Arg #1: QWERTY-1',
'Arg #2: QWERTY-2',
'Arg #3: QWERTY-3',
'Env Var: ',
]);

isSame([
"Started: 1\n{$expectecContent}\nFinished: 1",
"Started: 2\n{$expectecContent}\nFinished: 2",
"Started: 3\n{$expectecContent}\nFinished: 3",
"Started: 4\n{$expectecContent}\nFinished: 4",
"Started: 5\n{$expectecContent}\nFinished: 5",
], $outputAsArray);

isTrue($time < 5);
}
Expand Down
24 changes: 16 additions & 8 deletions tests/Helper.php
Expand Up @@ -21,6 +21,7 @@
use JBZoo\TestApp\Commands\TestCliOptions;
use JBZoo\TestApp\Commands\TestCliStdIn;
use JBZoo\TestApp\Commands\TestSleep;
use JBZoo\TestApp\Commands\TestSleepMulti;
use JBZoo\Utils\Cli;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\BufferedOutput;
Expand All @@ -34,21 +35,22 @@ class Helper extends PHPUnit
{
/**
* @param string $command
* @param array $args
* @param array $options
* @param string $preAction
* @param string $postAction
* @return array
*/
public static function executeReal(
string $command,
array $args = [],
array $options = [],
string $preAction = '',
string $postAction = ''
): array {
$cwd = __DIR__ . '/fake-app';
$args['no-ansi'] = null;
$options['no-ansi'] = null;

$realCommand = $preAction . 'php ' . Cli::build("{$cwd}/cli-wrapper.php {$command}", $args) . '' . $postAction;
$realCommand = $preAction . ' php ' . Cli::build("{$cwd}/cli-wrapper.php {$command}",
$options) . '' . $postAction;
$realCommand = trim($realCommand);

$process = Process::fromShellCommandline($realCommand, $cwd, null, null, 3600);
Expand All @@ -59,20 +61,26 @@ public static function executeReal(

/**
* @param string $command
* @param array $args
* @param array $inputString
* @return string
*/
public static function executeVirtaul(string $command, array $args = []): string
public static function executeVirtaul(string $command, array $options = []): string
{
$rootPath = dirname(__DIR__);

putenv("JBZOO_PATH_BIN={$rootPath}/tests/fake-app/cli-wrapper.php");
putenv("JBZOO_PATH_ROOT={$rootPath}/tests/fake-app");

$application = new CliApplication();
$application->add(new TestCliOptions());
$application->add(new TestCliStdIn());
$application->add(new TestSleep());
$application->add(new TestSleepMulti());
$command = $application->find($command);

$buffer = new BufferedOutput();
$args = new StringInput(Cli::build('', $args));
$exitCode = $command->run($args, $buffer);
$inputString = new StringInput(Cli::build('', $options));
$exitCode = $command->run($inputString, $buffer);

if ($exitCode) {
throw new Exception($buffer->fetch());
Expand Down
29 changes: 25 additions & 4 deletions tests/fake-app/Commands/TestSleepMulti.php
Expand Up @@ -18,9 +18,12 @@
namespace JBZoo\TestApp\Commands;

use JBZoo\Cli\CliCommandMultiProc;
use JBZoo\Utils\Env;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

use function JBZoo\Data\json;

/**
* Class TestSleepMulti
* @package JBZoo\TestApp\Commands
Expand All @@ -35,9 +38,9 @@ protected function configure(): void
$this
->setName('test:sleep-multi')
->addOption('sleep', null, InputOption::VALUE_REQUIRED, 'Timeout in seconds to sleep the PHP script', 5)
->addArgument('arg-1', InputArgument::REQUIRED, 'Custom Agrument #1')
->addArgument('arg-2', InputArgument::REQUIRED, 'Custom Agrument #2')
->addArgument('arg-3', InputArgument::OPTIONAL, 'Custom Agrument #3', 'qwerty');
->addArgument('arg-1', InputArgument::OPTIONAL, 'Custom Agrument #1', 'QWERTY-1')
->addArgument('arg-2', InputArgument::OPTIONAL, 'Custom Agrument #2', 'QWERTY-2')
->addArgument('arg-3', InputArgument::OPTIONAL, 'Custom Agrument #3', 'QWERTY-3');

parent::configure();
}
Expand All @@ -52,6 +55,8 @@ protected function executeOneProcessAction(string $pmThreadId): int
'Sleep : ' . $this->getOptString('sleep'),
'Arg #1: ' . $this->input->getArgument('arg-1'),
'Arg #2: ' . $this->input->getArgument('arg-2'),
'Arg #3: ' . $this->input->getArgument('arg-3'),
'Env Var: ' . Env::string('JBZOO_TEST_VAR'),
]);

sleep($this->getOptInt('sleep'));
Expand All @@ -66,6 +71,22 @@ protected function executeOneProcessAction(string $pmThreadId): int
*/
protected function getListOfChildIds(): array
{
return ['1', '2', '3', '4', '5', '6', '7', '8'];
return ['1', '2', '3', '4', '5'];
}

/**
* @param array $procPool
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @phan-suppress PhanPluginPossiblyStaticProtectedMethod
* @phan-suppress PhanUnusedProtectedNoOverrideMethodParameter
*/
protected function afterFinishAllProcesses(array $procPool): void
{
$result = [];
foreach ($procPool as $procId => $procInfo) {
$result[] = $procInfo['std_out'];
}

$this->_(json($result));
}
}

0 comments on commit 5726837

Please sign in to comment.