MockObjectTest.php (27475B)
1 <?php 2 /* 3 * This file is part of the PHPUnit_MockObject package. 4 * 5 * (c) Sebastian Bergmann <sebastian@phpunit.de> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11 /** 12 * 13 * 14 * @since Class available since Release 3.0.0 15 */ 16 class Framework_MockObjectTest extends PHPUnit_Framework_TestCase 17 { 18 public function testMockedMethodIsNeverCalled() 19 { 20 $mock = $this->getMock('AnInterface'); 21 $mock->expects($this->never()) 22 ->method('doSomething'); 23 } 24 25 public function testMockedMethodIsNeverCalledWithParameter() 26 { 27 $mock = $this->getMock('SomeClass'); 28 $mock->expects($this->never()) 29 ->method('doSomething') 30 ->with('someArg'); 31 } 32 33 public function testMockedMethodIsNotCalledWhenExpectsAnyWithParameter() 34 { 35 $mock = $this->getMock('SomeClass'); 36 $mock->expects($this->any()) 37 ->method('doSomethingElse') 38 ->with('someArg'); 39 } 40 41 public function testMockedMethodIsNotCalledWhenMethodSpecifiedDirectlyWithParameter() 42 { 43 $mock = $this->getMock('SomeClass'); 44 $mock->method('doSomethingElse') 45 ->with('someArg'); 46 } 47 48 public function testMockedMethodIsCalledAtLeastOnce() 49 { 50 $mock = $this->getMock('AnInterface'); 51 $mock->expects($this->atLeastOnce()) 52 ->method('doSomething'); 53 54 $mock->doSomething(); 55 } 56 57 public function testMockedMethodIsCalledAtLeastOnce2() 58 { 59 $mock = $this->getMock('AnInterface'); 60 $mock->expects($this->atLeastOnce()) 61 ->method('doSomething'); 62 63 $mock->doSomething(); 64 $mock->doSomething(); 65 } 66 67 public function testMockedMethodIsCalledAtLeastTwice() 68 { 69 $mock = $this->getMock('AnInterface'); 70 $mock->expects($this->atLeast(2)) 71 ->method('doSomething'); 72 73 $mock->doSomething(); 74 $mock->doSomething(); 75 } 76 77 public function testMockedMethodIsCalledAtLeastTwice2() 78 { 79 $mock = $this->getMock('AnInterface'); 80 $mock->expects($this->atLeast(2)) 81 ->method('doSomething'); 82 83 $mock->doSomething(); 84 $mock->doSomething(); 85 $mock->doSomething(); 86 } 87 88 public function testMockedMethodIsCalledAtMostTwice() 89 { 90 $mock = $this->getMock('AnInterface'); 91 $mock->expects($this->atMost(2)) 92 ->method('doSomething'); 93 94 $mock->doSomething(); 95 $mock->doSomething(); 96 } 97 98 public function testMockedMethodIsCalledAtMosttTwice2() 99 { 100 $mock = $this->getMock('AnInterface'); 101 $mock->expects($this->atMost(2)) 102 ->method('doSomething'); 103 104 $mock->doSomething(); 105 } 106 107 public function testMockedMethodIsCalledOnce() 108 { 109 $mock = $this->getMock('AnInterface'); 110 $mock->expects($this->once()) 111 ->method('doSomething'); 112 113 $mock->doSomething(); 114 } 115 116 public function testMockedMethodIsCalledOnceWithParameter() 117 { 118 $mock = $this->getMock('SomeClass'); 119 $mock->expects($this->once()) 120 ->method('doSomethingElse') 121 ->with($this->equalTo('something')); 122 123 $mock->doSomethingElse('something'); 124 } 125 126 public function testMockedMethodIsCalledExactly() 127 { 128 $mock = $this->getMock('AnInterface'); 129 $mock->expects($this->exactly(2)) 130 ->method('doSomething'); 131 132 $mock->doSomething(); 133 $mock->doSomething(); 134 } 135 136 public function testStubbedException() 137 { 138 $mock = $this->getMock('AnInterface'); 139 $mock->expects($this->any()) 140 ->method('doSomething') 141 ->will($this->throwException(new Exception)); 142 143 try { 144 $mock->doSomething(); 145 } catch (Exception $e) { 146 return; 147 } 148 149 $this->fail(); 150 } 151 152 public function testStubbedWillThrowException() 153 { 154 $mock = $this->getMock('AnInterface'); 155 $mock->expects($this->any()) 156 ->method('doSomething') 157 ->willThrowException(new Exception); 158 159 try { 160 $mock->doSomething(); 161 } catch (Exception $e) { 162 return; 163 } 164 165 $this->fail(); 166 } 167 168 public function testStubbedReturnValue() 169 { 170 $mock = $this->getMock('AnInterface'); 171 $mock->expects($this->any()) 172 ->method('doSomething') 173 ->will($this->returnValue('something')); 174 175 $this->assertEquals('something', $mock->doSomething()); 176 177 $mock = $this->getMock('AnInterface'); 178 $mock->expects($this->any()) 179 ->method('doSomething') 180 ->willReturn('something'); 181 182 $this->assertEquals('something', $mock->doSomething()); 183 } 184 185 public function testStubbedReturnValueMap() 186 { 187 $map = array( 188 array('a', 'b', 'c', 'd'), 189 array('e', 'f', 'g', 'h') 190 ); 191 192 $mock = $this->getMock('AnInterface'); 193 $mock->expects($this->any()) 194 ->method('doSomething') 195 ->will($this->returnValueMap($map)); 196 197 $this->assertEquals('d', $mock->doSomething('a', 'b', 'c')); 198 $this->assertEquals('h', $mock->doSomething('e', 'f', 'g')); 199 $this->assertEquals(null, $mock->doSomething('foo', 'bar')); 200 201 $mock = $this->getMock('AnInterface'); 202 $mock->expects($this->any()) 203 ->method('doSomething') 204 ->willReturnMap($map); 205 206 $this->assertEquals('d', $mock->doSomething('a', 'b', 'c')); 207 $this->assertEquals('h', $mock->doSomething('e', 'f', 'g')); 208 $this->assertEquals(null, $mock->doSomething('foo', 'bar')); 209 } 210 211 public function testStubbedReturnArgument() 212 { 213 $mock = $this->getMock('AnInterface'); 214 $mock->expects($this->any()) 215 ->method('doSomething') 216 ->will($this->returnArgument(1)); 217 218 $this->assertEquals('b', $mock->doSomething('a', 'b')); 219 220 $mock = $this->getMock('AnInterface'); 221 $mock->expects($this->any()) 222 ->method('doSomething') 223 ->willReturnArgument(1); 224 225 $this->assertEquals('b', $mock->doSomething('a', 'b')); 226 } 227 228 public function testFunctionCallback() 229 { 230 $mock = $this->getMock('SomeClass', array('doSomething'), array(), '', false); 231 $mock->expects($this->once()) 232 ->method('doSomething') 233 ->will($this->returnCallback('functionCallback')); 234 235 $this->assertEquals('pass', $mock->doSomething('foo', 'bar')); 236 237 $mock = $this->getMock('SomeClass', array('doSomething'), array(), '', false); 238 $mock->expects($this->once()) 239 ->method('doSomething') 240 ->willReturnCallback('functionCallback'); 241 242 $this->assertEquals('pass', $mock->doSomething('foo', 'bar')); 243 } 244 245 public function testStubbedReturnSelf() 246 { 247 $mock = $this->getMock('AnInterface'); 248 $mock->expects($this->any()) 249 ->method('doSomething') 250 ->will($this->returnSelf()); 251 252 $this->assertEquals($mock, $mock->doSomething()); 253 254 $mock = $this->getMock('AnInterface'); 255 $mock->expects($this->any()) 256 ->method('doSomething') 257 ->willReturnSelf(); 258 259 $this->assertEquals($mock, $mock->doSomething()); 260 } 261 262 public function testStubbedReturnOnConsecutiveCalls() 263 { 264 $mock = $this->getMock('AnInterface'); 265 $mock->expects($this->any()) 266 ->method('doSomething') 267 ->will($this->onConsecutiveCalls('a', 'b', 'c')); 268 269 $this->assertEquals('a', $mock->doSomething()); 270 $this->assertEquals('b', $mock->doSomething()); 271 $this->assertEquals('c', $mock->doSomething()); 272 273 $mock = $this->getMock('AnInterface'); 274 $mock->expects($this->any()) 275 ->method('doSomething') 276 ->willReturnOnConsecutiveCalls('a', 'b', 'c'); 277 278 $this->assertEquals('a', $mock->doSomething()); 279 $this->assertEquals('b', $mock->doSomething()); 280 $this->assertEquals('c', $mock->doSomething()); 281 } 282 283 public function testStaticMethodCallback() 284 { 285 $mock = $this->getMock('SomeClass', array('doSomething'), array(), '', false); 286 $mock->expects($this->once()) 287 ->method('doSomething') 288 ->will($this->returnCallback(array('MethodCallback', 'staticCallback'))); 289 290 $this->assertEquals('pass', $mock->doSomething('foo', 'bar')); 291 } 292 293 public function testPublicMethodCallback() 294 { 295 $mock = $this->getMock('SomeClass', array('doSomething'), array(), '', false); 296 $mock->expects($this->once()) 297 ->method('doSomething') 298 ->will($this->returnCallback(array(new MethodCallback, 'nonStaticCallback'))); 299 300 $this->assertEquals('pass', $mock->doSomething('foo', 'bar')); 301 } 302 303 public function testMockClassOnlyGeneratedOnce() 304 { 305 $mock1 = $this->getMock('AnInterface'); 306 $mock2 = $this->getMock('AnInterface'); 307 308 $this->assertEquals(get_class($mock1), get_class($mock2)); 309 } 310 311 public function testMockClassDifferentForPartialMocks() 312 { 313 $mock1 = $this->getMock('PartialMockTestClass'); 314 $mock2 = $this->getMock('PartialMockTestClass', array('doSomething')); 315 $mock3 = $this->getMock('PartialMockTestClass', array('doSomething')); 316 $mock4 = $this->getMock('PartialMockTestClass', array('doAnotherThing')); 317 $mock5 = $this->getMock('PartialMockTestClass', array('doAnotherThing')); 318 319 $this->assertNotEquals(get_class($mock1), get_class($mock2)); 320 $this->assertNotEquals(get_class($mock1), get_class($mock3)); 321 $this->assertNotEquals(get_class($mock1), get_class($mock4)); 322 $this->assertNotEquals(get_class($mock1), get_class($mock5)); 323 $this->assertEquals(get_class($mock2), get_class($mock3)); 324 $this->assertNotEquals(get_class($mock2), get_class($mock4)); 325 $this->assertNotEquals(get_class($mock2), get_class($mock5)); 326 $this->assertEquals(get_class($mock4), get_class($mock5)); 327 } 328 329 public function testMockClassStoreOverrulable() 330 { 331 $mock1 = $this->getMock('PartialMockTestClass'); 332 $mock2 = $this->getMock('PartialMockTestClass', array(), array(), 'MyMockClassNameForPartialMockTestClass1'); 333 $mock3 = $this->getMock('PartialMockTestClass'); 334 $mock4 = $this->getMock('PartialMockTestClass', array('doSomething'), array(), 'AnotherMockClassNameForPartialMockTestClass'); 335 $mock5 = $this->getMock('PartialMockTestClass', array(), array(), 'MyMockClassNameForPartialMockTestClass2'); 336 337 $this->assertNotEquals(get_class($mock1), get_class($mock2)); 338 $this->assertEquals(get_class($mock1), get_class($mock3)); 339 $this->assertNotEquals(get_class($mock1), get_class($mock4)); 340 $this->assertNotEquals(get_class($mock2), get_class($mock3)); 341 $this->assertNotEquals(get_class($mock2), get_class($mock4)); 342 $this->assertNotEquals(get_class($mock2), get_class($mock5)); 343 $this->assertNotEquals(get_class($mock3), get_class($mock4)); 344 $this->assertNotEquals(get_class($mock3), get_class($mock5)); 345 $this->assertNotEquals(get_class($mock4), get_class($mock5)); 346 } 347 348 /** 349 * @covers PHPUnit_Framework_MockObject_Generator::getMock 350 */ 351 public function testGetMockWithFixedClassNameCanProduceTheSameMockTwice() 352 { 353 $mock = $this->getMockBuilder('StdClass')->setMockClassName('FixedName')->getMock(); 354 $mock = $this->getMockBuilder('StdClass')->setMockClassName('FixedName')->getMock(); 355 $this->assertInstanceOf('StdClass', $mock); 356 } 357 358 public function testOriginalConstructorSettingConsidered() 359 { 360 $mock1 = $this->getMock('PartialMockTestClass'); 361 $mock2 = $this->getMock('PartialMockTestClass', array(), array(), '', false); 362 363 $this->assertTrue($mock1->constructorCalled); 364 $this->assertFalse($mock2->constructorCalled); 365 } 366 367 public function testOriginalCloneSettingConsidered() 368 { 369 $mock1 = $this->getMock('PartialMockTestClass'); 370 $mock2 = $this->getMock('PartialMockTestClass', array(), array(), '', true, false); 371 372 $this->assertNotEquals(get_class($mock1), get_class($mock2)); 373 } 374 375 public function testGetMockForAbstractClass() 376 { 377 $mock = $this->getMock('AbstractMockTestClass'); 378 $mock->expects($this->never()) 379 ->method('doSomething'); 380 } 381 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 /** 395 * @dataProvider traversableProvider 396 */ 397 public function testGetMockForTraversable($type) 398 { 399 $mock = $this->getMock($type); 400 $this->assertInstanceOf('Traversable', $mock); 401 } 402 403 public function testMultipleInterfacesCanBeMockedInSingleObject() 404 { 405 $mock = $this->getMock(array('AnInterface', 'AnotherInterface')); 406 $this->assertInstanceOf('AnInterface', $mock); 407 $this->assertInstanceOf('AnotherInterface', $mock); 408 } 409 410 /** 411 * @requires PHP 5.4.0 412 */ 413 public function testGetMockForTrait() 414 { 415 $mock = $this->getMockForTrait('AbstractTrait'); 416 $mock->expects($this->never())->method('doSomething'); 417 418 $parent = get_parent_class($mock); 419 $traits = class_uses($parent, false); 420 421 $this->assertContains('AbstractTrait', $traits); 422 } 423 424 public function testClonedMockObjectShouldStillEqualTheOriginal() 425 { 426 $a = $this->getMock('stdClass'); 427 $b = clone $a; 428 $this->assertEquals($a, $b); 429 } 430 431 public function testMockObjectsConstructedIndepentantlyShouldBeEqual() 432 { 433 $a = $this->getMock('stdClass'); 434 $b = $this->getMock('stdClass'); 435 $this->assertEquals($a, $b); 436 } 437 438 public function testMockObjectsConstructedIndepentantlyShouldNotBeTheSame() 439 { 440 $a = $this->getMock('stdClass'); 441 $b = $this->getMock('stdClass'); 442 $this->assertNotSame($a, $b); 443 } 444 445 public function testClonedMockObjectCanBeUsedInPlaceOfOriginalOne() 446 { 447 $x = $this->getMock('stdClass'); 448 $y = clone $x; 449 450 $mock = $this->getMock('stdClass', array('foo')); 451 $mock->expects($this->once())->method('foo')->with($this->equalTo($x)); 452 $mock->foo($y); 453 } 454 455 public function testClonedMockObjectIsNotIdenticalToOriginalOne() 456 { 457 $x = $this->getMock('stdClass'); 458 $y = clone $x; 459 460 $mock = $this->getMock('stdClass', array('foo')); 461 $mock->expects($this->once())->method('foo')->with($this->logicalNot($this->identicalTo($x))); 462 $mock->foo($y); 463 } 464 465 public function testObjectMethodCallWithArgumentCloningEnabled() 466 { 467 $expectedObject = new StdClass; 468 469 $mock = $this->getMockBuilder('SomeClass') 470 ->setMethods(array('doSomethingElse')) 471 ->enableArgumentCloning() 472 ->getMock(); 473 474 $actualArguments = array(); 475 476 $mock->expects($this->any()) 477 ->method('doSomethingElse') 478 ->will($this->returnCallback(function () use (&$actualArguments) { 479 $actualArguments = func_get_args(); 480 })); 481 482 $mock->doSomethingElse($expectedObject); 483 484 $this->assertEquals(1, count($actualArguments)); 485 $this->assertEquals($expectedObject, $actualArguments[0]); 486 $this->assertNotSame($expectedObject, $actualArguments[0]); 487 } 488 489 public function testObjectMethodCallWithArgumentCloningDisabled() 490 { 491 $expectedObject = new StdClass; 492 493 $mock = $this->getMockBuilder('SomeClass') 494 ->setMethods(array('doSomethingElse')) 495 ->disableArgumentCloning() 496 ->getMock(); 497 498 $actualArguments = array(); 499 500 $mock->expects($this->any()) 501 ->method('doSomethingElse') 502 ->will($this->returnCallback(function () use (&$actualArguments) { 503 $actualArguments = func_get_args(); 504 })); 505 506 $mock->doSomethingElse($expectedObject); 507 508 $this->assertEquals(1, count($actualArguments)); 509 $this->assertSame($expectedObject, $actualArguments[0]); 510 } 511 512 public function testArgumentCloningOptionGeneratesUniqueMock() 513 { 514 $mockWithCloning = $this->getMockBuilder('SomeClass') 515 ->setMethods(array('doSomethingElse')) 516 ->enableArgumentCloning() 517 ->getMock(); 518 519 $mockWithoutCloning = $this->getMockBuilder('SomeClass') 520 ->setMethods(array('doSomethingElse')) 521 ->disableArgumentCloning() 522 ->getMock(); 523 524 $this->assertNotEquals($mockWithCloning, $mockWithoutCloning); 525 } 526 527 public function testVerificationOfMethodNameFailsWithoutParameters() 528 { 529 $mock = $this->getMock('SomeClass', array('right', 'wrong'), array(), '', true, true, true); 530 $mock->expects($this->once()) 531 ->method('right'); 532 533 $mock->wrong(); 534 try { 535 $mock->__phpunit_verify(); 536 $this->fail('Expected exception'); 537 } catch (PHPUnit_Framework_ExpectationFailedException $e) { 538 $this->assertSame( 539 "Expectation failed for method name is equal to <string:right> when invoked 1 time(s).\n" 540 . "Method was expected to be called 1 times, actually called 0 times.\n", 541 $e->getMessage() 542 ); 543 } 544 545 $this->resetMockObjects(); 546 } 547 548 public function testVerificationOfMethodNameFailsWithParameters() 549 { 550 $mock = $this->getMock('SomeClass', array('right', 'wrong'), array(), '', true, true, true); 551 $mock->expects($this->once()) 552 ->method('right'); 553 554 $mock->wrong(); 555 try { 556 $mock->__phpunit_verify(); 557 $this->fail('Expected exception'); 558 } catch (PHPUnit_Framework_ExpectationFailedException $e) { 559 $this->assertSame( 560 "Expectation failed for method name is equal to <string:right> when invoked 1 time(s).\n" 561 . "Method was expected to be called 1 times, actually called 0 times.\n", 562 $e->getMessage() 563 ); 564 } 565 566 $this->resetMockObjects(); 567 } 568 569 public function testVerificationOfMethodNameFailsWithWrongParameters() 570 { 571 $mock = $this->getMock('SomeClass', array('right', 'wrong'), array(), '', true, true, true); 572 $mock->expects($this->once()) 573 ->method('right') 574 ->with(array('first', 'second')); 575 576 try { 577 $mock->right(array('second')); 578 } catch (PHPUnit_Framework_ExpectationFailedException $e) { 579 $this->assertSame( 580 "Expectation failed for method name is equal to <string:right> when invoked 1 time(s)\n" 581 . "Parameter 0 for invocation SomeClass::right(Array (...)) does not match expected value.\n" 582 . "Failed asserting that two arrays are equal.", 583 $e->getMessage() 584 ); 585 } 586 587 try { 588 $mock->__phpunit_verify(); 589 $this->fail('Expected exception'); 590 } catch (PHPUnit_Framework_ExpectationFailedException $e) { 591 $this->assertSame( 592 "Expectation failed for method name is equal to <string:right> when invoked 1 time(s).\n" 593 . "Parameter 0 for invocation SomeClass::right(Array (...)) does not match expected value.\n" 594 . "Failed asserting that two arrays are equal.\n" 595 . "--- Expected\n" 596 . "+++ Actual\n" 597 . "@@ @@\n" 598 . " Array (\n" 599 . "- 0 => 'first'\n" 600 . "- 1 => 'second'\n" 601 . "+ 0 => 'second'\n" 602 . " )\n", 603 $e->getMessage() 604 ); 605 } 606 607 $this->resetMockObjects(); 608 } 609 610 public function testVerificationOfNeverFailsWithEmptyParameters() 611 { 612 $mock = $this->getMock('SomeClass', array('right', 'wrong'), array(), '', true, true, true); 613 $mock->expects($this->never()) 614 ->method('right') 615 ->with(); 616 617 try { 618 $mock->right(); 619 $this->fail('Expected exception'); 620 } catch (PHPUnit_Framework_ExpectationFailedException $e) { 621 $this->assertSame( 622 'SomeClass::right() was not expected to be called.', 623 $e->getMessage() 624 ); 625 } 626 627 $this->resetMockObjects(); 628 } 629 630 public function testVerificationOfNeverFailsWithAnyParameters() 631 { 632 $mock = $this->getMock('SomeClass', array('right', 'wrong'), array(), '', true, true, true); 633 $mock->expects($this->never()) 634 ->method('right') 635 ->withAnyParameters(); 636 637 try { 638 $mock->right(); 639 $this->fail('Expected exception'); 640 } catch (PHPUnit_Framework_ExpectationFailedException $e) { 641 $this->assertSame( 642 'SomeClass::right() was not expected to be called.', 643 $e->getMessage() 644 ); 645 } 646 647 $this->resetMockObjects(); 648 } 649 650 /** 651 * @ticket 199 652 */ 653 public function testWithAnythingInsteadOfWithAnyParameters() 654 { 655 $mock = $this->getMock('SomeClass', array('right'), array(), '', true, true, true); 656 $mock->expects($this->once()) 657 ->method('right') 658 ->with($this->anything()); 659 660 try { 661 $mock->right(); 662 $this->fail('Expected exception'); 663 } catch (PHPUnit_Framework_ExpectationFailedException $e) { 664 $this->assertSame( 665 "Expectation failed for method name is equal to <string:right> when invoked 1 time(s)\n" . 666 "Parameter count for invocation SomeClass::right() is too low.\n" . 667 "To allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead.", 668 $e->getMessage() 669 ); 670 } 671 672 $this->resetMockObjects(); 673 } 674 675 /** 676 * See https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81 677 */ 678 public function testMockArgumentsPassedByReference() 679 { 680 $foo = $this->getMockBuilder('MethodCallbackByReference') 681 ->setMethods(array('bar')) 682 ->disableOriginalConstructor() 683 ->disableArgumentCloning() 684 ->getMock(); 685 686 $foo->expects($this->any()) 687 ->method('bar') 688 ->will($this->returnCallback(array($foo, 'callback'))); 689 690 $a = $b = $c = 0; 691 692 $foo->bar($a, $b, $c); 693 694 $this->assertEquals(1, $b); 695 } 696 697 /** 698 * See https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81 699 */ 700 public function testMockArgumentsPassedByReference2() 701 { 702 $foo = $this->getMockBuilder('MethodCallbackByReference') 703 ->disableOriginalConstructor() 704 ->disableArgumentCloning() 705 ->getMock(); 706 707 $foo->expects($this->any()) 708 ->method('bar') 709 ->will($this->returnCallback( 710 function (&$a, &$b, $c) { 711 $b = 1; 712 } 713 )); 714 715 $a = $b = $c = 0; 716 717 $foo->bar($a, $b, $c); 718 719 $this->assertEquals(1, $b); 720 } 721 722 /** 723 * https://github.com/sebastianbergmann/phpunit-mock-objects/issues/116 724 */ 725 public function testMockArgumentsPassedByReference3() 726 { 727 $foo = $this->getMockBuilder('MethodCallbackByReference') 728 ->setMethods(array('bar')) 729 ->disableOriginalConstructor() 730 ->disableArgumentCloning() 731 ->getMock(); 732 733 $a = new stdClass(); 734 $b = $c = 0; 735 736 $foo->expects($this->any()) 737 ->method('bar') 738 ->with($a, $b, $c) 739 ->will($this->returnCallback(array($foo, 'callback'))); 740 741 $foo->bar($a, $b, $c); 742 } 743 744 /** 745 * https://github.com/sebastianbergmann/phpunit/issues/796 746 */ 747 public function testMockArgumentsPassedByReference4() 748 { 749 $foo = $this->getMockBuilder('MethodCallbackByReference') 750 ->setMethods(array('bar')) 751 ->disableOriginalConstructor() 752 ->disableArgumentCloning() 753 ->getMock(); 754 755 $a = new stdClass(); 756 $b = $c = 0; 757 758 $foo->expects($this->any()) 759 ->method('bar') 760 ->with($this->isInstanceOf("stdClass"), $b, $c) 761 ->will($this->returnCallback(array($foo, 'callback'))); 762 763 $foo->bar($a, $b, $c); 764 } 765 766 /** 767 * @requires extension soap 768 */ 769 public function testCreateMockFromWsdl() 770 { 771 $mock = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl', 'WsdlMock'); 772 $this->assertStringStartsWith( 773 'Mock_WsdlMock_', 774 get_class($mock) 775 ); 776 } 777 778 /** 779 * @requires extension soap 780 */ 781 public function testCreateNamespacedMockFromWsdl() 782 { 783 $mock = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl', 'My\\Space\\WsdlMock'); 784 $this->assertStringStartsWith( 785 'Mock_WsdlMock_', 786 get_class($mock) 787 ); 788 } 789 790 /** 791 * @requires extension soap 792 */ 793 public function testCreateTwoMocksOfOneWsdlFile() 794 { 795 $mock = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl'); 796 $mock = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl'); 797 } 798 799 /** 800 * @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/156 801 * @ticket 156 802 */ 803 public function testInterfaceWithStaticMethodCanBeStubbed() 804 { 805 $this->assertInstanceOf( 806 'InterfaceWithStaticMethod', 807 $this->getMock('InterfaceWithStaticMethod') 808 ); 809 } 810 811 /** 812 * @expectedException PHPUnit_Framework_MockObject_BadMethodCallException 813 */ 814 public function testInvokingStubbedStaticMethodRaisesException() 815 { 816 $mock = $this->getMock('ClassWithStaticMethod'); 817 $mock->staticMethod(); 818 } 819 820 /** 821 * @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/171 822 * @ticket 171 823 */ 824 public function testStubForClassThatImplementsSerializableCanBeCreatedWithoutInvokingTheConstructor() 825 { 826 $this->assertInstanceOf( 827 'ClassThatImplementsSerializable', 828 $this->getMockBuilder('ClassThatImplementsSerializable') 829 ->disableOriginalConstructor() 830 ->getMock() 831 ); 832 } 833 834 private function resetMockObjects() 835 { 836 $refl = new ReflectionObject($this); 837 $refl = $refl->getParentClass(); 838 $prop = $refl->getProperty('mockObjects'); 839 $prop->setAccessible(true); 840 $prop->setValue($this, array()); 841 } 842 }