Skip to content
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

Export constants values when mocking method #3802

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 1 addition & 17 deletions src/Framework/MockObject/MockMethod.php
Expand Up @@ -306,30 +306,14 @@ private static function getMethodParameters(\ReflectionMethod $method, bool $for
if (!$parameter->isVariadic()) {
if ($parameter->isDefaultValueAvailable()) {
try {
$value = $parameter->getDefaultValueConstantName();
$value = \var_export($parameter->getDefaultValue(), true);
} catch (\ReflectionException $e) {
throw new RuntimeException(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}

if ($value === null) {
try {
$value = \var_export($parameter->getDefaultValue(), true);
} catch (\ReflectionException $e) {
throw new RuntimeException(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
} elseif (!\defined($value)) {
$rootValue = \preg_replace('/^.*\\\\/', '', $value);
$value = \defined($rootValue) ? $rootValue : $value;
}

$default = ' = ' . $value;
} elseif ($parameter->isOptional()) {
$default = ' = null';
Expand Down
Expand Up @@ -14,7 +14,7 @@ namespace Is\Namespaced;
*/

const A_CONSTANT = 17;
const PHP_VERSION = "0.0.0";
const PHP_VERSION = "";

class Issue3154
{
Expand Down Expand Up @@ -45,7 +45,7 @@ class Issue3154Mock extends Is\Namespaced\Issue3154 implements PHPUnit\Framework
use \PHPUnit\Framework\MockObject\Method;
use \PHPUnit\Framework\MockObject\MockedCloneMethod;

public function a(int $i = PHP_INT_MAX, int $j = Is\Namespaced\A_CONSTANT, string $v = PHP_VERSION, string $z = '#'): string
public function a(int $i = %d, int $j = 17, string $v = '%s', string $z = '#'): string
{
$__phpunit_arguments = [$i, $j, $v, $z];
$__phpunit_count = func_num_args();
Expand Down
Expand Up @@ -4,7 +4,7 @@
<?php declare(strict_types=1);
class Foo
{
public function bar(int $baz = PHP_INT_MIN)
public function bar(int $baz = PHP_INT_MAX)
{
}
}
Expand All @@ -31,7 +31,7 @@ class MockFoo extends Foo implements PHPUnit\Framework\MockObject\MockObject
use \PHPUnit\Framework\MockObject\Method;
use \PHPUnit\Framework\MockObject\MockedCloneMethod;

public function bar(int $baz = PHP_INT_MIN)
public function bar(int $baz = %d)
{
$__phpunit_arguments = [$baz];
$__phpunit_count = func_num_args();
Expand Down
Expand Up @@ -2,11 +2,14 @@
Mock static method
--FILE--
<?php declare(strict_types=1);
define('FOO_CONST', 1);
define('GLOBAL_CONSTANT', 1);

class Foo
{
private function bar($arg = FOO_CONST){}
public const CLASS_CONSTANT_PUBLIC = 2;
protected const CLASS_CONSTANT_PROTECTED = 3;
private const CLASS_CONSTANT_PRIVATE = 4;
private function bar($a = GLOBAL_CONSTANT, $b = self::CLASS_CONSTANT_PUBLIC, $c = self::CLASS_CONSTANT_PROTECTED, $d = self::CLASS_CONSTANT_PRIVATE){}
}

require __DIR__ . '/../../../../vendor/autoload.php';
Expand All @@ -23,15 +26,15 @@ $code = $mockMethod->generateCode();
print $code;
--EXPECT--

private function bar($arg = FOO_CONST)
private function bar($a = 1, $b = 2, $c = 3, $d = 4)
{
$__phpunit_arguments = [$arg];
$__phpunit_arguments = [$a, $b, $c, $d];
$__phpunit_count = func_num_args();

if ($__phpunit_count > 1) {
if ($__phpunit_count > 4) {
$__phpunit_arguments_tmp = func_get_args();

for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
for ($__phpunit_i = 4; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
$__phpunit_arguments[] = $__phpunit_arguments_tmp[$__phpunit_i];
}
}
Expand Down