Skip to content

adding support for optional and referenced parameters in BUILT-IN functions #133

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 4 commits into from
Sep 20, 2017
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
78 changes: 74 additions & 4 deletions src/AspectMock/Intercept/FunctionInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,95 @@ class FunctionInjector
<?php
namespace {{ns}};
if (!function_exists('{{ns}}\{{func}}')) {
function {{func}}() {
if ((\$__am_res = __amock_before_func('{{ns}}','{{func}}', func_get_args())) !== __AM_CONTINUE__) {
return \$__am_res;
function {{func}}() {
if ((\$__am_res = __amock_before_func('{{ns}}','{{func}}', func_get_args())) !== __AM_CONTINUE__) {
return \$__am_res;
}
return call_user_func_array('{{func}}', func_get_args());
}
return call_user_func_array('{{func}}', func_get_args());
}
EOF;

protected $templateByRefOptional = <<<EOF
<?php
namespace {{ns}};
if (!function_exists('{{ns}}\{{func}}')) {
function {{func}}({{arguments}}) {
\$args = [];
switch(count(func_get_args())) {
{{code}} }
if ((\$__am_res = __amock_before_func('{{ns}}','{{func}}', \$args)) !== __AM_CONTINUE__) {
return \$__am_res;
}
return call_user_func_array('{{func}}', \$args);
}
}
EOF;

protected $namespace;

protected $function;
protected $fileName;

function __construct($namespace, $function)
{
$this->namespace = $namespace;
$this->function = $function;
$this->placeOptionalAndReferenceFunction($namespace, $function);
$this->place('ns', $this->namespace);
$this->place('func', $this->function);
}

public function getParameterDeclaration(\ReflectionParameter $parameter, $internal)
{
$text = (string)$parameter;
if (preg_match('@Parameter\s#[0-9]+\s\[\s<(required|optional)>(.*)(\sor NULL)(.*)\s\]@', $text, $match)) {
$text = $match(2).$match[4];
} elseif (preg_match('@Parameter\s#[0-9]+\s\[\s<(required|optional)>\s(.*)\s\]@', $text, $match)) {
$text = $match[2];
} else {
throw new \Exception('reflection api changed. adjust code.');
}
if ($internal && $parameter->isOptional()) {
$text .= "=NULL";
}
return $text;
}

public function placeOptionalAndReferenceFunction($namespace, $function)
{
$reflect = new \ReflectionFunction($function);
$parameters = [];
$args = '';
$byRef = false;
$optionals = false;
$names = [];
$internal = $reflect->isInternal();
foreach ($reflect->getParameters() as $parameter) {
$name = '$'.$parameter->getName();
$newname = '$p'.$parameter->getPosition();
$declaration = str_replace($name, $newname, $this->getParameterDeclaration($parameter, $internal));
$name = $newname;
if (!$optionals && $parameter->isOptional()) {
$optionals = true;
}
if ($parameter->isPassedByReference()) {
$name = '&'.$name;
$byRef = true;
}
$names[] = $name;
$parameters[$newname] = $declaration;
}
if ($byRef) {
$this->template = $this->templateByRefOptional;
$this->place('arguments', join(', ', $parameters));
$code = '';
for ($i = count($parameters); $i > 0; $i--) {
$code .= " case {$i}: \$args = [" . join(', ', $names) . "]; break;\n";
array_pop($names);
}
$this->place('code', $code);
}
}

public function save()
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/FunctionInjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,20 @@ class FunctionInjectorTest extends \Codeception\TestCase\Test
* @var FunctionInjector
*/
protected $funcInjector;
/**
* @var FunctionInjector
*/
protected $funcOptionalParameterInjector;
/**
* @var FunctionInjector
*/
protected $funcReferencedParameterInjector;

public function _before()
{
$this->funcInjector = new FunctionInjector('demo', 'strlen');
$this->funcOptionalParameterInjector = new FunctionInjector('demo', 'explode');
$this->funcReferencedParameterInjector = new FunctionInjector('demo', 'preg_match');
test::clean();
}

Expand All @@ -25,6 +35,18 @@ public function testTemplate()
verify($php)->contains("return call_user_func_array('strlen', func_get_args());");
}

public function testReferencedParameterTemplate()
{
$php = $this->funcReferencedParameterInjector->getPHP();
verify($php)->contains("function preg_match(\$p0, \$p1, &\$p2=NULL, \$p3=NULL, \$p4=NULL)");
verify($php)->contains("case 5: \$args = [\$p0, \$p1, &\$p2, \$p3, \$p4]; break;");
verify($php)->contains("case 4: \$args = [\$p0, \$p1, &\$p2, \$p3]; break;");
verify($php)->contains("case 3: \$args = [\$p0, \$p1, &\$p2]; break;");
verify($php)->contains("case 2: \$args = [\$p0, \$p1]; break;");
verify($php)->contains("case 1: \$args = [\$p0]; break;");
verify($php)->contains("return call_user_func_array('preg_match', \$args);");
}

public function testSave()
{
$this->funcInjector->save();
Expand Down Expand Up @@ -82,4 +104,13 @@ public function testFailedVerification()
$func->verifyNeverInvoked('strlen');
}

public function testReferencedParameter()
{
$func = test::func('\demo', 'preg_match', 10);
expect(preg_match('@[0-9]+@', '1234', $match))->equals(10);
test::clean();
expect(preg_match('@[0-9]+@', '1234#', $match))->equals(1);
expect($match[0])->equals('1234');
}

}