Skip to content

Commit

Permalink
Refactor library/Mockery/CountValidator/* (#1387)
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 Mar 6, 2024
2 parents 4b9e432 + 6043720 commit fb35769
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 22 deletions.
12 changes: 8 additions & 4 deletions library/Mockery/CountValidator/AtLeast.php
Expand Up @@ -4,20 +4,23 @@
* Mockery (https://docs.mockery.io/)
*
* @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
* @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\CountValidator;

use Mockery;
use Mockery\Exception\InvalidCountException;

use const PHP_EOL;

class AtLeast extends CountValidatorAbstract
{
/**
* Checks if the validator can accept an additional nth call
*
* @param int $n
*
* @return bool
*/
public function isEligible($n)
Expand All @@ -29,12 +32,13 @@ public function isEligible($n)
* Validate the call count against this validator
*
* @param int $n
*
* @return bool
*/
public function validate($n)
{
if ($this->_limit > $n) {
$exception = new Mockery\Exception\InvalidCountException(
$exception = new InvalidCountException(
'Method ' . (string) $this->_expectation
. ' from ' . $this->_expectation->getMock()->mockery_getName()
. ' should be called' . PHP_EOL
Expand Down
11 changes: 7 additions & 4 deletions library/Mockery/CountValidator/AtMost.php
Expand Up @@ -4,26 +4,29 @@
* Mockery (https://docs.mockery.io/)
*
* @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
* @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\CountValidator;

use Mockery;
use Mockery\Exception\InvalidCountException;

use const PHP_EOL;

class AtMost extends CountValidatorAbstract
{
/**
* Validate the call count against this validator
*
* @param int $n
*
* @return bool
*/
public function validate($n)
{
if ($this->_limit < $n) {
$exception = new Mockery\Exception\InvalidCountException(
$exception = new InvalidCountException(
'Method ' . (string) $this->_expectation
. ' from ' . $this->_expectation->getMock()->mockery_getName()
. ' should be called' . PHP_EOL
Expand Down
17 changes: 10 additions & 7 deletions library/Mockery/CountValidator/CountValidatorAbstract.php
Expand Up @@ -4,18 +4,20 @@
* Mockery (https://docs.mockery.io/)
*
* @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
* @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\CountValidator;

abstract class CountValidatorAbstract
use Mockery\Expectation;

abstract class CountValidatorAbstract implements CountValidatorInterface
{
/**
* Expectation for which this validator is assigned
*
* @var \Mockery\Expectation
* @var Expectation
*/
protected $_expectation = null;

Expand All @@ -29,10 +31,9 @@ abstract class CountValidatorAbstract
/**
* Set Expectation object and upper call limit
*
* @param \Mockery\Expectation $expectation
* @param int $limit
*/
public function __construct(\Mockery\Expectation $expectation, $limit)
public function __construct(Expectation $expectation, $limit)
{
$this->_expectation = $expectation;
$this->_limit = $limit;
Expand All @@ -42,17 +43,19 @@ public function __construct(\Mockery\Expectation $expectation, $limit)
* Checks if the validator can accept an additional nth call
*
* @param int $n
*
* @return bool
*/
public function isEligible($n)
{
return ($n < $this->_limit);
return $n < $this->_limit;
}

/**
* Validate the call count against this validator
*
* @param int $n
*
* @return bool
*/
abstract public function validate($n);
Expand Down
24 changes: 24 additions & 0 deletions library/Mockery/CountValidator/CountValidatorInterface.php
@@ -0,0 +1,24 @@
<?php

namespace Mockery\CountValidator;

interface CountValidatorInterface
{
/**
* Checks if the validator can accept an additional nth call
*
* @param int $n
*
* @return bool
*/
public function isEligible($n);

Check failure on line 14 in library/Mockery/CountValidator/CountValidatorInterface.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyUnusedMethod

library/Mockery/CountValidator/CountValidatorInterface.php:14:21: PossiblyUnusedMethod: Cannot find explicit calls to method Mockery\CountValidator\CountValidatorInterface::isEligible (but did find some potential callers) (see https://psalm.dev/087)

/**
* Validate the call count against this validator
*
* @param int $n
*
* @return bool
*/
public function validate($n);

Check failure on line 23 in library/Mockery/CountValidator/CountValidatorInterface.php

View workflow job for this annotation

GitHub Actions / Psalm

PossiblyUnusedMethod

library/Mockery/CountValidator/CountValidatorInterface.php:23:21: PossiblyUnusedMethod: Cannot find explicit calls to method Mockery\CountValidator\CountValidatorInterface::validate (but did find some potential callers) (see https://psalm.dev/087)
}
11 changes: 7 additions & 4 deletions library/Mockery/CountValidator/Exact.php
Expand Up @@ -4,28 +4,31 @@
* Mockery (https://docs.mockery.io/)
*
* @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
* @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\CountValidator;

use Mockery;
use Mockery\Exception\InvalidCountException;

use const PHP_EOL;

class Exact extends CountValidatorAbstract
{
/**
* Validate the call count against this validator
*
* @param int $n
*
* @return bool
*/
public function validate($n)
{
if ($this->_limit !== $n) {
$because = $this->_expectation->getExceptionMessage();

$exception = new Mockery\Exception\InvalidCountException(
$exception = new InvalidCountException(
'Method ' . (string) $this->_expectation
. ' from ' . $this->_expectation->getMock()->mockery_getName()
. ' should be called' . PHP_EOL
Expand Down
7 changes: 4 additions & 3 deletions library/Mockery/CountValidator/Exception.php
Expand Up @@ -4,14 +4,15 @@
* Mockery (https://docs.mockery.io/)
*
* @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
* @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\CountValidator;

use Mockery\Exception\MockeryExceptionInterface;
use OutOfBoundsException;

class Exception extends \OutOfBoundsException implements MockeryExceptionInterface
class Exception extends OutOfBoundsException implements MockeryExceptionInterface
{
}

0 comments on commit fb35769

Please sign in to comment.