Skip to content

Commit 6d98a50

Browse files
Make createStub(), TestCase::createStubForIntersectionOfInterfaces(), and createConfiguredStub() static so that they can be used in static data provider methods
1 parent 3e46431 commit 6d98a50

File tree

2 files changed

+84
-107
lines changed

2 files changed

+84
-107
lines changed

ChangeLog-10.3.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ All notable changes of the PHPUnit 10.3 release series are documented in this fi
1313
* When a test case class inherits test methods from a parent class then, by default (when no test reordering is requested), the test methods from the class that is highest in the inheritance tree (instead of the class that is lowest in the inheritance tree) are now run first
1414
* Invocation count expectation failure messages have been slightly improved
1515
* When a test case class inherits test methods from a parent class then the TestDox output for such a test case class now starts with test methods from the class that is highest in the inheritance tree (instead of the class that is lowest in the inheritance tree)
16+
* `TestCase::createStub()`, `TestCase::createStubForIntersectionOfInterfaces()`, and `TestCase::createConfiguredStub()` are now static (and can be used from static data provider methods)
1617

1718
[10.3.0]: https://github.com/sebastianbergmann/phpunit/compare/10.2...main

src/Framework/TestCase.php

Lines changed: 83 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,8 @@ abstract class TestCase extends Assert implements Reorderable, SelfDescribing, T
161161
/**
162162
* @psalm-var array<string,string>
163163
*/
164-
private array $iniSettings = [];
165-
private array $locale = [];
166-
private ?MockGenerator $mockObjectGenerator = null;
164+
private array $iniSettings = [];
165+
private array $locale = [];
167166

