Skip to content

Commit 36a0937

Browse files
Merge branch '11.1'
2 parents 90010ca + 9cbac01 commit 36a0937

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

src/Framework/MockObject/Exception/CannotUseAddMethodsException.php renamed to src/Framework/MockObject/Generator/Exception/CannotUseAddMethodsException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10-
namespace PHPUnit\Framework\MockObject;
10+
namespace PHPUnit\Framework\MockObject\Generator;
1111

1212
use function sprintf;
1313

src/Framework/MockObject/MockBuilder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPUnit\Event\Facade as EventFacade;
1717
use PHPUnit\Framework\Exception;
1818
use PHPUnit\Framework\InvalidArgumentException;
19+
use PHPUnit\Framework\MockObject\Generator\CannotUseAddMethodsException;
1920
use PHPUnit\Framework\MockObject\Generator\ClassIsEnumerationException;
2021
use PHPUnit\Framework\MockObject\Generator\ClassIsFinalException;
2122
use PHPUnit\Framework\MockObject\Generator\DuplicateMethodException;

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

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@
99
*/
1010
namespace PHPUnit\Framework\MockObject;
1111

12+
use function assert;
13+
use function class_exists;
14+
use function interface_exists;
1215
use function md5;
1316
use function mt_rand;
17+
use function substr;
18+
use function trait_exists;
1419
use PHPUnit\Framework\Attributes\CoversClass;
1520
use PHPUnit\Framework\Attributes\Group;
1621
use PHPUnit\Framework\Attributes\IgnorePhpunitDeprecations;
1722
use PHPUnit\Framework\Attributes\Medium;
1823
use PHPUnit\Framework\Attributes\TestDox;
24+
use PHPUnit\Framework\MockObject\Generator\CannotUseAddMethodsException;
25+
use PHPUnit\Framework\MockObject\Generator\DuplicateMethodException;
26+
use PHPUnit\Framework\MockObject\Generator\InvalidMethodNameException;
1927
use PHPUnit\Framework\MockObject\Generator\NameAlreadyInUseException;
2028
use PHPUnit\Framework\TestCase;
2129
use PHPUnit\TestFixture\MockObject\AbstractClass;
@@ -24,8 +32,10 @@
2432
use PHPUnit\TestFixture\MockObject\TraitWithConcreteAndAbstractMethod;
2533

2634
#[CoversClass(MockBuilder::class)]
27-
#[CoversClass(NameAlreadyInUseException::class)]
2835
#[CoversClass(CannotUseAddMethodsException::class)]
36+
#[CoversClass(DuplicateMethodException::class)]
37+
#[CoversClass(InvalidMethodNameException::class)]
38+
#[CoversClass(NameAlreadyInUseException::class)]
2939
#[Group('test-doubles')]
3040
#[Group('test-doubles/creation')]
3141
#[Group('test-doubles/mock-object')]
@@ -71,7 +81,7 @@ public function testCanCreateMockObjectForExtendableClassWhileAddingMethodsToIt(
7181

7282
#[IgnorePhpunitDeprecations]
7383
#[TestDox('addMethods() cannot be used to configure an additional method for the mock object class when the original class has a method of the same name')]
74-
public function testCannotCreateMockObjectForExtendableClassAddingMethodsToItThatItAlreadyHas(): void
84+
public function testCannotCreateMockObjectForExtendableClassAddingMethodToItThatItAlreadyHas(): void
7585
{
7686
$this->expectException(CannotUseAddMethodsException::class);
7787

@@ -80,40 +90,79 @@ public function testCannotCreateMockObjectForExtendableClassAddingMethodsToItTha
8090
->getMock();
8191
}
8292

93+
#[IgnorePhpunitDeprecations]
94+
#[TestDox('addMethods() cannot be used to configure an additional method for the mock object class multiple times using the same name')]
95+
public function testCannotCreateMockObjectForExtendableClassAddingMultipleMethodsWithSameNameToIt(): void
96+
{
97+
$this->expectException(DuplicateMethodException::class);
98+
99+
$this->getMockBuilder(ExtendableClass::class)
100+
->addMethods(['additionalMethod', 'additionalMethod'])
101+
->getMock();
102+
}
103+
104+
#[IgnorePhpunitDeprecations]
105+
#[TestDox('addMethods() cannot be used to configure an additional method for the mock object class with invalid name')]
106+
public function testCannotCreateMockObjectForExtendableClassAddingMethodToItWithInvalidName(): void
107+
{
108+
$this->expectException(InvalidMethodNameException::class);
109+
110+
$this->getMockBuilder(ExtendableClass::class)
111+
->addMethods(['1234'])
112+
->getMock();
113+
}
114+
83115
#[IgnorePhpunitDeprecations]
84116
#[TestDox('getMockForAbstractClass() can be used to create a mock object for an abstract class')]
85117
public function testCreatesMockObjectForAbstractClassAndAllowsConfigurationOfAbstractMethods(): void
86118
{
87-
$mock = $this->getMockBuilder(AbstractClass::class)
119+
$double = $this->getMockBuilder(AbstractClass::class)
88120
->getMockForAbstractClass();
89121

90-
$mock->expects($this->once())->method('doSomethingElse')->willReturn(true);
122+
$double->expects($this->once())->method('doSomethingElse')->willReturn(true);
91123

92-
$this->assertTrue($mock->doSomething());
124+
$this->assertTrue($double->doSomething());
93125
}
94126

95127
#[IgnorePhpunitDeprecations]
96128
#[TestDox('getMockForTrait() can be used to create a mock object for a trait')]
97129
public function testCreatesMockObjectForTraitAndAllowsConfigurationOfMethods(): void
98130
{
99-
$mock = $this->getMockBuilder(TraitWithConcreteAndAbstractMethod::class)
131+
$double = $this->getMockBuilder(TraitWithConcreteAndAbstractMethod::class)
100132
->getMockForTrait();
101133

102-
$mock->method('abstractMethod')->willReturn(true);
134+
$double->method('abstractMethod')->willReturn(true);
103135

104-
$this->assertTrue($mock->concreteMethod());
136+
$this->assertTrue($double->concreteMethod());
105137
}
106138

107139
#[IgnorePhpunitDeprecations]
108140
#[TestDox('onlyMethods() can be used to configure which methods should be doubled')]
109141
public function testCreatesPartialMockObjectForExtendableClass(): void
110142
{
111-
$mock = $this->getMockBuilder(ExtendableClass::class)
143+
$double = $this->getMockBuilder(ExtendableClass::class)
112144
->onlyMethods(['doSomethingElse'])
113145
->getMock();
114146

115-
$mock->expects($this->once())->method('doSomethingElse')->willReturn(true);
147+
$double->expects($this->once())->method('doSomethingElse')->willReturn(true);
148+
149+
$this->assertTrue($double->doSomething());
150+
}
151+
152+
#[IgnorePhpunitDeprecations]
153+
#[TestDox('allowMockingUnknownTypes() can be used to allow mocking of unknown types')]
154+
public function testCreatesMockObjectForUnknownType(): void
155+
{
156+
$type = 'Type_' . substr(md5((string) mt_rand()), 0, 8);
157+
158+
assert(!class_exists($type) && !interface_exists($type) && !trait_exists($type));
159+
160+
$double = $this->getMockBuilder($type)
161+
->allowMockingUnknownTypes()
162+
->getMock();
163+
164+
$this->assertInstanceOf($type, $double);
165+
$this->assertInstanceOf(MockObject::class, $double);
116166

117-
$this->assertTrue($mock->doSomething());
118167
}
119168
}

0 commit comments

Comments
 (0)