Skip to content

Commit

Permalink
Merge pull request #85 from tarfin-labs/WB-610-event-machine-fakeable…
Browse files Browse the repository at this point in the history
…-invokable-behaviors

Wb 610 event machine fakeable invokable behaviors
  • Loading branch information
deligoez authored Nov 4, 2024
2 parents f1f6ddf + 46f1219 commit f881b7e
Show file tree
Hide file tree
Showing 4 changed files with 398 additions and 20 deletions.
24 changes: 12 additions & 12 deletions src/Behavior/InvokableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use ReflectionUnionType;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Tarfinlabs\EventMachine\Actor\State;
use Tarfinlabs\EventMachine\ContextManager;
use Tarfinlabs\EventMachine\Traits\Fakeable;
use Tarfinlabs\EventMachine\Exceptions\MissingMachineContextException;

/**
Expand All @@ -21,6 +23,8 @@
*/
abstract class InvokableBehavior
{
use Fakeable;

/** @var array<string> An array containing the required context and the type of the context for the code to execute correctly. */
public array $requiredContext = [];

Expand Down Expand Up @@ -169,20 +173,16 @@ public static function injectInvokableBehaviorParameters(
}

/**
* This method is used to create an instance of the invoking class.
*/
public static function make(): mixed
{
return app(static::class);
}

/**
* This method creates an instance of the invoking class and calls it as a callable, passing any provided arguments.
* Run the behavior with the given arguments.
*
* @param mixed ...$arguments
* @param mixed ...$args Arguments to pass to the behavior
*/
public static function run(...$arguments): mixed
public static function run(mixed ...$args): mixed
{
return static::make()(...$arguments);
$instance = static::isFaked()
? App::make(static::class)
: new static();

return $instance(...$args);
}
}
105 changes: 105 additions & 0 deletions src/Traits/Fakeable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

namespace Tarfinlabs\EventMachine\Traits;

use Mockery;
use RuntimeException;
use Mockery\MockInterface;
use Illuminate\Support\Facades\App;

trait Fakeable
{
/** @var array Stores fake instances */
private static array $fakes = [];

/**
* Create a fake instance of the behavior.
*/
public static function fake(): MockInterface
{
$mock = Mockery::mock(static::class);

static::$fakes[static::class] = $mock;

App::bind(abstract: static::class, concrete: fn () => $mock);

return $mock;
}

/**
* Check if the behavior is faked.
*/
public static function isFaked(): bool
{
return isset(static::$fakes[static::class]);
}

/**
* Get the fake instance if exists.
*/
public static function getFake(): ?MockInterface
{
return static::$fakes[static::class] ?? null;
}

/**
* Reset all fakes.
*/
public static function resetFakes(): void
{
static::$fakes = [];
if (App::has(id: static::class)) {
App::forgetInstance(abstract: static::class);
}
}

/**
* Set run expectations for the fake behavior.
*/
public static function shouldRun(): Mockery\Expectation|Mockery\CompositeExpectation
{
if (!isset(static::$fakes[static::class])) {
static::fake();
}

return static::$fakes[static::class]->shouldReceive('__invoke');
}

/**
* Set return value for the fake behavior.
*/
public static function shouldReturn(mixed $value): void
{
if (!isset(static::$fakes[static::class])) {
static::fake();
}

static::$fakes[static::class]->shouldReceive('__invoke')->andReturn($value);
}

/**
* Assert that the behavior was run.
*/
public static function assertRan(): void
{
if (!isset(static::$fakes[static::class])) {
throw new RuntimeException(message: 'Behavior '.static::class.' was not faked.');
}

static::$fakes[static::class]->shouldHaveReceived('__invoke');
}

/**
* Assert that the behavior was not run.
*/
public static function assertNotRan(): void
{
if (!isset(static::$fakes[static::class])) {
throw new RuntimeException(message: 'Behavior '.static::class.' was not faked.');
}

static::$fakes[static::class]->shouldNotHaveReceived('__invoke');
}
}
14 changes: 6 additions & 8 deletions tests/ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,12 @@ public function __invoke(EventBehavior $event): int
};

// 2. Act
$result = $multipleWithItselfAction(
EventDefinition::from([
'type' => 'ADD',
'payload' => [
'value' => $value,
],
]),
);
$result = $multipleWithItselfAction::run(EventDefinition::from([
'type' => 'ADD',
'payload' => [
'value' => $value,
],
]));

// 3. Assert
expect($result)->toBe($value * $value);
Expand Down
Loading

0 comments on commit f881b7e

Please sign in to comment.