Skip to content

Commit

Permalink
feat(Testing): Pass method parameters to expectation hook.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: hook closure now receives more properties (expectation is last).
  • Loading branch information
pionl committed Dec 14, 2022
1 parent 93855ce commit 84a913a
Show file tree
Hide file tree
Showing 68 changed files with 127 additions and 101 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"post-autoload-dump": [
"@php ./vendor/bin/testbench package:discover --ansi"
],
"check": "composer lint && composer test",
"check": "composer lint && composer test && composer lint:stan",
"lint:check": "./vendor/bin/ecs",
"lint:fix": "./vendor/bin/ecs --fix",
"lint:stan": "./vendor/bin/phpstan",
Expand Down
31 changes: 27 additions & 4 deletions src/Testing/Commands/MakeExpectationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,14 @@ protected function generateExpectationMethodAssert(
: $expectationClassName . '::class'
));

$hookParameters = [];

if ($parameters !== []) {
$assertMethod->addBody('$message = $this->getDebugMessage();');
$assertMethod->addBody('');

foreach ($parameters as $parameter) {
$hookParameters[] = sprintf('$%s', $parameter->name);
$assertMethod->addBody(sprintf(
'Assert::assertEquals($expectation->%s, $%s, $message);',
$parameter->name,
Expand All @@ -225,7 +228,11 @@ protected function generateExpectationMethodAssert(
$assertMethod->addBody('');

$assertMethod->addBody(sprintf('if (is_callable($expectation->%s)) {', self::HookProperty));
$assertMethod->addBody(sprintf(' call_user_func($expectation->%s, $expectation);', self::HookProperty));
$assertMethod->addBody(sprintf(
' call_user_func($expectation->%s, %s, $expectation);',
self::HookProperty,
implode(', ', $hookParameters),
));
$assertMethod->addBody('}');

$returnType = $method->getReturnType();
Expand Down Expand Up @@ -321,12 +328,13 @@ protected function getExpectationFileContents(
$this->setParameterType($returnType, $constructorParameter);
}

$parameterTypes = [];
foreach ($parameters as $parameter) {
$constructorParameter = $constructor
->addPromotedParameter($parameter->name)
->setReadOnly();

$this->setParameterType($parameter->getType(), $constructorParameter);
$parameterTypes[] = $this->setParameterType($parameter->getType(), $constructorParameter);
$this->setParameterDefaultValue($parameter, $constructorParameter);
}

Expand All @@ -337,15 +345,17 @@ protected function getExpectationFileContents(
->setNullable()
->setDefaultValue(null);

$constructor->addComment(sprintf('@param \Closure(self):void|null $%s', self::HookProperty));
$constructor->addComment(
sprintf('@param \Closure(%s,self):void|null $%s', implode(',', $parameterTypes), self::HookProperty)
);

return $printer->printFile($file);
}

protected function setParameterType(
ReflectionType|ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null $type,
PromotedParameter $constructorParameter
): void {
): string {
$proposedType = '';

$allowNull = false;
Expand All @@ -356,6 +366,11 @@ protected function setParameterType(
$allowNull = false;
}

// Fix global namespace
if (class_exists($name)) {
return '\\' . $name;
}

return $name;
}

Expand All @@ -366,6 +381,12 @@ protected function setParameterType(
if ($type instanceof ReflectionNamedType) {
$allowNull = $type->allowsNull();
$proposedType = $type->getName();

if (class_exists($proposedType)) {
// Fix global namespace
$proposedType = '\\' . $proposedType;
}

$constructorParameter->setNullable($type->allowsNull());
} elseif ($type instanceof ReflectionUnionType) {
$allowNull = $type->allowsNull();
Expand All @@ -389,6 +410,8 @@ protected function setParameterType(
}

$constructorParameter->setType($proposedType);

return $proposedType;
}

protected function setParameterDefaultValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class MultiFunctionContractAssert extends \LaraStrict\Testing\AbstractExpectatio
Assert::assertEquals($expectation->third, $third, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
call_user_func($expectation->hook, $first, $second, $third, $expectation);
}

return $this;
Expand All @@ -76,7 +76,7 @@ class MultiFunctionContractAssert extends \LaraStrict\Testing\AbstractExpectatio
Assert::assertEquals($expectation->third, $third, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
call_user_func($expectation->hook, $first, $second, $third, $expectation);
}

return $this;
Expand All @@ -99,7 +99,7 @@ class MultiFunctionContractAssert extends \LaraStrict\Testing\AbstractExpectatio
Assert::assertEquals($expectation->third, $third, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
call_user_func($expectation->hook, $first, $second, $third, $expectation);
}

return $this;
Expand All @@ -118,7 +118,7 @@ class MultiFunctionContractAssert extends \LaraStrict\Testing\AbstractExpectatio
Assert::assertEquals($expectation->third, $third, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
call_user_func($expectation->hook, $first, $second, $third, $expectation);
}

return $expectation->return;
Expand All @@ -137,7 +137,7 @@ class MultiFunctionContractAssert extends \LaraStrict\Testing\AbstractExpectatio
Assert::assertEquals($expectation->third, $third, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
call_user_func($expectation->hook, $first, $second, $third, $expectation);
}

return $expectation->return;
Expand All @@ -156,7 +156,7 @@ class MultiFunctionContractAssert extends \LaraStrict\Testing\AbstractExpectatio
Assert::assertEquals($expectation->third, $third, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
call_user_func($expectation->hook, $first, $second, $third, $expectation);
}

return $expectation->return;
Expand All @@ -175,7 +175,7 @@ class MultiFunctionContractAssert extends \LaraStrict\Testing\AbstractExpectatio
Assert::assertEquals($expectation->third, $third, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
call_user_func($expectation->hook, $first, $second, $third, $expectation);
}

return $expectation->return;
Expand All @@ -194,7 +194,7 @@ class MultiFunctionContractAssert extends \LaraStrict\Testing\AbstractExpectatio
Assert::assertEquals($expectation->third, $third, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
call_user_func($expectation->hook, $first, $second, $third, $expectation);
}

return $this;
Expand All @@ -213,7 +213,7 @@ class MultiFunctionContractAssert extends \LaraStrict\Testing\AbstractExpectatio
Assert::assertEquals($expectation->third, $third, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
call_user_func($expectation->hook, $first, $second, $third, $expectation);
}

return $expectation->return;
Expand All @@ -229,7 +229,7 @@ class MultiFunctionContractAssert extends \LaraStrict\Testing\AbstractExpectatio
Assert::assertEquals($expectation->third, $third, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
call_user_func($expectation->hook, $first, $second, $third, $expectation);
}
}

Expand All @@ -243,7 +243,7 @@ class MultiFunctionContractAssert extends \LaraStrict\Testing\AbstractExpectatio
Assert::assertEquals($expectation->third, $third, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
call_user_func($expectation->hook, $first, $second, $third, $expectation);
}

return $expectation->return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class MultiFunctionContractMixedExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(mixed,mixed,mixed,self):void|null $hook
*/
public function __construct(
public readonly mixed $return,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class MultiFunctionContractNoReturnExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(mixed,mixed,mixed,self):void|null $hook
*/
public function __construct(
public readonly mixed $first,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class MultiFunctionContractPhpDocBoolExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(mixed,mixed,\Closure,self):void|null $hook
*/
public function __construct(
public readonly mixed $return,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class MultiFunctionContractPhpDocFloatExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(mixed,mixed,mixed,self):void|null $hook
*/
public function __construct(
public readonly mixed $return,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class MultiFunctionContractPhpDocMixedExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(mixed,mixed,mixed,self):void|null $hook
*/
public function __construct(
public readonly mixed $return,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class MultiFunctionContractPhpDocStaticExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(mixed,mixed,mixed,self):void|null $hook
*/
public function __construct(
public readonly mixed $first,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class MultiFunctionContractPhpDocStringExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(mixed,mixed,mixed,self):void|null $hook
*/
public function __construct(
public readonly mixed $return,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class MultiFunctionContractPhpDocThisExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(string,int,bool,self):void|null $hook
*/
public function __construct(
public readonly string $first,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class MultiFunctionContractPhpDocThisParamsExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(mixed,mixed,mixed,self):void|null $hook
*/
public function __construct(
public readonly mixed $first,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class MultiFunctionContractSelfExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(string,int,bool,self):void|null $hook
*/
public function __construct(
public readonly string $first,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class MultiFunctionContractSelfViaClassExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(mixed,mixed,mixed,self):void|null $hook
*/
public function __construct(
public readonly mixed $return,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function execute(string $first, int $second, bool $third)
Assert::assertEquals($expectation->third, $third, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
call_user_func($expectation->hook, $first, $second, $third, $expectation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
final class SimpleActionContractExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(string,int,bool,self):void|null $hook
*/
public function __construct(
public readonly string $first,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class TestActionContractAssert extends \LaraStrict\Testing\AbstractExpectationCa
Assert::assertEquals($expectation->intersectionType, $intersectionType, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
call_user_func($expectation->hook, $int, $string, $noTypeHint, $type, $testingApplication, $multi, $multiNull, $multiNullDefault, $optional, $optionalString, $constant, $constantClass, $enumDefault, $noTypeHintDefault, $customConstants, $object, $intersectionType, $expectation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class TestActionContractExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(int,string,mixed,\LaraStrict\Enums\EnvironmentType,\LaraStrict\Testing\Laravel\TestingApplication,string|int,string|int|null,string|int|null,string,string,string,int,\LaraStrict\Enums\EnvironmentType,mixed,string,\LaraStrict\Testing\Laravel\TestingApplication,ArrayAccess&Illuminate\Support\Enumerable,self):void|null $hook
*/
public function __construct(
public readonly int $int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class TestActionExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(int,string,mixed,\LaraStrict\Enums\EnvironmentType,\LaraStrict\Testing\Laravel\TestingApplication,string|int,string|int|null,string|int|null,string,string,string,int,\LaraStrict\Enums\EnvironmentType,mixed,string,\LaraStrict\Testing\Laravel\TestingApplication,ArrayAccess&Illuminate\Support\Enumerable,self):void|null $hook
*/
public function __construct(
public readonly int $int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class TestReturnActionContractAssert extends \LaraStrict\Testing\AbstractExpecta
Assert::assertEquals($expectation->intersectionType, $intersectionType, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $expectation);
call_user_func($expectation->hook, $int, $string, $noTypeHint, $type, $testingApplication, $multi, $multiNull, $multiNullDefault, $optional, $optionalString, $constant, $constantClass, $enumDefault, $noTypeHintDefault, $customConstants, $object, $intersectionType, $expectation);
}

return $expectation->return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class TestReturnActionContractExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(int,string,mixed,\LaraStrict\Enums\EnvironmentType,\LaraStrict\Testing\Laravel\TestingApplication,string|int,string|int|null,string|int|null,string,string,string,int,\LaraStrict\Enums\EnvironmentType,mixed,string,\LaraStrict\Testing\Laravel\TestingApplication,ArrayAccess&Illuminate\Support\Enumerable,self):void|null $hook
*/
public function __construct(
public readonly ?int $return,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class TestReturnActionExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(int,string,mixed,\LaraStrict\Enums\EnvironmentType,\LaraStrict\Testing\Laravel\TestingApplication,string|int,string|int|null,string|int|null,string,string,string,int,\LaraStrict\Enums\EnvironmentType,mixed,string,\LaraStrict\Testing\Laravel\TestingApplication,ArrayAccess&Illuminate\Support\Enumerable,self):void|null $hook
*/
public function __construct(
public readonly ?int $return,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class TestReturnIntersectionActionExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(int,string,mixed,\LaraStrict\Enums\EnvironmentType,\LaraStrict\Testing\Laravel\TestingApplication,string|int,string|int|null,string|int|null,string,string,string,int,\LaraStrict\Enums\EnvironmentType,mixed,string,\LaraStrict\Testing\Laravel\TestingApplication,ArrayAccess&Illuminate\Support\Enumerable,self):void|null $hook
*/
public function __construct(
public readonly \ArrayAccess&\Illuminate\Support\Enumerable $return,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;
final class TestReturnRequiredActionExpectation
{
/**
* @param \Closure(self):void|null $hook
* @param \Closure(int,string,mixed,\LaraStrict\Enums\EnvironmentType,\LaraStrict\Testing\Laravel\TestingApplication,string|int,string|int|null,string|int|null,string,string,string,int,\LaraStrict\Enums\EnvironmentType,mixed,string,\LaraStrict\Testing\Laravel\TestingApplication,ArrayAccess&Illuminate\Support\Enumerable,self):void|null $hook
*/
public function __construct(
public readonly int $return,
Expand Down
Loading

0 comments on commit 84a913a

Please sign in to comment.