Skip to content

Commit

Permalink
Stop using __mocked field in objects created by Stub library
Browse files Browse the repository at this point in the history
  • Loading branch information
Naktibalda committed Dec 27, 2022
1 parent a6e6dab commit eca91d5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
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

0 comments on commit eca91d5

Please sign in to comment.