Skip to content

Commit

Permalink
require Actions and Conditions to provide a __toString method
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagof committed Aug 20, 2023
1 parent 0354fd4 commit 5ff7f08
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 14 deletions.
3 changes: 2 additions & 1 deletion database/factories/ActionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Factories\Factory;
use Squarebit\Volition\Contracts\IsAction;
use Squarebit\Volition\Models\Action;
use Squarebit\Volition\Tests\Support\TestObject;

/**
* @extends Factory<Action>
Expand All @@ -16,7 +17,7 @@ class ActionFactory extends Factory
public function definition(): array
{
return [
'rule_id' => fn () => RuleFactory::new()->create()->id,
'rule_id' => fn () => RuleFactory::new()->forObject(TestObject::class)->create()->id,
'active' => true,
];
}
Expand Down
3 changes: 2 additions & 1 deletion database/factories/ConditionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Factories\Factory;
use Squarebit\Volition\Contracts\IsCondition;
use Squarebit\Volition\Models\Condition;
use Squarebit\Volition\Tests\Support\TestObject;

/**
* @extends Factory<Condition>
Expand All @@ -16,7 +17,7 @@ class ConditionFactory extends Factory
public function definition(): array
{
return [
'rule_id' => fn () => RuleFactory::new()->create()->id,
'rule_id' => fn () => RuleFactory::new()->forObject(TestObject::class)->create()->id,
'active' => true,
];
}
Expand Down
1 change: 0 additions & 1 deletion database/factories/RuleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public function definition(): array
{
return [
'name' => fake()->name,
'applies_to' => TestObject::class,
];
}

Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/IsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
interface IsAction
{
public function execute(Volitional $object): mixed;

public function __toString();
}
2 changes: 2 additions & 0 deletions src/Contracts/IsCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ interface IsCondition
public function validate(Volitional $object, bool $isValid): void;

public function passes(Volitional $object): bool;

public function __toString();
}
2 changes: 1 addition & 1 deletion src/Models/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* @property int $id
* @property class-string $class
* @property classObj $payload
* @property object $payload
*/
abstract class Element extends Model
{
Expand Down
14 changes: 7 additions & 7 deletions tests/Support/ObjectPropertyCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ class ObjectPropertyCondition implements IsCondition
{
public function __construct(
public string $property,
public mixed $value,
) {
public mixed $value,
)
{
}

public function passes(object $object): bool
{
return $object->{$this->property} === $this->value;
}

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

public function validate(Volitional $object, bool $isValid): void
{
// You can throw an exception if invalid
}

public function __toString(): string
{
return $this->property . ' = ' . $this->value;
}
}
12 changes: 9 additions & 3 deletions tests/Support/PrefixAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ class PrefixAction implements IsAction
{
public function __construct(
public string $prefix = ''
) {
)
{
}

/**
* @param TestObject $object
* @param TestObject $object
*/
public function execute(mixed $object): string
{
throw_if($object->property === $this->prefix, ActionExecutionException::class, static::class, $object::class);

return $this->prefix.$object->property;
return $this->prefix . $object->property;
}

public function __toString(): string
{
return __('Prefix') . ': ' . $this->prefix;
}
}
5 changes: 5 additions & 0 deletions tests/Support/SuffixAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public function execute(mixed $object): string
{
return $object->property.$this->suffix;
}

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

0 comments on commit 5ff7f08

Please sign in to comment.