Skip to content

Add actual function that can be mocked in PHPUnit 12 #13

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

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions classes/MockDelegateFunction.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ use phpmock\functions\FunctionProvider;
* @license http://www.wtfpl.net/txt/copying/ WTFPL
* @internal
*/
abstract class MockDelegateFunction implements FunctionProvider
class MockDelegateFunction implements FunctionProvider
{

/**
* A mocked function will redirect its call to this method.
*
* @return mixed Returns the function output.
*/
abstract public function delegate({signatureParameters});
public function delegate({signatureParameters})
{
}

public function getCallable()
{
return [$this, "delegate"];
}

public function {functionName}()
{
}
}
5 changes: 4 additions & 1 deletion classes/MockDelegateFunctionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ public function build($functionName = null)
$parameterBuilder->build($functionName === null ? '' : $functionName);
$signatureParameters = $parameterBuilder->getSignatureParameters();

$populatedFunctionName = $functionName === null ? '_dummy' : $functionName;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@crocodele actually I am thinking if we should even create a method in the class, when the function name is null?

I am not really sure why we even allow the function name to be null here, and it looks like the null value is not even used in the satellite packages.

Also I wonder if we should be creating the method with a signatureParameters? As I see all seems to be working fine without the 'correct signature' tho 🤔


/**
* If a class with the same signature exists, it is considered equivalent
* to the generated class.
*/
$hash = md5($signatureParameters);
$hash = md5($populatedFunctionName . $signatureParameters);
$this->namespace = __NAMESPACE__ . $hash;
if (class_exists($this->getFullyQualifiedClassName())) {
return;
Expand All @@ -65,6 +67,7 @@ public function build($functionName = null)
$data = [
"namespace" => $this->namespace,
"signatureParameters" => $signatureParameters,
"functionName" => $populatedFunctionName,
];
$this->template->setVar($data, false);
$definition = $this->template->render();
Expand Down