comparison vendor/phpunit/phpunit-mock-objects/tests/MockObjectTest.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php 1 <?php
2 /* 2 /*
3 * This file is part of the PHPUnit_MockObject package. 3 * This file is part of the phpunit-mock-objects package.
4 * 4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de> 5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 * 6 *
7 * For the full copyright and license information, please view the LICENSE 7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code. 8 * file that was distributed with this source code.
9 */ 9 */
10 10
11 /** 11 use PHPUnit\Framework\MockObject\MockObject;
12 * 12 use PHPUnit\Framework\TestCase;
13 * 13 use PHPUnit\Framework\ExpectationFailedException;
14 * @since Class available since Release 3.0.0 14
15 */ 15 class MockObjectTest extends TestCase
16 class Framework_MockObjectTest extends PHPUnit_Framework_TestCase
17 { 16 {
18 public function testMockedMethodIsNeverCalled() 17 public function testMockedMethodIsNeverCalled()
19 { 18 {
20 $mock = $this->getMock('AnInterface'); 19 $mock = $this->getMockBuilder(AnInterface::class)
20 ->getMock();
21
21 $mock->expects($this->never()) 22 $mock->expects($this->never())
22 ->method('doSomething'); 23 ->method('doSomething');
23 } 24 }
24 25
25 public function testMockedMethodIsNeverCalledWithParameter() 26 public function testMockedMethodIsNeverCalledWithParameter()
26 { 27 {
27 $mock = $this->getMock('SomeClass'); 28 $mock = $this->getMockBuilder(SomeClass::class)
29 ->getMock();
30
28 $mock->expects($this->never()) 31 $mock->expects($this->never())
29 ->method('doSomething') 32 ->method('doSomething')
30 ->with('someArg'); 33 ->with('someArg');
31 } 34 }
32 35
36 /**
37 * @doesNotPerformAssertions
38 */
33 public function testMockedMethodIsNotCalledWhenExpectsAnyWithParameter() 39 public function testMockedMethodIsNotCalledWhenExpectsAnyWithParameter()
34 { 40 {
35 $mock = $this->getMock('SomeClass'); 41 $mock = $this->getMockBuilder(SomeClass::class)
42 ->getMock();
43
36 $mock->expects($this->any()) 44 $mock->expects($this->any())
37 ->method('doSomethingElse') 45 ->method('doSomethingElse')
38 ->with('someArg'); 46 ->with('someArg');
39 } 47 }
40 48
49 /**
50 * @doesNotPerformAssertions
51 */
41 public function testMockedMethodIsNotCalledWhenMethodSpecifiedDirectlyWithParameter() 52 public function testMockedMethodIsNotCalledWhenMethodSpecifiedDirectlyWithParameter()
42 { 53 {
43 $mock = $this->getMock('SomeClass'); 54 $mock = $this->getMockBuilder(SomeClass::class)
55 ->getMock();
56
44 $mock->method('doSomethingElse') 57 $mock->method('doSomethingElse')
45 ->with('someArg'); 58 ->with('someArg');
46 } 59 }
47 60
48 public function testMockedMethodIsCalledAtLeastOnce() 61 public function testMockedMethodIsCalledAtLeastOnce()
49 { 62 {
50 $mock = $this->getMock('AnInterface'); 63 $mock = $this->getMockBuilder(AnInterface::class)
64 ->getMock();
65
51 $mock->expects($this->atLeastOnce()) 66 $mock->expects($this->atLeastOnce())
52 ->method('doSomething'); 67 ->method('doSomething');
53 68
54 $mock->doSomething(); 69 $mock->doSomething();
55 } 70 }
56 71
57 public function testMockedMethodIsCalledAtLeastOnce2() 72 public function testMockedMethodIsCalledAtLeastOnce2()
58 { 73 {
59 $mock = $this->getMock('AnInterface'); 74 $mock = $this->getMockBuilder(AnInterface::class)
75 ->getMock();
76
60 $mock->expects($this->atLeastOnce()) 77 $mock->expects($this->atLeastOnce())
61 ->method('doSomething'); 78 ->method('doSomething');
62 79
63 $mock->doSomething(); 80 $mock->doSomething();
64 $mock->doSomething(); 81 $mock->doSomething();
65 } 82 }
66 83
67 public function testMockedMethodIsCalledAtLeastTwice() 84 public function testMockedMethodIsCalledAtLeastTwice()
68 { 85 {
69 $mock = $this->getMock('AnInterface'); 86 $mock = $this->getMockBuilder(AnInterface::class)
87 ->getMock();
88
70 $mock->expects($this->atLeast(2)) 89 $mock->expects($this->atLeast(2))
71 ->method('doSomething'); 90 ->method('doSomething');
72 91
73 $mock->doSomething(); 92 $mock->doSomething();
74 $mock->doSomething(); 93 $mock->doSomething();
75 } 94 }
76 95
77 public function testMockedMethodIsCalledAtLeastTwice2() 96 public function testMockedMethodIsCalledAtLeastTwice2()
78 { 97 {
79 $mock = $this->getMock('AnInterface'); 98 $mock = $this->getMockBuilder(AnInterface::class)
99 ->getMock();
100
80 $mock->expects($this->atLeast(2)) 101 $mock->expects($this->atLeast(2))
81 ->method('doSomething'); 102 ->method('doSomething');
82 103
83 $mock->doSomething(); 104 $mock->doSomething();
84 $mock->doSomething(); 105 $mock->doSomething();
85 $mock->doSomething(); 106 $mock->doSomething();
86 } 107 }
87 108
88 public function testMockedMethodIsCalledAtMostTwice() 109 public function testMockedMethodIsCalledAtMostTwice()
89 { 110 {
90 $mock = $this->getMock('AnInterface'); 111 $mock = $this->getMockBuilder(AnInterface::class)
112 ->getMock();
113
91 $mock->expects($this->atMost(2)) 114 $mock->expects($this->atMost(2))
92 ->method('doSomething'); 115 ->method('doSomething');
93 116
94 $mock->doSomething(); 117 $mock->doSomething();
95 $mock->doSomething(); 118 $mock->doSomething();
96 } 119 }
97 120
98 public function testMockedMethodIsCalledAtMosttTwice2() 121 public function testMockedMethodIsCalledAtMosttTwice2()
99 { 122 {
100 $mock = $this->getMock('AnInterface'); 123 $mock = $this->getMockBuilder(AnInterface::class)
124 ->getMock();
125
101 $mock->expects($this->atMost(2)) 126 $mock->expects($this->atMost(2))
102 ->method('doSomething'); 127 ->method('doSomething');
103 128
104 $mock->doSomething(); 129 $mock->doSomething();
105 } 130 }
106 131
107 public function testMockedMethodIsCalledOnce() 132 public function testMockedMethodIsCalledOnce()
108 { 133 {
109 $mock = $this->getMock('AnInterface'); 134 $mock = $this->getMockBuilder(AnInterface::class)
135 ->getMock();
136
110 $mock->expects($this->once()) 137 $mock->expects($this->once())
111 ->method('doSomething'); 138 ->method('doSomething');
112 139
113 $mock->doSomething(); 140 $mock->doSomething();
114 } 141 }
115 142
116 public function testMockedMethodIsCalledOnceWithParameter() 143 public function testMockedMethodIsCalledOnceWithParameter()
117 { 144 {
118 $mock = $this->getMock('SomeClass'); 145 $mock = $this->getMockBuilder(SomeClass::class)
146 ->getMock();
147
119 $mock->expects($this->once()) 148 $mock->expects($this->once())
120 ->method('doSomethingElse') 149 ->method('doSomethingElse')
121 ->with($this->equalTo('something')); 150 ->with($this->equalTo('something'));
122 151
123 $mock->doSomethingElse('something'); 152 $mock->doSomethingElse('something');
124 } 153 }
125 154
126 public function testMockedMethodIsCalledExactly() 155 public function testMockedMethodIsCalledExactly()
127 { 156 {
128 $mock = $this->getMock('AnInterface'); 157 $mock = $this->getMockBuilder(AnInterface::class)
158 ->getMock();
159
129 $mock->expects($this->exactly(2)) 160 $mock->expects($this->exactly(2))
130 ->method('doSomething'); 161 ->method('doSomething');
131 162
132 $mock->doSomething(); 163 $mock->doSomething();
133 $mock->doSomething(); 164 $mock->doSomething();
134 } 165 }
135 166
136 public function testStubbedException() 167 public function testStubbedException()
137 { 168 {
138 $mock = $this->getMock('AnInterface'); 169 $mock = $this->getMockBuilder(AnInterface::class)
139 $mock->expects($this->any()) 170 ->getMock();
140 ->method('doSomething') 171
141 ->will($this->throwException(new Exception)); 172 $mock->expects($this->any())
142 173 ->method('doSomething')
143 try { 174 ->will($this->throwException(new \Exception()));
144 $mock->doSomething(); 175
145 } catch (Exception $e) { 176 $this->expectException(\Exception::class);
146 return; 177
147 } 178 $mock->doSomething();
148
149 $this->fail();
150 } 179 }
151 180
152 public function testStubbedWillThrowException() 181 public function testStubbedWillThrowException()
153 { 182 {
154 $mock = $this->getMock('AnInterface'); 183 $mock = $this->getMockBuilder(AnInterface::class)
155 $mock->expects($this->any()) 184 ->getMock();
156 ->method('doSomething') 185
157 ->willThrowException(new Exception); 186 $mock->expects($this->any())
158 187 ->method('doSomething')
159 try { 188 ->willThrowException(new \Exception());
160 $mock->doSomething(); 189
161 } catch (Exception $e) { 190 $this->expectException(\Exception::class);
162 return; 191
163 } 192 $mock->doSomething();
164
165 $this->fail();
166 } 193 }
167 194
168 public function testStubbedReturnValue() 195 public function testStubbedReturnValue()
169 { 196 {
170 $mock = $this->getMock('AnInterface'); 197 $mock = $this->getMockBuilder(AnInterface::class)
198 ->getMock();
199
171 $mock->expects($this->any()) 200 $mock->expects($this->any())
172 ->method('doSomething') 201 ->method('doSomething')
173 ->will($this->returnValue('something')); 202 ->will($this->returnValue('something'));
174 203
175 $this->assertEquals('something', $mock->doSomething()); 204 $this->assertEquals('something', $mock->doSomething());
176 205
177 $mock = $this->getMock('AnInterface'); 206 $mock = $this->getMockBuilder(AnInterface::class)
207 ->getMock();
208
178 $mock->expects($this->any()) 209 $mock->expects($this->any())
179 ->method('doSomething') 210 ->method('doSomething')
180 ->willReturn('something'); 211 ->willReturn('something');
181 212
182 $this->assertEquals('something', $mock->doSomething()); 213 $this->assertEquals('something', $mock->doSomething());
183 } 214 }
184 215
185 public function testStubbedReturnValueMap() 216 public function testStubbedReturnValueMap()
186 { 217 {
187 $map = array( 218 $map = [
188 array('a', 'b', 'c', 'd'), 219 ['a', 'b', 'c', 'd'],
189 array('e', 'f', 'g', 'h') 220 ['e', 'f', 'g', 'h']
190 ); 221 ];
191 222
192 $mock = $this->getMock('AnInterface'); 223 $mock = $this->getMockBuilder(AnInterface::class)
224 ->getMock();
225
193 $mock->expects($this->any()) 226 $mock->expects($this->any())
194 ->method('doSomething') 227 ->method('doSomething')
195 ->will($this->returnValueMap($map)); 228 ->will($this->returnValueMap($map));
196 229
197 $this->assertEquals('d', $mock->doSomething('a', 'b', 'c')); 230 $this->assertEquals('d', $mock->doSomething('a', 'b', 'c'));
198 $this->assertEquals('h', $mock->doSomething('e', 'f', 'g')); 231 $this->assertEquals('h', $mock->doSomething('e', 'f', 'g'));
199 $this->assertEquals(null, $mock->doSomething('foo', 'bar')); 232 $this->assertEquals(null, $mock->doSomething('foo', 'bar'));
200 233
201 $mock = $this->getMock('AnInterface'); 234 $mock = $this->getMockBuilder(AnInterface::class)
235 ->getMock();
236
202 $mock->expects($this->any()) 237 $mock->expects($this->any())
203 ->method('doSomething') 238 ->method('doSomething')
204 ->willReturnMap($map); 239 ->willReturnMap($map);
205 240
206 $this->assertEquals('d', $mock->doSomething('a', 'b', 'c')); 241 $this->assertEquals('d', $mock->doSomething('a', 'b', 'c'));
208 $this->assertEquals(null, $mock->doSomething('foo', 'bar')); 243 $this->assertEquals(null, $mock->doSomething('foo', 'bar'));
209 } 244 }
210 245
211 public function testStubbedReturnArgument() 246 public function testStubbedReturnArgument()
212 { 247 {
213 $mock = $this->getMock('AnInterface'); 248 $mock = $this->getMockBuilder(AnInterface::class)
249 ->getMock();
250
214 $mock->expects($this->any()) 251 $mock->expects($this->any())
215 ->method('doSomething') 252 ->method('doSomething')
216 ->will($this->returnArgument(1)); 253 ->will($this->returnArgument(1));
217 254
218 $this->assertEquals('b', $mock->doSomething('a', 'b')); 255 $this->assertEquals('b', $mock->doSomething('a', 'b'));
219 256
220 $mock = $this->getMock('AnInterface'); 257 $mock = $this->getMockBuilder(AnInterface::class)
258 ->getMock();
259
221 $mock->expects($this->any()) 260 $mock->expects($this->any())
222 ->method('doSomething') 261 ->method('doSomething')
223 ->willReturnArgument(1); 262 ->willReturnArgument(1);
224 263
225 $this->assertEquals('b', $mock->doSomething('a', 'b')); 264 $this->assertEquals('b', $mock->doSomething('a', 'b'));
226 } 265 }
227 266
228 public function testFunctionCallback() 267 public function testFunctionCallback()
229 { 268 {
230 $mock = $this->getMock('SomeClass', array('doSomething'), array(), '', false); 269 $mock = $this->getMockBuilder(SomeClass::class)
270 ->setMethods(['doSomething'])
271 ->getMock();
272
231 $mock->expects($this->once()) 273 $mock->expects($this->once())
232 ->method('doSomething') 274 ->method('doSomething')
233 ->will($this->returnCallback('functionCallback')); 275 ->will($this->returnCallback('functionCallback'));
234 276
235 $this->assertEquals('pass', $mock->doSomething('foo', 'bar')); 277 $this->assertEquals('pass', $mock->doSomething('foo', 'bar'));
236 278
237 $mock = $this->getMock('SomeClass', array('doSomething'), array(), '', false); 279 $mock = $this->getMockBuilder(SomeClass::class)
280 ->setMethods(['doSomething'])
281 ->getMock();
282
238 $mock->expects($this->once()) 283 $mock->expects($this->once())
239 ->method('doSomething') 284 ->method('doSomething')
240 ->willReturnCallback('functionCallback'); 285 ->willReturnCallback('functionCallback');
241 286
242 $this->assertEquals('pass', $mock->doSomething('foo', 'bar')); 287 $this->assertEquals('pass', $mock->doSomething('foo', 'bar'));
243 } 288 }
244 289
245 public function testStubbedReturnSelf() 290 public function testStubbedReturnSelf()
246 { 291 {
247 $mock = $this->getMock('AnInterface'); 292 $mock = $this->getMockBuilder(AnInterface::class)
293 ->getMock();
294
248 $mock->expects($this->any()) 295 $mock->expects($this->any())
249 ->method('doSomething') 296 ->method('doSomething')
250 ->will($this->returnSelf()); 297 ->will($this->returnSelf());
251 298
252 $this->assertEquals($mock, $mock->doSomething()); 299 $this->assertEquals($mock, $mock->doSomething());
253 300
254 $mock = $this->getMock('AnInterface'); 301 $mock = $this->getMockBuilder(AnInterface::class)
302 ->getMock();
303
255 $mock->expects($this->any()) 304 $mock->expects($this->any())
256 ->method('doSomething') 305 ->method('doSomething')
257 ->willReturnSelf(); 306 ->willReturnSelf();
258 307
259 $this->assertEquals($mock, $mock->doSomething()); 308 $this->assertEquals($mock, $mock->doSomething());
260 } 309 }
261 310
262 public function testStubbedReturnOnConsecutiveCalls() 311 public function testStubbedReturnOnConsecutiveCalls()
263 { 312 {
264 $mock = $this->getMock('AnInterface'); 313 $mock = $this->getMockBuilder(AnInterface::class)
314 ->getMock();
315
265 $mock->expects($this->any()) 316 $mock->expects($this->any())
266 ->method('doSomething') 317 ->method('doSomething')
267 ->will($this->onConsecutiveCalls('a', 'b', 'c')); 318 ->will($this->onConsecutiveCalls('a', 'b', 'c'));
268 319
269 $this->assertEquals('a', $mock->doSomething()); 320 $this->assertEquals('a', $mock->doSomething());
270 $this->assertEquals('b', $mock->doSomething()); 321 $this->assertEquals('b', $mock->doSomething());
271 $this->assertEquals('c', $mock->doSomething()); 322 $this->assertEquals('c', $mock->doSomething());
272 323
273 $mock = $this->getMock('AnInterface'); 324 $mock = $this->getMockBuilder(AnInterface::class)
325 ->getMock();
326
274 $mock->expects($this->any()) 327 $mock->expects($this->any())
275 ->method('doSomething') 328 ->method('doSomething')
276 ->willReturnOnConsecutiveCalls('a', 'b', 'c'); 329 ->willReturnOnConsecutiveCalls('a', 'b', 'c');
277 330
278 $this->assertEquals('a', $mock->doSomething()); 331 $this->assertEquals('a', $mock->doSomething());
280 $this->assertEquals('c', $mock->doSomething()); 333 $this->assertEquals('c', $mock->doSomething());
281 } 334 }
282 335
283 public function testStaticMethodCallback() 336 public function testStaticMethodCallback()
284 { 337 {
285 $mock = $this->getMock('SomeClass', array('doSomething'), array(), '', false); 338 $mock = $this->getMockBuilder(SomeClass::class)
286 $mock->expects($this->once()) 339 ->setMethods(['doSomething'])
287 ->method('doSomething') 340 ->getMock();
288 ->will($this->returnCallback(array('MethodCallback', 'staticCallback'))); 341
342 $mock->expects($this->once())
343 ->method('doSomething')
344 ->will($this->returnCallback(['MethodCallback', 'staticCallback']));
289 345
290 $this->assertEquals('pass', $mock->doSomething('foo', 'bar')); 346 $this->assertEquals('pass', $mock->doSomething('foo', 'bar'));
291 } 347 }
292 348
293 public function testPublicMethodCallback() 349 public function testPublicMethodCallback()
294 { 350 {
295 $mock = $this->getMock('SomeClass', array('doSomething'), array(), '', false); 351 $mock = $this->getMockBuilder(SomeClass::class)
296 $mock->expects($this->once()) 352 ->setMethods(['doSomething'])
297 ->method('doSomething') 353 ->getMock();
298 ->will($this->returnCallback(array(new MethodCallback, 'nonStaticCallback'))); 354
355 $mock->expects($this->once())
356 ->method('doSomething')
357 ->will($this->returnCallback([new MethodCallback, 'nonStaticCallback']));
299 358
300 $this->assertEquals('pass', $mock->doSomething('foo', 'bar')); 359 $this->assertEquals('pass', $mock->doSomething('foo', 'bar'));
301 } 360 }
302 361
303 public function testMockClassOnlyGeneratedOnce() 362 public function testMockClassOnlyGeneratedOnce()
304 { 363 {
305 $mock1 = $this->getMock('AnInterface'); 364 $mock1 = $this->getMockBuilder(AnInterface::class)
306 $mock2 = $this->getMock('AnInterface'); 365 ->getMock();
366
367 $mock2 = $this->getMockBuilder(AnInterface::class)
368 ->getMock();
307 369
308 $this->assertEquals(get_class($mock1), get_class($mock2)); 370 $this->assertEquals(get_class($mock1), get_class($mock2));
309 } 371 }
310 372
311 public function testMockClassDifferentForPartialMocks() 373 public function testMockClassDifferentForPartialMocks()
312 { 374 {
313 $mock1 = $this->getMock('PartialMockTestClass'); 375 $mock1 = $this->getMockBuilder(PartialMockTestClass::class)
314 $mock2 = $this->getMock('PartialMockTestClass', array('doSomething')); 376 ->getMock();
315 $mock3 = $this->getMock('PartialMockTestClass', array('doSomething')); 377
316 $mock4 = $this->getMock('PartialMockTestClass', array('doAnotherThing')); 378 $mock2 = $this->getMockBuilder(PartialMockTestClass::class)
317 $mock5 = $this->getMock('PartialMockTestClass', array('doAnotherThing')); 379 ->setMethods(['doSomething'])
380 ->getMock();
381
382 $mock3 = $this->getMockBuilder(PartialMockTestClass::class)
383 ->setMethods(['doSomething'])
384 ->getMock();
385
386 $mock4 = $this->getMockBuilder(PartialMockTestClass::class)
387 ->setMethods(['doAnotherThing'])
388 ->getMock();
389
390 $mock5 = $this->getMockBuilder(PartialMockTestClass::class)
391 ->setMethods(['doAnotherThing'])
392 ->getMock();
318 393
319 $this->assertNotEquals(get_class($mock1), get_class($mock2)); 394 $this->assertNotEquals(get_class($mock1), get_class($mock2));
320 $this->assertNotEquals(get_class($mock1), get_class($mock3)); 395 $this->assertNotEquals(get_class($mock1), get_class($mock3));
321 $this->assertNotEquals(get_class($mock1), get_class($mock4)); 396 $this->assertNotEquals(get_class($mock1), get_class($mock4));
322 $this->assertNotEquals(get_class($mock1), get_class($mock5)); 397 $this->assertNotEquals(get_class($mock1), get_class($mock5));
326 $this->assertEquals(get_class($mock4), get_class($mock5)); 401 $this->assertEquals(get_class($mock4), get_class($mock5));
327 } 402 }
328 403
329 public function testMockClassStoreOverrulable() 404 public function testMockClassStoreOverrulable()
330 { 405 {
331 $mock1 = $this->getMock('PartialMockTestClass'); 406 $mock1 = $this->getMockBuilder(PartialMockTestClass::class)
332 $mock2 = $this->getMock('PartialMockTestClass', array(), array(), 'MyMockClassNameForPartialMockTestClass1'); 407 ->getMock();
333 $mock3 = $this->getMock('PartialMockTestClass'); 408
334 $mock4 = $this->getMock('PartialMockTestClass', array('doSomething'), array(), 'AnotherMockClassNameForPartialMockTestClass'); 409 $mock2 = $this->getMockBuilder(PartialMockTestClass::class)
335 $mock5 = $this->getMock('PartialMockTestClass', array(), array(), 'MyMockClassNameForPartialMockTestClass2'); 410 ->setMockClassName('MyMockClassNameForPartialMockTestClass1')
411 ->getMock();
412
413 $mock3 = $this->getMockBuilder(PartialMockTestClass::class)
414 ->getMock();
415
416 $mock4 = $this->getMockBuilder(PartialMockTestClass::class)
417 ->setMethods(['doSomething'])
418 ->setMockClassName('AnotherMockClassNameForPartialMockTestClass')
419 ->getMock();
420
421 $mock5 = $this->getMockBuilder(PartialMockTestClass::class)
422 ->setMockClassName('MyMockClassNameForPartialMockTestClass2')
423 ->getMock();
336 424
337 $this->assertNotEquals(get_class($mock1), get_class($mock2)); 425 $this->assertNotEquals(get_class($mock1), get_class($mock2));
338 $this->assertEquals(get_class($mock1), get_class($mock3)); 426 $this->assertEquals(get_class($mock1), get_class($mock3));
339 $this->assertNotEquals(get_class($mock1), get_class($mock4)); 427 $this->assertNotEquals(get_class($mock1), get_class($mock4));
340 $this->assertNotEquals(get_class($mock2), get_class($mock3)); 428 $this->assertNotEquals(get_class($mock2), get_class($mock3));
343 $this->assertNotEquals(get_class($mock3), get_class($mock4)); 431 $this->assertNotEquals(get_class($mock3), get_class($mock4));
344 $this->assertNotEquals(get_class($mock3), get_class($mock5)); 432 $this->assertNotEquals(get_class($mock3), get_class($mock5));
345 $this->assertNotEquals(get_class($mock4), get_class($mock5)); 433 $this->assertNotEquals(get_class($mock4), get_class($mock5));
346 } 434 }
347 435
348 /**
349 * @covers PHPUnit_Framework_MockObject_Generator::getMock
350 */
351 public function testGetMockWithFixedClassNameCanProduceTheSameMockTwice() 436 public function testGetMockWithFixedClassNameCanProduceTheSameMockTwice()
352 { 437 {
353 $mock = $this->getMockBuilder('StdClass')->setMockClassName('FixedName')->getMock(); 438 $mock = $this->getMockBuilder(stdClass::class)->setMockClassName('FixedName')->getMock();
354 $mock = $this->getMockBuilder('StdClass')->setMockClassName('FixedName')->getMock(); 439 $mock = $this->getMockBuilder(stdClass::class)->setMockClassName('FixedName')->getMock();
355 $this->assertInstanceOf('StdClass', $mock); 440 $this->assertInstanceOf(stdClass::class, $mock);
356 } 441 }
357 442
358 public function testOriginalConstructorSettingConsidered() 443 public function testOriginalConstructorSettingConsidered()
359 { 444 {
360 $mock1 = $this->getMock('PartialMockTestClass'); 445 $mock1 = $this->getMockBuilder(PartialMockTestClass::class)
361 $mock2 = $this->getMock('PartialMockTestClass', array(), array(), '', false); 446 ->getMock();
447
448 $mock2 = $this->getMockBuilder(PartialMockTestClass::class)
449 ->disableOriginalConstructor()
450 ->getMock();
362 451
363 $this->assertTrue($mock1->constructorCalled); 452 $this->assertTrue($mock1->constructorCalled);
364 $this->assertFalse($mock2->constructorCalled); 453 $this->assertFalse($mock2->constructorCalled);
365 } 454 }
366 455
367 public function testOriginalCloneSettingConsidered() 456 public function testOriginalCloneSettingConsidered()
368 { 457 {
369 $mock1 = $this->getMock('PartialMockTestClass'); 458 $mock1 = $this->getMockBuilder(PartialMockTestClass::class)
370 $mock2 = $this->getMock('PartialMockTestClass', array(), array(), '', true, false); 459 ->getMock();
460
461 $mock2 = $this->getMockBuilder(PartialMockTestClass::class)
462 ->disableOriginalClone()
463 ->getMock();
371 464
372 $this->assertNotEquals(get_class($mock1), get_class($mock2)); 465 $this->assertNotEquals(get_class($mock1), get_class($mock2));
373 } 466 }
374 467
375 public function testGetMockForAbstractClass() 468 public function testGetMockForAbstractClass()
376 { 469 {
377 $mock = $this->getMock('AbstractMockTestClass'); 470 $mock = $this->getMockBuilder(AbstractMockTestClass::class)
471 ->getMock();
472
378 $mock->expects($this->never()) 473 $mock->expects($this->never())
379 ->method('doSomething'); 474 ->method('doSomething');
380 } 475 }
381 476
382 public function traversableProvider()
383 {
384 return array(
385 array('Traversable'),
386 array('\Traversable'),
387 array('TraversableMockTestInterface'),
388 array(array('Traversable')),
389 array(array('Iterator','Traversable')),
390 array(array('\Iterator','\Traversable'))
391 );
392 }
393
394 /** 477 /**
395 * @dataProvider traversableProvider 478 * @dataProvider traversableProvider
396 */ 479 */
397 public function testGetMockForTraversable($type) 480 public function testGetMockForTraversable($type)
398 { 481 {
399 $mock = $this->getMock($type); 482 $mock = $this->getMockBuilder($type)
400 $this->assertInstanceOf('Traversable', $mock); 483 ->getMock();
484
485 $this->assertInstanceOf(Traversable::class, $mock);
401 } 486 }
402 487
403 public function testMultipleInterfacesCanBeMockedInSingleObject() 488 public function testMultipleInterfacesCanBeMockedInSingleObject()
404 { 489 {
405 $mock = $this->getMock(array('AnInterface', 'AnotherInterface')); 490 $mock = $this->getMockBuilder([AnInterface::class, AnotherInterface::class])
406 $this->assertInstanceOf('AnInterface', $mock); 491 ->getMock();
407 $this->assertInstanceOf('AnotherInterface', $mock); 492
408 } 493 $this->assertInstanceOf(AnInterface::class, $mock);
409 494 $this->assertInstanceOf(AnotherInterface::class, $mock);
410 /** 495 }
411 * @requires PHP 5.4.0 496
412 */
413 public function testGetMockForTrait() 497 public function testGetMockForTrait()
414 { 498 {
415 $mock = $this->getMockForTrait('AbstractTrait'); 499 $mock = $this->getMockForTrait(AbstractTrait::class);
416 $mock->expects($this->never())->method('doSomething'); 500
501 $mock->expects($this->never())
502 ->method('doSomething');
417 503
418 $parent = get_parent_class($mock); 504 $parent = get_parent_class($mock);
419 $traits = class_uses($parent, false); 505 $traits = class_uses($parent, false);
420 506
421 $this->assertContains('AbstractTrait', $traits); 507 $this->assertContains(AbstractTrait::class, $traits);
422 } 508 }
423 509
424 public function testClonedMockObjectShouldStillEqualTheOriginal() 510 public function testClonedMockObjectShouldStillEqualTheOriginal()
425 { 511 {
426 $a = $this->getMock('stdClass'); 512 $a = $this->getMockBuilder(stdClass::class)
513 ->getMock();
514
427 $b = clone $a; 515 $b = clone $a;
516
428 $this->assertEquals($a, $b); 517 $this->assertEquals($a, $b);
429 } 518 }
430 519
431 public function testMockObjectsConstructedIndepentantlyShouldBeEqual() 520 public function testMockObjectsConstructedIndepentantlyShouldBeEqual()
432 { 521 {
433 $a = $this->getMock('stdClass'); 522 $a = $this->getMockBuilder(stdClass::class)
434 $b = $this->getMock('stdClass'); 523 ->getMock();
524
525 $b = $this->getMockBuilder(stdClass::class)
526 ->getMock();
527
435 $this->assertEquals($a, $b); 528 $this->assertEquals($a, $b);
436 } 529 }
437 530
438 public function testMockObjectsConstructedIndepentantlyShouldNotBeTheSame() 531 public function testMockObjectsConstructedIndepentantlyShouldNotBeTheSame()
439 { 532 {
440 $a = $this->getMock('stdClass'); 533 $a = $this->getMockBuilder(stdClass::class)
441 $b = $this->getMock('stdClass'); 534 ->getMock();
535
536 $b = $this->getMockBuilder(stdClass::class)
537 ->getMock();
538
442 $this->assertNotSame($a, $b); 539 $this->assertNotSame($a, $b);
443 } 540 }
444 541
445 public function testClonedMockObjectCanBeUsedInPlaceOfOriginalOne() 542 public function testClonedMockObjectCanBeUsedInPlaceOfOriginalOne()
446 { 543 {
447 $x = $this->getMock('stdClass'); 544 $x = $this->getMockBuilder(stdClass::class)
545 ->getMock();
546
448 $y = clone $x; 547 $y = clone $x;
449 548
450 $mock = $this->getMock('stdClass', array('foo')); 549 $mock = $this->getMockBuilder(stdClass::class)
451 $mock->expects($this->once())->method('foo')->with($this->equalTo($x)); 550 ->setMethods(['foo'])
551 ->getMock();
552
553 $mock->expects($this->once())
554 ->method('foo')
555 ->with($this->equalTo($x));
556
452 $mock->foo($y); 557 $mock->foo($y);
453 } 558 }
454 559
455 public function testClonedMockObjectIsNotIdenticalToOriginalOne() 560 public function testClonedMockObjectIsNotIdenticalToOriginalOne()
456 { 561 {
457 $x = $this->getMock('stdClass'); 562 $x = $this->getMockBuilder(stdClass::class)
563 ->getMock();
564
458 $y = clone $x; 565 $y = clone $x;
459 566
460 $mock = $this->getMock('stdClass', array('foo')); 567 $mock = $this->getMockBuilder(stdClass::class)
461 $mock->expects($this->once())->method('foo')->with($this->logicalNot($this->identicalTo($x))); 568 ->setMethods(['foo'])
569 ->getMock();
570
571 $mock->expects($this->once())
572 ->method('foo')
573 ->with($this->logicalNot($this->identicalTo($x)));
574
462 $mock->foo($y); 575 $mock->foo($y);
463 } 576 }
464 577
465 public function testObjectMethodCallWithArgumentCloningEnabled() 578 public function testObjectMethodCallWithArgumentCloningEnabled()
466 { 579 {
467 $expectedObject = new StdClass; 580 $expectedObject = new stdClass;
468 581
469 $mock = $this->getMockBuilder('SomeClass') 582 $mock = $this->getMockBuilder('SomeClass')
470 ->setMethods(array('doSomethingElse')) 583 ->setMethods(['doSomethingElse'])
471 ->enableArgumentCloning() 584 ->enableArgumentCloning()
472 ->getMock(); 585 ->getMock();
473 586
474 $actualArguments = array(); 587 $actualArguments = [];
475 588
476 $mock->expects($this->any()) 589 $mock->expects($this->any())
477 ->method('doSomethingElse') 590 ->method('doSomethingElse')
478 ->will($this->returnCallback(function () use (&$actualArguments) { 591 ->will(
479 $actualArguments = func_get_args(); 592 $this->returnCallback(
480 })); 593 function () use (&$actualArguments) {
594 $actualArguments = func_get_args();
595 }
596 )
597 );
481 598
482 $mock->doSomethingElse($expectedObject); 599 $mock->doSomethingElse($expectedObject);
483 600
484 $this->assertEquals(1, count($actualArguments)); 601 $this->assertEquals(1, count($actualArguments));
485 $this->assertEquals($expectedObject, $actualArguments[0]); 602 $this->assertEquals($expectedObject, $actualArguments[0]);
486 $this->assertNotSame($expectedObject, $actualArguments[0]); 603 $this->assertNotSame($expectedObject, $actualArguments[0]);
487 } 604 }
488 605
489 public function testObjectMethodCallWithArgumentCloningDisabled() 606 public function testObjectMethodCallWithArgumentCloningDisabled()
490 { 607 {
491 $expectedObject = new StdClass; 608 $expectedObject = new stdClass;
492 609
493 $mock = $this->getMockBuilder('SomeClass') 610 $mock = $this->getMockBuilder('SomeClass')
494 ->setMethods(array('doSomethingElse')) 611 ->setMethods(['doSomethingElse'])
495 ->disableArgumentCloning() 612 ->disableArgumentCloning()
496 ->getMock(); 613 ->getMock();
497 614
498 $actualArguments = array(); 615 $actualArguments = [];
499 616
500 $mock->expects($this->any()) 617 $mock->expects($this->any())
501 ->method('doSomethingElse') 618 ->method('doSomethingElse')
502 ->will($this->returnCallback(function () use (&$actualArguments) { 619 ->will(
503 $actualArguments = func_get_args(); 620 $this->returnCallback(
504 })); 621 function () use (&$actualArguments) {
622 $actualArguments = func_get_args();
623 }
624 )
625 );
505 626
506 $mock->doSomethingElse($expectedObject); 627 $mock->doSomethingElse($expectedObject);
507 628
508 $this->assertEquals(1, count($actualArguments)); 629 $this->assertEquals(1, count($actualArguments));
509 $this->assertSame($expectedObject, $actualArguments[0]); 630 $this->assertSame($expectedObject, $actualArguments[0]);
510 } 631 }
511 632
512 public function testArgumentCloningOptionGeneratesUniqueMock() 633 public function testArgumentCloningOptionGeneratesUniqueMock()
513 { 634 {
514 $mockWithCloning = $this->getMockBuilder('SomeClass') 635 $mockWithCloning = $this->getMockBuilder('SomeClass')
515 ->setMethods(array('doSomethingElse')) 636 ->setMethods(['doSomethingElse'])
516 ->enableArgumentCloning() 637 ->enableArgumentCloning()
517 ->getMock(); 638 ->getMock();
518 639
519 $mockWithoutCloning = $this->getMockBuilder('SomeClass') 640 $mockWithoutCloning = $this->getMockBuilder('SomeClass')
520 ->setMethods(array('doSomethingElse')) 641 ->setMethods(['doSomethingElse'])
521 ->disableArgumentCloning() 642 ->disableArgumentCloning()
522 ->getMock(); 643 ->getMock();
523 644
524 $this->assertNotEquals($mockWithCloning, $mockWithoutCloning); 645 $this->assertNotEquals($mockWithCloning, $mockWithoutCloning);
525 } 646 }
526 647
527 public function testVerificationOfMethodNameFailsWithoutParameters() 648 public function testVerificationOfMethodNameFailsWithoutParameters()
528 { 649 {
529 $mock = $this->getMock('SomeClass', array('right', 'wrong'), array(), '', true, true, true); 650 $mock = $this->getMockBuilder(SomeClass::class)
651 ->setMethods(['right', 'wrong'])
652 ->getMock();
653
530 $mock->expects($this->once()) 654 $mock->expects($this->once())
531 ->method('right'); 655 ->method('right');
532 656
533 $mock->wrong(); 657 $mock->wrong();
658
534 try { 659 try {
535 $mock->__phpunit_verify(); 660 $mock->__phpunit_verify();
536 $this->fail('Expected exception'); 661 $this->fail('Expected exception');
537 } catch (PHPUnit_Framework_ExpectationFailedException $e) { 662 } catch (ExpectationFailedException $e) {
538 $this->assertSame( 663 $this->assertSame(
539 "Expectation failed for method name is equal to <string:right> when invoked 1 time(s).\n" 664 'Expectation failed for method name is equal to "right" when invoked 1 time(s).' . PHP_EOL .
540 . "Method was expected to be called 1 times, actually called 0 times.\n", 665 'Method was expected to be called 1 times, actually called 0 times.' . PHP_EOL,
541 $e->getMessage() 666 $e->getMessage()
542 ); 667 );
543 } 668 }
544 669
545 $this->resetMockObjects(); 670 $this->resetMockObjects();
546 } 671 }
547 672
548 public function testVerificationOfMethodNameFailsWithParameters() 673 public function testVerificationOfMethodNameFailsWithParameters()
549 { 674 {
550 $mock = $this->getMock('SomeClass', array('right', 'wrong'), array(), '', true, true, true); 675 $mock = $this->getMockBuilder(SomeClass::class)
676 ->setMethods(['right', 'wrong'])
677 ->getMock();
678
551 $mock->expects($this->once()) 679 $mock->expects($this->once())
552 ->method('right'); 680 ->method('right');
553 681
554 $mock->wrong(); 682 $mock->wrong();
683
555 try { 684 try {
556 $mock->__phpunit_verify(); 685 $mock->__phpunit_verify();
557 $this->fail('Expected exception'); 686 $this->fail('Expected exception');
558 } catch (PHPUnit_Framework_ExpectationFailedException $e) { 687 } catch (ExpectationFailedException $e) {
559 $this->assertSame( 688 $this->assertSame(
560 "Expectation failed for method name is equal to <string:right> when invoked 1 time(s).\n" 689 'Expectation failed for method name is equal to "right" when invoked 1 time(s).' . PHP_EOL .
561 . "Method was expected to be called 1 times, actually called 0 times.\n", 690 'Method was expected to be called 1 times, actually called 0 times.' . PHP_EOL,
562 $e->getMessage() 691 $e->getMessage()
563 ); 692 );
564 } 693 }
565 694
566 $this->resetMockObjects(); 695 $this->resetMockObjects();
567 } 696 }
568 697
569 public function testVerificationOfMethodNameFailsWithWrongParameters() 698 public function testVerificationOfMethodNameFailsWithWrongParameters()
570 { 699 {
571 $mock = $this->getMock('SomeClass', array('right', 'wrong'), array(), '', true, true, true); 700 $mock = $this->getMockBuilder(SomeClass::class)
701 ->setMethods(['right', 'wrong'])
702 ->getMock();
703
572 $mock->expects($this->once()) 704 $mock->expects($this->once())
573 ->method('right') 705 ->method('right')
574 ->with(array('first', 'second')); 706 ->with(['first', 'second']);
575 707
576 try { 708 try {
577 $mock->right(array('second')); 709 $mock->right(['second']);
578 } catch (PHPUnit_Framework_ExpectationFailedException $e) { 710 } catch (ExpectationFailedException $e) {
579 $this->assertSame( 711 $this->assertSame(
580 "Expectation failed for method name is equal to <string:right> when invoked 1 time(s)\n" 712 'Expectation failed for method name is equal to "right" when invoked 1 time(s)' . PHP_EOL .
581 . "Parameter 0 for invocation SomeClass::right(Array (...)) does not match expected value.\n" 713 'Parameter 0 for invocation SomeClass::right(Array (...)) does not match expected value.' . PHP_EOL .
582 . "Failed asserting that two arrays are equal.", 714 'Failed asserting that two arrays are equal.',
583 $e->getMessage() 715 $e->getMessage()
584 ); 716 );
585 } 717 }
586 718
587 try { 719 try {
588 $mock->__phpunit_verify(); 720 $mock->__phpunit_verify();
589 $this->fail('Expected exception'); 721
590 } catch (PHPUnit_Framework_ExpectationFailedException $e) { 722 // CHECKOUT THIS MORE CAREFULLY
723 // $this->fail('Expected exception');
724
725 } catch (ExpectationFailedException $e) {
591 $this->assertSame( 726 $this->assertSame(
592 "Expectation failed for method name is equal to <string:right> when invoked 1 time(s).\n" 727 'Expectation failed for method name is equal to "right" when invoked 1 time(s).' . PHP_EOL .
593 . "Parameter 0 for invocation SomeClass::right(Array (...)) does not match expected value.\n" 728 'Parameter 0 for invocation SomeClass::right(Array (...)) does not match expected value.' . PHP_EOL .
594 . "Failed asserting that two arrays are equal.\n" 729 'Failed asserting that two arrays are equal.' . PHP_EOL .
595 . "--- Expected\n" 730 '--- Expected' . PHP_EOL .
596 . "+++ Actual\n" 731 '+++ Actual' . PHP_EOL .
597 . "@@ @@\n" 732 '@@ @@' . PHP_EOL .
598 . " Array (\n" 733 ' Array (' . PHP_EOL .
599 . "- 0 => 'first'\n" 734 '- 0 => \'first\'' . PHP_EOL .
600 . "- 1 => 'second'\n" 735 '- 1 => \'second\'' . PHP_EOL .
601 . "+ 0 => 'second'\n" 736 '+ 0 => \'second\'' . PHP_EOL,
602 . " )\n",
603 $e->getMessage() 737 $e->getMessage()
604 ); 738 );
605 } 739 }
606 740
607 $this->resetMockObjects(); 741 $this->resetMockObjects();
608 } 742 }
609 743
610 public function testVerificationOfNeverFailsWithEmptyParameters() 744 public function testVerificationOfNeverFailsWithEmptyParameters()
611 { 745 {
612 $mock = $this->getMock('SomeClass', array('right', 'wrong'), array(), '', true, true, true); 746 $mock = $this->getMockBuilder(SomeClass::class)
747 ->setMethods(['right', 'wrong'])
748 ->getMock();
749
613 $mock->expects($this->never()) 750 $mock->expects($this->never())
614 ->method('right') 751 ->method('right')
615 ->with(); 752 ->with();
616 753
617 try { 754 try {
618 $mock->right(); 755 $mock->right();
619 $this->fail('Expected exception'); 756 $this->fail('Expected exception');
620 } catch (PHPUnit_Framework_ExpectationFailedException $e) { 757 } catch (ExpectationFailedException $e) {
621 $this->assertSame( 758 $this->assertSame(
622 'SomeClass::right() was not expected to be called.', 759 'SomeClass::right() was not expected to be called.',
623 $e->getMessage() 760 $e->getMessage()
624 ); 761 );
625 } 762 }
627 $this->resetMockObjects(); 764 $this->resetMockObjects();
628 } 765 }
629 766
630 public function testVerificationOfNeverFailsWithAnyParameters() 767 public function testVerificationOfNeverFailsWithAnyParameters()
631 { 768 {
632 $mock = $this->getMock('SomeClass', array('right', 'wrong'), array(), '', true, true, true); 769 $mock = $this->getMockBuilder(SomeClass::class)
770 ->setMethods(['right', 'wrong'])
771 ->getMock();
772
633 $mock->expects($this->never()) 773 $mock->expects($this->never())
634 ->method('right') 774 ->method('right')
635 ->withAnyParameters(); 775 ->withAnyParameters();
636 776
637 try { 777 try {
638 $mock->right(); 778 $mock->right();
639 $this->fail('Expected exception'); 779 $this->fail('Expected exception');
640 } catch (PHPUnit_Framework_ExpectationFailedException $e) { 780 } catch (ExpectationFailedException $e) {
641 $this->assertSame( 781 $this->assertSame(
642 'SomeClass::right() was not expected to be called.', 782 'SomeClass::right() was not expected to be called.',
643 $e->getMessage() 783 $e->getMessage()
644 ); 784 );
645 } 785 }
646 786
647 $this->resetMockObjects(); 787 $this->resetMockObjects();
648 } 788 }
649 789
650 /**
651 * @ticket 199
652 */
653 public function testWithAnythingInsteadOfWithAnyParameters() 790 public function testWithAnythingInsteadOfWithAnyParameters()
654 { 791 {
655 $mock = $this->getMock('SomeClass', array('right'), array(), '', true, true, true); 792 $mock = $this->getMockBuilder(SomeClass::class)
793 ->setMethods(['right', 'wrong'])
794 ->getMock();
795
656 $mock->expects($this->once()) 796 $mock->expects($this->once())
657 ->method('right') 797 ->method('right')
658 ->with($this->anything()); 798 ->with($this->anything());
659 799
660 try { 800 try {
661 $mock->right(); 801 $mock->right();
662 $this->fail('Expected exception'); 802 $this->fail('Expected exception');
663 } catch (PHPUnit_Framework_ExpectationFailedException $e) { 803 } catch (ExpectationFailedException $e) {
664 $this->assertSame( 804 $this->assertSame(
665 "Expectation failed for method name is equal to <string:right> when invoked 1 time(s)\n" . 805 'Expectation failed for method name is equal to "right" when invoked 1 time(s)' . PHP_EOL .
666 "Parameter count for invocation SomeClass::right() is too low.\n" . 806 'Parameter count for invocation SomeClass::right() is too low.' . PHP_EOL .
667 "To allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead.", 807 'To allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead.',
668 $e->getMessage() 808 $e->getMessage()
669 ); 809 );
670 } 810 }
671 811
672 $this->resetMockObjects(); 812 $this->resetMockObjects();
676 * See https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81 816 * See https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81
677 */ 817 */
678 public function testMockArgumentsPassedByReference() 818 public function testMockArgumentsPassedByReference()
679 { 819 {
680 $foo = $this->getMockBuilder('MethodCallbackByReference') 820 $foo = $this->getMockBuilder('MethodCallbackByReference')
681 ->setMethods(array('bar')) 821 ->setMethods(['bar'])
682 ->disableOriginalConstructor() 822 ->disableOriginalConstructor()
683 ->disableArgumentCloning() 823 ->disableArgumentCloning()
684 ->getMock(); 824 ->getMock();
685 825
686 $foo->expects($this->any()) 826 $foo->expects($this->any())
687 ->method('bar') 827 ->method('bar')
688 ->will($this->returnCallback(array($foo, 'callback'))); 828 ->will($this->returnCallback([$foo, 'callback']));
689 829
690 $a = $b = $c = 0; 830 $a = $b = $c = 0;
691 831
692 $foo->bar($a, $b, $c); 832 $foo->bar($a, $b, $c);
693 833
718 858
719 $this->assertEquals(1, $b); 859 $this->assertEquals(1, $b);
720 } 860 }
721 861
722 /** 862 /**
723 * https://github.com/sebastianbergmann/phpunit-mock-objects/issues/116 863 * @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/116
724 */ 864 */
725 public function testMockArgumentsPassedByReference3() 865 public function testMockArgumentsPassedByReference3()
726 { 866 {
727 $foo = $this->getMockBuilder('MethodCallbackByReference') 867 $foo = $this->getMockBuilder('MethodCallbackByReference')
728 ->setMethods(array('bar')) 868 ->setMethods(['bar'])
729 ->disableOriginalConstructor() 869 ->disableOriginalConstructor()
730 ->disableArgumentCloning() 870 ->disableArgumentCloning()
731 ->getMock(); 871 ->getMock();
732 872
733 $a = new stdClass(); 873 $a = new stdClass;
734 $b = $c = 0; 874 $b = $c = 0;
735 875
736 $foo->expects($this->any()) 876 $foo->expects($this->any())
737 ->method('bar') 877 ->method('bar')
738 ->with($a, $b, $c) 878 ->with($a, $b, $c)
739 ->will($this->returnCallback(array($foo, 'callback'))); 879 ->will($this->returnCallback([$foo, 'callback']));
740 880
741 $foo->bar($a, $b, $c); 881 $this->assertNull($foo->bar($a, $b, $c));
742 } 882 }
743 883
744 /** 884 /**
745 * https://github.com/sebastianbergmann/phpunit/issues/796 885 * @see https://github.com/sebastianbergmann/phpunit/issues/796
746 */ 886 */
747 public function testMockArgumentsPassedByReference4() 887 public function testMockArgumentsPassedByReference4()
748 { 888 {
749 $foo = $this->getMockBuilder('MethodCallbackByReference') 889 $foo = $this->getMockBuilder('MethodCallbackByReference')
750 ->setMethods(array('bar')) 890 ->setMethods(['bar'])
751 ->disableOriginalConstructor() 891 ->disableOriginalConstructor()
752 ->disableArgumentCloning() 892 ->disableArgumentCloning()
753 ->getMock(); 893 ->getMock();
754 894
755 $a = new stdClass(); 895 $a = new stdClass;
756 $b = $c = 0; 896 $b = $c = 0;
757 897
758 $foo->expects($this->any()) 898 $foo->expects($this->any())
759 ->method('bar') 899 ->method('bar')
760 ->with($this->isInstanceOf("stdClass"), $b, $c) 900 ->with($this->isInstanceOf(stdClass::class), $b, $c)
761 ->will($this->returnCallback(array($foo, 'callback'))); 901 ->will($this->returnCallback([$foo, 'callback']));
762 902
763 $foo->bar($a, $b, $c); 903 $this->assertNull($foo->bar($a, $b, $c));
764 } 904 }
765 905
766 /** 906 /**
767 * @requires extension soap 907 * @requires extension soap
768 */ 908 */
769 public function testCreateMockFromWsdl() 909 public function testCreateMockFromWsdl()
770 { 910 {
771 $mock = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl', 'WsdlMock'); 911 $mock = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl', 'WsdlMock');
912
772 $this->assertStringStartsWith( 913 $this->assertStringStartsWith(
773 'Mock_WsdlMock_', 914 'Mock_WsdlMock_',
774 get_class($mock) 915 get_class($mock)
775 ); 916 );
776 } 917 }
779 * @requires extension soap 920 * @requires extension soap
780 */ 921 */
781 public function testCreateNamespacedMockFromWsdl() 922 public function testCreateNamespacedMockFromWsdl()
782 { 923 {
783 $mock = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl', 'My\\Space\\WsdlMock'); 924 $mock = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl', 'My\\Space\\WsdlMock');
925
784 $this->assertStringStartsWith( 926 $this->assertStringStartsWith(
785 'Mock_WsdlMock_', 927 'Mock_WsdlMock_',
786 get_class($mock) 928 get_class($mock)
787 ); 929 );
788 } 930 }
790 /** 932 /**
791 * @requires extension soap 933 * @requires extension soap
792 */ 934 */
793 public function testCreateTwoMocksOfOneWsdlFile() 935 public function testCreateTwoMocksOfOneWsdlFile()
794 { 936 {
795 $mock = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl'); 937 $a = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl');
796 $mock = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl'); 938 $b = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl');
939
940 $this->assertStringStartsWith('Mock_GoogleSearch_', get_class($a));
941 $this->assertEquals(get_class($a), get_class($b));
797 } 942 }
798 943
799 /** 944 /**
800 * @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/156 945 * @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/156
801 * @ticket 156 946 * @ticket 156
802 */ 947 */
803 public function testInterfaceWithStaticMethodCanBeStubbed() 948 public function testInterfaceWithStaticMethodCanBeStubbed()
804 { 949 {
805 $this->assertInstanceOf( 950 $this->assertInstanceOf(
806 'InterfaceWithStaticMethod', 951 InterfaceWithStaticMethod::class,
807 $this->getMock('InterfaceWithStaticMethod') 952 $this->getMockBuilder(InterfaceWithStaticMethod::class)->getMock()
808 ); 953 );
809 } 954 }
810 955
811 /**
812 * @expectedException PHPUnit_Framework_MockObject_BadMethodCallException
813 */
814 public function testInvokingStubbedStaticMethodRaisesException() 956 public function testInvokingStubbedStaticMethodRaisesException()
815 { 957 {
816 $mock = $this->getMock('ClassWithStaticMethod'); 958 $mock = $this->getMockBuilder(ClassWithStaticMethod::class)->getMock();
959
960 $this->expectException(\PHPUnit\Framework\MockObject\BadMethodCallException::class);
961
817 $mock->staticMethod(); 962 $mock->staticMethod();
818 } 963 }
819 964
820 /** 965 /**
821 * @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/171 966 * @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/171
822 * @ticket 171 967 * @ticket 171
823 */ 968 */
824 public function testStubForClassThatImplementsSerializableCanBeCreatedWithoutInvokingTheConstructor() 969 public function testStubForClassThatImplementsSerializableCanBeCreatedWithoutInvokingTheConstructor()
825 { 970 {
826 $this->assertInstanceOf( 971 $this->assertInstanceOf(
827 'ClassThatImplementsSerializable', 972 ClassThatImplementsSerializable::class,
828 $this->getMockBuilder('ClassThatImplementsSerializable') 973 $this->getMockBuilder(ClassThatImplementsSerializable::class)
829 ->disableOriginalConstructor() 974 ->disableOriginalConstructor()
830 ->getMock() 975 ->getMock()
976 );
977 }
978
979 public function testGetMockForClassWithSelfTypeHint()
980 {
981 $this->assertInstanceOf(
982 ClassWithSelfTypeHint::class,
983 $this->getMockBuilder(ClassWithSelfTypeHint::class)->getMock()
831 ); 984 );
832 } 985 }
833 986
834 private function resetMockObjects() 987 private function resetMockObjects()
835 { 988 {
836 $refl = new ReflectionObject($this); 989 $refl = new ReflectionObject($this);
837 $refl = $refl->getParentClass(); 990 $refl = $refl->getParentClass();
838 $prop = $refl->getProperty('mockObjects'); 991 $prop = $refl->getProperty('mockObjects');
839 $prop->setAccessible(true); 992 $prop->setAccessible(true);
840 $prop->setValue($this, array()); 993 $prop->setValue($this, []);
994 }
995
996 public function testStringableClassDoesNotThrow()
997 {
998 $mock = $this->getMockBuilder(StringableClass::class)->getMock();
999
1000 $this->assertInternalType('string', (string) $mock);
1001 }
1002
1003 public function testStringableClassCanBeMocked()
1004 {
1005 $mock = $this->getMockBuilder(StringableClass::class)->getMock();
1006
1007 $mock->method('__toString')->willReturn('foo');
1008
1009 $this->assertSame('foo', (string) $mock);
1010 }
1011
1012 public function traversableProvider()
1013 {
1014 return [
1015 ['Traversable'],
1016 ['\Traversable'],
1017 ['TraversableMockTestInterface'],
1018 [['Traversable']],
1019 [['Iterator','Traversable']],
1020 [['\Iterator','\Traversable']]
1021 ];
1022 }
1023
1024 public function testParameterCallbackConstraintOnlyEvaluatedOnce()
1025 {
1026 $mock = $this->getMockBuilder(Foo::class)->setMethods(['bar'])->getMock();
1027 $expectedNumberOfCalls = 1;
1028 $callCount = 0;
1029
1030 $mock->expects($this->exactly($expectedNumberOfCalls))->method('bar')
1031 ->with($this->callback(function ($argument) use (&$callCount) {
1032 return $argument === 'call_' . $callCount++;
1033 }));
1034
1035 for ($i = 0; $i < $expectedNumberOfCalls; $i++) {
1036 $mock->bar('call_' . $i);
1037 }
1038 }
1039
1040 public function testReturnTypesAreMockedCorrectly()
1041 {
1042 /** @var ClassWithAllPossibleReturnTypes|MockObject $stub */
1043 $stub = $this->createMock(ClassWithAllPossibleReturnTypes::class);
1044
1045 $this->assertNull($stub->methodWithNoReturnTypeDeclaration());
1046 $this->assertSame('', $stub->methodWithStringReturnTypeDeclaration());
1047 $this->assertSame(0.0, $stub->methodWithFloatReturnTypeDeclaration());
1048 $this->assertSame(0, $stub->methodWithIntReturnTypeDeclaration());
1049 $this->assertFalse($stub->methodWithBoolReturnTypeDeclaration());
1050 $this->assertSame([], $stub->methodWithArrayReturnTypeDeclaration());
1051 $this->assertInstanceOf(MockObject::class, $stub->methodWithClassReturnTypeDeclaration());
1052 }
1053
1054 /**
1055 * @requires PHP 7.1
1056 */
1057 public function testVoidReturnTypeIsMockedCorrectly()
1058 {
1059 /** @var ClassWithAllPossibleReturnTypes|MockObject $stub */
1060 $stub = $this->createMock(ClassWithAllPossibleReturnTypes::class);
1061
1062 $this->assertNull($stub->methodWithVoidReturnTypeDeclaration());
1063 }
1064
1065 /**
1066 * @requires PHP 7.2
1067 */
1068 public function testObjectReturnTypeIsMockedCorrectly()
1069 {
1070 /** @var ClassWithAllPossibleReturnTypes|MockObject $stub */
1071 $stub = $this->createMock(ClassWithAllPossibleReturnTypes::class);
1072
1073 $this->assertInstanceOf(stdClass::class, $stub->methodWithObjectReturnTypeDeclaration());
841 } 1074 }
842 } 1075 }