Skip to content

Commit

Permalink
#145: Fix bug with empty arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
yogyrton committed Dec 18, 2024
1 parent d72fcea commit 3b159c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
34 changes: 29 additions & 5 deletions src/Traits/MockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,45 @@ protected function assertArguments(
$expectedCount = count($expected);
$actualCount = count($actual);

if ($expectedCount !== $actualCount) {
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));
}

if ($expectedCount > $actualCount || $expectedCount < $requiredParametersCount) {
throw new Exception("Failed assert that function {$function} was called with {$expectedCount} arguments, actually it calls with {$actualCount} arguments.");
}
if ($expectedCount !== $actualCount) {
$optionalParametersCount = $actualCount - $requiredParametersCount;

$this->assertFalse(
$expectedCount > $actualCount || $expectedCount < $requiredParametersCount,
"Failed assert that function {$function} was called with {$expectedCount} arguments, actually it calls with {$requiredParametersCount} required and {$optionalParametersCount} optional arguments."
);
}

$expected = array_pad($expected, $actualCount, self::OPTIONAL_ARGUMENT_NAME);
if ($isClass) {
foreach ($parameters as $index => $parameter) {
if (!isset($expected[$index]) && $parameter->isOptional()) {
$expected[$index] = $parameter->getDefaultValue();
}
}
} else {
$expected = array_pad($expected, $actualCount, self::OPTIONAL_ARGUMENT_NAME);
}

$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;

if ($isClass && $parameters[$index]->isOptional() && $argument !== $expectedArgument) {
$argument = $expected[$index];
}

$this->assertEquals(
$expected[$index],
$argument,
Expand Down
8 changes: 0 additions & 8 deletions tests/MockTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,4 @@ public function testMockWithDifferentFunction()
$this->assertEquals([3, 4], array_slice([1, 2, 3, 4, 5], 2, 2));
$this->assertEquals([3, 4, 5], array_slice([1, 2, 3, 4, 5], 2));
}

public function testAssertArgumentMismatchBetweenExpectedAndActualArguments()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Failed assert that function testFunction was called with 2 arguments, actually it calls with 1 arguments.');

$this->assertArguments(['test'], ['test', ''], 'TestClass', 'testFunction', 0);
}
}

0 comments on commit 3b159c5

Please sign in to comment.