Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"scripts": {
"tests": "vendor/bin/phpunit",
"phpstan": "vendor/bin/phpstan analyse",
"phpstan": "vendor/bin/phpstan analyse"

}
}
2 changes: 1 addition & 1 deletion src/Elements/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected function hash(array $elements)
$hashes[] = $value->getHash();
}
sort($hashes, SORT_STRING);
return md5($this->getName() . '.' . implode('.', $hashes));
return md5(static::getName() . '.' . implode('.', $hashes));
}

final public function jsonSerialize()
Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Operators/Greater.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Greater extends Operator
{
public static function getName(): string
{
return 'greaterThan';
return 'greater';
}

protected function getResult(array $vars, array $childrenValues)
Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Operators/GreaterOrEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class GreaterOrEqual extends Operator
{
public static function getName(): string
{
return 'greaterOrEqualThan';
return 'greaterOrEqual';
}

protected function getResult(array $vars, array $childrenValues): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Operators/LessOrEqual.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class LessOrEqual extends Operator
{
public static function getName(): string
{
return 'lessOrEqualThan';
return 'lessOrEqual';
}

protected function getResult(array $vars, array $childrenValues)
Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Operators/LessThan.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class LessThan extends Operator
{
public static function getName(): string
{
return 'greaterThan';
return 'less';
}

protected function getResult(array $vars, array $childrenValues)
Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Types/Condition/Operators/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function getJSONData(): ?array

public function __toString(): string
{
return implode(' AND ', $this->getOperands());
return '(' . implode(' AND ', $this->getOperands()) . ')';
}

protected function isValid(array $vars, array $childrenValues): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Types/Condition/Operators/Any.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function getJSONData(): ?array

public function __toString(): string
{
return implode(' OR ', $this->getOperands());
return '(' . implode(' OR ', $this->getOperands()) . ')';
}

protected function isValid(array $vars, array $childrenValues): bool
Expand Down
4 changes: 2 additions & 2 deletions src/Elements/Types/ParentTypes/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Action extends ParentType
/**
* @var \Ezweb\Workflow\Elements\Actions\Action
*/
public \Ezweb\Workflow\Elements\Actions\Action $function;
protected \Ezweb\Workflow\Elements\Actions\Action $function;

public static function getName(): string
{
Expand Down Expand Up @@ -44,7 +44,7 @@ public function getJSONData(): ?array

public function __toString(): string
{
return '('.$this->function.')';
return '(' . $this->function . ')';
}

public function getValues(): array
Expand Down
5 changes: 4 additions & 1 deletion src/Elements/Types/ScalarTypes/Scalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public function getJSONData(): ?array

public function __toString(): string
{
return (string) $this->getValue();
if (is_string($this->getValue())) {
return (string)'"' . $this->getValue() . '"';
}
return (string)$this->getValue();
}

protected function isValid(array $vars, array $childrenValues): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Elements/Types/ScalarTypes/ScalarType.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function setScalarValue($scalarValue)
public static function createFromParser(\stdClass $config, \Ezweb\Workflow\Loader $configLoader): self
{
$instance = new static();
if (!is_scalar($config->value) && $config->value !== null) {
if ((!is_scalar($config->value) || is_array($config->value)) && $config->value !== null) {
throw new \InvalidArgumentException('ScalarType must have a scalar value');
}
$instance->scalarValue = $config->value;
Expand Down
83 changes: 83 additions & 0 deletions tests/Elements/Actions/Arithmetics/DivideTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Ezweb\Workflow\Test\Elements\Action\Arithmetics;


class DivideTest extends \PHPUnit\Framework\TestCase
{
private \Ezweb\Workflow\Test\Mocker\ScalarMocker $scalarBuilder;

protected function setUp(): void
{
$this->scalarBuilder = new \Ezweb\Workflow\Test\Mocker\ScalarMocker();
}

public function testCreateANewDivideElement()
{
$divide = \Ezweb\Workflow\Elements\Actions\Arithmetics\Divide::create();
$this->assertInstanceOf(\Ezweb\Workflow\Elements\Actions\Arithmetics\Divide::class, $divide);
}

public function testAddArg()
{
$divide = \Ezweb\Workflow\Elements\Actions\Arithmetics\Divide::create();

$divide->addArgs($this->scalarBuilder->getMockWithValue(1));
$divide->addArgs($this->scalarBuilder->getMockWithValue(2));
$this->assertCount(2, $divide->getArgs());
}

/**
* @dataProvider resultIsOkProvider
*/
public function testResultIsOk($a, $b, $expected)
{
$divide = \Ezweb\Workflow\Elements\Actions\Arithmetics\Divide::create();

$method = new \ReflectionMethod($divide, 'getResult');
$method->setAccessible(true);

$this->assertSame(
$expected,
$method->invoke($divide, [], [$a, $b]),
'getResult for ' . $a . ' / ' . $b . ' = ' . $expected
);
}

public function resultIsOkProvider()
{
return [
[4, 2, 2],
[4.5, 2, 2.25],
[4.5, 2.5, 1.8],
];
}

/**
* @dataProvider isValidValuesProvider
*/
public function testIsValid($a, $b, $expected)
{
$divide = \Ezweb\Workflow\Elements\Actions\Arithmetics\Divide::create();

$method = new \ReflectionMethod($divide, 'isValid');
$method->setAccessible(true);

$this->assertSame(
$expected,
$method->invokeArgs($divide, [[], [$a, $b]]),
'isValid for ' . $a . ' and ' . $b . ' expect ' . ($expected ? "true" : "false")
);
}

public function isValidValuesProvider()
{
return [
[1, 2, true],
[1.1, 2.2, true],
['a', 2, false],
[1.1, 'b', false],
['a', 'b', false]
];
}
}
86 changes: 86 additions & 0 deletions tests/Elements/Actions/Arithmetics/MinusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Ezweb\Workflow\Test\Elements\Action\Arithmetics;


class MinusTest extends \PHPUnit\Framework\TestCase
{
private \Ezweb\Workflow\Test\Mocker\ScalarMocker $scalarBuilder;

protected function setUp(): void
{
$this->scalarBuilder = new \Ezweb\Workflow\Test\Mocker\ScalarMocker();
}

public function testCreateANewMinusElement()
{
$minus = \Ezweb\Workflow\Elements\Actions\Arithmetics\Minus::create();
$this->assertInstanceOf(\Ezweb\Workflow\Elements\Actions\Arithmetics\Minus::class, $minus);
}

public function testAddArg()
{
$minus = \Ezweb\Workflow\Elements\Actions\Arithmetics\Minus::create();

$minus->addArgs($this->scalarBuilder->getMockWithValue(1));
$minus->addArgs($this->scalarBuilder->getMockWithValue(2));
$this->assertCount(2, $minus->getArgs());
}

/**
* @dataProvider resultIsOkProvider
*/
public function testResultIsOk($a, $b, $expected)
{
$minus = \Ezweb\Workflow\Elements\Actions\Arithmetics\Minus::create();

$method = new \ReflectionMethod($minus, 'getResult');
$method->setAccessible(true);

$this->assertSame(
$expected,
$method->invoke($minus, [], [$a, $b]),
'getResult for ' . $a . ' % ' . $b . ' = ' . $expected
);
}

public function resultIsOkProvider()
{
return [
[4, 2, 2],
[2, 3, -1],
[4.5, 2, 2.5],
[2.5, 4.5, -2.0],
[3.1415, 3.1415, 0.0],
[9, 7, 2],
];
}

/**
* @dataProvider isValidValuesProvider
*/
public function testIsValid($a, $b, $expected)
{
$minus = \Ezweb\Workflow\Elements\Actions\Arithmetics\Minus::create();

$method = new \ReflectionMethod($minus, 'isValid');
$method->setAccessible(true);

$this->assertSame(
$expected,
$method->invokeArgs($minus, [[], [$a, $b]]),
'isValid for ' . $a . ' and ' . $b . ' expect ' . ($expected ? "true" : "false")
);
}

public function isValidValuesProvider()
{
return [
[1, 2, true],
[1.1, 2.2, true],
['a', 2, false],
[1.1, 'b', false],
['a', 'b', false]
];
}
}
98 changes: 98 additions & 0 deletions tests/Elements/Actions/Arithmetics/ModuloTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Ezweb\Workflow\Test\Actions\Arithmetics;

class ModuloTest extends \PHPUnit\Framework\TestCase
{
private \Ezweb\Workflow\Test\Mocker\ScalarMocker $scalarBuilder;

protected function setUp(): void
{
$this->scalarBuilder = new \Ezweb\Workflow\Test\Mocker\ScalarMocker();
}

public function testCreateANewModuloElement()
{
$modulo = \Ezweb\Workflow\Elements\Actions\Arithmetics\Modulo::create();
$this->assertInstanceOf(\Ezweb\Workflow\Elements\Actions\Arithmetics\Modulo::class, $modulo);
}

public function testAddArg()
{
$modulo = \Ezweb\Workflow\Elements\Actions\Arithmetics\Modulo::create();

$modulo->addArgs($this->scalarBuilder->getMockWithValue(1));
$modulo->addArgs($this->scalarBuilder->getMockWithValue(2));
$this->assertCount(2, $modulo->getArgs());
}

/**
* @dataProvider resultIsOkProvider
*/
public function testResultIsOk($a, $b, $expected)
{
$modulo = \Ezweb\Workflow\Elements\Actions\Arithmetics\Modulo::create();
$modulo->addArgs($this->scalarBuilder->getMockWithValue(4));
$modulo->addArgs($this->scalarBuilder->getMockWithValue(2));

$method = new \ReflectionMethod($modulo, 'getResult');
$method->setAccessible(true);

$this->assertSame(
$expected,
$method->invoke($modulo, [], [$a, $b]),
'getResult for ' . $a . ' % ' . $b . ' = ' . $expected
);
}

public function resultIsOkProvider()
{
return [
[4, 2, 0],
[5, 2, 1],
[2, 5, 2],
[4.0, 2.0, 0],
[4.0, 2.1, 0],
[4.2, 2.2, 0],
[4.5, 2.2, 0],
[4.6, 2.2, 0],
[4.6, 2, 0],
["4.6", "2", 0],
[5.2, 2.2, 1],
[5.6, 2.2, 1],
[5.2, 2.5, 1],
[5.2, 2.8, 1],
[2.8, 5.2, 2],
];
}

/**
* @dataProvider isValidValuesProvider
*/
public function testIsValid($a, $b, $expected)
{
$modulo = \Ezweb\Workflow\Elements\Actions\Arithmetics\Modulo::create();
$modulo->addArgs($this->scalarBuilder->getMockWithValue(4));
$modulo->addArgs($this->scalarBuilder->getMockWithValue(2));

$method = new \ReflectionMethod($modulo, 'isValid');
$method->setAccessible(true);

$this->assertSame(
$expected,
$method->invokeArgs($modulo, [[], [$a, $b]]),
'isValid for ' . $a . ' and ' . $b . ' expect ' . ($expected ? "true" : "false")
);
}

public function isValidValuesProvider()
{
return [
[1, 2, true],
[1.1, 2.2, true],
['a', 2, false],
[1.1, 'b', false],
['a', 'b', false]
];
}
}
Loading