Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include only call count constrained expectations in expectation count #844

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion library/Mockery/ExpectationDirector.php
Expand Up @@ -213,6 +213,12 @@ public function getDefaultExpectations()
*/
public function getExpectationCount()
{
return count($this->getExpectations());
$count = 0;
foreach ($this->getExpectations() as $expectation) {
if ($expectation->isCallCountConstrained()) {
$count++;
}
}
return $count;
}
}
6 changes: 3 additions & 3 deletions tests/Mockery/ContainerTest.php
Expand Up @@ -476,7 +476,7 @@ public function testCanMockClassContainingAPublicWakeupMethod()
public function testCanMockClassUsingMagicCallMethodsInPlaceOfNormalMethods()
{
$m = Mockery::mock('Gateway');
$m->shouldReceive('iDoSomethingReallyCoolHere');
$m->shouldReceive('iDoSomethingReallyCoolHere')->once();
$m->iDoSomethingReallyCoolHere();
}

Expand All @@ -486,7 +486,7 @@ public function testCanMockClassUsingMagicCallMethodsInPlaceOfNormalMethods()
public function testCanPartialMockObjectUsingMagicCallMethodsInPlaceOfNormalMethods()
{
$m = Mockery::mock(new Gateway);
$m->shouldReceive('iDoSomethingReallyCoolHere');
$m->shouldReceive('iDoSomethingReallyCoolHere')->once();
$m->iDoSomethingReallyCoolHere();
}

Expand Down Expand Up @@ -913,7 +913,7 @@ public function testGetExpectationCount_simplestMock()
{
$m = mock();
$m->shouldReceive('foo')->andReturn('bar');
$this->assertEquals(1, Mockery::getContainer()->mockery_getExpectationCount());
$this->assertEquals(0, Mockery::getContainer()->mockery_getExpectationCount());
}

public function testMethodsReturningParamsByReferenceDoesNotErrorOut()
Expand Down
56 changes: 28 additions & 28 deletions tests/Mockery/ExpectationTest.php
Expand Up @@ -340,7 +340,7 @@ public function testMultipleExpectationsWithReturns()

public function testExpectsNoArguments()
{
$this->mock->shouldReceive('foo')->withNoArgs();
$this->mock->shouldReceive('foo')->withNoArgs()->once();
$this->mock->foo();
}

Expand All @@ -356,7 +356,7 @@ public function testExpectsNoArgumentsThrowsExceptionIfAnyPassed()

public function testExpectsArgumentsArray()
{
$this->mock->shouldReceive('foo')->withArgs(array(1, 2));
$this->mock->shouldReceive('foo')->withArgs(array(1, 2))->once();
$this->mock->foo(1, 2);
}

Expand Down Expand Up @@ -416,7 +416,7 @@ public function testExpectsArgumentsArrayAcceptAClosureThatValidatesPassedArgume
$closure = function ($odd, $even) {
return ($odd % 2 != 0) && ($even % 2 == 0);
};
$this->mock->shouldReceive('foo')->withArgs($closure);
$this->mock->shouldReceive('foo')->withArgs($closure)->once();
$this->mock->foo(1, 2);
}

Expand All @@ -442,7 +442,7 @@ public function testExpectsArgumentsArrayClosureDoesNotThrowExceptionIfOptionalA
}
return $result;
};
$this->mock->shouldReceive('foo')->withArgs($closure);
$this->mock->shouldReceive('foo')->withArgs($closure)->once();
$this->mock->foo(1, 4);
}

Expand All @@ -455,7 +455,7 @@ public function testExpectsArgumentsArrayClosureDoesNotThrowExceptionIfOptionalA
}
return $result;
};
$this->mock->shouldReceive('foo')->withArgs($closure);
$this->mock->shouldReceive('foo')->withArgs($closure)->once();
$this->mock->foo(1, 4, 5);
}

Expand All @@ -478,15 +478,15 @@ public function testExpectsArgumentsArrayClosureThrowsExceptionIfOptionalArgumen

public function testExpectsAnyArguments()
{
$this->mock->shouldReceive('foo')->withAnyArgs();
$this->mock->shouldReceive('foo')->withAnyArgs()->times(3);
$this->mock->foo();
$this->mock->foo(1);
$this->mock->foo(1, 'k', new stdClass);
}

public function testExpectsArgumentMatchingObjectType()
{
$this->mock->shouldReceive('foo')->with('\stdClass');
$this->mock->shouldReceive('foo')->with('\stdClass')->once();
$this->mock->foo(new stdClass);
}

Expand Down Expand Up @@ -781,8 +781,8 @@ public function testCallCountingThrowsExceptionFirst()

public function testOrderedCallsWithoutError()
{
$this->mock->shouldReceive('foo')->ordered();
$this->mock->shouldReceive('bar')->ordered();
$this->mock->shouldReceive('foo')->ordered()->once();
$this->mock->shouldReceive('bar')->ordered()->once();
$this->mock->foo();
$this->mock->bar();
}
Expand All @@ -801,8 +801,8 @@ public function testOrderedCallsWithOutOfOrderError()

public function testDifferentArgumentsAndOrderingsPassWithoutException()
{
$this->mock->shouldReceive('foo')->with(1)->ordered();
$this->mock->shouldReceive('foo')->with(2)->ordered();
$this->mock->shouldReceive('foo')->with(1)->ordered()->once();
$this->mock->shouldReceive('foo')->with(2)->ordered()->once();
$this->mock->foo(1);
$this->mock->foo(2);
}
Expand All @@ -821,9 +821,9 @@ public function testDifferentArgumentsAndOrderingsThrowExceptionWhenInWrongOrder

public function testUnorderedCallsIgnoredForOrdering()
{
$this->mock->shouldReceive('foo')->with(1)->ordered();
$this->mock->shouldReceive('foo')->with(2);
$this->mock->shouldReceive('foo')->with(3)->ordered();
$this->mock->shouldReceive('foo')->with(1)->ordered()->once();
$this->mock->shouldReceive('foo')->with(2)->times(3);
$this->mock->shouldReceive('foo')->with(3)->ordered()->once();
$this->mock->foo(2);
$this->mock->foo(1);
$this->mock->foo(2);
Expand All @@ -833,8 +833,8 @@ public function testUnorderedCallsIgnoredForOrdering()

public function testOrderingOfDefaultGrouping()
{
$this->mock->shouldReceive('foo')->ordered();
$this->mock->shouldReceive('bar')->ordered();
$this->mock->shouldReceive('foo')->ordered()->once();
$this->mock->shouldReceive('bar')->ordered()->once();
$this->mock->foo();
$this->mock->bar();
}
Expand All @@ -853,10 +853,10 @@ public function testOrderingOfDefaultGroupingThrowsExceptionOnWrongOrder()

public function testOrderingUsingNumberedGroups()
{
$this->mock->shouldReceive('start')->ordered(1);
$this->mock->shouldReceive('foo')->ordered(2);
$this->mock->shouldReceive('bar')->ordered(2);
$this->mock->shouldReceive('final')->ordered();
$this->mock->shouldReceive('start')->ordered(1)->once();
$this->mock->shouldReceive('foo')->ordered(2)->once();
$this->mock->shouldReceive('bar')->ordered(2)->twice();
$this->mock->shouldReceive('final')->ordered()->once();
$this->mock->start();
$this->mock->bar();
$this->mock->foo();
Expand All @@ -866,10 +866,10 @@ public function testOrderingUsingNumberedGroups()

public function testOrderingUsingNamedGroups()
{
$this->mock->shouldReceive('start')->ordered('start');
$this->mock->shouldReceive('foo')->ordered('foobar');
$this->mock->shouldReceive('bar')->ordered('foobar');
$this->mock->shouldReceive('final')->ordered();
$this->mock->shouldReceive('start')->ordered('start')->once();
$this->mock->shouldReceive('foo')->ordered('foobar')->once();
$this->mock->shouldReceive('bar')->ordered('foobar')->twice();
$this->mock->shouldReceive('final')->ordered()->once();
$this->mock->start();
$this->mock->bar();
$this->mock->foo();
Expand Down Expand Up @@ -923,9 +923,9 @@ public function testExpectationMatchingWithAnyArgsOrderings()

public function testEnsuresOrderingIsNotCrossMockByDefault()
{
$this->mock->shouldReceive('foo')->ordered();
$this->mock->shouldReceive('foo')->ordered()->once();
$mock2 = mock('bar');
$mock2->shouldReceive('bar')->ordered();
$mock2->shouldReceive('bar')->ordered()->once();
$mock2->bar();
$this->mock->foo();
}
Expand Down Expand Up @@ -1020,8 +1020,8 @@ public function testDefaultExpectationsCanBeOrderedAndReplaced()
{
$this->mock->shouldReceive('foo')->ordered()->byDefault();
$this->mock->shouldReceive('bar')->ordered()->byDefault();
$this->mock->shouldReceive('bar')->ordered();
$this->mock->shouldReceive('foo')->ordered();
$this->mock->shouldReceive('bar')->ordered()->once();
$this->mock->shouldReceive('foo')->ordered()->once();
$this->mock->bar();
$this->mock->foo();
}
Expand Down
20 changes: 10 additions & 10 deletions tests/Mockery/MockTest.php
Expand Up @@ -28,7 +28,7 @@ public function testAnonymousMockWorksWithNotAllowingMockingOfNonExistentMethods
\Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
$m = mock();
$m->shouldReceive("test123")->andReturn(true);
assertThat($m->test123(), equalTo(true));
$this->assertTrue($m->test123());
\Mockery::getConfiguration()->allowMockingNonExistentMethods(true);
}

Expand All @@ -38,7 +38,7 @@ public function testMockWithNotAllowingMockingOfNonExistentMethodsCanBeGivenAddi
$m = mock('ExampleClassForTestingNonExistentMethod');
$m->shouldAllowMockingMethod('testSomeNonExistentMethod');
$m->shouldReceive("testSomeNonExistentMethod")->andReturn(true);
assertThat($m->testSomeNonExistentMethod(), equalTo(true));
$this->assertTrue($m->testSomeNonExistentMethod());
\Mockery::getConfiguration()->allowMockingNonExistentMethods(true);
}

Expand Down Expand Up @@ -105,24 +105,24 @@ public function testShouldIgnoreMissingCallingExistentMethods()
{
Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
$mock = mock('ClassWithMethods')->shouldIgnoreMissing();
assertThat(nullValue($mock->foo()));
$this->assertNull($mock->foo());
$mock->shouldReceive('bar')->passthru();
assertThat($mock->bar(), equalTo('bar'));
$this->assertSame('bar', $mock->bar());
}

public function testShouldIgnoreMissingCallingNonExistentMethods()
{
Mockery::getConfiguration()->allowMockingNonExistentMethods(true);
$mock = mock('ClassWithMethods')->shouldIgnoreMissing();
assertThat(nullValue($mock->foo()));
assertThat(nullValue($mock->bar()));
assertThat(nullValue($mock->nonExistentMethod()));
$this->assertNull($mock->foo());
$this->assertNull($mock->bar());
$this->assertNull($mock->nonExistentMethod());

$mock->shouldReceive(array('foo' => 'new_foo', 'nonExistentMethod' => 'result'));
$mock->shouldReceive('bar')->passthru();
assertThat($mock->foo(), equalTo('new_foo'));
assertThat($mock->bar(), equalTo('bar'));
assertThat($mock->nonExistentMethod(), equalTo('result'));
$this->assertSame('new_foo', $mock->foo());
$this->assertSame('bar', $mock->bar());
$this->assertSame('result', $mock->nonExistentMethod());
}

public function testCanMockException()
Expand Down
Expand Up @@ -30,6 +30,8 @@ public function shouldNotDuplicateDoublyInheritedMethods()
{
$container = new \Mockery\Container;
$mock = $container->mock('Mockery\Tests\Evenement_EventEmitter', 'Mockery\Tests\Chatroulette_ConnectionInterface');

$this->assertInstanceOf('Mockery\Tests\Evenement_EventEmitterInterface', $mock);
}
}

Expand Down
30 changes: 15 additions & 15 deletions tests/Mockery/MockingNullableMethodsTest.php
Expand Up @@ -49,7 +49,7 @@ public function itShouldAllowNonNullableTypeToBeSet()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");

$mock->shouldReceive('nonNullablePrimitive')->andReturn('a string');
$mock->shouldReceive('nonNullablePrimitive')->andReturn('a string')->once();
$mock->nonNullablePrimitive();
}

Expand All @@ -61,7 +61,7 @@ public function itShouldNotAllowNonNullToBeNull()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");

$mock->shouldReceive('nonNullablePrimitive')->andReturn(null);
$mock->shouldReceive('nonNullablePrimitive')->andReturn(null)->once();
$mock->nonNullablePrimitive();
}

Expand All @@ -72,7 +72,7 @@ public function itShouldAllowPrimitiveNullableToBeNull()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");

$mock->shouldReceive('nullablePrimitive')->andReturn(null);
$mock->shouldReceive('nullablePrimitive')->andReturn(null)->once();
$mock->nullablePrimitive();
}

Expand All @@ -83,7 +83,7 @@ public function itShouldAllowPrimitiveNullabeToBeSet()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");

$mock->shouldReceive('nullablePrimitive')->andReturn('a string');
$mock->shouldReceive('nullablePrimitive')->andReturn('a string')->once();
$mock->nullablePrimitive();
}

Expand All @@ -94,7 +94,7 @@ public function itShouldAllowSelfToBeSet()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");

$mock->shouldReceive('nonNullableSelf')->andReturn(new MethodWithNullableReturnType());
$mock->shouldReceive('nonNullableSelf')->andReturn(new MethodWithNullableReturnType())->once();
$mock->nonNullableSelf();
}

Expand All @@ -106,7 +106,7 @@ public function itShouldNotAllowSelfToBeNull()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");

$mock->shouldReceive('nonNullableSelf')->andReturn(null);
$mock->shouldReceive('nonNullableSelf')->andReturn(null)->once();
$mock->nonNullableSelf();
}

Expand All @@ -117,7 +117,7 @@ public function itShouldAllowNullableSelfToBeSet()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");

$mock->shouldReceive('nullableSelf')->andReturn(new MethodWithNullableReturnType());
$mock->shouldReceive('nullableSelf')->andReturn(new MethodWithNullableReturnType())->once();
$mock->nullableSelf();
}

Expand All @@ -128,7 +128,7 @@ public function itShouldAllowNullableSelfToBeNull()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");

$mock->shouldReceive('nullableSelf')->andReturn(null);
$mock->shouldReceive('nullableSelf')->andReturn(null)->once();
$mock->nullableSelf();
}

Expand All @@ -139,7 +139,7 @@ public function itShouldAllowClassToBeSet()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");

$mock->shouldReceive('nonNullableClass')->andReturn(new MethodWithNullableReturnType());
$mock->shouldReceive('nonNullableClass')->andReturn(new MethodWithNullableReturnType())->once();
$mock->nonNullableClass();
}

Expand All @@ -151,7 +151,7 @@ public function itShouldNotAllowClassToBeNull()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");

$mock->shouldReceive('nonNullableClass')->andReturn(null);
$mock->shouldReceive('nonNullableClass')->andReturn(null)->once();
$mock->nonNullableClass();
}

Expand All @@ -162,7 +162,7 @@ public function itShouldAllowNullalbeClassToBeSet()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");

$mock->shouldReceive('nullableClass')->andReturn(new MethodWithNullableReturnType());
$mock->shouldReceive('nullableClass')->andReturn(new MethodWithNullableReturnType())->once();
$mock->nullableClass();
}

Expand All @@ -173,7 +173,7 @@ public function itShouldAllowNullableClassToBeNull()
{
$mock = mock("test\Mockery\Fixtures\MethodWithNullableReturnType");

$mock->shouldReceive('nullableClass')->andReturn(null);
$mock->shouldReceive('nullableClass')->andReturn(null)->once();
$mock->nullableClass();
}

Expand All @@ -182,7 +182,7 @@ public function it_allows_returning_null_for_nullable_object_return_types()
{
$double= \Mockery::mock(MethodWithNullableReturnType::class);

$double->shouldReceive("nullableClass")->andReturnNull();
$double->shouldReceive("nullableClass")->andReturnNull()->once();

$this->assertNull($double->nullableClass());
}
Expand All @@ -192,7 +192,7 @@ public function it_allows_returning_null_for_nullable_string_return_types()
{
$double= \Mockery::mock(MethodWithNullableReturnType::class);

$double->shouldReceive("nullableString")->andReturnNull();
$double->shouldReceive("nullableString")->andReturnNull()->once();

$this->assertNull($double->nullableString());
}
Expand All @@ -202,7 +202,7 @@ public function it_allows_returning_null_for_nullable_int_return_types()
{
$double= \Mockery::mock(MethodWithNullableReturnType::class);

$double->shouldReceive("nullableInt")->andReturnNull();
$double->shouldReceive("nullableInt")->andReturnNull()->once();

$this->assertNull($double->nullableInt());
}
Expand Down