168167
/**
169168
* @psalm-var list<MockObjectInternal>
@@ -1169,66 +1168,6 @@ protected function setLocale(mixed ...$arguments): void
11691168
}
11701169
}
11711170

1172-
/**
1173-
* Creates a test stub for the specified interface or class.
1174-
*
1175-
* @psalm-template RealInstanceType of object
1176-
*
1177-
* @psalm-param class-string<RealInstanceType> $originalClassName
1178-
*
1179-
* @psalm-return Stub&RealInstanceType
1180-
*
1181-
* @throws \PHPUnit\Framework\MockObject\Exception
1182-
* @throws InvalidArgumentException
1183-
* @throws NoPreviousThrowableException
1184-
*/
1185-
protected function createStub(string $originalClassName): Stub
1186-
{
1187-
$stub = $this->createTestDouble($originalClassName, false);
1188-
1189-
Event\Facade::emitter()->testCreatedStub($originalClassName);
1190-
1191-
return $stub;
1192-
}
1193-
1194-
/**
1195-
* @psalm-param list<class-string> $interfaces
1196-
*
1197-
* @throws \PHPUnit\Framework\MockObject\Exception
1198-
*/
1199-
protected function createStubForIntersectionOfInterfaces(array $interfaces): Stub
1200-
{
1201-
$stub = $this->mockObjectGenerator()->getMockForInterfaces($interfaces);
1202-
1203-
Event\Facade::emitter()->testCreatedStubForIntersectionOfInterfaces($interfaces);
1204-
1205-
return $stub;
1206-
}
1207-
1208-
/**
1209-
* Creates (and configures) a test stub for the specified interface or class.
1210-
*
1211-
* @psalm-template RealInstanceType of object
1212-
*
1213-
* @psalm-param class-string<RealInstanceType> $originalClassName
1214-
*
1215-
* @psalm-return Stub&RealInstanceType
1216-
*
1217-
* @throws \PHPUnit\Framework\MockObject\Exception
1218-
* @throws InvalidArgumentException
1219-
* @throws NoPreviousThrowableException
1220-
*/
1221-
final protected function createConfiguredStub(string $originalClassName, array $configuration): Stub
1222-
{
1223-
$o = $this->createStub($originalClassName);
1224-
1225-
foreach ($configuration as $method => $return) {
1226-
$o->method($method)->willReturn($return);
1227-
}
1228-
1229-
return $o;
1230-
}
1231-
12321171
/**
12331172
* Creates a mock object for the specified interface or class.
12341173
*
@@ -1244,7 +1183,15 @@ final protected function createConfiguredStub(string $originalClassName, array $
12441183
*/
12451184
protected function createMock(string $originalClassName): MockObject
12461185
{
1247-
$mock = $this->createTestDouble($originalClassName);
1186+
$mock = (new MockGenerator)->getMock(
1187+
$originalClassName,
1188+
callOriginalConstructor: false,
1189+
callOriginalClone: false,
1190+
cloneArguments: false,
1191+
allowMockingUnknownTypes: false,
1192+
);
1193+
1194+
$this->registerMockObject($mock);
12481195

12491196
Event\Facade::emitter()->testCreatedMockObject($originalClassName);
12501197

@@ -1258,7 +1205,7 @@ protected function createMock(string $originalClassName): MockObject
12581205
*/
12591206
protected function createMockForIntersectionOfInterfaces(array $interfaces): MockObject
12601207
{
1261-
$mock = $this->mockObjectGenerator()->getMockForInterfaces($interfaces);
1208+
$mock = (new MockGenerator)->getMockForInterfaces($interfaces);
12621209

12631210
Event\Facade::emitter()->testCreatedMockObjectForIntersectionOfInterfaces($interfaces);
12641211

@@ -1368,7 +1315,7 @@ protected function createTestProxy(string $originalClassName, array $constructor
13681315
*/
13691316
protected function getMockForAbstractClass(string $originalClassName, array $arguments = [], string $mockClassName = '', bool $callOriginalConstructor = true, bool $callOriginalClone = true, bool $callAutoload = true, array $mockedMethods = [], bool $cloneArguments = false): MockObject
13701317
{
1371-
$mockObject = $this->mockObjectGenerator()->getMockForAbstractClass(
1318+
$mockObject = (new MockGenerator)->getMockForAbstractClass(
13721319
$originalClassName,
13731320
$arguments,
13741321
$mockClassName,
@@ -1408,7 +1355,7 @@ protected function getMockFromWsdl(string $wsdlFile, string $originalClassName =
14081355

14091356
if (!class_exists($originalClassName)) {
14101357
eval(
1411-
$this->mockObjectGenerator()->generateClassFromWsdl(
1358+
(new MockGenerator)->generateClassFromWsdl(
14121359
$wsdlFile,
14131360
$originalClassName,
14141361
$methods,
@@ -1417,7 +1364,7 @@ protected function getMockFromWsdl(string $wsdlFile, string $originalClassName =
14171364
);
14181365
}
14191366

1420-
$mockObject = $this->mockObjectGenerator()->getMock(
1367+
$mockObject = (new MockGenerator)->getMock(
14211368
$originalClassName,
14221369
$methods,
14231370
['', $options],
@@ -1455,7 +1402,7 @@ protected function getMockFromWsdl(string $wsdlFile, string $originalClassName =
14551402
*/
14561403
protected function getMockForTrait(string $traitName, array $arguments = [], string $mockClassName = '', bool $callOriginalConstructor = true, bool $callOriginalClone = true, bool $callAutoload = true, array $mockedMethods = [], bool $cloneArguments = false): MockObject
14571404
{
1458-
$mockObject = $this->mockObjectGenerator()->getMockForTrait(
1405+
$mockObject = (new MockGenerator)->getMockForTrait(
14591406
$traitName,
14601407
$arguments,
14611408
$mockClassName,
@@ -1484,7 +1431,7 @@ protected function getMockForTrait(string $traitName, array $arguments = [], str
14841431
*/
14851432
protected function getObjectForTrait(string $traitName, array $arguments = [], string $traitClassName = '', bool $callOriginalConstructor = true, bool $callOriginalClone = true, bool $callAutoload = true): object
14861433
{
1487-
return $this->mockObjectGenerator()->getObjectForTrait(
1434+
return (new MockGenerator)->getObjectForTrait(
14881435
$traitName,
14891436
$traitClassName,
14901437
$callAutoload,
@@ -1660,15 +1607,6 @@ private function markSkippedForMissingDependency(ExecutionOrderDependency $depen
16601607
$this->status = TestStatus::skipped($message);
16611608
}
16621609

1663-
private function mockObjectGenerator(): MockGenerator
1664-
{
1665-
if ($this->mockObjectGenerator === null) {
1666-
$this->mockObjectGenerator = new MockGenerator;
1667-
}
1668-
1669-
return $this->mockObjectGenerator;
1670-
}
1671-
16721610
private function startOutputBuffering(): void
16731611
{
16741612
ob_start();
@@ -1990,34 +1928,6 @@ private function isCallableTestMethod(string $dependency): bool
19901928
);
19911929
}
19921930

1993-
/**
1994-
* @psalm-template RealInstanceType of object
1995-
*
1996-
* @psalm-param class-string<RealInstanceType> $originalClassName
1997-
*
1998-
* @psalm-return MockObject&RealInstanceType
1999-
*
2000-
* @throws \PHPUnit\Framework\MockObject\Exception
2001-
* @throws InvalidArgumentException
2002-
* @throws NoPreviousThrowableException
2003-
*/
2004-
private function createTestDouble(string $originalClassName, bool $register = true): MockObject
2005-
{
2006-
$testDouble = $this->mockObjectGenerator()->getMock(
2007-
$originalClassName,
2008-
callOriginalConstructor: false,
2009-
callOriginalClone: false,
2010-
cloneArguments: false,
2011-
allowMockingUnknownTypes: false,
2012-
);
2013-
2014-
if ($register) {
2015-
$this->registerMockObject($testDouble);
2016-
}
2017-
2018-
return $testDouble;
2019-
}
2020-
20211931
/**
20221932
* @throws Exception
20231933
* @throws ExpectationFailedException
@@ -2273,4 +2183,70 @@ private function isRegisteredFailure(Throwable $t): bool
22732183

22742184
return false;
22752185
}
2186+
2187+
/**
2188+
* Creates a test stub for the specified interface or class.
2189+
*
2190+
* @psalm-template RealInstanceType of object
2191+
*
2192+
* @psalm-param class-string<RealInstanceType> $originalClassName
2193+
*
2194+
* @psalm-return Stub&RealInstanceType
2195+
*
2196+
* @throws \PHPUnit\Framework\MockObject\Exception
2197+
* @throws InvalidArgumentException
2198+
* @throws NoPreviousThrowableException
2199+
*/
2200+
protected static function createStub(string $originalClassName): Stub
2201+
{
2202+
$stub = (new MockGenerator)->getMock(
2203+
$originalClassName,
2204+
callOriginalConstructor: false,
2205+
callOriginalClone: false,
2206+
cloneArguments: false,
2207+
allowMockingUnknownTypes: false,
2208+
);
2209+
2210+
Event\Facade::emitter()->testCreatedStub($originalClassName);
2211+
2212+
return $stub;
2213+
}
2214+
2215+
/**
2216+
* @psalm-param list<class-string> $interfaces
2217+
*
2218+
* @throws \PHPUnit\Framework\MockObject\Exception
2219+
*/
2220+
protected static function createStubForIntersectionOfInterfaces(array $interfaces): Stub
2221+
{
2222+
$stub = (new MockGenerator)->getMockForInterfaces($interfaces);
2223+
2224+
Event\Facade::emitter()->testCreatedStubForIntersectionOfInterfaces($interfaces);
2225+
2226+
return $stub;
2227+
}
2228+
2229+
/**
2230+
* Creates (and configures) a test stub for the specified interface or class.
2231+
*
2232+
* @psalm-template RealInstanceType of object
2233+
*
2234+
* @psalm-param class-string<RealInstanceType> $originalClassName
2235+
*
2236+
* @psalm-return Stub&RealInstanceType
2237+
*
2238+
* @throws \PHPUnit\Framework\MockObject\Exception
2239+
* @throws InvalidArgumentException
2240+
* @throws NoPreviousThrowableException
2241+
*/
2242+
final protected static function createConfiguredStub(string $originalClassName, array $configuration): Stub
2243+
{
2244+
$o = self::createStub($originalClassName);
2245+
2246+
foreach ($configuration as $method => $return) {
2247+
$o->method($method)->willReturn($return);
2248+
}
2249+
2250+
return $o;
2251+
}
22762252
}

0 commit comments

Comments
 (0)