Skip to content

Commit

Permalink
Trigger an error in patch and throw in next minor
Browse files Browse the repository at this point in the history
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
  • Loading branch information
ghostwriter committed Aug 15, 2023
1 parent d828962 commit e181f30
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
10 changes: 8 additions & 2 deletions library/Mockery/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

class Expectation implements ExpectationInterface
{
public const ERROR_ZERO_INVOCATION = 'shouldNotReceive(), never(), times(0) chaining additional invocation count methods has been deprecated and will throw an exception in a future version of Mockery';
/**
* Mock object to which this expectation belongs
*
Expand Down Expand Up @@ -356,7 +357,7 @@ public function matchArgs(array $args)
protected function _matchArgs($args)
{
$argCount = count($args);
for ($i=0; $i<$argCount; $i++) {
for ($i = 0; $i < $argCount; $i++) {
$param =& $args[$i];
if (!$this->_matchArg($this->_expectedArgs[$i], $param)) {
return false;
Expand Down Expand Up @@ -728,7 +729,12 @@ public function times($limit = null)
}

if ($this->_expectedCount === 0) {
throw new \InvalidArgumentException('\Mockery\Expectation::shouldNotReceive() method does not accept chaining additional invocation count methods.');
@trigger_error(self::ERROR_ZERO_INVOCATION, E_USER_DEPRECATED);
// throw new \InvalidArgumentException(self::ERROR_ZERO_INVOCATION);
}

if ($limit === 0) {
$this->_countValidators = [];
}

$this->_expectedCount = $limit;
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Regression/Issue1328Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use InvalidArgumentException;
use Mockery;
use Mockery\Exception\InvalidCountException;
use Mockery\Expectation;
use PHPUnit\Framework\TestCase;

final class Issue1328Test extends TestCase
Expand All @@ -25,7 +26,16 @@ public function testShouldFailWithAnInvocationCountError(): void

public function testThrowsInvalidArgumentExceptionWhenInvocationCountChanges(): void
{
set_error_handler(
static function (int $errorCode, string $errorMessage): void {
restore_error_handler();
throw new InvalidArgumentException($errorMessage, $errorCode);
},
E_ALL
);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(Expectation::ERROR_ZERO_INVOCATION);

$mock = Mockery::mock(DateTime::class);

Expand All @@ -38,7 +48,16 @@ public function testThrowsInvalidArgumentExceptionWhenInvocationCountChanges():

public function testThrowsInvalidArgumentExceptionForChainingAdditionalInvocationCountMethod(): void
{
set_error_handler(
static function (int $errorCode, string $errorMessage): void {
restore_error_handler();
throw new InvalidArgumentException($errorMessage, $errorCode);
},
E_ALL
);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(Expectation::ERROR_ZERO_INVOCATION);

$mock = Mockery::mock(DateTime::class);

Expand Down

0 comments on commit e181f30

Please sign in to comment.