From 5651459d9fcf5b8316a2ecf23afdcf308aeaa4cd Mon Sep 17 00:00:00 2001 From: tienvx Date: Wed, 27 Apr 2022 22:14:21 +0700 Subject: [PATCH] Isolate transition expression from commands values --- src/Command/CommandPreprocessor.php | 16 ++-- src/Command/CommandPreprocessorInterface.php | 4 +- src/Command/CommandRunnerInterface.php | 4 +- src/Command/CommandRunnerManager.php | 6 +- src/Command/CommandRunnerManagerInterface.php | 4 +- src/Command/Runner/AlertCommandRunner.php | 4 +- src/Command/Runner/AssertionRunner.php | 6 +- src/Command/Runner/KeyboardCommandRunner.php | 4 +- src/Command/Runner/MouseCommandRunner.php | 4 +- src/Command/Runner/ScriptCommandRunner.php | 8 +- src/Command/Runner/StoreCommandRunner.php | 20 ++--- src/Command/Runner/WaitCommandRunner.php | 4 +- src/Command/Runner/WindowCommandRunner.php | 4 +- src/Model/Model/Revision/Transition.php | 13 +++ .../Model/Revision/TransitionInterface.php | 4 + src/Model/Values.php | 32 +++++++ src/Model/ValuesInterface.php | 12 +++ src/Service/Petrinet/PetrinetHelper.php | 8 +- .../Step/Builder/ShortestPathStepsBuilder.php | 14 +--- src/Service/Step/Runner/StepRunner.php | 11 +-- src/ValueObject/Model/Transition.php | 8 ++ tests/Command/CommandPreprocessorTest.php | 7 +- tests/Command/CommandRunnerManagerTest.php | 83 ++++++++++++------- .../Command/Runner/AlertCommandRunnerTest.php | 6 +- tests/Command/Runner/AssertionRunnerTest.php | 68 +++++++-------- .../Runner/KeyboardCommandRunnerTest.php | 4 +- .../Command/Runner/MouseCommandRunnerTest.php | 50 +++++------ tests/Command/Runner/RunnerTestCase.php | 6 +- .../Runner/ScriptCommandRunnerTest.php | 16 ++-- .../Command/Runner/StoreCommandRunnerTest.php | 32 +++---- .../Command/Runner/WaitCommandRunnerTest.php | 14 ++-- .../Runner/WindowCommandRunnerTest.php | 16 ++-- tests/Model/Model/Revision/TransitionTest.php | 6 +- tests/Model/Model/RevisionTest.php | 3 + tests/Model/ValuesTest.php | 39 +++++++++ tests/Service/Petrinet/PetrinetHelperTest.php | 8 ++ tests/Service/Step/Runner/StepRunnerTest.php | 21 +++-- 37 files changed, 362 insertions(+), 207 deletions(-) create mode 100644 src/Model/Values.php create mode 100644 src/Model/ValuesInterface.php create mode 100644 tests/Model/ValuesTest.php diff --git a/src/Command/CommandPreprocessor.php b/src/Command/CommandPreprocessor.php index 6a657468..5ad0c5e9 100644 --- a/src/Command/CommandPreprocessor.php +++ b/src/Command/CommandPreprocessor.php @@ -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() ); @@ -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 + ); } } diff --git a/src/Command/CommandPreprocessorInterface.php b/src/Command/CommandPreprocessorInterface.php index 54a367d5..8e72880e 100644 --- a/src/Command/CommandPreprocessorInterface.php +++ b/src/Command/CommandPreprocessorInterface.php @@ -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; } diff --git a/src/Command/CommandRunnerInterface.php b/src/Command/CommandRunnerInterface.php index bb22e055..fc943033 100644 --- a/src/Command/CommandRunnerInterface.php +++ b/src/Command/CommandRunnerInterface.php @@ -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 { @@ -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; } diff --git a/src/Command/CommandRunnerManager.php b/src/Command/CommandRunnerManager.php index c5f25e59..013216a4 100644 --- a/src/Command/CommandRunnerManager.php +++ b/src/Command/CommandRunnerManager.php @@ -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 { @@ -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; } } diff --git a/src/Command/CommandRunnerManagerInterface.php b/src/Command/CommandRunnerManagerInterface.php index f4db5a6e..d68e3d88 100644 --- a/src/Command/CommandRunnerManagerInterface.php +++ b/src/Command/CommandRunnerManagerInterface.php @@ -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 { @@ -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; } diff --git a/src/Command/Runner/AlertCommandRunner.php b/src/Command/Runner/AlertCommandRunner.php index 309a2136..a09787e6 100644 --- a/src/Command/Runner/AlertCommandRunner.php +++ b/src/Command/Runner/AlertCommandRunner.php @@ -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 { @@ -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: diff --git a/src/Command/Runner/AssertionRunner.php b/src/Command/Runner/AssertionRunner.php index 7ca303b1..21466672 100644 --- a/src/Command/Runner/AssertionRunner.php +++ b/src/Command/Runner/AssertionRunner.php @@ -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 { @@ -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()) diff --git a/src/Command/Runner/KeyboardCommandRunner.php b/src/Command/Runner/KeyboardCommandRunner.php index 2c984f7d..21ef7a38 100644 --- a/src/Command/Runner/KeyboardCommandRunner.php +++ b/src/Command/Runner/KeyboardCommandRunner.php @@ -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 { @@ -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: diff --git a/src/Command/Runner/MouseCommandRunner.php b/src/Command/Runner/MouseCommandRunner.php index b379b765..8a2e5e60 100644 --- a/src/Command/Runner/MouseCommandRunner.php +++ b/src/Command/Runner/MouseCommandRunner.php @@ -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 { @@ -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: diff --git a/src/Command/Runner/ScriptCommandRunner.php b/src/Command/Runner/ScriptCommandRunner.php index db1970db..7dda669a 100644 --- a/src/Command/Runner/ScriptCommandRunner.php +++ b/src/Command/Runner/ScriptCommandRunner.php @@ -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 { @@ -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: @@ -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: diff --git a/src/Command/Runner/StoreCommandRunner.php b/src/Command/Runner/StoreCommandRunner.php index 78ea80e5..6aaa07ca 100644 --- a/src/Command/Runner/StoreCommandRunner.php +++ b/src/Command/Runner/StoreCommandRunner.php @@ -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 { @@ -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; diff --git a/src/Command/Runner/WaitCommandRunner.php b/src/Command/Runner/WaitCommandRunner.php index 069c18bc..6873fa3b 100644 --- a/src/Command/Runner/WaitCommandRunner.php +++ b/src/Command/Runner/WaitCommandRunner.php @@ -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 { @@ -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: diff --git a/src/Command/Runner/WindowCommandRunner.php b/src/Command/Runner/WindowCommandRunner.php index e5a7d03e..dc83fa55 100644 --- a/src/Command/Runner/WindowCommandRunner.php +++ b/src/Command/Runner/WindowCommandRunner.php @@ -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 { @@ -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: diff --git a/src/Model/Model/Revision/Transition.php b/src/Model/Model/Revision/Transition.php index a0afe081..22063240 100644 --- a/src/Model/Model/Revision/Transition.php +++ b/src/Model/Model/Revision/Transition.php @@ -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 = []; @@ -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']); @@ -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; @@ -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), diff --git a/src/Model/Model/Revision/TransitionInterface.php b/src/Model/Model/Revision/TransitionInterface.php index 6e1938e4..49559ca7 100644 --- a/src/Model/Model/Revision/TransitionInterface.php +++ b/src/Model/Model/Revision/TransitionInterface.php @@ -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; diff --git a/src/Model/Values.php b/src/Model/Values.php new file mode 100644 index 00000000..0d689c59 --- /dev/null +++ b/src/Model/Values.php @@ -0,0 +1,32 @@ +values = []; + + foreach ($values as $key => $value) { + $this->setValue($key, $value); + } + } + + public function getValues(): array + { + return $this->values; + } + + public function setValue(string $key, mixed $value): void + { + $this->values[$key] = $value; + } + + public function getValue(string $key): mixed + { + return $this->values[$key] ?? null; + } +} diff --git a/src/Model/ValuesInterface.php b/src/Model/ValuesInterface.php new file mode 100644 index 00000000..ad05d826 --- /dev/null +++ b/src/Model/ValuesInterface.php @@ -0,0 +1,12 @@ +getValues() ) : null; - $transitions[$index] = $builder->transition($guardCallback); + $expressionCallback = $transition->getExpression() + ? fn (ColorInterface $color): array => (array) $this->expressionLanguage->evaluate( + $transition->getExpression(), + $color->getValues() + ) + : null; + $transitions[$index] = $builder->transition($guardCallback, $expressionCallback); $transitions[$index]->setId($index); } } diff --git a/src/Service/Step/Builder/ShortestPathStepsBuilder.php b/src/Service/Step/Builder/ShortestPathStepsBuilder.php index 6c35355a..738a602c 100644 --- a/src/Service/Step/Builder/ShortestPathStepsBuilder.php +++ b/src/Service/Step/Builder/ShortestPathStepsBuilder.php @@ -29,19 +29,9 @@ public function __construct( */ public function create(BugInterface $bug, int $from, int $to): Generator { - foreach ($bug->getSteps() as $index => $step) { - if ($index < $from) { - yield $step; - } - } - + yield from array_slice($bug->getSteps(), 0, $from); yield from $this->getSteps($bug, $from, $to); - - foreach ($bug->getSteps() as $index => $step) { - if ($index > $to) { - yield $step; - } - } + yield from array_slice($bug->getSteps(), $to + 1); } protected function getSteps(BugInterface $bug, int $from, int $to): iterable diff --git a/src/Service/Step/Runner/StepRunner.php b/src/Service/Step/Runner/StepRunner.php index 14a34186..6b76c602 100644 --- a/src/Service/Step/Runner/StepRunner.php +++ b/src/Service/Step/Runner/StepRunner.php @@ -3,13 +3,13 @@ namespace Tienvx\Bundle\MbtBundle\Service\Step\Runner; use Facebook\WebDriver\Remote\RemoteWebDriver; -use SingleColorPetrinet\Model\ColorInterface; use Tienvx\Bundle\MbtBundle\Command\CommandRunnerManagerInterface; use Tienvx\Bundle\MbtBundle\Model\Bug\StepInterface; use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface; use Tienvx\Bundle\MbtBundle\Model\Model\Revision\PlaceInterface; use Tienvx\Bundle\MbtBundle\Model\Model\Revision\TransitionInterface; use Tienvx\Bundle\MbtBundle\Model\Model\RevisionInterface; +use Tienvx\Bundle\MbtBundle\Model\Values; class StepRunner implements StepRunnerInterface { @@ -24,21 +24,22 @@ public function run(StepInterface $step, RevisionInterface $revision, RemoteWebD { $transition = $revision->getTransition($step->getTransition()); if ($transition instanceof TransitionInterface) { - $this->executeCommands($transition->getCommands(), $step->getColor(), $driver); + $this->runCommands($transition->getCommands(), $driver); } foreach ($step->getPlaces() as $place => $tokens) { $place = $revision->getPlace($place); if ($place instanceof PlaceInterface) { - $this->executeCommands($place->getCommands(), $step->getColor(), $driver); + $this->runCommands($place->getCommands(), $driver); } } } - protected function executeCommands(array $commands, ColorInterface $color, RemoteWebDriver $driver): void + protected function runCommands(array $commands, RemoteWebDriver $driver): void { + $values = new Values(); foreach ($commands as $command) { if ($command instanceof CommandInterface) { - $this->commandRunnerManager->run($command, $color, $driver); + $this->commandRunnerManager->run($command, $values, $driver); } } } diff --git a/src/ValueObject/Model/Transition.php b/src/ValueObject/Model/Transition.php index 0eb0023e..242b07db 100644 --- a/src/ValueObject/Model/Transition.php +++ b/src/ValueObject/Model/Transition.php @@ -20,6 +20,14 @@ class Transition extends TransitionModel */ protected ?string $guard = null; + /** + * @Assert\AtLeastOneOf({ + * @Assert\IsNull, + * @Assert\ExpressionLanguageSyntax + * }) + */ + protected ?string $expression = null; + /** * @Assert\All({ * @Assert\Type("\Tienvx\Bundle\MbtBundle\ValueObject\Model\Command") diff --git a/tests/Command/CommandPreprocessorTest.php b/tests/Command/CommandPreprocessorTest.php index 41623619..076ac292 100644 --- a/tests/Command/CommandPreprocessorTest.php +++ b/tests/Command/CommandPreprocessorTest.php @@ -3,7 +3,6 @@ namespace Tienvx\Bundle\MbtBundle\Tests\Command; use PHPUnit\Framework\TestCase; -use SingleColorPetrinet\Model\Color; use Tienvx\Bundle\MbtBundle\Command\CommandPreprocessor; use Tienvx\Bundle\MbtBundle\Command\Runner\KeyboardCommandRunner; use Tienvx\Bundle\MbtBundle\Command\Runner\MouseCommandRunner; @@ -11,11 +10,13 @@ use Tienvx\Bundle\MbtBundle\Command\Runner\StoreCommandRunner; use Tienvx\Bundle\MbtBundle\Command\Runner\WindowCommandRunner; use Tienvx\Bundle\MbtBundle\Model\Model\Revision\Command; +use Tienvx\Bundle\MbtBundle\Model\Values; /** * @covers \Tienvx\Bundle\MbtBundle\Command\CommandPreprocessor * * @uses \Tienvx\Bundle\MbtBundle\Model\Model\Revision\Command + * @uses \Tienvx\Bundle\MbtBundle\Model\Values */ class CommandPreprocessorTest extends TestCase { @@ -34,10 +35,8 @@ public function testProcess( $command->setCommand($commandString); $command->setTarget($target); $command->setValue($value); - $color = new Color(); - $color->setValues($values); $preprocessor = new CommandPreprocessor(); - $newCommand = $preprocessor->process($command, $color); + $newCommand = $preprocessor->process($command, new Values($values)); $this->assertSame($commandString, $newCommand->getCommand()); $this->assertSame($newTarget, $newCommand->getTarget()); $this->assertSame($newValue, $newCommand->getValue()); diff --git a/tests/Command/CommandRunnerManagerTest.php b/tests/Command/CommandRunnerManagerTest.php index 5dcb20c0..4f1fabfd 100644 --- a/tests/Command/CommandRunnerManagerTest.php +++ b/tests/Command/CommandRunnerManagerTest.php @@ -5,11 +5,11 @@ use Facebook\WebDriver\Remote\RemoteWebDriver; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use SingleColorPetrinet\Model\ColorInterface; use Tienvx\Bundle\MbtBundle\Command\CommandPreprocessorInterface; -use Tienvx\Bundle\MbtBundle\Command\CommandRunner; +use Tienvx\Bundle\MbtBundle\Command\CommandRunnerInterface; use Tienvx\Bundle\MbtBundle\Command\CommandRunnerManager; use Tienvx\Bundle\MbtBundle\Model\Model\Revision\CommandInterface; +use Tienvx\Bundle\MbtBundle\Model\ValuesInterface; /** * @covers \Tienvx\Bundle\MbtBundle\Command\CommandRunnerManager @@ -22,15 +22,24 @@ class CommandRunnerManagerTest extends TestCase protected array $runners; protected CommandPreprocessorInterface $preprocessor; protected CommandRunnerManager $manager; + protected CommandInterface $command; + protected CommandInterface $processedCommand; + protected RemoteWebDriver $driver; + protected ValuesInterface $values; protected function setUp(): void { $this->runners = [ - $runner1 = $this->createMock(CommandRunner::class), - $runner2 = $this->createMock(CommandRunner::class), + $runner1 = $this->createMock(CommandRunnerInterface::class), + $runner2 = $this->createMock(CommandRunnerInterface::class), + $runner3 = $this->createMock(CommandRunnerInterface::class), ]; $this->preprocessor = $this->createMock(CommandPreprocessorInterface::class); $this->manager = new CommandRunnerManager($this->runners, $this->preprocessor); + $this->command = $this->createMock(CommandInterface::class); + $this->processedCommand = $this->createMock(CommandInterface::class); + $this->driver = $this->createMock(RemoteWebDriver::class); + $this->values = $this->createMock(ValuesInterface::class); } public function testGetAllCommands(): void @@ -42,10 +51,16 @@ public function testGetAllCommands(): void $this->runners[1]->expects($this->once())->method('getAllCommands')->willReturn([ 'Action 3' => 'action3', ]); + $this->runners[2]->expects($this->once())->method('getAllCommands')->willReturn([ + 'Action 4' => 'action4', + 'Action 5' => 'action5', + ]); $this->assertSame([ 'Action 1' => 'action1', 'Action 2' => 'action2', 'Action 3' => 'action3', + 'Action 4' => 'action4', + 'Action 5' => 'action5', ], $this->manager->getAllCommands()); } @@ -58,10 +73,14 @@ public function testGetCommandsRequireTarget(): void 'Action 2' => 'action2', 'Action 3' => 'action3', ]); + $this->runners[2]->expects($this->once())->method('getCommandsRequireTarget')->willReturn([ + 'Action 4' => 'action4', + ]); $this->assertSame([ 'Action 1' => 'action1', 'Action 2' => 'action2', 'Action 3' => 'action3', + 'Action 4' => 'action4', ], $this->manager->getCommandsRequireTarget()); } @@ -75,39 +94,45 @@ public function testGetCommandsRequireValue(): void 'Action 2' => 'action2', 'Action 3' => 'action3', ]); + $this->runners[2]->expects($this->once())->method('getCommandsRequireValue')->willReturn([ + 'Action 5' => 'action5', + ]); $this->assertSame([ 'Action 1' => 'action1', 'Action 4' => 'action4', 'Action 2' => 'action2', 'Action 3' => 'action3', + 'Action 5' => 'action5', ], $this->manager->getCommandsRequireValue()); } - public function testRunCommandInSecondRunner(): void - { - $command = $this->createMock(CommandInterface::class); - $newCommand = $this->createMock(CommandInterface::class); - $driver = $this->createMock(RemoteWebDriver::class); - $color = $this->createMock(ColorInterface::class); - $this->runners[0]->expects($this->once())->method('supports')->willReturn(false); - $this->runners[0]->expects($this->never())->method('run'); - $this->runners[1]->expects($this->once())->method('supports')->willReturn(true); - $this->runners[1]->expects($this->once())->method('run')->with($newCommand, $color, $driver); - $this->preprocessor->expects($this->once())->method('process')->with($command, $color)->willReturn($newCommand); - $this->manager->run($command, $color, $driver); - } - - public function testRunCommandInFirstRunner(): void + /** + * @testWith [0] + * [1] + * [2] + */ + public function testRunCommand(int $support): void { - $command = $this->createMock(CommandInterface::class); - $newCommand = $this->createMock(CommandInterface::class); - $driver = $this->createMock(RemoteWebDriver::class); - $color = $this->createMock(ColorInterface::class); - $this->runners[0]->expects($this->once())->method('supports')->willReturn(true); - $this->runners[0]->expects($this->once())->method('run')->with($newCommand, $color, $driver); - $this->runners[1]->expects($this->never())->method('supports'); - $this->runners[1]->expects($this->never())->method('run'); - $this->preprocessor->expects($this->once())->method('process')->with($command, $color)->willReturn($newCommand); - $this->manager->run($command, $color, $driver); + foreach ($this->runners as $index => $runner) { + if ($index < $support) { + $runner->expects($this->once())->method('supports')->willReturn(false); + $runner->expects($this->never())->method('run'); + } elseif ($index === $support) { + $runner->expects($this->once())->method('supports')->willReturn(true); + $runner + ->expects($this->once()) + ->method('run') + ->with($this->processedCommand, $this->values, $this->driver); + } else { + $runner->expects($this->never())->method('supports'); + $runner->expects($this->never())->method('run'); + } + } + $this->preprocessor + ->expects($this->once()) + ->method('process') + ->with($this->command, $this->values) + ->willReturn($this->processedCommand); + $this->manager->run($this->command, $this->values, $this->driver); } } diff --git a/tests/Command/Runner/AlertCommandRunnerTest.php b/tests/Command/Runner/AlertCommandRunnerTest.php index 2fe12f93..78764c98 100644 --- a/tests/Command/Runner/AlertCommandRunnerTest.php +++ b/tests/Command/Runner/AlertCommandRunnerTest.php @@ -33,7 +33,7 @@ public function testAcceptAlert(string $acceptAlertCommand): void $targetLocator = $this->createMock(RemoteTargetLocator::class); $targetLocator->expects($this->once())->method('alert')->willReturn($alert); $this->driver->expects($this->once())->method('switchTo')->willReturn($targetLocator); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } /** @@ -48,7 +48,7 @@ public function testDismissAlert(string $acceptAlertCommand): void $targetLocator = $this->createMock(RemoteTargetLocator::class); $targetLocator->expects($this->once())->method('alert')->willReturn($alert); $this->driver->expects($this->once())->method('switchTo')->willReturn($targetLocator); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAnswerPrompt(): void @@ -62,7 +62,7 @@ public function testAnswerPrompt(): void $targetLocator = $this->createMock(RemoteTargetLocator::class); $targetLocator->expects($this->once())->method('alert')->willReturn($alert); $this->driver->expects($this->once())->method('switchTo')->willReturn($targetLocator); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function acceptAlertCommandProvider(): array diff --git a/tests/Command/Runner/AssertionRunnerTest.php b/tests/Command/Runner/AssertionRunnerTest.php index 188d514f..b2f4c522 100644 --- a/tests/Command/Runner/AssertionRunnerTest.php +++ b/tests/Command/Runner/AssertionRunnerTest.php @@ -30,8 +30,8 @@ public function testAssertPassed(): void $command->setCommand(AssertionRunner::ASSERT); $command->setTarget('var name'); $command->setValue('var value'); - $this->color->expects($this->once())->method('getValue')->with('var name')->willReturn('var value'); - $this->runner->run($command, $this->color, $this->driver); + $this->values->expects($this->once())->method('getValue')->with('var name')->willReturn('var value'); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertFailed(): void @@ -44,8 +44,8 @@ public function testAssertFailed(): void $command->setCommand(AssertionRunner::ASSERT); $command->setTarget('var name'); $command->setValue($expected); - $this->color->expects($this->once())->method('getValue')->with('var name')->willReturn($actual); - $this->runner->run($command, $this->color, $this->driver); + $this->values->expects($this->once())->method('getValue')->with('var name')->willReturn($actual); + $this->runner->run($command, $this->values, $this->driver); } /** @@ -61,7 +61,7 @@ public function testAssertAlertPassed(string $alertCommand): void $locator = $this->createMock(RemoteTargetLocator::class); $locator->expects($this->once())->method('alert')->willReturn($alert); $this->driver->expects($this->once())->method('switchTo')->willReturn($locator); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } /** @@ -81,7 +81,7 @@ public function testAssertAlertFailed(string $alertCommand, string $type): void $locator = $this->createMock(RemoteTargetLocator::class); $locator->expects($this->once())->method('alert')->willReturn($alert); $this->driver->expects($this->once())->method('switchTo')->willReturn($locator); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertTitlePassed(): void @@ -90,7 +90,7 @@ public function testAssertTitlePassed(): void $command->setCommand(AssertionRunner::ASSERT_TITLE); $command->setTarget('Welcome'); $this->driver->expects($this->exactly(2))->method('getTitle')->willReturn('Welcome'); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertTitleFailed(): void @@ -101,7 +101,7 @@ public function testAssertTitleFailed(): void $command->setCommand(AssertionRunner::ASSERT_TITLE); $command->setTarget('Welcome'); $this->driver->expects($this->exactly(2))->method('getTitle')->willReturn('Goodbye'); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertTextPassed(): void @@ -117,7 +117,7 @@ public function testAssertTextPassed(): void && 'xpath' === $selector->getMechanism() && '//h4[@href="#"]' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertTextFailed(): void @@ -135,7 +135,7 @@ public function testAssertTextFailed(): void && 'xpath' === $selector->getMechanism() && '//h4[@href="#"]' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertNotTextFailed(): void @@ -154,7 +154,7 @@ public function testAssertNotTextFailed(): void && 'xpath' === $selector->getMechanism() && '//h4[@href="#"]' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertNotTextPassed(): void @@ -170,7 +170,7 @@ public function testAssertNotTextPassed(): void && 'xpath' === $selector->getMechanism() && '//h4[@href="#"]' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertValuePassed(): void @@ -186,7 +186,7 @@ public function testAssertValuePassed(): void && 'css selector' === $selector->getMechanism() && '.quality' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertValueFailed(): void @@ -204,7 +204,7 @@ public function testAssertValueFailed(): void && 'css selector' === $selector->getMechanism() && '.quality' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertEditablePassed(): void @@ -223,7 +223,7 @@ public function testAssertEditablePassed(): void ->method('executeScript') ->with('return { enabled: !arguments[0].disabled, readonly: arguments[0].readOnly };', [$element]) ->willReturn((object) ['enabled' => true, 'readonly' => false]); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertEditableFailed(): void @@ -244,7 +244,7 @@ public function testAssertEditableFailed(): void ->method('executeScript') ->with('return { enabled: !arguments[0].disabled, readonly: arguments[0].readOnly };', [$element]) ->willReturn((object) ['enabled' => false, 'readonly' => true]); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertNotEditableFailed(): void @@ -265,7 +265,7 @@ public function testAssertNotEditableFailed(): void ->method('executeScript') ->with('return { enabled: !arguments[0].disabled, readonly: arguments[0].readOnly };', [$element]) ->willReturn((object) ['enabled' => true, 'readonly' => false]); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertNotEditablePassed(): void @@ -284,7 +284,7 @@ public function testAssertNotEditablePassed(): void ->method('executeScript') ->with('return { enabled: !arguments[0].disabled, readonly: arguments[0].readOnly };', [$element]) ->willReturn((object) ['enabled' => false, 'readonly' => true]); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertElementPresentPassed(): void @@ -298,7 +298,7 @@ public function testAssertElementPresentPassed(): void && 'css selector' === $selector->getMechanism() && '.cart' === $selector->getValue(); }))->willReturn([$element]); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertElementPresentFailed(): void @@ -313,7 +313,7 @@ public function testAssertElementPresentFailed(): void && 'css selector' === $selector->getMechanism() && '.cart' === $selector->getValue(); }))->willReturn([]); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertElementNotPresentFailed(): void @@ -329,7 +329,7 @@ public function testAssertElementNotPresentFailed(): void && 'css selector' === $selector->getMechanism() && '.cart' === $selector->getValue(); }))->willReturn([$element]); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertElementNotPresentPassed(): void @@ -342,7 +342,7 @@ public function testAssertElementNotPresentPassed(): void && 'css selector' === $selector->getMechanism() && '.cart' === $selector->getValue(); }))->willReturn([]); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertCheckedPassed(): void @@ -357,7 +357,7 @@ public function testAssertCheckedPassed(): void && 'css selector' === $selector->getMechanism() && '.term-and-condition' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertCheckedFailed(): void @@ -374,7 +374,7 @@ public function testAssertCheckedFailed(): void && 'css selector' === $selector->getMechanism() && '.term-and-condition' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertNotCheckedFailed(): void @@ -391,7 +391,7 @@ public function testAssertNotCheckedFailed(): void && 'css selector' === $selector->getMechanism() && '.term-and-condition' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertNotCheckedPassed(): void @@ -406,7 +406,7 @@ public function testAssertNotCheckedPassed(): void && 'css selector' === $selector->getMechanism() && '.term-and-condition' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testAssertSelectedValuePassed(): void @@ -427,7 +427,7 @@ public function testAssertSelectedValuePassed(): void $select->expects($this->once())->method('getFirstSelectedOption')->willReturn($option); $runner = $this->createPartialMock(AssertionRunner::class, ['getSelect']); $runner->expects($this->once())->method('getSelect')->with($element)->willReturn($select); - $runner->run($command, $this->color, $this->driver); + $runner->run($command, $this->values, $this->driver); } public function testAssertSelectedValueFailed(): void @@ -450,7 +450,7 @@ public function testAssertSelectedValueFailed(): void $select->expects($this->once())->method('getFirstSelectedOption')->willReturn($option); $runner = $this->createPartialMock(AssertionRunner::class, ['getSelect']); $runner->expects($this->once())->method('getSelect')->with($element)->willReturn($select); - $runner->run($command, $this->color, $this->driver); + $runner->run($command, $this->values, $this->driver); } public function testAssertNotSelectedValueFailed(): void @@ -473,7 +473,7 @@ public function testAssertNotSelectedValueFailed(): void $select->expects($this->once())->method('getFirstSelectedOption')->willReturn($option); $runner = $this->createPartialMock(AssertionRunner::class, ['getSelect']); $runner->expects($this->once())->method('getSelect')->with($element)->willReturn($select); - $runner->run($command, $this->color, $this->driver); + $runner->run($command, $this->values, $this->driver); } public function testAssertNotSelectedValuePassed(): void @@ -494,7 +494,7 @@ public function testAssertNotSelectedValuePassed(): void $select->expects($this->once())->method('getFirstSelectedOption')->willReturn($option); $runner = $this->createPartialMock(AssertionRunner::class, ['getSelect']); $runner->expects($this->once())->method('getSelect')->with($element)->willReturn($select); - $runner->run($command, $this->color, $this->driver); + $runner->run($command, $this->values, $this->driver); } public function testAssertSelectedLabelPassed(): void @@ -515,7 +515,7 @@ public function testAssertSelectedLabelPassed(): void $select->expects($this->once())->method('getFirstSelectedOption')->willReturn($option); $runner = $this->createPartialMock(AssertionRunner::class, ['getSelect']); $runner->expects($this->once())->method('getSelect')->with($element)->willReturn($select); - $runner->run($command, $this->color, $this->driver); + $runner->run($command, $this->values, $this->driver); } public function testAssertSelectedLabelFailed(): void @@ -538,7 +538,7 @@ public function testAssertSelectedLabelFailed(): void $select->expects($this->once())->method('getFirstSelectedOption')->willReturn($option); $runner = $this->createPartialMock(AssertionRunner::class, ['getSelect']); $runner->expects($this->once())->method('getSelect')->with($element)->willReturn($select); - $runner->run($command, $this->color, $this->driver); + $runner->run($command, $this->values, $this->driver); } public function testAssertNotSelectedLabelFailed(): void @@ -561,7 +561,7 @@ public function testAssertNotSelectedLabelFailed(): void $select->expects($this->once())->method('getFirstSelectedOption')->willReturn($option); $runner = $this->createPartialMock(AssertionRunner::class, ['getSelect']); $runner->expects($this->once())->method('getSelect')->with($element)->willReturn($select); - $runner->run($command, $this->color, $this->driver); + $runner->run($command, $this->values, $this->driver); } public function testAssertNotSelectedLabelPassed(): void @@ -582,7 +582,7 @@ public function testAssertNotSelectedLabelPassed(): void $select->expects($this->once())->method('getFirstSelectedOption')->willReturn($option); $runner = $this->createPartialMock(AssertionRunner::class, ['getSelect']); $runner->expects($this->once())->method('getSelect')->with($element)->willReturn($select); - $runner->run($command, $this->color, $this->driver); + $runner->run($command, $this->values, $this->driver); } public function alertCommandProvider(): array diff --git a/tests/Command/Runner/KeyboardCommandRunnerTest.php b/tests/Command/Runner/KeyboardCommandRunnerTest.php index f8f9c84b..a475ca28 100644 --- a/tests/Command/Runner/KeyboardCommandRunnerTest.php +++ b/tests/Command/Runner/KeyboardCommandRunnerTest.php @@ -36,7 +36,7 @@ public function testType(): void && 'name' === $selector->getMechanism() && 'age' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testSendKeys(): void @@ -53,7 +53,7 @@ public function testSendKeys(): void && 'css selector' === $selector->getMechanism() && '.quantity' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function targetProvider(): array diff --git a/tests/Command/Runner/MouseCommandRunnerTest.php b/tests/Command/Runner/MouseCommandRunnerTest.php index 8f591c5f..f9762668 100644 --- a/tests/Command/Runner/MouseCommandRunnerTest.php +++ b/tests/Command/Runner/MouseCommandRunnerTest.php @@ -42,7 +42,7 @@ public function testAddSelectionByIndex(): void $select->expects($this->once())->method('selectByIndex')->with(123); $runner = $this->createPartialMock(MouseCommandRunner::class, ['getSelect']); $runner->expects($this->once())->method('getSelect')->with($element)->willReturn($select); - $runner->run($command, $this->color, $this->driver); + $runner->run($command, $this->values, $this->driver); } public function testAddSelectionByValue(): void @@ -61,7 +61,7 @@ public function testAddSelectionByValue(): void $select->expects($this->once())->method('selectByValue')->with('en_GB'); $runner = $this->createPartialMock(MouseCommandRunner::class, ['getSelect']); $runner->expects($this->once())->method('getSelect')->with($element)->willReturn($select); - $runner->run($command, $this->color, $this->driver); + $runner->run($command, $this->values, $this->driver); } public function testAddSelectionByLabel(): void @@ -80,7 +80,7 @@ public function testAddSelectionByLabel(): void $select->expects($this->once())->method('selectByVisibleText')->with('English (UK)'); $runner = $this->createPartialMock(MouseCommandRunner::class, ['getSelect']); $runner->expects($this->once())->method('getSelect')->with($element)->willReturn($select); - $runner->run($command, $this->color, $this->driver); + $runner->run($command, $this->values, $this->driver); } public function testRemoveSelectionByIndex(): void @@ -99,7 +99,7 @@ public function testRemoveSelectionByIndex(): void $select->expects($this->once())->method('deselectByIndex')->with(123); $runner = $this->createPartialMock(MouseCommandRunner::class, ['getSelect']); $runner->expects($this->once())->method('getSelect')->with($element)->willReturn($select); - $runner->run($command, $this->color, $this->driver); + $runner->run($command, $this->values, $this->driver); } public function testRemoveSelectionByValue(): void @@ -118,7 +118,7 @@ public function testRemoveSelectionByValue(): void $select->expects($this->once())->method('deselectByValue')->with('en_GB'); $runner = $this->createPartialMock(MouseCommandRunner::class, ['getSelect']); $runner->expects($this->once())->method('getSelect')->with($element)->willReturn($select); - $runner->run($command, $this->color, $this->driver); + $runner->run($command, $this->values, $this->driver); } public function testRemoveSelectionByLabel(): void @@ -137,7 +137,7 @@ public function testRemoveSelectionByLabel(): void $select->expects($this->once())->method('deselectByVisibleText')->with('English (UK)'); $runner = $this->createPartialMock(MouseCommandRunner::class, ['getSelect']); $runner->expects($this->once())->method('getSelect')->with($element)->willReturn($select); - $runner->run($command, $this->color, $this->driver); + $runner->run($command, $this->values, $this->driver); } /** @@ -156,7 +156,7 @@ public function testCheck(bool $selected, bool $checked): void && 'id' === $selector->getMechanism() && 'language' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } /** @@ -175,7 +175,7 @@ public function testUncheck(bool $selected, bool $unchecked): void && 'id' === $selector->getMechanism() && 'language' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testClick(): void @@ -190,7 +190,7 @@ public function testClick(): void && 'id' === $selector->getMechanism() && 'add-to-cart' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testClickAt(): void @@ -210,7 +210,7 @@ public function testClickAt(): void $action->expects($this->once())->method('click')->willReturnSelf(); $action->expects($this->once())->method('perform'); $this->driver->expects($this->once())->method('action')->willReturn($action); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testDoubleClick(): void @@ -228,7 +228,7 @@ public function testDoubleClick(): void $action->expects($this->once())->method('doubleClick')->with($element)->willReturnSelf(); $action->expects($this->once())->method('perform'); $this->driver->expects($this->once())->method('action')->willReturn($action); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testDoubleClickAt(): void @@ -248,7 +248,7 @@ public function testDoubleClickAt(): void $action->expects($this->once())->method('doubleClick')->willReturnSelf(); $action->expects($this->once())->method('perform'); $this->driver->expects($this->once())->method('action')->willReturn($action); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testDragAndDropToObject(): void @@ -278,7 +278,7 @@ public function testDragAndDropToObject(): void $action->expects($this->once())->method('dragAndDrop')->with($source, $target)->willReturnSelf(); $action->expects($this->once())->method('perform'); $this->driver->expects($this->once())->method('action')->willReturn($action); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testMouseDown(): void @@ -297,7 +297,7 @@ public function testMouseDown(): void $mouse = $this->createMock(RemoteMouse::class); $mouse->expects($this->once())->method('mouseDown')->with($coord); $this->driver->expects($this->once())->method('getMouse')->willReturn($mouse); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testMouseDownAt(): void @@ -318,7 +318,7 @@ public function testMouseDownAt(): void $mouse->expects($this->once())->method('mouseMove')->with($coord, 5, 10)->willReturnSelf(); $mouse->expects($this->once())->method('mouseDown'); $this->driver->expects($this->once())->method('getMouse')->willReturn($mouse); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testMouseMoveAt(): void @@ -338,7 +338,7 @@ public function testMouseMoveAt(): void $mouse = $this->createMock(RemoteMouse::class); $mouse->expects($this->once())->method('mouseMove')->with($coord, 5, 10); $this->driver->expects($this->once())->method('getMouse')->willReturn($mouse); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testMouseOutTop(): void @@ -364,7 +364,7 @@ public function testMouseOutTop(): void $rect = (object) ['top' => 1, 'height' => 2], $vp = (object) [], ]); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testMouseOutRight(): void @@ -390,7 +390,7 @@ public function testMouseOutRight(): void $rect = (object) ['top' => 0, 'right' => 2], $vp = (object) ['width' => 3], ]); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testMouseOutBottom(): void @@ -416,7 +416,7 @@ public function testMouseOutBottom(): void $rect = (object) ['top' => 0, 'right' => 2, 'bottom' => 1, 'height' => 4], $vp = (object) ['width' => 2, 'height' => 2], ]); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testMouseOutLeft(): void @@ -442,7 +442,7 @@ public function testMouseOutLeft(): void $rect = (object) ['top' => 0, 'right' => 2, 'bottom' => 1, 'left' => 1], $vp = (object) ['width' => 2, 'height' => 1], ]); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testUnableMouseOut(): void @@ -467,7 +467,7 @@ public function testUnableMouseOut(): void ]); $this->expectException(\Exception::class); $this->expectExceptionMessage('Unable to perform mouse out as the element takes up the entire viewport'); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testMouseOver(): void @@ -486,7 +486,7 @@ public function testMouseOver(): void $mouse = $this->createMock(RemoteMouse::class); $mouse->expects($this->once())->method('mouseMove')->with($coord); $this->driver->expects($this->once())->method('getMouse')->willReturn($mouse); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testMouseUp(): void @@ -505,7 +505,7 @@ public function testMouseUp(): void $mouse = $this->createMock(RemoteMouse::class); $mouse->expects($this->once())->method('mouseUp')->with($coord); $this->driver->expects($this->once())->method('getMouse')->willReturn($mouse); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testMouseUpAt(): void @@ -526,7 +526,7 @@ public function testMouseUpAt(): void $mouse->expects($this->once())->method('mouseMove')->with($coord, 5, 10)->willReturnSelf(); $mouse->expects($this->once())->method('mouseUp'); $this->driver->expects($this->once())->method('getMouse')->willReturn($mouse); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testSelect(): void @@ -548,7 +548,7 @@ public function testSelect(): void && 'css selector' === $selector->getMechanism() && 'option[value=en_US]' === $selector->getValue(); }))->willReturn($option); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function checkDataProvider(): array diff --git a/tests/Command/Runner/RunnerTestCase.php b/tests/Command/Runner/RunnerTestCase.php index 40917c86..be6604f3 100644 --- a/tests/Command/Runner/RunnerTestCase.php +++ b/tests/Command/Runner/RunnerTestCase.php @@ -4,21 +4,21 @@ use Facebook\WebDriver\Remote\RemoteWebDriver; use PHPUnit\Framework\TestCase; -use SingleColorPetrinet\Model\ColorInterface; use Tienvx\Bundle\MbtBundle\Command\CommandRunner; +use Tienvx\Bundle\MbtBundle\Model\ValuesInterface; use Tienvx\Bundle\MbtBundle\ValueObject\Model\Command; abstract class RunnerTestCase extends TestCase { protected RemoteWebDriver $driver; protected CommandRunner $runner; - protected ColorInterface $color; + protected ValuesInterface $values; protected function setUp(): void { $this->driver = $this->createMock(RemoteWebDriver::class); $this->runner = $this->createRunner(); - $this->color = $this->createMock(ColorInterface::class); + $this->values = $this->createMock(ValuesInterface::class); } abstract protected function createRunner(): CommandRunner; diff --git a/tests/Command/Runner/ScriptCommandRunnerTest.php b/tests/Command/Runner/ScriptCommandRunnerTest.php index 7c9651b4..b8cd47ef 100644 --- a/tests/Command/Runner/ScriptCommandRunnerTest.php +++ b/tests/Command/Runner/ScriptCommandRunnerTest.php @@ -24,12 +24,12 @@ public function testRunScript(): void $command = new Command(); $command->setCommand(ScriptCommandRunner::RUN_SCRIPT); $command->setTarget('alert("Hello World!")'); - $this->color->expects($this->never())->method('getValues'); + $this->values->expects($this->never())->method('getValues'); $this->driver->expects($this->once())->method('executeScript')->with( 'alert("Hello World!")', [], ); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testExecuteScript(): void @@ -38,13 +38,13 @@ public function testExecuteScript(): void $command->setCommand(ScriptCommandRunner::EXECUTE_SCRIPT); $command->setTarget('return 2 + 1;'); $command->setValue('total'); - $this->color->expects($this->never())->method('getValues'); - $this->color->expects($this->once())->method('setValue')->with('total', 3); + $this->values->expects($this->never())->method('getValues'); + $this->values->expects($this->once())->method('setValue')->with('total', 3); $this->driver->expects($this->once())->method('executeScript')->with( 'return 2 + 1;', [], )->willReturn(3); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testExecuteAsyncScript(): void @@ -53,13 +53,13 @@ public function testExecuteAsyncScript(): void $command->setCommand(ScriptCommandRunner::EXECUTE_ASYNC_SCRIPT); $command->setTarget('window.setTimeout(function() { return "Hello";}, 1000);'); $command->setValue('message'); - $this->color->expects($this->never())->method('getValues'); - $this->color->expects($this->once())->method('setValue')->with('message', 'Hello'); + $this->values->expects($this->never())->method('getValues'); + $this->values->expects($this->once())->method('setValue')->with('message', 'Hello'); $this->driver->expects($this->once())->method('executeAsyncScript')->with( 'window.setTimeout(function() { return "Hello";}, 1000);', [], )->willReturn('Hello'); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function targetProvider(): array diff --git a/tests/Command/Runner/StoreCommandRunnerTest.php b/tests/Command/Runner/StoreCommandRunnerTest.php index 018a48cc..0ad2034a 100644 --- a/tests/Command/Runner/StoreCommandRunnerTest.php +++ b/tests/Command/Runner/StoreCommandRunnerTest.php @@ -27,8 +27,8 @@ public function testStore(): void $command->setCommand(StoreCommandRunner::STORE); $command->setTarget('1'); $command->setValue('count'); - $this->color->expects($this->once())->method('setValue')->with('count', '1'); - $this->runner->run($command, $this->color, $this->driver); + $this->values->expects($this->once())->method('setValue')->with('count', '1'); + $this->runner->run($command, $this->values, $this->driver); } public function testStoreAttribute(): void @@ -37,7 +37,7 @@ public function testStoreAttribute(): void $command->setCommand(StoreCommandRunner::STORE_ATTRIBUTE); $command->setTarget('css=.readmore@href'); $command->setValue('readmoreLink'); - $this->color->expects($this->once())->method('setValue')->with('readmoreLink', 'http://example.com'); + $this->values->expects($this->once())->method('setValue')->with('readmoreLink', 'http://example.com'); $element = $this->createMock(WebDriverElement::class); $element->expects($this->once())->method('getAttribute')->with('href')->willReturn('http://example.com'); $this->driver->expects($this->once())->method('findElement')->with($this->callback(function ($selector) { @@ -45,7 +45,7 @@ public function testStoreAttribute(): void && 'css selector' === $selector->getMechanism() && '.readmore' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testStoreElementCount(): void @@ -54,14 +54,14 @@ public function testStoreElementCount(): void $command->setCommand(StoreCommandRunner::STORE_ELEMENT_COUNT); $command->setTarget('css=.item'); $command->setValue('itemCount'); - $this->color->expects($this->once())->method('setValue')->with('itemCount', 2); + $this->values->expects($this->once())->method('setValue')->with('itemCount', 2); $element = $this->createMock(WebDriverElement::class); $this->driver->expects($this->once())->method('findElements')->with($this->callback(function ($selector) { return $selector instanceof WebDriverBy && 'css selector' === $selector->getMechanism() && '.item' === $selector->getValue(); }))->willReturn([$element, $element]); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testStoreJson(): void @@ -70,11 +70,11 @@ public function testStoreJson(): void $command->setCommand(StoreCommandRunner::STORE_JSON); $command->setTarget('{ "items": [1, 2, 3] }'); $command->setValue('json'); - $this->color->expects($this->once())->method('setValue')->with( + $this->values->expects($this->once())->method('setValue')->with( 'json', $this->callback(fn ($object) => $object instanceof \stdClass && $object->items === [1, 2, 3]) ); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testStoreText(): void @@ -83,7 +83,7 @@ public function testStoreText(): void $command->setCommand(StoreCommandRunner::STORE_TEXT); $command->setTarget('css=.head-line'); $command->setValue('headLine'); - $this->color->expects($this->once())->method('setValue')->with('headLine', 'Welcome to our site'); + $this->values->expects($this->once())->method('setValue')->with('headLine', 'Welcome to our site'); $element = $this->createMock(WebDriverElement::class); $element->expects($this->once())->method('getText')->willReturn('Welcome to our site'); $this->driver->expects($this->once())->method('findElement')->with($this->callback(function ($selector) { @@ -91,7 +91,7 @@ public function testStoreText(): void && 'css selector' === $selector->getMechanism() && '.head-line' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testStoreTitle(): void @@ -99,9 +99,9 @@ public function testStoreTitle(): void $command = new Command(); $command->setCommand(StoreCommandRunner::STORE_TITLE); $command->setTarget('title'); - $this->color->expects($this->once())->method('setValue')->with('title', 'Welcome'); + $this->values->expects($this->once())->method('setValue')->with('title', 'Welcome'); $this->driver->expects($this->once())->method('getTitle')->willReturn('Welcome'); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testStoreValue(): void @@ -110,7 +110,7 @@ public function testStoreValue(): void $command->setCommand(StoreCommandRunner::STORE_VALUE); $command->setTarget('css=.age'); $command->setValue('age'); - $this->color->expects($this->once())->method('setValue')->with('age', 23); + $this->values->expects($this->once())->method('setValue')->with('age', 23); $element = $this->createMock(WebDriverElement::class); $element->expects($this->once())->method('getAttribute')->with('value')->willReturn(23); $this->driver->expects($this->once())->method('findElement')->with($this->callback(function ($selector) { @@ -118,7 +118,7 @@ public function testStoreValue(): void && 'css selector' === $selector->getMechanism() && '.age' === $selector->getValue(); }))->willReturn($element); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testStoreWindowHandle(): void @@ -126,9 +126,9 @@ public function testStoreWindowHandle(): void $command = new Command(); $command->setCommand(StoreCommandRunner::STORE_WINDOW_HANDLE); $command->setTarget('windowHandle'); - $this->color->expects($this->once())->method('setValue')->with('windowHandle', 'window-123'); + $this->values->expects($this->once())->method('setValue')->with('windowHandle', 'window-123'); $this->driver->expects($this->once())->method('getWindowHandle')->willReturn('window-123'); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function targetProvider(): array diff --git a/tests/Command/Runner/WaitCommandRunnerTest.php b/tests/Command/Runner/WaitCommandRunnerTest.php index 9b146f23..bed3b764 100644 --- a/tests/Command/Runner/WaitCommandRunnerTest.php +++ b/tests/Command/Runner/WaitCommandRunnerTest.php @@ -48,7 +48,7 @@ public function testWaitForElementEditable(): void ->method('executeScript') ->with('return { enabled: !arguments[0].disabled, readonly: arguments[0].readOnly };', [$element]) ->willReturn((object) ['enabled' => true, 'readonly' => false]); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testWaitForElementNotEditable(): void @@ -74,7 +74,7 @@ public function testWaitForElementNotEditable(): void ->method('executeScript') ->with('return { enabled: !arguments[0].disabled, readonly: arguments[0].readOnly };', [$element]) ->willReturn((object) ['enabled' => false, 'readonly' => true]); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testWaitForElementPresent(): void @@ -94,7 +94,7 @@ public function testWaitForElementPresent(): void && call_user_func($condition->getApply(), $this->driver); })); $this->driver->expects($this->once())->method('wait')->willReturn($wait); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testNoElementsPresent(): void @@ -108,7 +108,7 @@ public function testNoElementsPresent(): void && 'button' === $selector->getValue(); }))->willReturn([]); $this->driver->expects($this->never())->method('wait'); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testWaitForElementNotPresent(): void @@ -132,7 +132,7 @@ public function testWaitForElementNotPresent(): void && call_user_func($condition->getApply(), $this->driver); })); $this->driver->expects($this->once())->method('wait')->willReturn($wait); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testWaitForElementVisible(): void @@ -153,7 +153,7 @@ public function testWaitForElementVisible(): void && call_user_func($condition->getApply(), $this->driver); })); $this->driver->expects($this->once())->method('wait')->willReturn($wait); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testWaitForElementNotVisible(): void @@ -174,7 +174,7 @@ public function testWaitForElementNotVisible(): void && call_user_func($condition->getApply(), $this->driver); })); $this->driver->expects($this->once())->method('wait')->willReturn($wait); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function targetProvider(): array diff --git a/tests/Command/Runner/WindowCommandRunnerTest.php b/tests/Command/Runner/WindowCommandRunnerTest.php index 61ca8009..ba24e40b 100644 --- a/tests/Command/Runner/WindowCommandRunnerTest.php +++ b/tests/Command/Runner/WindowCommandRunnerTest.php @@ -33,7 +33,7 @@ public function testOpen(): void $command->setCommand(WindowCommandRunner::OPEN); $command->setTarget('https://demo.sylius.com/en_US/'); $this->driver->expects($this->once())->method('get')->with('https://demo.sylius.com/en_US/'); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testSetWindowSize(): void @@ -50,7 +50,7 @@ public function testSetWindowSize(): void $options = $this->createMock(WebDriverOptions::class); $options->expects($this->once())->method('window')->willReturn($window); $this->driver->expects($this->once())->method('manage')->willReturn($options); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testSelectWindow(): void @@ -61,7 +61,7 @@ public function testSelectWindow(): void $targetLocator = $this->createMock(RemoteTargetLocator::class); $targetLocator->expects($this->once())->method('window')->with('testing'); $this->driver->expects($this->once())->method('switchTo')->willReturn($targetLocator); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testClose(): void @@ -69,7 +69,7 @@ public function testClose(): void $command = new Command(); $command->setCommand(WindowCommandRunner::CLOSE); $this->driver->expects($this->once())->method('close'); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testSelectFrameRelativeTop(): void @@ -80,7 +80,7 @@ public function testSelectFrameRelativeTop(): void $targetLocator = $this->createMock(RemoteTargetLocator::class); $targetLocator->expects($this->once())->method('defaultContent'); $this->driver->expects($this->once())->method('switchTo')->willReturn($targetLocator); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testSelectFrameRelativeParent(): void @@ -91,7 +91,7 @@ public function testSelectFrameRelativeParent(): void $targetLocator = $this->createMock(RemoteTargetLocator::class); $targetLocator->expects($this->once())->method('parent'); $this->driver->expects($this->once())->method('switchTo')->willReturn($targetLocator); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testSelectFrameIndex(): void @@ -102,7 +102,7 @@ public function testSelectFrameIndex(): void $targetLocator = $this->createMock(RemoteTargetLocator::class); $targetLocator->expects($this->once())->method('frame')->with(123); $this->driver->expects($this->once())->method('switchTo')->willReturn($targetLocator); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function testSelectFrameSelector(): void @@ -125,7 +125,7 @@ public function testSelectFrameSelector(): void && call_user_func($condition->getApply(), $this->driver); })); $this->driver->expects($this->once())->method('wait')->willReturn($wait); - $this->runner->run($command, $this->color, $this->driver); + $this->runner->run($command, $this->values, $this->driver); } public function targetProvider(): array diff --git a/tests/Model/Model/Revision/TransitionTest.php b/tests/Model/Model/Revision/TransitionTest.php index 4c728355..a1a945a2 100644 --- a/tests/Model/Model/Revision/TransitionTest.php +++ b/tests/Model/Model/Revision/TransitionTest.php @@ -29,6 +29,7 @@ protected function setUp(): void $this->transition = $this->createTransition(); $this->transition->setLabel('transition label'); $this->transition->setGuard('count > 2'); + $this->transition->setExpression('{count: count + 1}'); $this->transition->setFromPlaces([1, 2, 3]); $this->transition->setToPlaces([12, 23]); $this->transition->setCommands([ @@ -60,17 +61,18 @@ public function testSerialize(): void { $className = get_class($this->transition); // phpcs:ignore Generic.Files.LineLength - $this->assertSame('O:' . strlen($className) . ':"' . $className . '":5:{s:5:"label";s:16:"transition label";s:5:"guard";s:9:"count > 2";s:10:"fromPlaces";a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}s:8:"toPlaces";a:2:{i:0;i:12;i:1;i:23;}s:8:"commands";a:2:{i:0;a:3:{s:7:"command";s:4:"type";s:6:"target";s:10:"css=.email";s:5:"value";s:16:"test@example.com";}i:1;a:3:{s:7:"command";s:5:"click";s:6:"target";s:9:"css=.link";s:5:"value";N;}}}', serialize($this->transition)); + $this->assertSame('O:' . strlen($className) . ':"' . $className . '":6:{s:5:"label";s:16:"transition label";s:5:"guard";s:9:"count > 2";s:10:"expression";s:18:"{count: count + 1}";s:10:"fromPlaces";a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}s:8:"toPlaces";a:2:{i:0;i:12;i:1;i:23;}s:8:"commands";a:2:{i:0;a:3:{s:7:"command";s:4:"type";s:6:"target";s:10:"css=.email";s:5:"value";s:16:"test@example.com";}i:1;a:3:{s:7:"command";s:5:"click";s:6:"target";s:9:"css=.link";s:5:"value";N;}}}', serialize($this->transition)); } public function testUnerialize(): void { $className = get_class($this->transition); // phpcs:ignore Generic.Files.LineLength - $transition = unserialize('O:' . strlen($className) . ':"' . $className . '":5:{s:5:"label";s:10:"Serialized";s:5:"guard";s:10:"count == 3";s:10:"fromPlaces";a:2:{i:0;i:1;i:1;i:4;}s:8:"toPlaces";a:1:{i:0;i:15;}s:8:"commands";a:1:{i:0;a:3:{s:7:"command";s:5:"store";s:6:"target";s:2:"55";s:5:"value";s:6:"number";}}}'); + $transition = unserialize('O:' . strlen($className) . ':"' . $className . '":6:{s:5:"label";s:10:"Serialized";s:5:"guard";s:10:"count == 3";s:10:"expression";s:18:"{count: count - 1}";s:10:"fromPlaces";a:2:{i:0;i:1;i:1;i:4;}s:8:"toPlaces";a:1:{i:0;i:15;}s:8:"commands";a:1:{i:0;a:3:{s:7:"command";s:5:"store";s:6:"target";s:2:"55";s:5:"value";s:6:"number";}}}'); $this->assertInstanceOf(TransitionInterface::class, $transition); $this->assertSame('Serialized', $transition->getLabel()); $this->assertSame('count == 3', $transition->getGuard()); + $this->assertSame('{count: count - 1}', $transition->getExpression()); $this->assertSame([1, 4], $transition->getFromPlaces()); $this->assertSame([15], $transition->getToPlaces()); $this->assertInstanceOf(CommandInterface::class, $transition->getCommands()[0]); diff --git a/tests/Model/Model/RevisionTest.php b/tests/Model/Model/RevisionTest.php index 93994025..aca7e1ce 100644 --- a/tests/Model/Model/RevisionTest.php +++ b/tests/Model/Model/RevisionTest.php @@ -70,6 +70,7 @@ protected function setUp(): void $t1->setLabel('t1'); $t1->setFromPlaces([1]); $t1->setToPlaces([1, 2]); + $t1->setExpression('{count: count + 1}'); $t2->setLabel(''); $t2->setFromPlaces([1, 2]); $t2->setToPlaces([]); @@ -143,6 +144,7 @@ public function testToArray(): void 0 => [ 'label' => 't1', 'guard' => null, + 'expression' => '{count: count + 1}', 'fromPlaces' => [ 0 => 1, ], @@ -156,6 +158,7 @@ public function testToArray(): void 1 => [ 'label' => '', 'guard' => 'count > 1', + 'expression' => null, 'fromPlaces' => [ 0 => 1, 1 => 2, diff --git a/tests/Model/ValuesTest.php b/tests/Model/ValuesTest.php new file mode 100644 index 00000000..cda452bc --- /dev/null +++ b/tests/Model/ValuesTest.php @@ -0,0 +1,39 @@ + 'value', 'key2' => 'value2']); + $this->assertSame(['key' => 'value', 'key2' => 'value2'], $values->getValues()); + } + + public function testGetValue(): void + { + $values = new Values(['key' => 'value']); + $this->assertSame('value', $values->getValue('key')); + $this->assertNull($values->getValue('key2')); + } + + public function testSetValue(): void + { + $values = new Values(); + $this->assertSame([], $values->getValues()); + $values->setValue('key', 'value'); + $values->setValue('key2', null); + $values->setValue('key3', 123); + $values->setValue('key4', json_decode('{"test1":"Test 1","test2":{},"test3":{"test4":"Test 4"}}')); + $this->assertSame( + '{"key":"value","key2":null,"key3":123,"key4":{"test1":"Test 1","test2":{},"test3":{"test4":"Test 4"}}}', + json_encode($values->getValues()) + ); + } +} diff --git a/tests/Service/Petrinet/PetrinetHelperTest.php b/tests/Service/Petrinet/PetrinetHelperTest.php index 3676c23a..4e1b5e69 100644 --- a/tests/Service/Petrinet/PetrinetHelperTest.php +++ b/tests/Service/Petrinet/PetrinetHelperTest.php @@ -50,6 +50,7 @@ public function testBuild(): void $transition1->setToPlaces([1, 2]); $transition2->setFromPlaces([2]); $transition2->setToPlaces([1]); + $transition3->setExpression('{count: count + 1, status: "open"}'); $transition3->setFromPlaces([1]); $transition3->setToPlaces([0, 2]); @@ -113,6 +114,13 @@ public function testBuild(): void } else { $this->assertNull($transition->getGuard()); } + if (2 === $index) { + $this->assertIsCallable($expressionCallback = $transition->getExpression()); + $this->assertSame(['count' => 2, 'status' => 'open'], $expressionCallback(new Color(['count' => 1]))); + $this->assertSame(['count' => 1, 'status' => 'open'], $expressionCallback(new Color(['count' => 0]))); + } else { + $this->assertNull($transition->getExpression()); + } } } } diff --git a/tests/Service/Step/Runner/StepRunnerTest.php b/tests/Service/Step/Runner/StepRunnerTest.php index ef709f5e..e43c3bf1 100644 --- a/tests/Service/Step/Runner/StepRunnerTest.php +++ b/tests/Service/Step/Runner/StepRunnerTest.php @@ -12,6 +12,7 @@ use Tienvx\Bundle\MbtBundle\Entity\Model\Revision; use Tienvx\Bundle\MbtBundle\Factory\Model\Revision\CommandFactory; use Tienvx\Bundle\MbtBundle\Model\Model\RevisionInterface; +use Tienvx\Bundle\MbtBundle\Model\ValuesInterface; use Tienvx\Bundle\MbtBundle\Service\Step\Runner\StepRunner; use Tienvx\Bundle\MbtBundle\ValueObject\Bug\Step; use Tienvx\Bundle\MbtBundle\ValueObject\Model\Place; @@ -26,6 +27,7 @@ * @uses \Tienvx\Bundle\MbtBundle\Model\Model\Revision\Command * @uses \Tienvx\Bundle\MbtBundle\Model\Model\Revision * @uses \Tienvx\Bundle\MbtBundle\Factory\Model\Revision\CommandFactory + * @uses \Tienvx\Bundle\MbtBundle\Model\Values */ class StepRunnerTest extends TestCase { @@ -34,6 +36,7 @@ class StepRunnerTest extends TestCase protected CommandRunnerManager $commandRunnerManager; protected RemoteWebDriver $driver; protected ColorInterface $color; + protected array $valuesInstances = []; protected function setUp(): void { @@ -60,12 +63,19 @@ protected function setUp(): void $command5 = CommandFactory::create(AssertionRunner::ASSERT_TEXT), ]); $this->revision->setPlaces($places); + $assertValuesInstance = function (ValuesInterface $values) { + if (!in_array($values, $this->valuesInstances, true)) { + $this->valuesInstances[] = $values; + } + + return true; + }; $this->commands = [ - [$command1, $this->color, $this->driver], - [$command2, $this->color, $this->driver], - [$command3, $this->color, $this->driver], - [$command4, $this->color, $this->driver], - [$command5, $this->color, $this->driver], + [$command1, $this->callback($assertValuesInstance), $this->driver], + [$command2, $this->callback($assertValuesInstance), $this->driver], + [$command3, $this->callback($assertValuesInstance), $this->driver], + [$command4, $this->callback($assertValuesInstance), $this->driver], + [$command5, $this->callback($assertValuesInstance), $this->driver], ]; $this->commandRunnerManager = $this->createMock(CommandRunnerManager::class); @@ -77,5 +87,6 @@ public function testRun(): void $this->commandRunnerManager->expects($this->exactly(5))->method('run')->withConsecutive(...$this->commands); $stepRunner = new StepRunner($this->commandRunnerManager); $stepRunner->run($step, $this->revision, $this->driver); + $this->assertCount(3, $this->valuesInstances); } }