Skip to content

Commit

Permalink
Refactor library/Mockery/VerificationDirector.php (#1381)
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 Feb 29, 2024
2 parents 72d7072 + 735b92d commit 72f1eb6
Showing 1 changed file with 108 additions and 36 deletions.
144 changes: 108 additions & 36 deletions library/Mockery/VerificationDirector.php
Expand Up @@ -5,93 +5,165 @@
*
* @copyright https://github.com/mockery/mockery/blob/HEAD/COPYRIGHT.md
* @license https://github.com/mockery/mockery/blob/HEAD/LICENSE BSD 3-Clause License
*
* @link https://github.com/mockery/mockery for the canonical source repository
*/

namespace Mockery;

class VerificationDirector
{
private $receivedMethodCalls;
/**
* @var VerificationExpectation
*/
private $expectation;

/**
* @var ReceivedMethodCalls
*/
private $receivedMethodCalls;

public function __construct(ReceivedMethodCalls $receivedMethodCalls, VerificationExpectation $expectation)
{
$this->receivedMethodCalls = $receivedMethodCalls;
$this->expectation = $expectation;
}

public function verify()
/**
* @return self
*/
public function atLeast()
{
return $this->receivedMethodCalls->verify($this->expectation);
return $this->cloneWithoutCountValidatorsApplyAndVerify('atLeast', []);
}

public function with(...$args)
/**
* @return self
*/
public function atMost()
{
return $this->cloneApplyAndVerify("with", $args);
return $this->cloneWithoutCountValidatorsApplyAndVerify('atMost', []);
}

public function withArgs($args)
/**
* @param int $minimum
* @param int $maximum
*
* @return self
*/
public function between($minimum, $maximum)
{
return $this->cloneApplyAndVerify("withArgs", array($args));
return $this->cloneWithoutCountValidatorsApplyAndVerify('between', [$minimum, $maximum]);
}

public function withNoArgs()
/**
* @return self
*/
public function once()
{
return $this->cloneApplyAndVerify("withNoArgs", array());
return $this->cloneWithoutCountValidatorsApplyAndVerify('once', []);
}

public function withAnyArgs()
/**
* @param int $limit
*
* @return self
*/
public function times($limit = null)
{
return $this->cloneApplyAndVerify("withAnyArgs", array());
return $this->cloneWithoutCountValidatorsApplyAndVerify('times', [$limit]);
}

public function times($limit = null)
/**
* @return self
*/
public function twice()
{
return $this->cloneWithoutCountValidatorsApplyAndVerify("times", array($limit));
return $this->cloneWithoutCountValidatorsApplyAndVerify('twice', []);
}

public function once()
public function verify()
{
return $this->cloneWithoutCountValidatorsApplyAndVerify("once", array());
$this->receivedMethodCalls->verify($this->expectation);
}

public function twice()
/**
* @template TArgs
*
* @param TArgs $args
*
* @return self
*/
public function with(...$args)
{
return $this->cloneWithoutCountValidatorsApplyAndVerify("twice", array());
return $this->cloneApplyAndVerify('with', $args);
}

public function atLeast()
/**
* @return self
*/
public function withAnyArgs()
{
return $this->cloneWithoutCountValidatorsApplyAndVerify("atLeast", array());
return $this->cloneApplyAndVerify('withAnyArgs', []);
}

public function atMost()
/**
* @template TArgs
*
* @param TArgs $args
*
* @return self
*/
public function withArgs($args)
{
return $this->cloneWithoutCountValidatorsApplyAndVerify("atMost", array());
return $this->cloneApplyAndVerify('withArgs', [$args]);
}

public function between($minimum, $maximum)
/**
* @return self
*/
public function withNoArgs()
{
return $this->cloneWithoutCountValidatorsApplyAndVerify("between", array($minimum, $maximum));
return $this->cloneApplyAndVerify('withNoArgs', []);
}

protected function cloneWithoutCountValidatorsApplyAndVerify($method, $args)
/**
* @param string $method
* @param array $args
*
* @return self
*/
protected function cloneApplyAndVerify($method, $args)
{
$expectation = clone $this->expectation;
$expectation->clearCountValidators();
call_user_func_array(array($expectation, $method), $args);
$director = new VerificationDirector($this->receivedMethodCalls, $expectation);
$director->verify();
return $director;
$verificationExpectation = clone $this->expectation;

$verificationExpectation->{$method}(...$args);

$verificationDirector = new self($this->receivedMethodCalls, $verificationExpectation);

$verificationDirector->verify();

return $verificationDirector;
}

protected function cloneApplyAndVerify($method, $args)
/**
* @param string $method
* @param array $args
*
* @return self
*/
protected function cloneWithoutCountValidatorsApplyAndVerify($method, $args)
{
$expectation = clone $this->expectation;
call_user_func_array(array($expectation, $method), $args);
$director = new VerificationDirector($this->receivedMethodCalls, $expectation);
$director->verify();
return $director;
$verificationExpectation = clone $this->expectation;

$verificationExpectation->clearCountValidators();

$verificationExpectation->{$method}(...$args);

$verificationDirector = new self($this->receivedMethodCalls, $verificationExpectation);

$verificationDirector->verify();

return $verificationDirector;
}
}

0 comments on commit 72f1eb6

Please sign in to comment.