Skip to content

Commit

Permalink
Merge pull request #616 from tienvx/isolate-transition-expression-fro…
Browse files Browse the repository at this point in the history
…m-commands-values

Isolate transition expression from commands values
  • Loading branch information
tienvx committed Apr 27, 2022
2 parents fe84f38 + 5651459 commit 827514b
Show file tree
Hide file tree
Showing 37 changed files with 362 additions and 207 deletions.
16 changes: 9 additions & 7 deletions src/Command/CommandPreprocessor.php
Expand Up @@ -2,24 +2,24 @@

namespace Tienvx\Bundle\MbtBundle\Command;

use SingleColorPetrinet\Model\ColorInterface;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\Command;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class CommandPreprocessor implements CommandPreprocessorInterface
{
public function process(CommandInterface $command, ColorInterface $color): CommandInterface
public function process(CommandInterface $command, ValuesInterface $values): CommandInterface
{
$processed = new Command();
$processed->setCommand($command->getCommand());
$processed->setTarget(
$command->getTarget()
? $this->replaceVariables($command->getTarget(), $color->getValues())
? $this->replaceVariables($command->getTarget(), $values->getValues())
: $command->getTarget()
);
$processed->setValue(
$command->getValue()
? $this->replaceVariables($command->getValue(), $color->getValues())
? $this->replaceVariables($command->getValue(), $values->getValues())
: $command->getValue()
);

Expand All @@ -28,8 +28,10 @@ public function process(CommandInterface $command, ColorInterface $color): Comma

protected function replaceVariables(string $text, array $values): string
{
return preg_replace_callback('/\$\{(.*?)\}/', function ($matches) use ($values) {
return $values[$matches[1]] ?? $matches[1];
}, $text);
return preg_replace_callback(
'/\$\{(.*?)\}/',
fn (array $matches): string => $values[$matches[1]] ?? $matches[1],
$text
);
}
}
4 changes: 2 additions & 2 deletions src/Command/CommandPreprocessorInterface.php
Expand Up @@ -2,10 +2,10 @@

namespace Tienvx\Bundle\MbtBundle\Command;

use SingleColorPetrinet\Model\ColorInterface;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

interface CommandPreprocessorInterface
{
public function process(CommandInterface $command, ColorInterface $color): CommandInterface;
public function process(CommandInterface $command, ValuesInterface $values): CommandInterface;
}
4 changes: 2 additions & 2 deletions src/Command/CommandRunnerInterface.php
Expand Up @@ -3,8 +3,8 @@
namespace Tienvx\Bundle\MbtBundle\Command;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use SingleColorPetrinet\Model\ColorInterface;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

interface CommandRunnerInterface
{
Expand Down Expand Up @@ -35,5 +35,5 @@ public function validateTarget(CommandInterface $command): bool;

public function supports(CommandInterface $command): bool;

public function run(CommandInterface $command, ColorInterface $color, RemoteWebDriver $driver): void;
public function run(CommandInterface $command, ValuesInterface $values, RemoteWebDriver $driver): void;
}
6 changes: 3 additions & 3 deletions src/Command/CommandRunnerManager.php
Expand Up @@ -3,8 +3,8 @@
namespace Tienvx\Bundle\MbtBundle\Command;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use SingleColorPetrinet\Model\ColorInterface;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class CommandRunnerManager implements CommandRunnerManagerInterface
{
Expand Down Expand Up @@ -43,11 +43,11 @@ public function validateTarget(CommandInterface $command): bool
return false;
}

public function run(CommandInterface $command, ColorInterface $color, RemoteWebDriver $driver): void
public function run(CommandInterface $command, ValuesInterface $values, RemoteWebDriver $driver): void
{
foreach ($this->runners as $runner) {
if ($runner instanceof CommandRunnerInterface && $runner->supports($command)) {
$runner->run($this->commandPreprocessor->process($command, $color), $color, $driver);
$runner->run($this->commandPreprocessor->process($command, $values), $values, $driver);
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Command/CommandRunnerManagerInterface.php
Expand Up @@ -3,8 +3,8 @@
namespace Tienvx\Bundle\MbtBundle\Command;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use SingleColorPetrinet\Model\ColorInterface;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

interface CommandRunnerManagerInterface
{
Expand All @@ -16,5 +16,5 @@ public function getCommandsRequireValue(): array;

public function validateTarget(CommandInterface $command): bool;

public function run(CommandInterface $command, ColorInterface $color, RemoteWebDriver $driver): void;
public function run(CommandInterface $command, ValuesInterface $values, RemoteWebDriver $driver): void;
}
4 changes: 2 additions & 2 deletions src/Command/Runner/AlertCommandRunner.php
Expand Up @@ -3,9 +3,9 @@
namespace Tienvx\Bundle\MbtBundle\Command\Runner;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use SingleColorPetrinet\Model\ColorInterface;
use Tienvx\Bundle\MbtBundle\Command\CommandRunner;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class AlertCommandRunner extends CommandRunner
{
Expand Down Expand Up @@ -38,7 +38,7 @@ public function getCommandsRequireValue(): array
return [];
}

public function run(CommandInterface $command, ColorInterface $color, RemoteWebDriver $driver): void
public function run(CommandInterface $command, ValuesInterface $values, RemoteWebDriver $driver): void
{
switch ($command->getCommand()) {
case self::ACCEPT_ALERT:
Expand Down
6 changes: 3 additions & 3 deletions src/Command/Runner/AssertionRunner.php
Expand Up @@ -4,9 +4,9 @@

use Exception;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use SingleColorPetrinet\Model\ColorInterface;
use Tienvx\Bundle\MbtBundle\Command\CommandRunner;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class AssertionRunner extends CommandRunner
{
Expand Down Expand Up @@ -72,11 +72,11 @@ public function getCommandsRequireValue(): array
];
}

public function run(CommandInterface $command, ColorInterface $color, RemoteWebDriver $driver): void
public function run(CommandInterface $command, ValuesInterface $values, RemoteWebDriver $driver): void
{
switch ($command->getCommand()) {
case self::ASSERT:
$actual = $color->getValue($command->getTarget());
$actual = $values->getValue($command->getTarget());
$this->assert(
$actual === $command->getValue(),
sprintf('Actual value "%s" did not match "%s"', $actual, $command->getValue())
Expand Down
4 changes: 2 additions & 2 deletions src/Command/Runner/KeyboardCommandRunner.php
Expand Up @@ -3,9 +3,9 @@
namespace Tienvx\Bundle\MbtBundle\Command\Runner;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use SingleColorPetrinet\Model\ColorInterface;
use Tienvx\Bundle\MbtBundle\Command\CommandRunner;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class KeyboardCommandRunner extends CommandRunner
{
Expand All @@ -30,7 +30,7 @@ public function getCommandsRequireValue(): array
return [];
}

public function run(CommandInterface $command, ColorInterface $color, RemoteWebDriver $driver): void
public function run(CommandInterface $command, ValuesInterface $values, RemoteWebDriver $driver): void
{
switch ($command->getCommand()) {
case self::TYPE:
Expand Down
4 changes: 2 additions & 2 deletions src/Command/Runner/MouseCommandRunner.php
Expand Up @@ -5,9 +5,9 @@
use Exception;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverPoint;
use SingleColorPetrinet\Model\ColorInterface;
use Tienvx\Bundle\MbtBundle\Command\CommandRunner;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class MouseCommandRunner extends CommandRunner
{
Expand Down Expand Up @@ -72,7 +72,7 @@ public function getCommandsRequireValue(): array
];
}

public function run(CommandInterface $command, ColorInterface $color, RemoteWebDriver $driver): void
public function run(CommandInterface $command, ValuesInterface $values, RemoteWebDriver $driver): void
{
switch ($command->getCommand()) {
case self::ADD_SELECTION:
Expand Down
8 changes: 4 additions & 4 deletions src/Command/Runner/ScriptCommandRunner.php
Expand Up @@ -3,9 +3,9 @@
namespace Tienvx\Bundle\MbtBundle\Command\Runner;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use SingleColorPetrinet\Model\ColorInterface;
use Tienvx\Bundle\MbtBundle\Command\CommandRunner;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class ScriptCommandRunner extends CommandRunner
{
Expand Down Expand Up @@ -35,7 +35,7 @@ public function getCommandsRequireValue(): array
];
}

public function run(CommandInterface $command, ColorInterface $color, RemoteWebDriver $driver): void
public function run(CommandInterface $command, ValuesInterface $values, RemoteWebDriver $driver): void
{
switch ($command->getCommand()) {
case self::RUN_SCRIPT:
Expand All @@ -44,13 +44,13 @@ public function run(CommandInterface $command, ColorInterface $color, RemoteWebD
case self::EXECUTE_SCRIPT:
$value = $driver->executeScript($command->getTarget());
if ($command->getValue()) {
$color->setValue($command->getValue(), $value);
$values->setValue($command->getValue(), $value);
}
break;
case self::EXECUTE_ASYNC_SCRIPT:
$value = $driver->executeAsyncScript($command->getTarget());
if ($command->getValue()) {
$color->setValue($command->getValue(), $value);
$values->setValue($command->getValue(), $value);
}
break;
default:
Expand Down
20 changes: 10 additions & 10 deletions src/Command/Runner/StoreCommandRunner.php
Expand Up @@ -3,9 +3,9 @@
namespace Tienvx\Bundle\MbtBundle\Command\Runner;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use SingleColorPetrinet\Model\ColorInterface;
use Tienvx\Bundle\MbtBundle\Command\CommandRunner;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class StoreCommandRunner extends CommandRunner
{
Expand Down Expand Up @@ -58,48 +58,48 @@ public function getCommandsRequireValue(): array
];
}

public function run(CommandInterface $command, ColorInterface $color, RemoteWebDriver $driver): void
public function run(CommandInterface $command, ValuesInterface $values, RemoteWebDriver $driver): void
{
switch ($command->getCommand()) {
case self::STORE:
$color->setValue($command->getValue(), $command->getTarget());
$values->setValue($command->getValue(), $command->getTarget());
break;
case self::STORE_ATTRIBUTE:
list($elementLocator, $attributeName) = explode('@', $command->getTarget(), 2);
$color->setValue(
$values->setValue(
$command->getValue(),
$driver->findElement($this->getSelector($elementLocator))->getAttribute($attributeName)
);
break;
case self::STORE_ELEMENT_COUNT:
$color->setValue(
$values->setValue(
$command->getValue(),
count($driver->findElements($this->getSelector($command->getTarget())))
);
break;
case self::STORE_JSON:
$color->setValue(
$values->setValue(
$command->getValue(),
json_decode($command->getTarget())
);
break;
case self::STORE_TEXT:
$color->setValue(
$values->setValue(
$command->getValue(),
$driver->findElement($this->getSelector($command->getTarget()))->getText()
);
break;
case self::STORE_TITLE:
$color->setValue($command->getTarget(), $driver->getTitle());
$values->setValue($command->getTarget(), $driver->getTitle());
break;
case self::STORE_VALUE:
$color->setValue(
$values->setValue(
$command->getValue(),
$driver->findElement($this->getSelector($command->getTarget()))->getAttribute('value')
);
break;
case self::STORE_WINDOW_HANDLE:
$color->setValue($command->getTarget(), $driver->getWindowHandle());
$values->setValue($command->getTarget(), $driver->getWindowHandle());
break;
default:
break;
Expand Down
4 changes: 2 additions & 2 deletions src/Command/Runner/WaitCommandRunner.php
Expand Up @@ -4,9 +4,9 @@

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverExpectedCondition;
use SingleColorPetrinet\Model\ColorInterface;
use Tienvx\Bundle\MbtBundle\Command\CommandRunner;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class WaitCommandRunner extends CommandRunner
{
Expand Down Expand Up @@ -39,7 +39,7 @@ public function getCommandsRequireValue(): array
return $this->getAllCommands();
}

public function run(CommandInterface $command, ColorInterface $color, RemoteWebDriver $driver): void
public function run(CommandInterface $command, ValuesInterface $values, RemoteWebDriver $driver): void
{
switch ($command->getCommand()) {
case self::WAIT_FOR_ELEMENT_EDITABLE:
Expand Down
4 changes: 2 additions & 2 deletions src/Command/Runner/WindowCommandRunner.php
Expand Up @@ -6,9 +6,9 @@
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverDimension;
use Facebook\WebDriver\WebDriverExpectedCondition;
use SingleColorPetrinet\Model\ColorInterface;
use Tienvx\Bundle\MbtBundle\Command\CommandRunner;
use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface;
use Tienvx\Bundle\MbtBundle\Model\ValuesInterface;

class WindowCommandRunner extends CommandRunner
{
Expand Down Expand Up @@ -44,7 +44,7 @@ public function getCommandsRequireValue(): array
return [];
}

public function run(CommandInterface $command, ColorInterface $color, RemoteWebDriver $driver): void
public function run(CommandInterface $command, ValuesInterface $values, RemoteWebDriver $driver): void
{
switch ($command->getCommand()) {
case self::OPEN:
Expand Down
13 changes: 13 additions & 0 deletions src/Model/Model/Revision/Transition.php
Expand Up @@ -11,6 +11,7 @@ class Transition implements TransitionInterface

protected string $label = '';
protected ?string $guard = null;
protected ?string $expression = null;
protected array $fromPlaces = [];
protected array $toPlaces = [];

Expand All @@ -23,6 +24,7 @@ public function __unserialize(array $data)
{
$this->label = $data['label'];
$this->guard = $data['guard'];
$this->expression = $data['expression'];
$this->fromPlaces = $data['fromPlaces'];
$this->toPlaces = $data['toPlaces'];
$this->commands = array_map([CommandFactory::class, 'createFromArray'], $data['commands']);
Expand All @@ -48,6 +50,16 @@ public function setGuard(?string $guard): void
$this->guard = $guard;
}

public function getExpression(): ?string
{
return $this->expression;
}

public function setExpression(?string $expression): void
{
$this->expression = $expression;
}

public function getFromPlaces(): array
{
return $this->fromPlaces;
Expand Down Expand Up @@ -91,6 +103,7 @@ public function toArray(): array
return [
'label' => $this->label,
'guard' => $this->guard,
'expression' => $this->expression,
'fromPlaces' => $this->fromPlaces,
'toPlaces' => $this->toPlaces,
'commands' => array_map(fn (CommandInterface $command) => $command->toArray(), $this->commands),
Expand Down
4 changes: 4 additions & 0 deletions src/Model/Model/Revision/TransitionInterface.php
Expand Up @@ -12,6 +12,10 @@ public function getGuard(): ?string;

public function setGuard(?string $guard): void;

public function getExpression(): ?string;

public function setExpression(?string $expression): void;

public function getCommands(): array;

public function setCommands(array $commands): void;
Expand Down

0 comments on commit 827514b

Please sign in to comment.