Skip to content

Commit 90010ca

Browse files
Merge branch '11.1'
2 parents c9608ce + 296f314 commit 90010ca

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

src/Framework/MockObject/Generator/Exception/ClassAlreadyExistsException.php renamed to src/Framework/MockObject/Generator/Exception/NameAlreadyInUseException.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@
1414
/**
1515
* @internal This class is not covered by the backward compatibility promise for PHPUnit
1616
*/
17-
final class ClassAlreadyExistsException extends \PHPUnit\Framework\Exception implements Exception
17+
final class NameAlreadyInUseException extends \PHPUnit\Framework\Exception implements Exception
1818
{
19-
public function __construct(string $className)
19+
/**
20+
* @psalm-param class-string|trait-string $name
21+
*/
22+
public function __construct(string $name)
2023
{
2124
parent::__construct(
2225
sprintf(
23-
'Class "%s" already exists',
24-
$className,
26+
'The name "%s" is already in use',
27+
$name,
2528
),
2629
);
2730
}

src/Framework/MockObject/Generator/Generator.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ final class Generator
100100
/**
101101
* Returns a test double for the specified class.
102102
*
103-
* @throws ClassAlreadyExistsException
104103
* @throws ClassIsEnumerationException
105104
* @throws ClassIsFinalException
106105
* @throws DuplicateMethodException
107106
* @throws InvalidMethodNameException
107+
* @throws NameAlreadyInUseException
108108
* @throws OriginalConstructorInvocationRequiredException
109109
* @throws ReflectionException
110110
* @throws RuntimeException
@@ -121,7 +121,7 @@ public function testDouble(string $type, bool $mockObject, bool $markAsMockObjec
121121
}
122122

123123
$this->ensureValidMethods($methods);
124-
$this->ensureMockedClassDoesNotAlreadyExist($mockClassName);
124+
$this->ensureNameForTestDoubleClassIsAvailable($mockClassName);
125125

126126
if (!$callOriginalConstructor && $callOriginalMethods) {
127127
throw new OriginalConstructorInvocationRequiredException;
@@ -232,12 +232,12 @@ public function testDoubleForInterfaceIntersection(array $interfaces, bool $mock
232232
*
233233
* Concrete methods to mock can be specified with the $mockedMethods parameter.
234234
*
235-
* @throws ClassAlreadyExistsException
236235
* @throws ClassIsEnumerationException
237236
* @throws ClassIsFinalException
238237
* @throws DuplicateMethodException
239238
* @throws InvalidArgumentException
240239
* @throws InvalidMethodNameException
240+
* @throws NameAlreadyInUseException
241241
* @throws OriginalConstructorInvocationRequiredException
242242
* @throws ReflectionException
243243
* @throws RuntimeException
@@ -292,12 +292,12 @@ interface_exists($originalClassName, $callAutoload)) {
292292
*
293293
* @psalm-param trait-string $traitName
294294
*
295-
* @throws ClassAlreadyExistsException
296295
* @throws ClassIsEnumerationException
297296
* @throws ClassIsFinalException
298297
* @throws DuplicateMethodException
299298
* @throws InvalidArgumentException
300299
* @throws InvalidMethodNameException
300+
* @throws NameAlreadyInUseException
301301
* @throws OriginalConstructorInvocationRequiredException
302302
* @throws ReflectionException
303303
* @throws RuntimeException
@@ -987,17 +987,19 @@ private function ensureValidMethods(?array $methods): void
987987
}
988988

989989
/**
990-
* @throws ClassAlreadyExistsException
990+
* @throws NameAlreadyInUseException
991991
* @throws ReflectionException
992992
*/
993-
private function ensureMockedClassDoesNotAlreadyExist(string $mockClassName): void
993+
private function ensureNameForTestDoubleClassIsAvailable(string $className): void
994994
{
995-
if ($mockClassName !== '' && class_exists($mockClassName, false)) {
996-
$reflector = $this->reflectClass($mockClassName);
995+
if ($className === '') {
996+
return;
997+
}
997998

998-
if (!$reflector->implementsInterface(MockObject::class)) {
999-
throw new ClassAlreadyExistsException($mockClassName);
1000-
}
999+
if (class_exists($className, false) ||
1000+
interface_exists($className, false) ||
1001+
trait_exists($className, false)) {
1002+
throw new NameAlreadyInUseException($className);
10011003
}
10021004
}
10031005

src/Framework/MockObject/MockBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
use PHPUnit\Event\Facade as EventFacade;
1717
use PHPUnit\Framework\Exception;
1818
use PHPUnit\Framework\InvalidArgumentException;
19-
use PHPUnit\Framework\MockObject\Generator\ClassAlreadyExistsException;
2019
use PHPUnit\Framework\MockObject\Generator\ClassIsEnumerationException;
2120
use PHPUnit\Framework\MockObject\Generator\ClassIsFinalException;
2221
use PHPUnit\Framework\MockObject\Generator\DuplicateMethodException;
2322
use PHPUnit\Framework\MockObject\Generator\Generator;
2423
use PHPUnit\Framework\MockObject\Generator\InvalidMethodNameException;
24+
use PHPUnit\Framework\MockObject\Generator\NameAlreadyInUseException;
2525
use PHPUnit\Framework\MockObject\Generator\OriginalConstructorInvocationRequiredException;
2626
use PHPUnit\Framework\MockObject\Generator\ReflectionException;
2727
use PHPUnit\Framework\MockObject\Generator\RuntimeException;
@@ -77,12 +77,12 @@ public function __construct(TestCase $testCase, string $type)
7777
/**
7878
* Creates a mock object using a fluent interface.
7979
*
80-
* @throws ClassAlreadyExistsException
8180
* @throws ClassIsEnumerationException
8281
* @throws ClassIsFinalException
8382
* @throws DuplicateMethodException
8483
* @throws InvalidArgumentException
8584
* @throws InvalidMethodNameException
85+
* @throws NameAlreadyInUseException
8686
* @throws OriginalConstructorInvocationRequiredException
8787
* @throws ReflectionException
8888
* @throws RuntimeException

tests/unit/Framework/MockObject/Creation/MockBuilderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
use PHPUnit\Framework\Attributes\IgnorePhpunitDeprecations;
1717
use PHPUnit\Framework\Attributes\Medium;
1818
use PHPUnit\Framework\Attributes\TestDox;
19+
use PHPUnit\Framework\MockObject\Generator\NameAlreadyInUseException;
1920
use PHPUnit\Framework\TestCase;
2021
use PHPUnit\TestFixture\MockObject\AbstractClass;
2122
use PHPUnit\TestFixture\MockObject\ExtendableClass;
2223
use PHPUnit\TestFixture\MockObject\InterfaceWithReturnTypeDeclaration;
2324
use PHPUnit\TestFixture\MockObject\TraitWithConcreteAndAbstractMethod;
2425

2526
#[CoversClass(MockBuilder::class)]
27+
#[CoversClass(NameAlreadyInUseException::class)]
2628
#[CoversClass(CannotUseAddMethodsException::class)]
2729
#[Group('test-doubles')]
2830
#[Group('test-doubles/creation')]
@@ -42,6 +44,16 @@ public function testCanCreateMockObjectWithSpecifiedClassName(): void
4244
$this->assertSame($className, $double::class);
4345
}
4446

47+
#[TestDox('setMockClassName() cannot be used to configure the name of the mock object class when a class with that name already exists')]
48+
public function testCannotCreateMockObjectWithSpecifiedClassNameWhenClassWithThatNameAlreadyExists(): void
49+
{
50+
$this->expectException(NameAlreadyInUseException::class);
51+
52+
$this->getMockBuilder(InterfaceWithReturnTypeDeclaration::class)
53+
->setMockClassName(__CLASS__)
54+
->getMock();
55+
}
56+
4557
#[IgnorePhpunitDeprecations]
4658
#[TestDox('addMethods() can be used to configure an additional method for the mock object class when the original class does not have a method of the same name')]
4759
public function testCanCreateMockObjectForExtendableClassWhileAddingMethodsToIt(): void

0 commit comments

Comments
 (0)