-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#145: Fix bug with empty arguments #170
Conversation
src/Traits/MockTrait.php
Outdated
if ($isClass) { | ||
$reflectionMethod = new \ReflectionMethod($class, $function); | ||
$parameters = $reflectionMethod->getParameters(); | ||
$requiredParametersCount = count(array_filter($parameters, fn($param) => !$param->isOptional())); | ||
} else { | ||
$parameters = $actual; | ||
$requiredParametersCount = count(array_filter($actual, fn ($item) => $item !== self::OPTIONAL_ARGUMENT_NAME)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's move it to the separate method which will return [$parameters, $requiredParametersCount]
src/Traits/MockTrait.php
Outdated
@@ -134,21 +134,43 @@ protected function assertArguments( | |||
$expectedCount = count($expected); | |||
$actualCount = count($actual); | |||
|
|||
if ($expectedCount !== $actualCount) { | |||
if ($isClass) { | |||
$reflectionMethod = new \ReflectionMethod($class, $function); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$reflectionMethod = new \ReflectionMethod($class, $function); | |
$reflectionMethod = new ReflectionMethod($class, $function); |
src/Traits/MockTrait.php
Outdated
if ($isClass) { | ||
$reflectionMethod = new \ReflectionMethod($class, $function); | ||
$parameters = $reflectionMethod->getParameters(); | ||
$requiredParametersCount = count(array_filter($parameters, fn($param) => !$param->isOptional())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$requiredParametersCount = count(array_filter($parameters, fn($param) => !$param->isOptional())); | |
$requiredParametersCount = count(array_filter($parameters, fn ($param) => !$param->isOptional())); |
src/Traits/MockTrait.php
Outdated
|
||
$message = ($isClass) | ||
? "Class '{$class}'\nMethod: '{$function}'\nMethod call index: {$callIndex}" | ||
: "Namespace '{$class}'\nFunction: '{$function}'\nCall index: {$callIndex}"; | ||
|
||
foreach ($actual as $index => $argument) { | ||
$expectedArgument = $expected[$index] ?? null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as discussed, please check that this is realy need
src/Traits/MockTrait.php
Outdated
): void { | ||
$reflection = $isClass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$reflection = $isClass | |
$reflection = ($isClass) |
src/Traits/MockTrait.php
Outdated
|
||
$this->fillOptionalArguments($parameters, $actual, $expected, $isClass); | ||
|
||
$message = $isClass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$message = $isClass | |
$message = ($isClass) |
src/Traits/MockTrait.php
Outdated
|
||
protected function assertArgumentCount(int $expectedCount, int $actualCount, int $requiredParametersCount, string $function): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
protected function assertArgumentCount(int $expectedCount, int $actualCount, int $requiredParametersCount, string $function): void | |
protected function assertArgumentsCount(int $expectedCount, int $actualCount, int $requiredParametersCount, string $function): void |
src/Traits/MockTrait.php
Outdated
$expectedCount = count($expected); | ||
$actualCount = count($actual); | ||
$parameters = $reflection->getParameters(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$parameters = $reflection->getParameters(); | |
$reflectionArgs = $reflection->getParameters(); |
src/Traits/MockTrait.php
Outdated
$requiredParametersCount = count(array_filter($parameters, fn ($param) => !$param->isOptional())); | ||
|
||
$this->assertArgumentCount($expectedCount, $actualCount, $requiredParametersCount, $function); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's move counting into the assertArgumentCount function
$requiredParametersCount = count(array_filter($parameters, fn ($param) => !$param->isOptional())); | |
$this->assertArgumentCount($expectedCount, $actualCount, $requiredParametersCount, $function); | |
$this->assertArgumentCount($expected, $actual, $reflectionArgs, $function); |
tests/MockTraitTest.php
Outdated
$this->assertEquals('mockFunction', ($mock->mockFunction('firstRequired', 'secondRequired'))); | ||
$this->assertEquals('mockFunction', ($mock->mockFunction('firstRequired', 'secondRequired', 'firstOptional'))); | ||
$this->assertEquals('mockFunction', ($mock->mockFunction('firstRequired', 'secondRequired', 'firstOptional', 'secondOptional'))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$this->assertEquals('mockFunction', ($mock->mockFunction('firstRequired', 'secondRequired'))); | |
$this->assertEquals('mockFunction', ($mock->mockFunction('firstRequired', 'secondRequired', 'firstOptional'))); | |
$this->assertEquals('mockFunction', ($mock->mockFunction('firstRequired', 'secondRequired', 'firstOptional', 'secondOptional'))); | |
$this->assertEquals('mockFunction', $mock->mockFunction('firstRequired', 'secondRequired')); | |
$this->assertEquals('mockFunction', $mock->mockFunction('firstRequired', 'secondRequired', 'firstOptional')); | |
$this->assertEquals('mockFunction', $mock->mockFunction('firstRequired', 'secondRequired', 'firstOptional', 'secondOptional')); |
tests/MockTraitTest.php
Outdated
$this->functionCall('mockFunction', ['firstRequired', 'secondRequired'], 'mockFunction'), | ||
]); | ||
|
||
$this->assertNotEquals('result', $mock->mockFunction('firstRequired', 'secondRequired')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$this->functionCall('mockFunction', ['firstRequired', 'secondRequired'], 'mockFunction'), | |
]); | |
$this->assertNotEquals('result', $mock->mockFunction('firstRequired', 'secondRequired')); | |
$this->functionCall('mockFunction', ['firstRequired', 'secondRequired'], 'mocked_result'), | |
]); | |
$this->assertEquals('mocked_result', $mock->mockFunction('firstRequired', 'secondRequired')); |
tests/MockTraitTest.php
Outdated
$this->assertEquals('mockFunction', ($mock->mockFunction('firstRequired', 'secondRequired', 'firstOptional', 'secondOptional'))); | ||
} | ||
|
||
public function testMockFunctionInClassWhenDifferentResult() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function testMockFunctionInClassWhenDifferentResult() | |
public function testMockClassMethodCheckMockedResult() |
tests/MockTraitTest.php
Outdated
$this->assertNotEquals('result', $mock->mockFunction('firstRequired', 'secondRequired')); | ||
} | ||
|
||
public function testMockFunctionInClassWhenLessRequiredParameters() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function testMockFunctionInClassWhenLessRequiredParameters() | |
public function testMockClassMethodWhenLessRequiredParameters() |
tests/MockTraitTest.php
Outdated
} | ||
|
||
public function testAssertArgumentMismatchBetweenExpectedAndActualArguments() | ||
public function testMockFunctionInClassWhenMoreExpectedParameters() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function testMockFunctionInClassWhenMoreExpectedParameters() | |
public function testMockClassMethodWhenMoreExpectedParameters() |
src/Traits/MockTrait.php
Outdated
$requiredParametersCount = count(array_filter($actual, fn ($item) => $item !== self::OPTIONAL_ARGUMENT_NAME)); | ||
$this->assertFalse( | ||
$expectedCount < $requiredParametersCount, | ||
"Failed assert that function {$function} was called with {$expectedCount} require arguments, actually it calls with {$requiredParametersCount} require arguments." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Failed assert that function {$function} was called with {$expectedCount} require arguments, actually it calls with {$requiredParametersCount} require arguments." | |
"Failed assert that function {$function} was called with {$expectedCount} arguments, actually it has {$requiredParametersCount} required arguments." |
src/Traits/MockTrait.php
Outdated
? "Class '{$class}'\nMethod: '{$function}'\nMethod call index: {$callIndex}" | ||
: "Namespace '{$class}'\nFunction: '{$function}'\nCall index: {$callIndex}"; | ||
if (!$isClass && $actual[$index] === 'optionalParameter') { | ||
$actual[$index] = $expected[$index] ?? $parameter->getDefaultValue(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please move actual args filling before the expected args filling
$actual[$index] = $expected[$index] ?? $parameter->getDefaultValue(); | |
$actual[$index] = $parameter->getDefaultValue(); |
#145 (comment)
#146 (comment)