Skip to content

Commit

Permalink
feat(Testing)!: ContextServiceContractGetExpectation createState remo…
Browse files Browse the repository at this point in the history
…ved and add ability to assert passed $createState

$runCreateState should contain a closure that will execute given $createState method with correct parameters. The returned value will be asserted with given $expectation->return value.
  • Loading branch information
pionl committed Dec 23, 2022
1 parent 5c5a2dd commit 022b496
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,17 @@ public function get(AbstractContext $context, Closure $createState): ContextValu
$message = $this->getDebugMessage();

Assert::assertEquals($expectation->context, $context, $message);
Assert::assertEquals($expectation->createState, $createState, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $context, $createState, $expectation);
}

if (is_callable($expectation->runCreateState)) {
$result = call_user_func($expectation->runCreateState, $createState);

Assert::assertEquals($result, $expectation->return);
}

/** @phpstan-ignore-next-line */
return $expectation->return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@

final class ContextServiceContractGetExpectation
{
public readonly Closure $createState;

/**
* @param Closure(AbstractContext, Closure, self):void|null $hook
* @param Closure(Closure $context):mixed $runCreateState should contain a closure that will
* execute given $createState method with correct
* parameters. The returned value will be asserted with
* given $expectation->return value.
*/
public function __construct(
public readonly ContextValueContract $return,
public readonly AbstractContext $context,
?Closure $createState = null,
public readonly ?Closure $hook = null,
public readonly ?Closure $runCreateState = null
) {
$this->createState = $createState ?? static function () {
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public function generateData(): array
new ContextServiceContractGetExpectation(
return: $value,
context: $context,
createState: static function () {
},
hook: function (
AbstractContext $context,
Closure $createState,
Expand All @@ -90,17 +88,11 @@ public function generateData(): array
),
new AssertExpectationEntity(
methodName: 'get',
createAssert: fn () => new ContextServiceContractAssert(get: [
createAssert: static fn () => new ContextServiceContractAssert(get: [
new ContextServiceContractGetExpectation(
return: $value,
context: $context,
hook: function (
AbstractContext $context,
Closure $createState,
ContextServiceContractGetExpectation $expectation
) use ($value): void {
$this->assertSame($value, $createState('test'));
}
runCreateState: static fn (Closure $createState): TestValue => $createState('test')
),
]),
call: fn (ContextServiceContractAssert $assert) => $assert->get(
Expand Down

0 comments on commit 022b496

Please sign in to comment.