annotate vendor/phpunit/phpunit-mock-objects/tests/MockObjectTest.php @ 0:4c8ae668cc8c

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