Skip to content

Commit 296f314

Browse files
Merge branch '10.5' into 11.1
2 parents fb9c7de + 63183f2 commit 296f314

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
@@ -96,12 +96,12 @@ final class Generator
9696
/**
9797
* Returns a test double for the specified class.
9898
*
99-
* @throws ClassAlreadyExistsException
10099
* @throws ClassIsEnumerationException
101100
* @throws ClassIsFinalException
102101
* @throws ClassIsReadonlyException
103102
* @throws DuplicateMethodException
104103
* @throws InvalidMethodNameException
104+
* @throws NameAlreadyInUseException
105105
* @throws OriginalConstructorInvocationRequiredException
106106
* @throws ReflectionException
107107
* @throws RuntimeException
@@ -118,7 +118,7 @@ public function testDouble(string $type, bool $mockObject, bool $markAsMockObjec
118118
}
119119

120120
$this->ensureValidMethods($methods);
121-
$this->ensureMockedClassDoesNotAlreadyExist($mockClassName);
121+
$this->ensureNameForTestDoubleClassIsAvailable($mockClassName);
122122

123123
if (!$callOriginalConstructor && $callOriginalMethods) {
124124
throw new OriginalConstructorInvocationRequiredException;
@@ -229,13 +229,13 @@ public function testDoubleForInterfaceIntersection(array $interfaces, bool $mock
229229
*
230230
* Concrete methods to mock can be specified with the $mockedMethods parameter.
231231
*
232-
* @throws ClassAlreadyExistsException
233232
* @throws ClassIsEnumerationException
234233
* @throws ClassIsFinalException
235234
* @throws ClassIsReadonlyException
236235
* @throws DuplicateMethodException
237236
* @throws InvalidArgumentException
238237
* @throws InvalidMethodNameException
238+
* @throws NameAlreadyInUseException
239239
* @throws OriginalConstructorInvocationRequiredException
240240
* @throws ReflectionException
241241
* @throws RuntimeException
@@ -290,13 +290,13 @@ interface_exists($originalClassName, $callAutoload)) {
290290
*
291291
* @psalm-param trait-string $traitName
292292
*
293-
* @throws ClassAlreadyExistsException
294293
* @throws ClassIsEnumerationException
295294
* @throws ClassIsFinalException
296295
* @throws ClassIsReadonlyException
297296
* @throws DuplicateMethodException
298297
* @throws InvalidArgumentException
299298
* @throws InvalidMethodNameException
299+
* @throws NameAlreadyInUseException
300300
* @throws OriginalConstructorInvocationRequiredException
301301
* @throws ReflectionException
302302
* @throws RuntimeException
@@ -960,17 +960,19 @@ private function ensureValidMethods(?array $methods): void
960960
}
961961

962962
/**
963-
* @throws ClassAlreadyExistsException
963+
* @throws NameAlreadyInUseException
964964
* @throws ReflectionException
965965
*/
966-
private function ensureMockedClassDoesNotAlreadyExist(string $mockClassName): void
966+
private function ensureNameForTestDoubleClassIsAvailable(string $className): void
967967
{
968-
if ($mockClassName !== '' && class_exists($mockClassName, false)) {
969-
$reflector = $this->reflectClass($mockClassName);
968+
if ($className === '') {
969+
return;
970+
}
970971

971-
if (!$reflector->implementsInterface(MockObject::class)) {
972-
throw new ClassAlreadyExistsException($mockClassName);
973-
}
972+
if (class_exists($className, false) ||
973+
interface_exists($className, false) ||
974+
trait_exists($className, false)) {
975+
throw new NameAlreadyInUseException($className);
974976
}
975977
}
976978

src/Framework/MockObject/MockBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
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\ClassIsReadonlyException;
2322
use PHPUnit\Framework\MockObject\Generator\DuplicateMethodException;
2423
use PHPUnit\Framework\MockObject\Generator\Generator;
2524
use PHPUnit\Framework\MockObject\Generator\InvalidMethodNameException;
25+
use PHPUnit\Framework\MockObject\Generator\NameAlreadyInUseException;
2626
use PHPUnit\Framework\MockObject\Generator\OriginalConstructorInvocationRequiredException;
2727
use PHPUnit\Framework\MockObject\Generator\ReflectionException;
2828
use PHPUnit\Framework\MockObject\Generator\RuntimeException;
@@ -78,13 +78,13 @@ public function __construct(TestCase $testCase, string $type)
7878
/**
7979
* Creates a mock object using a fluent interface.
8080
*
81-
* @throws ClassAlreadyExistsException
8281
* @throws ClassIsEnumerationException
8382
* @throws ClassIsFinalException
8483
* @throws ClassIsReadonlyException
8584
* @throws DuplicateMethodException
8685
* @throws InvalidArgumentException
8786
* @throws InvalidMethodNameException
87+
* @throws NameAlreadyInUseException
8888
* @throws OriginalConstructorInvocationRequiredException
8989
* @throws ReflectionException
9090
* @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)