Skip to content

Commit

Permalink
Pass arguments to the fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Feb 24, 2024
1 parent a86ef32 commit 4c97885
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
}
},
"scripts": {
"test": "php -ddisable_functions=time vendor/bin/phpunit"
"test": "php -ddisable_functions=time,header vendor/bin/phpunit"
}
}
2 changes: 1 addition & 1 deletion src/Mocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function $functionName(...\$arguments)
if (MockerState::checkCondition(__NAMESPACE__, "$functionName", \$arguments)) {
\$result = MockerState::getResult(__NAMESPACE__, "$functionName", \$arguments);
} else {
\$result = MockerState::getDefaultResult(__NAMESPACE__, "$functionName", $function);
\$result = MockerState::getDefaultResult(__NAMESPACE__, "$functionName", \$arguments, $function);
}
return MockerState::saveTraceResult(__NAMESPACE__, "$functionName", \$position, \$result);
Expand Down
3 changes: 2 additions & 1 deletion src/MockerState.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,14 @@ public static function getResult(
public static function getDefaultResult(
string $namespace,
string $functionName,
array $arguments,
callable $fallback,
): mixed {
if (isset(self::$defaults[$namespace][$functionName])) {
return self::$defaults[$namespace][$functionName];
}

return $fallback();
return $fallback(...$arguments);
}

public static function saveState(): void
Expand Down
23 changes: 23 additions & 0 deletions tests/Integration/HeaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Xepozz\InternalMocker\Tests\Integration;

use PHPUnit\Framework\TestCase;
use Xepozz\InternalMocker\MockerState;

final class HeaderTest extends TestCase
{
public function testRun()
{
header('Content-Type: application/json');

$trace = MockerState::getTraces(
'',
'header',
);

$this->assertEquals(['Content-Type: application/json'], $trace[0]['arguments']);
}
}
2 changes: 1 addition & 1 deletion tests/Integration/TraceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testBacktrace(): void

$this->assertEquals(['test'], $traces[0]['arguments']);
$this->assertEquals('saveTrace', $traces[0]['trace'][0]['function']);
$this->assertEquals(120, $traces[0]['trace'][0]['line']);
$this->assertEquals(132, $traces[0]['trace'][0]['line']);
$this->assertEquals(
[
__NAMESPACE__,
Expand Down
5 changes: 5 additions & 0 deletions tests/MockerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public static function load(): void
'name' => 'time',
'function' => fn () => `date +%s`,
],
[
'namespace' => '',
'name' => 'header',
'function' => fn (string $value) => `date +%s`,
],
];

$mocker = new Mocker();
Expand Down
4 changes: 2 additions & 2 deletions tests/MockerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function time(...\$arguments)
if (MockerState::checkCondition(__NAMESPACE__, "time", \$arguments)) {
\$result = MockerState::getResult(__NAMESPACE__, "time", \$arguments);
} else {
\$result = MockerState::getDefaultResult(__NAMESPACE__, "time", fn() => \\time(...\$arguments));
\$result = MockerState::getDefaultResult(__NAMESPACE__, "time", \$arguments, fn() => \\time(...\$arguments));
}
return MockerState::saveTraceResult(__NAMESPACE__, "time", \$position, \$result);
Expand Down Expand Up @@ -112,7 +112,7 @@ function str_contains(...\$arguments)
if (MockerState::checkCondition(__NAMESPACE__, "str_contains", \$arguments)) {
\$result = MockerState::getResult(__NAMESPACE__, "str_contains", \$arguments);
} else {
\$result = MockerState::getDefaultResult(__NAMESPACE__, "str_contains", fn() => \\str_contains(...\$arguments));
\$result = MockerState::getDefaultResult(__NAMESPACE__, "str_contains", \$arguments, fn() => \\str_contains(...\$arguments));
}
return MockerState::saveTraceResult(__NAMESPACE__, "str_contains", \$position, \$result);
Expand Down

0 comments on commit 4c97885

Please sign in to comment.