Skip to content

Commit

Permalink
Merge pull request #1052 from danielsd/fix-and-any-others-matcher
Browse files Browse the repository at this point in the history
Fix andAnyOthers() to properly match earlier expectations
  • Loading branch information
davedevelopment committed May 13, 2020
2 parents 2cbbf95 + 9ee0b47 commit 5f9688e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## 1.3.2 (XXXX-XX-XX)
* Fix andAnyOthers() to properly match earlier expectations (#1051)

## 1.3.1 (2019-12-26)
* Revert improved exception debugging due to BC breaks (#1032)

Expand All @@ -24,7 +27,7 @@

## 1.2.2 (2019-02-13)

* Fix a BC breaking change for PHP 5.6/PHPUnit 5.7.27 (#947)
* Fix a BC breaking change for PHP 5.6/PHPUnit 5.7.27 (#947)

## 1.2.1 (2019-02-07)

Expand Down Expand Up @@ -67,7 +70,7 @@
## 1.0.0 (2017-09-06)

* Destructors (`__destruct`) are stubbed out where it makes sense
* Allow passing a closure argument to `withArgs()` to validate multiple arguments at once.
* Allow passing a closure argument to `withArgs()` to validate multiple arguments at once.
* `Mockery\Adapter\Phpunit\TestListener` has been rewritten because it
incorrectly marked some tests as risky. It will no longer verify mock
expectations but instead check that tests do that themselves. PHPUnit 6 is
Expand All @@ -90,7 +93,7 @@
* BC BREAK - Fix Mockery not trying default expectations if there is any concrete expectation
* BC BREAK - Mockery's PHPUnit integration will mark a test as risky if it
thinks one it's exceptions has been swallowed in PHPUnit > 5.7.6. Use `$e->dismiss()` to dismiss.

## 0.9.4 (XXXX-XX-XX)

* `shouldIgnoreMissing` will respect global `allowMockingNonExistentMethods`
Expand Down
3 changes: 1 addition & 2 deletions library/Mockery/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,7 @@ public function matchArgs(array $args)
reset($this->_expectedArgs);

if ($this->isAndAnyOtherArgumentsMatcher($lastExpectedArgument)) {
$argCountToSkipMatching = $argCount - count($this->_expectedArgs);
$args = array_slice($args, 0, $argCountToSkipMatching);
$args = array_slice($args, 0, array_search($lastExpectedArgument, $this->_expectedArgs, true));
return $this->_matchArgs($args);
}

Expand Down
12 changes: 10 additions & 2 deletions tests/Mockery/ExpectationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ public function testAndAnyOtherConstraintMatchesTheRestOfTheArguments()
{
$this->mock->shouldReceive('foo')->with(1, 2, Mockery::andAnyOthers())->twice();
$this->mock->foo(1, 2, 3, 4, 5);
$this->mock->foo(1, 'str', 3, 4);
$this->mock->foo(1, 2, 'str', 3, 4);
}

public function testAndAnyOtherConstraintDoesNotPreventMatchingOfRegularArguments()
Expand All @@ -1092,6 +1092,14 @@ public function testAndAnyOtherConstraintDoesNotPreventMatchingOfRegularArgument
Mockery::close();
}

public function testAndAnyOtherConstraintMultipleExpectationsButNoOthers()
{
$this->mock->shouldReceive('foo')->with('a', Mockery::andAnyOthers())->andReturn('a');
$this->mock->shouldReceive('foo')->with('b', Mockery::andAnyOthers())->andReturn('b');
$this->assertEquals('a', $this->mock->foo('a'));
$this->assertEquals('b', $this->mock->foo('b'));
}

public function testArrayConstraintMatchesArgument()
{
$this->mock->shouldReceive('foo')->with(Mockery::type('array'))->once();
Expand Down Expand Up @@ -2078,7 +2086,7 @@ public function testWetherMockWithInterfaceOnlyCanNotImplementNonExistingMethods
public function testCountWithBecauseExceptionMessage()
{
$this->expectException(InvalidCountException::class);
$this->expectExceptionMessageRegexp(
$this->expectExceptionMessageRegExp(
'/Method foo\(<Any Arguments>\) from Mockery_[\d]+ should be called' . PHP_EOL . ' ' .
'exactly 1 times but called 0 times. Because We like foo/'
);
Expand Down

0 comments on commit 5f9688e

Please sign in to comment.