8000 Merge pull request #6620 from Codeception/stop-using-__mocked-field-i… · Codeception/Codeception@c437596 · GitHub
[go: up one dir, main page]

Ski 8000 p to content

Commit c437596

Browse files
authored
Merge pull request #6620 from Codeception/stop-using-__mocked-field-in-stub
Stop using __mocked field in objects created by Stub library
2 parents a6e6dab + eca91d5 commit c437596

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"ext-mbstring": "*",
2222
"behat/gherkin": "^4.6.2",
2323
"codeception/lib-asserts": "2.0.*@dev",
24-
"codeception/stub": "^3.7 | ^4.0",
24+
"codeception/stub": "^4.1",
2525
"phpunit/phpunit": "^9.5",
2626
"phpunit/php-code-coverage": "^9.2",
2727
"phpunit/php-text-template": "^2.0",

src/Codeception/Step.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,24 @@ protected function getClassName(object $argument): string
185185
{
186186
if ($argument instanceof Closure) {
187187
return Closure::class;
188-
} elseif ($argument instanceof MockObject && (property_exists($argument, '__mocked') && $argument->__mocked !== null)) {
189-
return $this->formatClassName($argument->__mocked);
188+
} elseif ($argument instanceof MockObject) {
189+
$parentClass = get_parent_class($argument);
190+
$reflection = new \ReflectionClass($argument);
191+
192+
if ($parentClass !== false) {
193+
return $this->formatClassName($parentClass);
194+
}
195+
196+
$interfaces = $reflection->getInterfaceNames();
197+
foreach ($interfaces as $interface) {
198+
if (str_starts_with($interface, 'PHPUnit\\')) {
199+
continue;
200+
}
201+
if (str_starts_with($interface, 'Codeception\\')) {
202+
continue;
203+
}
204+
return $this->formatClassName($interface);
205+
}
190206
}
191207

192208
return $this->formatClassName($argument::class);

tests/data/DummyOverloadableClass.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,11 @@ public function exceptionalMethod(): void
5757

5858
public function __get($name)
5959
{
60-
//seeing as we're not implementing __set here, add check for __mocked
61-
$return = null;
62-
if ($name === '__mocked') {
63-
$return = $this->__mocked ?? null;
64-
} elseif ($this->__isset($name)) {
65-
$return = $this->properties[$name];
60+
if ($this->__isset($name)) {
61+
return $this->properties[$name];
6662
}
6763

68-
return $return;
64+
return null;
6965
}
7066

7167
public function __isset($name)

tests/unit/Codeception/StepTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public function testGetArguments()
4343

4444
$mock = $this->createMock($this::class);
4545
$step = $this->getStep(['', [[$mock, 'testGetArguments']]]);
46-
$className = $mock::class;
47-
$this->assertSame('["' . $className . '","testGetArguments"]', $step->getArgumentsAsString());
46+
$this->assertSame('["StepTest","testGetArguments"]', $step->getArgumentsAsString());
4847
}
4948

5049
public function testGetHtml()

0 commit comments

Comments
 (0)
0