Skip to content

Commit 67192d4

Browse files
committed
fix(Phpunit/MockedMethod): add callOriginal and callOriginalStatic missing return statement
1 parent 8d4390e commit 67192d4

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

src/Phpunit/MockedMethod.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,23 @@ static function (...$args) use ($mockObject, $method) {
7878
* @param object $object
7979
* @param mixed ...$args
8080
*
81-
* @return void
81+
* @return mixed
8282
* @throws ReflectionException
8383
*/
8484
public function callOriginal($object, ...$args)
8585
{
86-
$this->mockedMethod->callOriginal($object, ...$args);
86+
return $this->mockedMethod->callOriginal($object, ...$args);
8787
}
8888

8989
/**
9090
* @param mixed ...$args
9191
*
92-
* @return void
92+
* @return mixed
9393
* @throws ReflectionException
9494
*/
9595
public function callOriginalStatic(...$args)
9696
{
97-
$this->mockedMethod->callOriginalStatic(...$args);
97+
return $this->mockedMethod->callOriginalStatic(...$args);
9898
}
9999

100100
public function getMocker(): InvocationMocker

test/phpunit/Phpunit/MockedMethodTest.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use QratorLabs\Smocky\Test\PhpUnit\Helpers\ClassWithMethods;
1010
use ReflectionException;
1111

12+
use function get_class;
1213
use function uniqid;
1314

1415
/**
@@ -100,4 +101,88 @@ public function testCallOriginal(): void
100101
);
101102
$methodMock->callOriginal($object);
102103
}
104+
105+
/**
106+
* @throws ReflectionException
107+
*/
108+
public function testCallOriginalWithReturn(): void
109+
{
110+
$object = new ClassWithMethods();
111+
$originalValue = $object->publicMethod();
112+
$methodMock = new MockedMethod(
113+
$this,
114+
ClassWithMethods::class,
115+
'publicMethod',
116+
self::exactly(2)
117+
);
118+
$methodMock->getMocker()->willReturnCallback(static function () use ($object, $methodMock) {
119+
static $firstTime = true;
120+
if ($firstTime) {
121+
$firstTime = false;
122+
123+
return $methodMock->callOriginal($object);
124+
}
125+
126+
return 'mockedMethod';
127+
});
128+
self::assertSame($originalValue, $object->publicMethod());
129+
self::assertSame('mockedMethod', $object->publicMethod());
130+
}
131+
132+
/**
133+
* @throws ReflectionException
134+
*/
135+
public function testCallOriginalStaticWithReturn(): void
136+
{
137+
$originalValue = ClassWithMethods::publicStaticMethod();
138+
$methodMock = new MockedMethod(
139+
$this,
140+
ClassWithMethods::class,
141+
'publicStaticMethod',
142+
self::exactly(2)
143+
);
144+
$methodMock->getMocker()->willReturnCallback(static function () use (&$methodMock) {
145+
static $firstTime = true;
146+
if ($firstTime) {
147+
$firstTime = false;
148+
149+
return $methodMock->callOriginalStatic();
150+
}
151+
152+
return 'mockedMethod';
153+
});
154+
self::assertSame($originalValue, ClassWithMethods::publicStaticMethod());
155+
self::assertSame('mockedMethod', ClassWithMethods::publicStaticMethod());
156+
$methodMock = null;
157+
self::assertSame($originalValue, ClassWithMethods::publicStaticMethod());
158+
}
159+
160+
/**
161+
* @throws ReflectionException
162+
*/
163+
public function testCallOriginalWithSideEffect(): void
164+
{
165+
$object = new class {
166+
/** @var string */
167+
public $value = 'initial';
168+
169+
public function getValue(): string
170+
{
171+
return $this->value;
172+
}
173+
};
174+
175+
self::assertSame('initial', $object->getValue());
176+
$mock = new MockedMethod($this, get_class($object), 'getValue', self::once());
177+
$mock->getMocker()->willReturnCallback(static function () use ($object) {
178+
$object->value = 'changed';
179+
180+
return 'mocked';
181+
});
182+
183+
self::assertSame('mocked', $object->getValue());
184+
unset($mock);
185+
self::assertSame('changed', $object->getValue());
186+
self::assertSame('changed', $object->value);
187+
}
103188
}

0 commit comments

Comments
 (0)