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

Stop using __mocked field in objects created by Stub library #6620

Merged
merged 1 commit into from Dec 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -21,7 +21,7 @@
"ext-mbstring": "*",
"behat/gherkin": "^4.6.2",
"codeception/lib-asserts": "2.0.*@dev",
"codeception/stub": "^3.7 | ^4.0",
"codeception/stub": "^4.1",
"phpunit/phpunit": "^9.5",
"phpunit/php-code-coverage": "^9.2",
"phpunit/php-text-template": "^2.0",
Expand Down
20 changes: 18 additions & 2 deletions src/Codeception/Step.php
Expand Up @@ -185,8 +185,24 @@ protected function getClassName(object $argument): string
{
if ($argument instanceof Closure) {
return Closure::class;
} elseif ($argument instanceof MockObject && (property_exists($argument, '__mocked') && $argument->__mocked !== null)) {
return $this->formatClassName($argument->__mocked);
} elseif ($argument instanceof MockObject) {
$parentClass = get_parent_class($argument);
$reflection = new \ReflectionClass($argument);

if ($parentClass !== false) {
return $this->formatClassName($parentClass);
}

$interfaces = $reflection->getInterfaceNames();
foreach ($interfaces as $interface) {
if (str_starts_with($interface, 'PHPUnit\\')) {
continue;
}
if (str_starts_with($interface, 'Codeception\\')) {
continue;
}
return $this->formatClassName($interface);
}
}

return $this->formatClassName($argument::class);
Expand Down
10 changes: 3 additions & 7 deletions tests/data/DummyOverloadableClass.php
Expand Up @@ -57,15 +57,11 @@ public function exceptionalMethod(): void

public function __get($name)
{
//seeing as we're not implementing __set here, add check for __mocked
$return = null;
if ($name === '__mocked') {
$return = $this->__mocked ?? null;
} elseif ($this->__isset($name)) {
$return = $this->properties[$name];
if ($this->__isset($name)) {
return $this->properties[$name];
}

return $return;
return null;
}

public function __isset($name)
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/Codeception/StepTest.php
Expand Up @@ -43,8 +43,7 @@ public function testGetArguments()

$mock = $this->createMock($this::class);
$step = $this->getStep(['', [[$mock, 'testGetArguments']]]);
$className = $mock::class;
$this->assertSame('["' . $className . '","testGetArguments"]', $step->getArgumentsAsString());
$this->assertSame('["StepTest","testGetArguments"]', $step->getArgumentsAsString());
}

public function testGetHtml()
Expand Down