How to use foo method of mock class

Best Atoum code snippet using mock.foo

ExpectationTest.php

Source:ExpectationTest.php Github

copy

Full Screen

...34 \Mockery::getConfiguration()->allowMockingNonExistentMethods(true);35 }36 public function testReturnsNullWhenNoArgs()37 {38 $this->mock->shouldReceive('foo');39 $this->assertNull($this->mock->foo());40 }41 public function testReturnsNullWhenSingleArg()42 {43 $this->mock->shouldReceive('foo');44 $this->assertNull($this->mock->foo(1));45 }46 public function testReturnsNullWhenManyArgs()47 {48 $this->mock->shouldReceive('foo');49 $this->assertNull($this->mock->foo('foo', array(), new stdClass));50 }51 public function testReturnsNullIfNullIsReturnValue()52 {53 $this->mock->shouldReceive('foo')->andReturn(null);54 $this->assertNull($this->mock->foo());55 }56 public function testReturnsNullForMockedExistingClassIfAndreturnnullCalled()57 {58 $mock = mock('MockeryTest_Foo');59 $mock->shouldReceive('foo')->andReturn(null);60 $this->assertNull($mock->foo());61 }62 public function testReturnsNullForMockedExistingClassIfNullIsReturnValue()63 {64 $mock = mock('MockeryTest_Foo');65 $mock->shouldReceive('foo')->andReturnNull();66 $this->assertNull($mock->foo());67 }68 public function testReturnsSameValueForAllIfNoArgsExpectationAndNoneGiven()69 {70 $this->mock->shouldReceive('foo')->andReturn(1);71 $this->assertEquals(1, $this->mock->foo());72 }73 public function testSetsPublicPropertyWhenRequested()74 {75 $this->mock->bar = null;76 $this->mock->shouldReceive('foo')->andSet('bar', 'baz');77 $this->assertNull($this->mock->bar);78 $this->mock->foo();79 $this->assertEquals('baz', $this->mock->bar);80 }81 public function testSetsPublicPropertyWhenRequestedUsingAlias()82 {83 $this->mock->bar = null;84 $this->mock->shouldReceive('foo')->set('bar', 'baz');85 $this->assertNull($this->mock->bar);86 $this->mock->foo();87 $this->assertEquals('baz', $this->mock->bar);88 }89 public function testSetsPublicPropertiesWhenRequested()90 {91 $this->mock->bar = null;92 $this->mock->shouldReceive('foo')->andSet('bar', 'baz', 'bazz', 'bazzz');93 $this->assertNull($this->mock->bar);94 $this->mock->foo();95 $this->assertEquals('baz', $this->mock->bar);96 $this->mock->foo();97 $this->assertEquals('bazz', $this->mock->bar);98 $this->mock->foo();99 $this->assertEquals('bazzz', $this->mock->bar);100 }101 public function testSetsPublicPropertiesWhenRequestedUsingAlias()102 {103 $this->mock->bar = null;104 $this->mock->shouldReceive('foo')->set('bar', 'baz', 'bazz', 'bazzz');105 $this->assertTrue(empty($this->mock->bar));106 $this->mock->foo();107 $this->assertEquals('baz', $this->mock->bar);108 $this->mock->foo();109 $this->assertEquals('bazz', $this->mock->bar);110 $this->mock->foo();111 $this->assertEquals('bazzz', $this->mock->bar);112 }113 public function testSetsPublicPropertiesWhenRequestedMoreTimesThanSetValues()114 {115 $this->mock->bar = null;116 $this->mock->shouldReceive('foo')->andSet('bar', 'baz', 'bazz');117 $this->assertNull($this->mock->bar);118 $this->mock->foo();119 $this->assertEquals('baz', $this->mock->bar);120 $this->mock->foo();121 $this->assertEquals('bazz', $this->mock->bar);122 $this->mock->foo();123 $this->assertEquals('bazz', $this->mock->bar);124 }125 public function testSetsPublicPropertiesWhenRequestedMoreTimesThanSetValuesUsingAlias()126 {127 $this->mock->bar = null;128 $this->mock->shouldReceive('foo')->andSet('bar', 'baz', 'bazz');129 $this->assertNull($this->mock->bar);130 $this->mock->foo();131 $this->assertEquals('baz', $this->mock->bar);132 $this->mock->foo();133 $this->assertEquals('bazz', $this->mock->bar);134 $this->mock->foo();135 $this->assertEquals('bazz', $this->mock->bar);136 }137 public function testSetsPublicPropertiesWhenRequestedMoreTimesThanSetValuesWithDirectSet()138 {139 $this->mock->bar = null;140 $this->mock->shouldReceive('foo')->andSet('bar', 'baz', 'bazz');141 $this->assertNull($this->mock->bar);142 $this->mock->foo();143 $this->assertEquals('baz', $this->mock->bar);144 $this->mock->foo();145 $this->assertEquals('bazz', $this->mock->bar);146 $this->mock->bar = null;147 $this->mock->foo();148 $this->assertNull($this->mock->bar);149 }150 public function testSetsPublicPropertiesWhenRequestedMoreTimesThanSetValuesWithDirectSetUsingAlias()151 {152 $this->mock->bar = null;153 $this->mock->shouldReceive('foo')->set('bar', 'baz', 'bazz');154 $this->assertNull($this->mock->bar);155 $this->mock->foo();156 $this->assertEquals('baz', $this->mock->bar);157 $this->mock->foo();158 $this->assertEquals('bazz', $this->mock->bar);159 $this->mock->bar = null;160 $this->mock->foo();161 $this->assertNull($this->mock->bar);162 }163 /**164 * @group issue/1005165 */166 public function testSetsPublicPropertiesCorrectlyForDifferentInstancesOfSameClass()167 {168 $mockInstanceOne = mock('MockeryTest_Foo');169 $mockInstanceTwo = mock('MockeryTest_Foo');170 $mockInstanceOne->shouldReceive('foo')171 ->andSet('bar', 'baz');172 $mockInstanceTwo->shouldReceive('foo')173 ->andSet('bar', 'bazz');174 $mockInstanceOne->foo();175 $mockInstanceTwo->foo();176 $this->assertEquals('baz', $mockInstanceOne->bar);177 $this->assertEquals('bazz', $mockInstanceTwo->bar);178 }179 public function testReturnsSameValueForAllIfNoArgsExpectationAndSomeGiven()180 {181 $this->mock->shouldReceive('foo')->andReturn(1);182 $this->assertEquals(1, $this->mock->foo('foo'));183 }184 public function testReturnsValueFromSequenceSequentially()185 {186 $this->mock->shouldReceive('foo')->andReturn(1, 2, 3);187 $this->mock->foo('foo');188 $this->assertEquals(2, $this->mock->foo('foo'));189 }190 public function testReturnsValueFromSequenceSequentiallyAndRepeatedlyReturnsFinalValueOnExtraCalls()191 {192 $this->mock->shouldReceive('foo')->andReturn(1, 2, 3);193 $this->mock->foo('foo');194 $this->mock->foo('foo');195 $this->assertEquals(3, $this->mock->foo('foo'));196 $this->assertEquals(3, $this->mock->foo('foo'));197 }198 public function testReturnsValueFromSequenceSequentiallyAndRepeatedlyReturnsFinalValueOnExtraCallsWithManyAndReturnCalls()199 {200 $this->mock->shouldReceive('foo')->andReturn(1)->andReturn(2, 3);201 $this->mock->foo('foo');202 $this->mock->foo('foo');203 $this->assertEquals(3, $this->mock->foo('foo'));204 $this->assertEquals(3, $this->mock->foo('foo'));205 }206 public function testReturnsValueOfClosure()207 {208 $this->mock->shouldReceive('foo')->with(5)->andReturnUsing(function ($v) {209 return $v+1;210 });211 $this->assertEquals(6, $this->mock->foo(5));212 }213 public function testReturnsValueOfArgument()214 {215 $args = [1, 2, 3, 4, 5];216 $index = 2;217 $this->mock->shouldReceive('foo')->withArgs($args)->andReturnArg($index);218 $this->assertEquals($args[$index], $this->mock->foo(...$args));219 }220 public function testReturnsNullArgument()221 {222 $args = [1, null, 3];223 $index = 1;224 $this->mock->shouldReceive('foo')->withArgs($args)->andReturnArg($index);225 $this->assertNull($this->mock->foo(...$args));226 }227 public function testExceptionOnInvalidArgumentIndexValue()228 {229 $this->expectException(\InvalidArgumentException::class);230 $this->mock->shouldReceive('foo')->andReturnArg("invalid");231 }232 public function testExceptionOnArgumentIndexOutOfRange()233 {234 $this->expectException(\OutOfBoundsException::class);235 $this->mock->shouldReceive('foo')->andReturnArg(2);236 $this->mock->foo(0, 1); // only pass 2 arguments so index #2 won't exist237 }238 public function testReturnsUndefined()239 {240 $this->mock->shouldReceive('foo')->andReturnUndefined();241 $this->assertInstanceOf(\Mockery\Undefined::class, $this->mock->foo());242 }243 public function testReturnsValuesSetAsArray()244 {245 $this->mock->shouldReceive('foo')->andReturnValues(array(1, 2, 3));246 $this->assertEquals(1, $this->mock->foo());247 $this->assertEquals(2, $this->mock->foo());248 $this->assertEquals(3, $this->mock->foo());249 }250 public function testThrowsException()251 {252 $this->mock->shouldReceive('foo')->andThrow(new OutOfBoundsException);253 $this->expectException(OutOfBoundsException::class);254 $this->mock->foo();255 Mockery::close();256 }257 /** @test */258 public function and_throws_is_an_alias_to_and_throw()259 {260 $this->mock->shouldReceive('foo')->andThrows(new OutOfBoundsException);261 $this->expectException(OutOfBoundsException::class);262 $this->mock->foo();263 }264 /**265 * @test266 * @requires PHP 7.0.0267 */268 public function it_can_throw_a_throwable()269 {270 $this->expectException(\Error::class);271 $this->mock->shouldReceive('foo')->andThrow(new \Error());272 $this->mock->foo();273 }274 public function testThrowsExceptionBasedOnArgs()275 {276 $this->mock->shouldReceive('foo')->andThrow('OutOfBoundsException');277 $this->expectException(OutOfBoundsException::class);278 $this->mock->foo();279 Mockery::close();280 }281 public function testThrowsExceptionBasedOnArgsWithMessage()282 {283 $this->mock->shouldReceive('foo')->andThrow('OutOfBoundsException', 'foo');284 try {285 $this->mock->foo();286 } catch (OutOfBoundsException $e) {287 $this->assertEquals('foo', $e->getMessage());288 }289 }290 public function testThrowsExceptionSequentially()291 {292 $this->mock->shouldReceive('foo')->andThrow(new Exception)->andThrow(new OutOfBoundsException);293 try {294 $this->mock->foo();295 } catch (Exception $e) {296 }297 $this->expectException(OutOfBoundsException::class);298 $this->mock->foo();299 Mockery::close();300 }301 public function testAndThrowExceptions()302 {303 $this->mock->shouldReceive('foo')->andThrowExceptions(array(304 new OutOfBoundsException,305 new InvalidArgumentException,306 ));307 try {308 $this->mock->foo();309 throw new Exception("Expected OutOfBoundsException, non thrown");310 } catch (\Exception $e) {311 $this->assertInstanceOf("OutOfBoundsException", $e, "Wrong or no exception thrown: {$e->getMessage()}");312 }313 try {314 $this->mock->foo();315 throw new Exception("Expected InvalidArgumentException, non thrown");316 } catch (\Exception $e) {317 $this->assertInstanceOf("InvalidArgumentException", $e, "Wrong or no exception thrown: {$e->getMessage()}");318 }319 }320 public function testAndThrowExceptionsCatchNonExceptionArgument()321 {322 $this->expectException(\Mockery\Exception::class);323 $this->expectExceptionMessage('You must pass an array of exception objects to andThrowExceptions');324 $this->mock325 ->shouldReceive('foo')326 ->andThrowExceptions(array('NotAnException'));327 Mockery::close();328 }329 public function testMultipleExpectationsWithReturns()330 {331 $this->mock->shouldReceive('foo')->with(1)->andReturn(10);332 $this->mock->shouldReceive('bar')->with(2)->andReturn(20);333 $this->assertEquals(10, $this->mock->foo(1));334 $this->assertEquals(20, $this->mock->bar(2));335 }336 public function testExpectsNoArguments()337 {338 $this->mock->shouldReceive('foo')->withNoArgs();339 $this->mock->foo();340 }341 public function testExpectsNoArgumentsThrowsExceptionIfAnyPassed()342 {343 $this->mock->shouldReceive('foo')->withNoArgs();344 $this->expectException(\Mockery\Exception::class);345 $this->mock->foo(1);346 Mockery::close();347 }348 public function testExpectsArgumentsArray()349 {350 $this->mock->shouldReceive('foo')->withArgs(array(1, 2));351 $this->mock->foo(1, 2);352 }353 public function testExpectsArgumentsArrayThrowsExceptionIfPassedEmptyArray()354 {355 $this->mock->shouldReceive('foo')->withArgs(array());356 $this->expectException(\Mockery\Exception::class);357 $this->mock->foo(1, 2);358 Mockery::close();359 }360 public function testExpectsArgumentsArrayThrowsExceptionIfNoArgumentsPassed()361 {362 $this->mock->shouldReceive('foo')->with();363 $this->expectException(\Mockery\Exception::class);364 $this->mock->foo(1);365 Mockery::close();366 }367 public function testExpectsArgumentsArrayThrowsExceptionIfPassedWrongArguments()368 {369 $this->mock->shouldReceive('foo')->withArgs(array(1, 2));370 $this->expectException(\Mockery\Exception::class);371 $this->mock->foo(3, 4);372 Mockery::close();373 }374 public function testExpectsStringArgumentExceptionMessageDifferentiatesBetweenNullAndEmptyString()375 {376 $this->mock->shouldReceive('foo')->withArgs(array('a string'));377 $this->expectException(\Mockery\Exception::class);378 $this->expectExceptionMessageRegExp('/foo\(NULL\)/');379 $this->mock->foo(null);380 Mockery::close();381 }382 public function testExpectsArgumentsArrayThrowsExceptionIfPassedWrongArgumentType()383 {384 $this->expectException(\InvalidArgumentException::class);385 $this->expectExceptionMessageRegExp('/invalid argument (.+), only array and closure are allowed/');386 $this->mock->shouldReceive('foo')->withArgs(5);387 Mockery::close();388 }389 public function testExpectsArgumentsArrayAcceptAClosureThatValidatesPassedArguments()390 {391 $closure = function ($odd, $even) {392 return ($odd % 2 != 0) && ($even % 2 == 0);393 };394 $this->mock->shouldReceive('foo')->withArgs($closure);395 $this->mock->foo(1, 2);396 }397 public function testExpectsArgumentsArrayThrowsExceptionWhenClosureEvaluatesToFalse()398 {399 $closure = function ($odd, $even) {400 return ($odd % 2 != 0) && ($even % 2 == 0);401 };402 $this->mock->shouldReceive('foo')->withArgs($closure);403 $this->expectException(\Mockery\Exception::class);404 $this->mock->foo(4, 2);405 Mockery::close();406 }407 public function testExpectsArgumentsArrayClosureDoesNotThrowExceptionIfOptionalArgumentsAreMissing()408 {409 $closure = function ($odd, $even, $sum = null) {410 $result = ($odd % 2 != 0) && ($even % 2 == 0);411 if (!is_null($sum)) {412 return $result && ($odd + $even == $sum);413 }414 return $result;415 };416 $this->mock->shouldReceive('foo')->withArgs($closure);417 $this->mock->foo(1, 4);418 }419 public function testExpectsArgumentsArrayClosureDoesNotThrowExceptionIfOptionalArgumentsMathTheExpectation()420 {421 $closure = function ($odd, $even, $sum = null) {422 $result = ($odd % 2 != 0) && ($even % 2 == 0);423 if (!is_null($sum)) {424 return $result && ($odd + $even == $sum);425 }426 return $result;427 };428 $this->mock->shouldReceive('foo')->withArgs($closure);429 $this->mock->foo(1, 4, 5);430 }431 public function testExpectsArgumentsArrayClosureThrowsExceptionIfOptionalArgumentsDontMatchTheExpectation()432 {433 $closure = function ($odd, $even, $sum = null) {434 $result = ($odd % 2 != 0) && ($even % 2 == 0);435 if (!is_null($sum)) {436 return $result && ($odd + $even == $sum);437 }438 return $result;439 };440 $this->mock->shouldReceive('foo')->withArgs($closure);441 $this->expectException(\Mockery\Exception::class);442 $this->mock->foo(1, 4, 2);443 Mockery::close();444 }445 public function testExpectsSomeOfArgumentsMatchRealArguments()446 {447 $this->mock->shouldReceive('foo')->withSomeOfArgs(1, 3, 5)->times(4);448 $this->mock->foo(1, 2, 3, 4, 5);449 $this->mock->foo(1, 3, 5, 2, 4);450 $this->mock->foo(1, 'foo', 3, 'bar', 5);451 $this->mock->foo(1, 3, 5);452 $this->mock->shouldReceive('foo')->withSomeOfArgs('foo')->times(2);453 $this->mock->foo('foo', 'bar');454 $this->mock->foo('bar', 'foo');455 }456 public function testExpectsSomeOfArgumentsGivenArgsDoNotMatchRealArgsAndThrowNoMatchingException()457 {458 $this->mock->shouldReceive('foo')->withSomeOfArgs(1, 3, 5);459 $this->expectException(\Mockery\Exception\NoMatchingExpectationException::class);460 $this->mock->foo(1, 2, 4, 5);461 }462 public function testExpectsAnyArguments()463 {464 $this->mock->shouldReceive('foo')->withAnyArgs();465 $this->mock->foo();466 $this->mock->foo(1);467 $this->mock->foo(1, 'k', new stdClass);468 }469 public function testExpectsArgumentMatchingObjectType()470 {471 $this->mock->shouldReceive('foo')->with('\stdClass');472 $this->mock->foo(new stdClass);473 }474 public function testThrowsExceptionOnNoArgumentMatch()475 {476 $this->mock->shouldReceive('foo')->with(1);477 $this->expectException(\Mockery\Exception::class);478 $this->mock->foo(2);479 Mockery::close();480 }481 public function testNeverCalled()482 {483 $this->mock->shouldReceive('foo')->never();484 }485 public function testShouldNotReceive()486 {487 $this->mock->shouldNotReceive('foo');488 }489 public function testShouldNotReceiveThrowsExceptionIfMethodCalled()490 {491 $this->mock->shouldNotReceive('foo');492 $this->expectException(\Mockery\Exception\InvalidCountException::class);493 $this->mock->foo();494 Mockery::close();495 }496 public function testShouldNotReceiveWithArgumentThrowsExceptionIfMethodCalled()497 {498 $this->mock->shouldNotReceive('foo')->with(2);499 $this->expectException(\Mockery\Exception\InvalidCountException::class);500 $this->mock->foo(2);501 Mockery::close();502 }503 public function testNeverCalledThrowsExceptionOnCall()504 {505 $this->mock->shouldReceive('foo')->never();506 $this->expectException(\Mockery\CountValidator\Exception::class);507 $this->mock->foo();508 Mockery::close();509 }510 public function testCalledOnce()511 {512 $this->mock->shouldReceive('foo')->once();513 $this->mock->foo();514 }515 public function testCalledOnceThrowsExceptionIfNotCalled()516 {517 $this->expectException(\Mockery\CountValidator\Exception::class);518 $this->mock->shouldReceive('foo')->once();519 Mockery::close();520 }521 public function testCalledOnceThrowsExceptionIfCalledTwice()522 {523 $this->mock->shouldReceive('foo')->once();524 $this->mock->foo();525 $this->expectException(\Mockery\CountValidator\Exception::class);526 $this->mock->foo();527 Mockery::close();528 }529 public function testCalledTwice()530 {531 $this->mock->shouldReceive('foo')->twice();532 $this->mock->foo();533 $this->mock->foo();534 }535 public function testCalledTwiceThrowsExceptionIfNotCalled()536 {537 $this->mock->shouldReceive('foo')->twice();538 $this->expectException(\Mockery\CountValidator\Exception::class);539 Mockery::close();540 }541 public function testCalledOnceThrowsExceptionIfCalledThreeTimes()542 {543 $this->mock->shouldReceive('foo')->twice();544 $this->mock->foo();545 $this->mock->foo();546 $this->expectException(\Mockery\CountValidator\Exception::class);547 $this->mock->foo();548 Mockery::close();549 }550 public function testCalledZeroOrMoreTimesAtZeroCalls()551 {552 $this->mock->shouldReceive('foo')->zeroOrMoreTimes();553 }554 public function testCalledZeroOrMoreTimesAtThreeCalls()555 {556 $this->mock->shouldReceive('foo')->zeroOrMoreTimes();557 $this->mock->foo();558 $this->mock->foo();559 $this->mock->foo();560 }561 public function testTimesCountCalls()562 {563 $this->mock->shouldReceive('foo')->times(4);564 $this->mock->foo();565 $this->mock->foo();566 $this->mock->foo();567 $this->mock->foo();568 }569 public function testTimesCountCallThrowsExceptionOnTooFewCalls()570 {571 $this->mock->shouldReceive('foo')->times(2);572 $this->mock->foo();573 $this->expectException(\Mockery\CountValidator\Exception::class);574 Mockery::close();575 }576 public function testTimesCountCallThrowsExceptionOnTooManyCalls()577 {578 $this->mock->shouldReceive('foo')->times(2);579 $this->mock->foo();580 $this->mock->foo();581 $this->expectException(\Mockery\CountValidator\Exception::class);582 $this->mock->foo();583 Mockery::close();584 }585 public function testCalledAtLeastOnceAtExactlyOneCall()586 {587 $this->mock->shouldReceive('foo')->atLeast()->once();588 $this->mock->foo();589 }590 public function testCalledAtLeastOnceAtExactlyThreeCalls()591 {592 $this->mock->shouldReceive('foo')->atLeast()->times(3);593 $this->mock->foo();594 $this->mock->foo();595 $this->mock->foo();596 }597 public function testCalledAtLeastThrowsExceptionOnTooFewCalls()598 {599 $this->mock->shouldReceive('foo')->atLeast()->twice();600 $this->mock->foo();601 $this->expectException(\Mockery\CountValidator\Exception::class);602 Mockery::close();603 }604 public function testCalledAtMostOnceAtExactlyOneCall()605 {606 $this->mock->shouldReceive('foo')->atMost()->once();607 $this->mock->foo();608 }609 public function testCalledAtMostAtExactlyThreeCalls()610 {611 $this->mock->shouldReceive('foo')->atMost()->times(3);612 $this->mock->foo();613 $this->mock->foo();614 $this->mock->foo();615 }616 public function testCalledAtLeastThrowsExceptionOnTooManyCalls()617 {618 $this->mock->shouldReceive('foo')->atMost()->twice();619 $this->mock->foo();620 $this->mock->foo();621 $this->expectException(\Mockery\CountValidator\Exception::class);622 $this->mock->foo();623 Mockery::close();624 }625 public function testExactCountersOverrideAnyPriorSetNonExactCounters()626 {627 $this->mock->shouldReceive('foo')->atLeast()->once()->once();628 $this->mock->foo();629 $this->expectException(\Mockery\CountValidator\Exception::class);630 $this->mock->foo();631 Mockery::close();632 }633 public function testComboOfLeastAndMostCallsWithOneCall()634 {635 $this->mock->shouldReceive('foo')->atleast()->once()->atMost()->twice();636 $this->mock->foo();637 }638 public function testComboOfLeastAndMostCallsWithTwoCalls()639 {640 $this->mock->shouldReceive('foo')->atleast()->once()->atMost()->twice();641 $this->mock->foo();642 $this->mock->foo();643 }644 public function testComboOfLeastAndMostCallsThrowsExceptionAtTooFewCalls()645 {646 $this->mock->shouldReceive('foo')->atleast()->once()->atMost()->twice();647 $this->expectException(\Mockery\CountValidator\Exception::class);648 Mockery::close();649 }650 public function testComboOfLeastAndMostCallsThrowsExceptionAtTooManyCalls()651 {652 $this->mock->shouldReceive('foo')->atleast()->once()->atMost()->twice();653 $this->mock->foo();654 $this->expectException(\Mockery\CountValidator\Exception::class);655 $this->mock->foo();656 $this->mock->foo();657 Mockery::close();658 }659 public function testCallCountingOnlyAppliesToMatchedExpectations()660 {661 $this->mock->shouldReceive('foo')->with(1)->once();662 $this->mock->shouldReceive('foo')->with(2)->twice();663 $this->mock->shouldReceive('foo')->with(3);664 $this->mock->foo(1);665 $this->mock->foo(2);666 $this->mock->foo(2);667 $this->mock->foo(3);668 }669 public function testCallCountingThrowsExceptionOnAnyMismatch()670 {671 $this->mock->shouldReceive('foo')->with(1)->once();672 $this->mock->shouldReceive('foo')->with(2)->twice();673 $this->mock->shouldReceive('foo')->with(3);674 $this->mock->shouldReceive('bar');675 $this->mock->foo(1);676 $this->mock->foo(2);677 $this->mock->foo(3);678 $this->mock->bar();679 $this->expectException(\Mockery\CountValidator\Exception::class);680 Mockery::close();681 }682 public function testCallCountingThrowsExceptionFirst()683 {684 $number_of_calls = 0;685 $this->mock->shouldReceive('foo')686 ->times(2)687 ->with(\Mockery::on(function ($argument) use (&$number_of_calls) {688 $number_of_calls++;689 return $number_of_calls <= 3;690 }));691 $this->mock->foo(1);692 $this->mock->foo(1);693 $this->expectException(\Mockery\CountValidator\Exception::class);694 $this->mock->foo(1);695 Mockery::close();696 }697 public function testOrderedCallsWithoutError()698 {699 $this->mock->shouldReceive('foo')->ordered();700 $this->mock->shouldReceive('bar')->ordered();701 $this->mock->foo();702 $this->mock->bar();703 }704 public function testOrderedCallsWithOutOfOrderError()705 {706 $this->mock->shouldReceive('foo')->ordered();707 $this->mock->shouldReceive('bar')->ordered();708 $this->expectException(\Mockery\Exception::class);709 $this->mock->bar();710 $this->mock->foo();711 Mockery::close();712 }713 public function testDifferentArgumentsAndOrderingsPassWithoutException()714 {715 $this->mock->shouldReceive('foo')->with(1)->ordered();716 $this->mock->shouldReceive('foo')->with(2)->ordered();717 $this->mock->foo(1);718 $this->mock->foo(2);719 }720 public function testDifferentArgumentsAndOrderingsThrowExceptionWhenInWrongOrder()721 {722 $this->mock->shouldReceive('foo')->with(1)->ordered();723 $this->mock->shouldReceive('foo')->with(2)->ordered();724 $this->expectException(\Mockery\Exception::class);725 $this->mock->foo(2);726 $this->mock->foo(1);727 Mockery::close();728 }729 public function testUnorderedCallsIgnoredForOrdering()730 {731 $this->mock->shouldReceive('foo')->with(1)->ordered();732 $this->mock->shouldReceive('foo')->with(2);733 $this->mock->shouldReceive('foo')->with(3)->ordered();734 $this->mock->foo(2);735 $this->mock->foo(1);736 $this->mock->foo(2);737 $this->mock->foo(3);738 $this->mock->foo(2);739 }740 public function testOrderingOfDefaultGrouping()741 {742 $this->mock->shouldReceive('foo')->ordered();743 $this->mock->shouldReceive('bar')->ordered();744 $this->mock->foo();745 $this->mock->bar();746 }747 public function testOrderingOfDefaultGroupingThrowsExceptionOnWrongOrder()748 {749 $this->mock->shouldReceive('foo')->ordered();750 $this->mock->shouldReceive('bar')->ordered();751 $this->expectException(\Mockery\Exception::class);752 $this->mock->bar();753 $this->mock->foo();754 Mockery::close();755 }756 public function testOrderingUsingNumberedGroups()757 {758 $this->mock->shouldReceive('start')->ordered(1);759 $this->mock->shouldReceive('foo')->ordered(2);760 $this->mock->shouldReceive('bar')->ordered(2);761 $this->mock->shouldReceive('final')->ordered();762 $this->mock->start();763 $this->mock->bar();764 $this->mock->foo();765 $this->mock->bar();766 $this->mock->final();767 }768 public function testOrderingUsingNamedGroups()769 {770 $this->mock->shouldReceive('start')->ordered('start');771 $this->mock->shouldReceive('foo')->ordered('foobar');772 $this->mock->shouldReceive('bar')->ordered('foobar');773 $this->mock->shouldReceive('final')->ordered();774 $this->mock->start();775 $this->mock->bar();776 $this->mock->foo();777 $this->mock->bar();778 $this->mock->final();779 }780 /**781 * @group 2A782 */783 public function testGroupedUngroupedOrderingDoNotOverlap()784 {785 $s = $this->mock->shouldReceive('start')->ordered();786 $m = $this->mock->shouldReceive('mid')->ordered('foobar');787 $e = $this->mock->shouldReceive('end')->ordered();788 $this->assertLessThan($m->getOrderNumber(), $s->getOrderNumber());789 $this->assertLessThan($e->getOrderNumber(), $m->getOrderNumber());790 }791 public function testGroupedOrderingThrowsExceptionWhenCallsDisordered()792 {793 $this->mock->shouldReceive('foo')->ordered('first');794 $this->mock->shouldReceive('bar')->ordered('second');795 $this->expectException(\Mockery\Exception::class);796 $this->mock->bar();797 $this->mock->foo();798 Mockery::close();799 }800 public function testExpectationMatchingWithNoArgsOrderings()801 {802 $this->mock->shouldReceive('foo')->withNoArgs()->once()->ordered();803 $this->mock->shouldReceive('bar')->withNoArgs()->once()->ordered();804 $this->mock->shouldReceive('foo')->withNoArgs()->once()->ordered();805 $this->mock->foo();806 $this->mock->bar();807 $this->mock->foo();808 }809 public function testExpectationMatchingWithAnyArgsOrderings()810 {811 $this->mock->shouldReceive('foo')->withAnyArgs()->once()->ordered();812 $this->mock->shouldReceive('bar')->withAnyArgs()->once()->ordered();813 $this->mock->shouldReceive('foo')->withAnyArgs()->once()->ordered();814 $this->mock->foo();815 $this->mock->bar();816 $this->mock->foo();817 }818 public function testEnsuresOrderingIsNotCrossMockByDefault()819 {820 $this->mock->shouldReceive('foo')->ordered();821 $mock2 = mock('bar');822 $mock2->shouldReceive('bar')->ordered();823 $mock2->bar();824 $this->mock->foo();825 }826 public function testEnsuresOrderingIsCrossMockWhenGloballyFlagSet()827 {828 $this->mock->shouldReceive('foo')->globally()->ordered();829 $mock2 = mock('bar');830 $mock2->shouldReceive('bar')->globally()->ordered();831 $this->expectException(\Mockery\Exception::class);832 $mock2->bar();833 $this->mock->foo();834 Mockery::close();835 }836 public function testExpectationCastToStringFormatting()837 {838 $exp = $this->mock->shouldReceive('foo')->with(1, 'bar', new stdClass, array('Spam' => 'Ham', 'Bar' => 'Baz'));839 $this->assertEquals("[foo(1, 'bar', object(stdClass), ['Spam' => 'Ham', 'Bar' => 'Baz'])]", (string) $exp);840 }841 public function testLongExpectationCastToStringFormatting()842 {843 $exp = $this->mock->shouldReceive('foo')->with(array('Spam' => 'Ham', 'Bar' => 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'End'));844 $this->assertEquals("[foo(['Spam' => 'Ham', 'Bar' => 'Baz', 0 => 'Bar', 1 => 'Baz', 2 => 'Bar', 3 => 'Baz', 4 => 'Bar', 5 => 'Baz', 6 => 'Bar', 7 => 'Baz', 8 => 'Bar', 9 => 'Baz', 10 => 'Bar', 11 => 'Baz', 12 => 'Bar', 13 => 'Baz', 14 => 'Bar', 15 => 'Baz', 16 => 'Bar', 17 => 'Baz', 18 => 'Bar', 19 => 'Baz', 20 => 'Bar', 21 => 'Baz', 22 => 'Bar', 23 => 'Baz', 24 => 'Bar', 25 => 'Baz', 26 => 'Bar', 27 => 'Baz', 28 => 'Bar', 29 => 'Baz', 30 => 'Bar', 31 => 'Baz', 32 => 'Bar', 33 => 'Baz', 34 => 'Bar', 35 => 'Baz', 36 => 'Bar', 37 => 'Baz', 38 => 'Bar', 39 => 'Baz', 40 => 'Bar', 41 => 'Baz', 42 => 'Bar', 43 => 'Baz', 44 => 'Bar', 45 => 'Baz', 46 => 'Baz', 47 => 'Bar', 48 => 'Baz', 49 => 'Bar', 50 => 'Baz', 51 => 'Bar', 52 => 'Baz', 53 => 'Bar', 54 => 'Baz', 55 => 'Bar', 56 => 'Baz', 57 => 'Baz', 58 => 'Bar', 59 => 'Baz', 60 => 'Bar', 61 => 'Baz', 62 => 'Bar', 63 => 'Baz', 64 => 'Bar', 65 => 'Baz', 66 => 'Bar', 67 => 'Baz', 68 => 'Baz', 69 => 'Bar', 70 => 'Baz', 71 => 'Bar', 72 => 'Baz', 73 => 'Bar', 74 => 'Baz', 7...])]", (string) $exp);845 }846 public function testMultipleExpectationCastToStringFormatting()847 {848 $exp = $this->mock->shouldReceive('foo', 'bar')->with(1);849 $this->assertEquals('[foo(1), bar(1)]', (string) $exp);850 }851 public function testGroupedOrderingWithLimitsAllowsMultipleReturnValues()852 {853 $this->mock->shouldReceive('foo')->with(2)->once()->andReturn('first');854 $this->mock->shouldReceive('foo')->with(2)->twice()->andReturn('second/third');855 $this->mock->shouldReceive('foo')->with(2)->andReturn('infinity');856 $this->assertEquals('first', $this->mock->foo(2));857 $this->assertEquals('second/third', $this->mock->foo(2));858 $this->assertEquals('second/third', $this->mock->foo(2));859 $this->assertEquals('infinity', $this->mock->foo(2));860 $this->assertEquals('infinity', $this->mock->foo(2));861 $this->assertEquals('infinity', $this->mock->foo(2));862 }863 public function testExpectationsCanBeMarkedAsDefaults()864 {865 $this->mock->shouldReceive('foo')->andReturn('bar')->byDefault();866 $this->assertEquals('bar', $this->mock->foo());867 }868 public function testDefaultExpectationsValidatedInCorrectOrder()869 {870 $this->mock->shouldReceive('foo')->with(1)->once()->andReturn('first')->byDefault();871 $this->mock->shouldReceive('foo')->with(2)->once()->andReturn('second')->byDefault();872 $this->assertEquals('first', $this->mock->foo(1));873 $this->assertEquals('second', $this->mock->foo(2));874 }875 public function testDefaultExpectationsAreReplacedByLaterConcreteExpectations()876 {877 $this->mock->shouldReceive('foo')->andReturn('bar')->once()->byDefault();878 $this->mock->shouldReceive('foo')->andReturn('baz')->twice();879 $this->assertEquals('baz', $this->mock->foo());880 $this->assertEquals('baz', $this->mock->foo());881 }882 public function testExpectationFallsBackToDefaultExpectationWhenConcreteExpectationsAreUsedUp()883 {884 $this->mock->shouldReceive('foo')->with(1)->andReturn('bar')->once()->byDefault();885 $this->mock->shouldReceive('foo')->with(2)->andReturn('baz')->once();886 $this->assertEquals('baz', $this->mock->foo(2));887 $this->assertEquals('bar', $this->mock->foo(1));888 }889 public function testDefaultExpectationsCanBeOrdered()890 {891 $this->mock->shouldReceive('foo')->ordered()->byDefault();892 $this->mock->shouldReceive('bar')->ordered()->byDefault();893 $this->expectException(\Mockery\Exception::class);894 $this->mock->bar();895 $this->mock->foo();896 Mockery::close();897 }898 public function testDefaultExpectationsCanBeOrderedAndReplaced()899 {900 $this->mock->shouldReceive('foo')->ordered()->byDefault();901 $this->mock->shouldReceive('bar')->ordered()->byDefault();902 $this->mock->shouldReceive('bar')->ordered();903 $this->mock->shouldReceive('foo')->ordered();904 $this->mock->bar();905 $this->mock->foo();906 }907 public function testByDefaultOperatesFromMockConstruction()908 {909 $container = new \Mockery\Container(\Mockery::getDefaultGenerator(), \Mockery::getDefaultLoader());910 $mock = $container->mock('f', array('foo'=>'rfoo', 'bar'=>'rbar', 'baz'=>'rbaz'))->byDefault();911 $mock->shouldReceive('foo')->andReturn('foobar');912 $this->assertEquals('foobar', $mock->foo());913 $this->assertEquals('rbar', $mock->bar());914 $this->assertEquals('rbaz', $mock->baz());915 }916 public function testByDefaultOnAMockDoesSquatWithoutExpectations()917 {918 $this->assertInstanceOf(MockInterface::class, mock('f')->byDefault());919 }920 public function testDefaultExpectationsCanBeOverridden()921 {922 $this->mock->shouldReceive('foo')->with('test')->andReturn('bar')->byDefault();923 $this->mock->shouldReceive('foo')->with('test')->andReturn('newbar')->byDefault();924 $this->mock->foo('test');925 $this->assertEquals('newbar', $this->mock->foo('test'));926 }927 public function testByDefaultPreventedFromSettingDefaultWhenDefaultingExpectationWasReplaced()928 {929 $exp = $this->mock->shouldReceive('foo')->andReturn(1);930 $this->mock->shouldReceive('foo')->andReturn(2);931 $this->expectException(\Mockery\Exception::class);932 $exp->byDefault();933 Mockery::close();934 }935 /**936 * Argument Constraint Tests937 */938 public function testAnyConstraintMatchesAnyArg()939 {940 $this->mock->shouldReceive('foo')->with(1, Mockery::any())->twice();941 $this->mock->foo(1, 2);942 $this->mock->foo(1, 'str');943 }944 public function testAnyConstraintNonMatchingCase()945 {946 $this->mock->shouldReceive('foo')->times(3);947 $this->mock->shouldReceive('foo')->with(1, Mockery::any())->never();948 $this->mock->foo();949 $this->mock->foo(1);950 $this->mock->foo(1, 2, 3);951 }952 public function testAndAnyOtherConstraintMatchesTheRestOfTheArguments()953 {954 $this->mock->shouldReceive('foo')->with(1, 2, Mockery::andAnyOthers())->twice();955 $this->mock->foo(1, 2, 3, 4, 5);956 $this->mock->foo(1, 'str', 3, 4);957 }958 public function testAndAnyOtherConstraintDoesNotPreventMatchingOfRegularArguments()959 {960 $this->mock->shouldReceive('foo')->with(1, 2, Mockery::andAnyOthers());961 $this->expectException(\Mockery\Exception::class);962 $this->mock->foo(10, 2, 3, 4, 5);963 Mockery::close();964 }965 public function testArrayConstraintMatchesArgument()966 {967 $this->mock->shouldReceive('foo')->with(Mockery::type('array'))->once();968 $this->mock->foo(array());969 }970 public function testArrayConstraintNonMatchingCase()971 {972 $this->mock->shouldReceive('foo')->times(3);973 $this->mock->shouldReceive('foo')->with(1, Mockery::type('array'))->never();974 $this->mock->foo();975 $this->mock->foo(1);976 $this->mock->foo(1, 2, 3);977 }978 public function testArrayConstraintThrowsExceptionWhenConstraintUnmatched()979 {980 $this->mock->shouldReceive('foo')->with(Mockery::type('array'));981 $this->expectException(\Mockery\Exception::class);982 $this->mock->foo(1);983 Mockery::close();984 }985 public function testBoolConstraintMatchesArgument()986 {987 $this->mock->shouldReceive('foo')->with(Mockery::type('bool'))->once();988 $this->mock->foo(true);989 }990 public function testBoolConstraintNonMatchingCase()991 {992 $this->mock->shouldReceive('foo')->times(3);993 $this->mock->shouldReceive('foo')->with(1, Mockery::type('bool'))->never();994 $this->mock->foo();995 $this->mock->foo(1);996 $this->mock->foo(1, 2, 3);997 }998 public function testBoolConstraintThrowsExceptionWhenConstraintUnmatched()999 {1000 $this->mock->shouldReceive('foo')->with(Mockery::type('bool'));1001 $this->expectException(\Mockery\Exception::class);1002 $this->mock->foo(1);1003 Mockery::close();1004 }1005 public function testCallableConstraintMatchesArgument()1006 {1007 $this->mock->shouldReceive('foo')->with(Mockery::type('callable'))->once();1008 $this->mock->foo(function () {1009 return 'f';1010 });1011 }1012 public function testCallableConstraintNonMatchingCase()1013 {1014 $this->mock->shouldReceive('foo')->times(3);1015 $this->mock->shouldReceive('foo')->with(1, Mockery::type('callable'))->never();1016 $this->mock->foo();1017 $this->mock->foo(1);1018 $this->mock->foo(1, 2, 3);1019 }1020 public function testCallableConstraintThrowsExceptionWhenConstraintUnmatched()1021 {1022 $this->mock->shouldReceive('foo')->with(Mockery::type('callable'));1023 $this->expectException(\Mockery\Exception::class);1024 $this->mock->foo(1);1025 Mockery::close();1026 }1027 public function testDoubleConstraintMatchesArgument()1028 {1029 $this->mock->shouldReceive('foo')->with(Mockery::type('double'))->once();1030 $this->mock->foo(2.25);1031 }1032 public function testDoubleConstraintNonMatchingCase()1033 {1034 $this->mock->shouldReceive('foo')->times(3);1035 $this->mock->shouldReceive('foo')->with(1, Mockery::type('double'))->never();1036 $this->mock->foo();1037 $this->mock->foo(1);1038 $this->mock->foo(1, 2, 3);1039 }1040 public function testDoubleConstraintThrowsExceptionWhenConstraintUnmatched()1041 {1042 $this->mock->shouldReceive('foo')->with(Mockery::type('double'));1043 $this->expectException(\Mockery\Exception::class);1044 $this->mock->foo(1);1045 Mockery::close();1046 }1047 public function testFloatConstraintMatchesArgument()1048 {1049 $this->mock->shouldReceive('foo')->with(Mockery::type('float'))->once();1050 $this->mock->foo(2.25);1051 }1052 public function testFloatConstraintNonMatchingCase()1053 {1054 $this->mock->shouldReceive('foo')->times(3);1055 $this->mock->shouldReceive('foo')->with(1, Mockery::type('float'))->never();1056 $this->mock->foo();1057 $this->mock->foo(1);1058 $this->mock->foo(1, 2, 3);1059 }1060 public function testFloatConstraintThrowsExceptionWhenConstraintUnmatched()1061 {1062 $this->mock->shouldReceive('foo')->with(Mockery::type('float'));1063 $this->expectException(\Mockery\Exception::class);1064 $this->mock->foo(1);1065 Mockery::close();1066 }1067 public function testIntConstraintMatchesArgument()1068 {1069 $this->mock->shouldReceive('foo')->with(Mockery::type('int'))->once();1070 $this->mock->foo(2);1071 }1072 public function testIntConstraintNonMatchingCase()1073 {1074 $this->mock->shouldReceive('foo')->times(3);1075 $this->mock->shouldReceive('foo')->with(1, Mockery::type('int'))->never();1076 $this->mock->foo();1077 $this->mock->foo(1);1078 $this->mock->foo(1, 2, 3);1079 }1080 public function testIntConstraintThrowsExceptionWhenConstraintUnmatched()1081 {1082 $this->mock->shouldReceive('foo')->with(Mockery::type('int'));1083 $this->expectException(\Mockery\Exception::class);1084 $this->mock->foo('f');1085 Mockery::close();1086 }1087 public function testLongConstraintMatchesArgument()1088 {1089 $this->mock->shouldReceive('foo')->with(Mockery::type('long'))->once();1090 $this->mock->foo(2);1091 }1092 public function testLongConstraintNonMatchingCase()1093 {1094 $this->mock->shouldReceive('foo')->times(3);1095 $this->mock->shouldReceive('foo')->with(1, Mockery::type('long'))->never();1096 $this->mock->foo();1097 $this->mock->foo(1);1098 $this->mock->foo(1, 2, 3);1099 }1100 public function testLongConstraintThrowsExceptionWhenConstraintUnmatched()1101 {1102 $this->mock->shouldReceive('foo')->with(Mockery::type('long'));1103 $this->expectException(\Mockery\Exception::class);1104 $this->mock->foo('f');1105 Mockery::close();1106 }1107 public function testNullConstraintMatchesArgument()1108 {1109 $this->mock->shouldReceive('foo')->with(Mockery::type('null'))->once();1110 $this->mock->foo(null);1111 }1112 public function testNullConstraintNonMatchingCase()1113 {1114 $this->mock->shouldReceive('foo')->times(3);1115 $this->mock->shouldReceive('foo')->with(1, Mockery::type('null'))->never();1116 $this->mock->foo();1117 $this->mock->foo(1);1118 $this->mock->foo(1, 2, 3);1119 }1120 public function testNullConstraintThrowsExceptionWhenConstraintUnmatched()1121 {1122 $this->mock->shouldReceive('foo')->with(Mockery::type('null'));1123 $this->expectException(\Mockery\Exception::class);1124 $this->mock->foo('f');1125 Mockery::close();1126 }1127 public function testNumericConstraintMatchesArgument()1128 {1129 $this->mock->shouldReceive('foo')->with(Mockery::type('numeric'))->once();1130 $this->mock->foo('2');1131 }1132 public function testNumericConstraintNonMatchingCase()1133 {1134 $this->mock->shouldReceive('foo')->times(3);1135 $this->mock->shouldReceive('foo')->with(1, Mockery::type('numeric'))->never();1136 $this->mock->foo();1137 $this->mock->foo(1);1138 $this->mock->foo(1, 2, 3);1139 }1140 public function testNumericConstraintThrowsExceptionWhenConstraintUnmatched()1141 {1142 $this->mock->shouldReceive('foo')->with(Mockery::type('numeric'));1143 $this->expectException(\Mockery\Exception::class);1144 $this->mock->foo('f');1145 Mockery::close();1146 }1147 public function testObjectConstraintMatchesArgument()1148 {1149 $this->mock->shouldReceive('foo')->with(Mockery::type('object'))->once();1150 $this->mock->foo(new stdClass);1151 }1152 public function testObjectConstraintNonMatchingCase()1153 {1154 $this->mock->shouldReceive('foo')->times(3);1155 $this->mock->shouldReceive('foo')->with(1, Mockery::type('object`'))->never();1156 $this->mock->foo();1157 $this->mock->foo(1);1158 $this->mock->foo(1, 2, 3);1159 }1160 public function testObjectConstraintThrowsExceptionWhenConstraintUnmatched()1161 {1162 $this->mock->shouldReceive('foo')->with(Mockery::type('object'));1163 $this->expectException(\Mockery\Exception::class);1164 $this->mock->foo('f');1165 Mockery::close();1166 }1167 public function testRealConstraintMatchesArgument()1168 {1169 $this->mock->shouldReceive('foo')->with(Mockery::type('float'))->once();1170 $this->mock->foo(2.25);1171 }1172 public function testRealConstraintNonMatchingCase()1173 {1174 $this->mock->shouldReceive('foo')->times(3);1175 $this->mock->shouldReceive('foo')->with(1, Mockery::type('float'))->never();1176 $this->mock->foo();1177 $this->mock->foo(1);1178 $this->mock->foo(1, 2, 3);1179 }1180 public function testRealConstraintThrowsExceptionWhenConstraintUnmatched()1181 {1182 $this->mock->shouldReceive('foo')->with(Mockery::type('float'));1183 $this->expectException(\Mockery\Exception::class);1184 $this->mock->foo('f');1185 Mockery::close();1186 }1187 public function testResourceConstraintMatchesArgument()1188 {1189 $this->mock->shouldReceive('foo')->with(Mockery::type('resource'))->once();1190 $r = fopen(dirname(__FILE__) . '/_files/file.txt', 'r');1191 $this->mock->foo($r);1192 }1193 public function testResourceConstraintNonMatchingCase()1194 {1195 $this->mock->shouldReceive('foo')->times(3);1196 $this->mock->shouldReceive('foo')->with(1, Mockery::type('resource'))->never();1197 $this->mock->foo();1198 $this->mock->foo(1);1199 $this->mock->foo(1, 2, 3);1200 }1201 public function testResourceConstraintThrowsExceptionWhenConstraintUnmatched()1202 {1203 $this->mock->shouldReceive('foo')->with(Mockery::type('resource'));1204 $this->expectException(\Mockery\Exception::class);1205 $this->mock->foo('f');1206 Mockery::close();1207 }1208 public function testScalarConstraintMatchesArgument()1209 {1210 $this->mock->shouldReceive('foo')->with(Mockery::type('scalar'))->once();1211 $this->mock->foo(2);1212 }1213 public function testScalarConstraintNonMatchingCase()1214 {1215 $this->mock->shouldReceive('foo')->times(3);1216 $this->mock->shouldReceive('foo')->with(1, Mockery::type('scalar'))->never();1217 $this->mock->foo();1218 $this->mock->foo(1);1219 $this->mock->foo(1, 2, 3);1220 }1221 public function testScalarConstraintThrowsExceptionWhenConstraintUnmatched()1222 {1223 $this->mock->shouldReceive('foo')->with(Mockery::type('scalar'));1224 $this->expectException(\Mockery\Exception::class);1225 $this->mock->foo(array());1226 Mockery::close();1227 }1228 public function testStringConstraintMatchesArgument()1229 {1230 $this->mock->shouldReceive('foo')->with(Mockery::type('string'))->once();1231 $this->mock->foo('2');1232 }1233 public function testStringConstraintNonMatchingCase()1234 {1235 $this->mock->shouldReceive('foo')->times(3);1236 $this->mock->shouldReceive('foo')->with(1, Mockery::type('string'))->never();1237 $this->mock->foo();1238 $this->mock->foo(1);1239 $this->mock->foo(1, 2, 3);1240 }1241 public function testStringConstraintThrowsExceptionWhenConstraintUnmatched()1242 {1243 $this->mock->shouldReceive('foo')->with(Mockery::type('string'));1244 $this->expectException(\Mockery\Exception::class);1245 $this->mock->foo(1);1246 Mockery::close();1247 }1248 public function testClassConstraintMatchesArgument()1249 {1250 $this->mock->shouldReceive('foo')->with(Mockery::type('stdClass'))->once();1251 $this->mock->foo(new stdClass);1252 }1253 public function testClassConstraintNonMatchingCase()1254 {1255 $this->mock->shouldReceive('foo')->times(3);1256 $this->mock->shouldReceive('foo')->with(1, Mockery::type('stdClass'))->never();1257 $this->mock->foo();1258 $this->mock->foo(1);1259 $this->mock->foo(1, 2, 3);1260 }1261 public function testClassConstraintThrowsExceptionWhenConstraintUnmatched()1262 {1263 $this->mock->shouldReceive('foo')->with(Mockery::type('stdClass'));1264 $this->expectException(\Mockery\Exception::class);1265 $this->mock->foo(new Exception);1266 Mockery::close();1267 }1268 public function testDucktypeConstraintMatchesArgument()1269 {1270 $this->mock->shouldReceive('foo')->with(Mockery::ducktype('quack', 'swim'))->once();1271 $this->mock->foo(new Mockery_Duck);1272 }1273 public function testDucktypeConstraintNonMatchingCase()1274 {1275 $this->mock->shouldReceive('foo')->times(3);1276 $this->mock->shouldReceive('foo')->with(1, Mockery::ducktype('quack', 'swim'))->never();1277 $this->mock->foo();1278 $this->mock->foo(1);1279 $this->mock->foo(1, 2, 3);1280 }1281 public function testDucktypeConstraintThrowsExceptionWhenConstraintUnmatched()1282 {1283 $this->mock->shouldReceive('foo')->with(Mockery::ducktype('quack', 'swim'));1284 $this->expectException(\Mockery\Exception::class);1285 $this->mock->foo(new Mockery_Duck_Nonswimmer);1286 Mockery::close();1287 }1288 public function testArrayContentConstraintMatchesArgument()1289 {1290 $this->mock->shouldReceive('foo')->with(Mockery::subset(array('a'=>1, 'b'=>2)))->once();1291 $this->mock->foo(array('a'=>1, 'b'=>2, 'c'=>3));1292 }1293 public function testArrayContentConstraintNonMatchingCase()1294 {1295 $this->mock->shouldReceive('foo')->times(3);1296 $this->mock->shouldReceive('foo')->with(1, Mockery::subset(array('a'=>1, 'b'=>2)))->never();1297 $this->mock->foo();1298 $this->mock->foo(1);1299 $this->mock->foo(1, 2, 3);1300 }1301 public function testArrayContentConstraintThrowsExceptionWhenConstraintUnmatched()1302 {1303 $this->mock->shouldReceive('foo')->with(Mockery::subset(array('a'=>1, 'b'=>2)));1304 $this->expectException(\Mockery\Exception::class);1305 $this->mock->foo(array('a'=>1, 'c'=>3));1306 Mockery::close();1307 }1308 public function testContainsConstraintMatchesArgument()1309 {1310 $this->mock->shouldReceive('foo')->with(Mockery::contains(1, 2))->once();1311 $this->mock->foo(array('a'=>1, 'b'=>2, 'c'=>3));1312 }1313 public function testContainsConstraintNonMatchingCase()1314 {1315 $this->mock->shouldReceive('foo')->times(3);1316 $this->mock->shouldReceive('foo')->with(1, Mockery::contains(1, 2))->never();1317 $this->mock->foo();1318 $this->mock->foo(1);1319 $this->mock->foo(1, 2, 3);1320 }1321 public function testContainsConstraintThrowsExceptionWhenConstraintUnmatched()1322 {1323 $this->mock->shouldReceive('foo')->with(Mockery::contains(1, 2));1324 $this->expectException(\Mockery\Exception::class);1325 $this->mock->foo(array('a'=>1, 'c'=>3));1326 Mockery::close();1327 }1328 public function testHasKeyConstraintMatchesArgument()1329 {1330 $this->mock->shouldReceive('foo')->with(Mockery::hasKey('c'))->once();1331 $this->mock->foo(array('a'=>1, 'b'=>2, 'c'=>3));1332 }1333 public function testHasKeyConstraintNonMatchingCase()1334 {1335 $this->mock->shouldReceive('foo')->times(3);1336 $this->mock->shouldReceive('foo')->with(1, Mockery::hasKey('a'))->never();1337 $this->mock->foo();1338 $this->mock->foo(1);1339 $this->mock->foo(1, array('a'=>1), 3);1340 }1341 public function testHasKeyConstraintThrowsExceptionWhenConstraintUnmatched()1342 {1343 $this->mock->shouldReceive('foo')->with(Mockery::hasKey('c'));1344 $this->expectException(\Mockery\Exception::class);1345 $this->mock->foo(array('a'=>1, 'b'=>3));1346 Mockery::close();1347 }1348 public function testHasValueConstraintMatchesArgument()1349 {1350 $this->mock->shouldReceive('foo')->with(Mockery::hasValue(1))->once();1351 $this->mock->foo(array('a'=>1, 'b'=>2, 'c'=>3));1352 }1353 public function testHasValueConstraintNonMatchingCase()1354 {1355 $this->mock->shouldReceive('foo')->times(3);1356 $this->mock->shouldReceive('foo')->with(1, Mockery::hasValue(1))->never();1357 $this->mock->foo();1358 $this->mock->foo(1);1359 $this->mock->foo(1, array('a'=>1), 3);1360 }1361 public function testHasValueConstraintThrowsExceptionWhenConstraintUnmatched()1362 {1363 $this->mock->shouldReceive('foo')->with(Mockery::hasValue(2));1364 $this->expectException(\Mockery\Exception::class);1365 $this->mock->foo(array('a'=>1, 'b'=>3));1366 Mockery::close();1367 }1368 public function testCaptureStoresArgumentOfTypeScalar_ClosureEvaluatesToTrue()1369 {1370 $temp = null;1371 $this->mock->shouldReceive('foo')->with(Mockery::capture($temp))->once();1372 $this->mock->foo(4);1373 $this->assertSame(4, $temp);1374 }1375 public function testCaptureStoresArgumentOfTypeArgument_ClosureEvaluatesToTrue()1376 {1377 $object = new stdClass();1378 $temp = null;1379 $this->mock->shouldReceive('foo')->with(Mockery::capture($temp))->once();1380 $this->mock->foo($object);1381 $this->assertSame($object, $temp);1382 }1383 public function testOnConstraintMatchesArgument_ClosureEvaluatesToTrue()1384 {1385 $function = function ($arg) {1386 return $arg % 2 == 0;1387 };1388 $this->mock->shouldReceive('foo')->with(Mockery::on($function))->once();1389 $this->mock->foo(4);1390 }1391 public function testOnConstraintMatchesArgumentOfTypeArray_ClosureEvaluatesToTrue()1392 {1393 $function = function ($arg) {1394 return is_array($arg);1395 };1396 $this->mock->shouldReceive('foo')->with(Mockery::on($function))->once();1397 $this->mock->foo([4, 5]);1398 }1399 public function testOnConstraintThrowsExceptionWhenConstraintUnmatched_ClosureEvaluatesToFalse()1400 {1401 $function = function ($arg) {1402 return $arg % 2 == 0;1403 };1404 $this->mock->shouldReceive('foo')->with(Mockery::on($function));1405 $this->expectException(\Mockery\Exception::class);1406 $this->mock->foo(5);1407 Mockery::close();1408 }1409 public function testMustBeConstraintMatchesArgument()1410 {1411 $this->mock->shouldReceive('foo')->with(Mockery::mustBe(2))->once();1412 $this->mock->foo(2);1413 }1414 public function testMustBeConstraintNonMatchingCase()1415 {1416 $this->mock->shouldReceive('foo')->times(3);1417 $this->mock->shouldReceive('foo')->with(1, Mockery::mustBe(2))->never();1418 $this->mock->foo();1419 $this->mock->foo(1);1420 $this->mock->foo(1, 2, 3);1421 }1422 public function testMustBeConstraintThrowsExceptionWhenConstraintUnmatched()1423 {1424 $this->mock->shouldReceive('foo')->with(Mockery::mustBe(2));1425 $this->expectException(\Mockery\Exception::class);1426 $this->mock->foo('2');1427 Mockery::close();1428 }1429 public function testMustBeConstraintMatchesObjectArgumentWithEqualsComparisonNotIdentical()1430 {1431 $a = new stdClass;1432 $a->foo = 1;1433 $b = new stdClass;1434 $b->foo = 1;1435 $this->mock->shouldReceive('foo')->with(Mockery::mustBe($a))->once();1436 $this->mock->foo($b);1437 }1438 public function testMustBeConstraintNonMatchingCaseWithObject()1439 {1440 $a = new stdClass;1441 $a->foo = 1;1442 $this->mock->shouldReceive('foo')->times(3);1443 $this->mock->shouldReceive('foo')->with(1, Mockery::mustBe($a))->never();1444 $this->mock->foo();1445 $this->mock->foo(1);1446 $this->mock->foo(1, $a, 3);1447 }1448 public function testMustBeConstraintThrowsExceptionWhenConstraintUnmatchedWithObject()1449 {1450 $a = new stdClass;1451 $a->foo = 1;1452 $b = new stdClass;1453 $b->foo = 2;1454 $this->mock->shouldReceive('foo')->with(Mockery::mustBe($a));1455 $this->expectException(\Mockery\Exception::class);1456 $this->mock->foo($b);1457 Mockery::close();1458 }1459 public function testMatchPrecedenceBasedOnExpectedCallsFavouringExplicitMatch()1460 {1461 $this->mock->shouldReceive('foo')->with(1)->once();1462 $this->mock->shouldReceive('foo')->with(Mockery::any())->never();1463 $this->mock->foo(1);1464 }1465 public function testMatchPrecedenceBasedOnExpectedCallsFavouringAnyMatch()1466 {1467 $this->mock->shouldReceive('foo')->with(Mockery::any())->once();1468 $this->mock->shouldReceive('foo')->with(1)->never();1469 $this->mock->foo(1);1470 }1471 public function testReturnNullIfIgnoreMissingMethodsSet()1472 {1473 $this->mock->shouldIgnoreMissing();1474 $this->assertNull($this->mock->g(1, 2));1475 }1476 public function testReturnUndefinedIfIgnoreMissingMethodsSet()1477 {1478 $this->mock->shouldIgnoreMissing()->asUndefined();1479 $this->assertInstanceOf(\Mockery\Undefined::class, $this->mock->g(1, 2));1480 }1481 public function testReturnAsUndefinedAllowsForInfiniteSelfReturningChain()1482 {1483 $this->mock->shouldIgnoreMissing()->asUndefined();1484 $this->assertInstanceOf(\Mockery\Undefined::class, $this->mock->g(1, 2)->a()->b()->c());1485 }1486 public function testShouldIgnoreMissingFluentInterface()1487 {1488 $this->assertInstanceOf(\Mockery\MockInterface::class, $this->mock->shouldIgnoreMissing());1489 }1490 public function testShouldIgnoreMissingAsUndefinedFluentInterface()1491 {1492 $this->assertInstanceOf(\Mockery\MockInterface::class, $this->mock->shouldIgnoreMissing()->asUndefined());1493 }1494 public function testShouldIgnoreMissingAsDefinedProxiesToUndefinedAllowingToString()1495 {1496 $this->mock->shouldIgnoreMissing()->asUndefined();1497 $this->assertTrue(is_string("{$this->mock->g()}"));1498 $this->assertTrue(is_string("{$this->mock}"));1499 }1500 public function testShouldIgnoreMissingDefaultReturnValue()1501 {1502 $this->mock->shouldIgnoreMissing(1);1503 $this->assertEquals(1, $this->mock->a());1504 }1505 /** @issue #253 */1506 public function testShouldIgnoreMissingDefaultSelfAndReturnsSelf()1507 {1508 $this->mock->shouldIgnoreMissing(\Mockery::self());1509 $this->assertSame($this->mock, $this->mock->a()->b());1510 }1511 public function testToStringMagicMethodCanBeMocked()1512 {1513 $this->mock->shouldReceive("__toString")->andReturn('dave');1514 $this->assertEquals("{$this->mock}", "dave");1515 }1516 public function testOptionalMockRetrieval()1517 {1518 $m = mock('f')->shouldReceive('foo')->with(1)->andReturn(3)->mock();1519 $this->assertInstanceOf(\Mockery\MockInterface::class, $m);1520 }1521 public function testNotConstraintMatchesArgument()1522 {1523 $this->mock->shouldReceive('foo')->with(Mockery::not(1))->once();1524 $this->mock->foo(2);1525 }1526 public function testNotConstraintNonMatchingCase()1527 {1528 $this->mock->shouldReceive('foo')->times(3);1529 $this->mock->shouldReceive('foo')->with(1, Mockery::not(2))->never();1530 $this->mock->foo();1531 $this->mock->foo(1);1532 $this->mock->foo(1, 2, 3);1533 }1534 public function testNotConstraintThrowsExceptionWhenConstraintUnmatched()1535 {1536 $this->mock->shouldReceive('foo')->with(Mockery::not(2));1537 $this->expectException(\Mockery\Exception::class);1538 $this->mock->foo(2);1539 Mockery::close();1540 }1541 public function testAnyOfConstraintMatchesArgument()1542 {1543 $this->mock->shouldReceive('foo')->with(Mockery::anyOf(1, 2))->twice();1544 $this->mock->foo(2);1545 $this->mock->foo(1);1546 }1547 public function testAnyOfConstraintNonMatchingCase()1548 {1549 $this->mock->shouldReceive('foo')->times(3);1550 $this->mock->shouldReceive('foo')->with(1, Mockery::anyOf(1, 2))->never();1551 $this->mock->foo();1552 $this->mock->foo(1);1553 $this->mock->foo(1, 2, 3);1554 }1555 public function testAnyOfConstraintThrowsExceptionWhenConstraintUnmatched()1556 {1557 $this->mock->shouldReceive('foo')->with(Mockery::anyOf(1, 2));1558 $this->expectException(\Mockery\Exception::class);1559 $this->mock->foo(3);1560 Mockery::close();1561 }1562 public function testAnyOfConstraintThrowsExceptionWhenTrueIsNotAnExpectedArgument()1563 {1564 $this->mock->shouldReceive('foo')->with(Mockery::anyOf(1, 2));1565 $this->expectException(\Mockery\Exception::class);1566 $this->mock->foo(true);1567 }1568 public function testAnyOfConstraintThrowsExceptionWhenFalseIsNotAnExpectedArgument()1569 {1570 $this->mock->shouldReceive('foo')->with(Mockery::anyOf(0, 1, 2));1571 $this->expectException(\Mockery\Exception::class);1572 $this->mock->foo(false);1573 }1574 public function testNotAnyOfConstraintMatchesArgument()1575 {1576 $this->mock->shouldReceive('foo')->with(Mockery::notAnyOf(1, 2))->once();1577 $this->mock->foo(3);1578 }1579 public function testNotAnyOfConstraintNonMatchingCase()1580 {1581 $this->mock->shouldReceive('foo')->times(3);1582 $this->mock->shouldReceive('foo')->with(1, Mockery::notAnyOf(1, 2))->never();1583 $this->mock->foo();1584 $this->mock->foo(1);1585 $this->mock->foo(1, 4, 3);1586 }1587 public function testNotAnyOfConstraintThrowsExceptionWhenConstraintUnmatched()1588 {1589 $this->mock->shouldReceive('foo')->with(Mockery::notAnyOf(1, 2));1590 $this->expectException(\Mockery\Exception::class);1591 $this->mock->foo(2);1592 Mockery::close();1593 }1594 public function testPatternConstraintMatchesArgument()1595 {1596 $this->mock->shouldReceive('foo')->with(Mockery::pattern('/foo.*/'))->once();1597 $this->mock->foo('foobar');1598 }1599 public function testPatternConstraintNonMatchingCase()1600 {1601 $this->mock->shouldReceive('foo')->once();1602 $this->mock->shouldReceive('foo')->with(Mockery::pattern('/foo.*/'))->never();1603 $this->mock->foo('bar');1604 }1605 public function testPatternConstraintThrowsExceptionWhenConstraintUnmatched()1606 {1607 $this->mock->shouldReceive('foo')->with(Mockery::pattern('/foo.*/'));1608 $this->expectException(\Mockery\Exception::class);1609 $this->mock->foo('bar');1610 Mockery::close();1611 }1612 public function testGlobalConfigMayForbidMockingNonExistentMethodsOnClasses()1613 {1614 \Mockery::getConfiguration()->allowMockingNonExistentMethods(false);1615 $mock = mock('stdClass');1616 $this->expectException(\Mockery\Exception::class);1617 $mock->shouldReceive('foo');1618 Mockery::close();1619 }1620 public function testGlobalConfigMayForbidMockingNonExistentMethodsOnAutoDeclaredClasses()1621 {1622 \Mockery::getConfiguration()->allowMockingNonExistentMethods(false);1623 $this->expectException(\Mockery\Exception::class);1624 $this->expectExceptionMessage("Mockery can't find 'SomeMadeUpClass' so can't mock it");1625 $mock = mock('SomeMadeUpClass');1626 $mock->shouldReceive('foo');1627 Mockery::close();1628 }1629 public function testGlobalConfigMayForbidMockingNonExistentMethodsOnObjects()1630 {1631 \Mockery::getConfiguration()->allowMockingNonExistentMethods(false);1632 $mock = mock(new stdClass);1633 $this->expectException(\Mockery\Exception::class);1634 $mock->shouldReceive('foo');1635 Mockery::close();1636 }1637 public function testAnExampleWithSomeExpectationAmends()1638 {1639 $service = mock('MyService');1640 $service->shouldReceive('login')->with('user', 'pass')->once()->andReturn(true);1641 $service->shouldReceive('hasBookmarksTagged')->with('php')->once()->andReturn(false);1642 $service->shouldReceive('addBookmark')->with(Mockery::pattern('/^http:/'), \Mockery::type('string'))->times(3)->andReturn(true);1643 $service->shouldReceive('hasBookmarksTagged')->with('php')->once()->andReturn(true);1644 $this->assertTrue($service->login('user', 'pass'));1645 $this->assertFalse($service->hasBookmarksTagged('php'));1646 $this->assertTrue($service->addBookmark('http://example.com/1', 'some_tag1'));1647 $this->assertTrue($service->addBookmark('http://example.com/2', 'some_tag2'));1648 $this->assertTrue($service->addBookmark('http://example.com/3', 'some_tag3'));1649 $this->assertTrue($service->hasBookmarksTagged('php'));1650 }1651 public function testAnExampleWithSomeExpectationAmendsOnCallCounts()1652 {1653 $service = mock('MyService');1654 $service->shouldReceive('login')->with('user', 'pass')->once()->andReturn(true);1655 $service->shouldReceive('hasBookmarksTagged')->with('php')->once()->andReturn(false);1656 $service->shouldReceive('addBookmark')->with(Mockery::pattern('/^http:/'), \Mockery::type('string'))->times(3)->andReturn(true);1657 $service->shouldReceive('hasBookmarksTagged')->with('php')->twice()->andReturn(true);1658 $this->assertTrue($service->login('user', 'pass'));1659 $this->assertFalse($service->hasBookmarksTagged('php'));1660 $this->assertTrue($service->addBookmark('http://example.com/1', 'some_tag1'));1661 $this->assertTrue($service->addBookmark('http://example.com/2', 'some_tag2'));1662 $this->assertTrue($service->addBookmark('http://example.com/3', 'some_tag3'));1663 $this->assertTrue($service->hasBookmarksTagged('php'));1664 $this->assertTrue($service->hasBookmarksTagged('php'));1665 }1666 public function testAnExampleWithSomeExpectationAmendsOnCallCounts_PHPUnitTest()1667 {1668 $service = $this->createMock('MyService2');1669 $service->expects($this->once())->method('login')->with('user', 'pass')->will($this->returnValue(true));1670 $service->expects($this->exactly(3))->method('hasBookmarksTagged')->with('php')1671 ->will($this->onConsecutiveCalls(false, true, true));1672 $service->expects($this->exactly(3))->method('addBookmark')1673 ->with($this->matchesRegularExpression('/^http:/'), $this->isType('string'))1674 ->will($this->returnValue(true));1675 $this->assertTrue($service->login('user', 'pass'));1676 $this->assertFalse($service->hasBookmarksTagged('php'));1677 $this->assertTrue($service->addBookmark('http://example.com/1', 'some_tag1'));1678 $this->assertTrue($service->addBookmark('http://example.com/2', 'some_tag2'));1679 $this->assertTrue($service->addBookmark('http://example.com/3', 'some_tag3'));1680 $this->assertTrue($service->hasBookmarksTagged('php'));1681 $this->assertTrue($service->hasBookmarksTagged('php'));1682 }1683 public function testMockedMethodsCallableFromWithinOriginalClass()1684 {1685 $mock = mock('MockeryTest_InterMethod1[doThird]');1686 $mock->shouldReceive('doThird')->andReturn(true);1687 $this->assertTrue($mock->doFirst());1688 }1689 /**1690 * @group issue #201691 */1692 public function testMockingDemeterChainsPassesMockeryExpectationToCompositeExpectation()1693 {1694 $mock = mock('Mockery_Demeterowski');1695 $mock->shouldReceive('foo->bar->baz')->andReturn('Spam!');1696 $demeter = new Mockery_UseDemeter($mock);1697 $this->assertSame('Spam!', $demeter->doit());1698 }1699 /**1700 * @group issue #20 - with args in demeter chain1701 */1702 public function testMockingDemeterChainsPassesMockeryExpectationToCompositeExpectationWithArgs()1703 {1704 $mock = mock('Mockery_Demeterowski');1705 $mock->shouldReceive('foo->bar->baz')->andReturn('Spam!');1706 $demeter = new Mockery_UseDemeter($mock);1707 $this->assertSame('Spam!', $demeter->doitWithArgs());1708 }1709 public function testShouldNotReceiveCanBeAddedToCompositeExpectation()1710 {1711 $mock = mock('Foo');1712 $mock->shouldReceive('a')->once()->andReturn('Spam!')1713 ->shouldNotReceive('b');1714 $mock->a();1715 }1716 public function testPassthruEnsuresRealMethodCalledForReturnValues()1717 {1718 $mock = mock('MockeryTest_SubjectCall1');1719 $mock->shouldReceive('foo')->once()->passthru();1720 $this->assertEquals('bar', $mock->foo());1721 }1722 public function testPassthruCallMagic()1723 {1724 $mock = mock('Mockery_Magic');1725 $mock->shouldReceive('theAnswer')->once()->passthru();1726 $this->assertSame(42, $mock->theAnswer());1727 }1728 public function testShouldIgnoreMissingExpectationBasedOnArgs()1729 {1730 $mock = mock("MyService2")->shouldIgnoreMissing();1731 $mock->shouldReceive("hasBookmarksTagged")->with("dave")->once();1732 $mock->hasBookmarksTagged("dave");1733 $mock->hasBookmarksTagged("padraic");1734 }1735 public function testMakePartialExpectationBasedOnArgs()1736 {1737 $mock = mock("MockeryTest_SubjectCall1")->makePartial();1738 $this->assertEquals('bar', $mock->foo());1739 $this->assertEquals('bar', $mock->foo("baz"));1740 $this->assertEquals('bar', $mock->foo("qux"));1741 $mock->shouldReceive("foo")->with("baz")->twice()->andReturn('123');1742 $this->assertEquals('bar', $mock->foo());1743 $this->assertEquals('123', $mock->foo("baz"));1744 $this->assertEquals('bar', $mock->foo("qux"));1745 $mock->shouldReceive("foo")->withNoArgs()->once()->andReturn('456');1746 $this->assertEquals('456', $mock->foo());1747 $this->assertEquals('123', $mock->foo("baz"));1748 $this->assertEquals('bar', $mock->foo("qux"));1749 }1750 public function testCanReturnSelf()1751 {1752 $this->mock->shouldReceive("foo")->andReturnSelf();1753 $this->assertSame($this->mock, $this->mock->foo());1754 }1755 public function testReturnsTrueIfTrueIsReturnValue()1756 {1757 $this->mock->shouldReceive("foo")->andReturnTrue();1758 $this->assertTrue($this->mock->foo());1759 }1760 public function testReturnsFalseIfFalseIsReturnValue()1761 {1762 $this->mock->shouldReceive("foo")->andReturnFalse();1763 $this->assertFalse($this->mock->foo());1764 }1765 public function testExpectationCanBeOverridden()1766 {1767 $this->mock->shouldReceive('foo')->once()->andReturn('green');1768 $this->mock->shouldReceive('foo')->andReturn('blue');1769 $this->assertEquals('green', $this->mock->foo());1770 $this->assertEquals('blue', $this->mock->foo());1771 }1772 public function testTimesExpectationForbidsFloatNumbers()1773 {1774 $this->expectException(\InvalidArgumentException::class);1775 $this->mock->shouldReceive('foo')->times(1.3);1776 Mockery::close();1777 }1778 public function testIfExceptionIndicatesAbsenceOfMethodAndExpectationsOnMock()1779 {1780 $mock = mock('Mockery_Duck');1781 $this->expectException(1782 '\BadMethodCallException',1783 'Method ' . get_class($mock) .1784 '::nonExistent() does not exist on this mock object'1785 );1786 $mock->nonExistent();1787 Mockery::close();1788 }1789 public function testIfCallingMethodWithNoExpectationsHasSpecificExceptionMessage()1790 {1791 $mock = mock('Mockery_Duck');1792 $this->expectException(1793 '\BadMethodCallException',1794 'Received ' . get_class($mock) .1795 '::quack(), ' . 'but no expectations were specified'1796 );1797 $mock->quack();1798 Mockery::close();1799 }1800 public function testMockShouldNotBeAnonymousWhenImplementingSpecificInterface()1801 {1802 $waterMock = mock('IWater');1803 $this->assertFalse($waterMock->mockery_isAnonymous());1804 }1805 public function testWetherMockWithInterfaceOnlyCanNotImplementNonExistingMethods()1806 {1807 \Mockery::getConfiguration()->allowMockingNonExistentMethods(false);1808 $waterMock = \Mockery::mock('IWater');1809 $this->expectException(\Mockery\Exception::class);1810 $waterMock1811 ->shouldReceive('nonExistentMethod')1812 ->once()1813 ->andReturnNull();1814 \Mockery::close();1815 }1816 public function testCountWithBecauseExceptionMessage()1817 {1818 $this->expectException(InvalidCountException::class);1819 $this->expectExceptionMessageRegexp(1820 '/Method foo\(<Any Arguments>\) from Mockery_[\d]+ should be called' . PHP_EOL . ' ' .1821 'exactly 1 times but called 0 times. Because We like foo/'1822 );1823 $this->mock->shouldReceive('foo')->once()->because('We like foo');1824 Mockery::close();1825 }1826 /** @test */1827 public function it_uses_a_matchers_to_string_method_in_the_exception_output()1828 {1829 $mock = Mockery::mock();1830 $mock->expects()->foo(Mockery::hasKey('foo'));1831 $this->expectException(1832 InvalidCountException::class,1833 "Method foo(<HasKey[foo]>)"1834 );1835 Mockery::close();1836 }1837}1838interface IWater1839{1840 public function dry();1841}1842class MockeryTest_SubjectCall11843{1844 public function foo()1845 {1846 return 'bar';1847 }1848}1849class MockeryTest_InterMethod11850{1851 public function doFirst()1852 {1853 return $this->doSecond();1854 }1855 private function doSecond()1856 {1857 return $this->doThird();1858 }1859 public function doThird()1860 {1861 return false;1862 }1863}1864class MyService21865{1866 public function login($user, $pass)1867 {1868 }1869 public function hasBookmarksTagged($tag)1870 {1871 }1872 public function addBookmark($uri, $tag)1873 {1874 }1875}1876class Mockery_Duck1877{1878 public function quack()1879 {1880 }1881 public function swim()1882 {1883 }1884}1885class Mockery_Duck_Nonswimmer1886{1887 public function quack()1888 {1889 }1890}1891class Mockery_Demeterowski1892{1893 public function foo()1894 {1895 return $this;1896 }1897 public function bar()1898 {1899 return $this;1900 }1901 public function baz()1902 {1903 return 'Ham!';1904 }1905}1906class Mockery_UseDemeter1907{1908 public function __construct($demeter)1909 {1910 $this->demeter = $demeter;1911 }1912 public function doit()1913 {1914 return $this->demeter->foo()->bar()->baz();1915 }1916 public function doitWithArgs()1917 {1918 return $this->demeter->foo("foo")->bar("bar")->baz("baz");1919 }1920}1921class MockeryTest_Foo1922{1923 public function foo()1924 {1925 }1926}1927class Mockery_Magic1928{1929 public function __call($method, $args)1930 {1931 return 42;1932 }1933}...

Full Screen

Full Screen

foo

Using AI Code Generation

copy

Full Screen

1$mock->foo();2$mock->bar();3$mock->foo();4$mock->bar();5$mock->cacheAll();6$mock->returns('foo', 'bar');7$mock->returns(array('foo', 'bar'), 'baz');8$mock->returns(function ($method, $params) {9 if ($method == 'foo' && $params[0] == 'bar') {

Full Screen

Full Screen

foo

Using AI Code Generation

copy

Full Screen

1$mock = new MockFoo();2$mock->expectOnce('foo');3$mock->foo();4$mock->tally();5$mock = new MockFoo();6$mock->expectNever('foo');7$mock->tally();8$mock = new MockFoo();9$mock->expectAt(0, 'foo');10$mock->expectAt(1, 'foo');11$mock->foo();12$mock->foo();13$mock->tally();14$mock = new MockFoo();15$mock->expectAt(0, 'foo');16$mock->expectAt(1, 'foo');17$mock->foo();18$mock->foo();19$mock->tally();20$mock = new MockFoo();21$mock->expectAt(0, 'foo');22$mock->expectAt(1, 'foo');23$mock->foo();24$mock->foo();25$mock->tally();26$mock = new MockFoo();27$mock->expectAt(0, 'foo');28$mock->expectAt(1, 'foo');29$mock->foo();30$mock->foo();31$mock->tally();32$mock = new MockFoo();33$mock->expectAt(0, 'foo');34$mock->expectAt(1, 'foo');35$mock->foo();36$mock->foo();37$mock->tally();38$mock = new MockFoo();39$mock->expectAt(0, 'foo');40$mock->expectAt(1, 'foo');41$mock->foo();42$mock->foo();43$mock->tally();44$mock = new MockFoo();45$mock->expectAt(0, 'foo');46$mock->expectAt(1, 'foo');47$mock->foo();48$mock->foo();49$mock->tally();

Full Screen

Full Screen

foo

Using AI Code Generation

copy

Full Screen

1$mock->foo();2$mock->bar();3$mock->baz();4$mock->foo();5$mock->bar();6$mock->baz();7$mock->foo();8$mock->bar();9$mock->baz();10$mock->foo();11$mock->bar();12$mock->baz();13$mock->foo();14$mock->bar();15$mock->baz();16$mock->foo();17$mock->bar();18$mock->baz();19$mock->foo();20$mock->bar();21$mock->baz();22$mock->foo();23$mock->bar();24$mock->baz();25$mock->foo();26$mock->bar();27$mock->baz();28$mock->foo();29$mock->bar();

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful