Skip to content

Commit

Permalink
feat: only count assertions on expectations which can fail a test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jessica Mauerhan committed May 22, 2022
1 parent c72ff6f commit 72cf17b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
10 changes: 9 additions & 1 deletion library/Mockery/ExpectationDirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ public function getDefaultExpectations()
*/
public function getExpectationCount()
{
return count($this->getExpectations()) ?: count($this->getDefaultExpectations());
$count = 0;
/** @var Expectation $expectations */
$expectations = $this->getExpectations() ?: $this->getDefaultExpectations();
foreach ($expectations as $expectation) {
if ($expectation->isCallCountConstrained()) {
$count++;
}
}
return $count;
}
}
28 changes: 26 additions & 2 deletions tests/Mockery/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -992,10 +992,34 @@ public function testGetExpectationCount_freshContainer()
$this->assertEquals(0, Mockery::getContainer()->mockery_getExpectationCount());
}

public function testGetExpectationCount_simplestMock()
public function testGetExpectationCount_stub()
{
$m = mock();
$m->shouldReceive('foo')->andReturn('bar');
$m->shouldReceive('foo');
$this->assertEquals(0, Mockery::getContainer()->mockery_getExpectationCount());
}

public function testGetExpectationCount_mockWithOnce()
{
$m = mock();
$m->shouldReceive('foo')->once();
$this->assertEquals(1, Mockery::getContainer()->mockery_getExpectationCount());
$m->foo();
}

public function testGetExpectationCount_mockWithAtLeast()
{
$m = mock();
$m->shouldReceive('foo')->atLeast()->once();
$this->assertEquals(1, Mockery::getContainer()->mockery_getExpectationCount());
$m->foo();
$m->foo();
}

public function testGetExpectationCount_mockWithNever()
{
$m = mock();
$m->shouldReceive('foo')->never();
$this->assertEquals(1, Mockery::getContainer()->mockery_getExpectationCount());
}

Expand Down

0 comments on commit 72cf17b

Please sign in to comment.