9
9
*/
10
10
namespace PHPUnit \Framework \MockObject ;
11
11
12
+ use function assert ;
13
+ use function class_exists ;
14
+ use function interface_exists ;
12
15
use function md5 ;
13
16
use function mt_rand ;
17
+ use function substr ;
18
+ use function trait_exists ;
14
19
use PHPUnit \Framework \Attributes \CoversClass ;
15
20
use PHPUnit \Framework \Attributes \Group ;
16
21
use PHPUnit \Framework \Attributes \IgnorePhpunitDeprecations ;
17
22
use PHPUnit \Framework \Attributes \Medium ;
18
23
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 ;
19
27
use PHPUnit \Framework \MockObject \Generator \NameAlreadyInUseException ;
20
28
use PHPUnit \Framework \TestCase ;
21
29
use PHPUnit \TestFixture \MockObject \AbstractClass ;
24
32
use PHPUnit \TestFixture \MockObject \TraitWithConcreteAndAbstractMethod ;
25
33
26
34
#[CoversClass(MockBuilder::class)]
27
- #[CoversClass(NameAlreadyInUseException::class)]
28
35
#[CoversClass(CannotUseAddMethodsException::class)]
36
+ #[CoversClass(DuplicateMethodException::class)]
37
+ #[CoversClass(InvalidMethodNameException::class)]
38
+ #[CoversClass(NameAlreadyInUseException::class)]
29
39
#[Group('test-doubles ' )]
30
40
#[Group('test-doubles/creation ' )]
31
41
#[Group('test-doubles/mock-object ' )]
@@ -71,7 +81,7 @@ public function testCanCreateMockObjectForExtendableClassWhileAddingMethodsToIt(
71
81
72
82
#[IgnorePhpunitDeprecations]
73
83
#[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
75
85
{
76
86
$ this ->expectException (CannotUseAddMethodsException::class);
77
87
@@ -80,40 +90,79 @@ public function testCannotCreateMockObjectForExtendableClassAddingMethodsToItTha
80
90
->getMock ();
81
91
}
82
92
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
+
83
115
#[IgnorePhpunitDeprecations]
84
116
#[TestDox('getMockForAbstractClass() can be used to create a mock object for an abstract class ' )]
85
117
public function testCreatesMockObjectForAbstractClassAndAllowsConfigurationOfAbstractMethods (): void
86
118
{
87
- $ mock = $ this ->getMockBuilder (AbstractClass::class)
119
+ $ double = $ this ->getMockBuilder (AbstractClass::class)
88
120
->getMockForAbstractClass ();
89
121
90
- $ mock ->expects ($ this ->once ())->method ('doSomethingElse ' )->willReturn (true );
122
+ $ double ->expects ($ this ->once ())->method ('doSomethingElse ' )->willReturn (true );
91
123
92
- $ this ->assertTrue ($ mock ->doSomething ());
124
+ $ this ->assertTrue ($ double ->doSomething ());
93
125
}
94
126
95
127
#[IgnorePhpunitDeprecations]
96
128
#[TestDox('getMockForTrait() can be used to create a mock object for a trait ' )]
97
129
public function testCreatesMockObjectForTraitAndAllowsConfigurationOfMethods (): void
98
130
{
99
- $ mock = $ this ->getMockBuilder (TraitWithConcreteAndAbstractMethod::class)
131
+ $ double = $ this ->getMockBuilder (TraitWithConcreteAndAbstractMethod::class)
100
132
->getMockForTrait ();
101
133
102
- $ mock ->method ('abstractMethod ' )->willReturn (true );
134
+ $ double ->method ('abstractMethod ' )->willReturn (true );
103
135
104
- $ this ->assertTrue ($ mock ->concreteMethod ());
136
+ $ this ->assertTrue ($ double ->concreteMethod ());
105
137
}
106
138
107
139
#[IgnorePhpunitDeprecations]
108
140
#[TestDox('onlyMethods() can be used to configure which methods should be doubled ' )]
109
141
public function testCreatesPartialMockObjectForExtendableClass (): void
110
142
{
111
- $ mock = $ this ->getMockBuilder (ExtendableClass::class)
143
+ $ double = $ this ->getMockBuilder (ExtendableClass::class)
112
144
->onlyMethods (['doSomethingElse ' ])
113
145
->getMock ();
114
146
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 );
116
166
117
- $ this ->assertTrue ($ mock ->doSomething ());
118
167
}
119
168
}
0 commit comments