Skip to content

Commit

Permalink
Merge 9423190 into d4f0db5
Browse files Browse the repository at this point in the history
  • Loading branch information
tflori committed Jan 11, 2017
2 parents d4f0db5 + 9423190 commit 5e04fad
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 32 deletions.
66 changes: 38 additions & 28 deletions library/Mockery/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Expectation implements ExpectationInterface
/**
* Count validator store
*
* @var array
* @var \Mockery\CountValidator\CountValidatorAbstract[]
*/
protected $_countValidators = array();

Expand Down Expand Up @@ -238,6 +238,16 @@ public function isEligible()
return true;
}

public function isExpected()
{
foreach ($this->_countValidators as $validator) {
if ($validator->isEligible($this->_actualCount)) {
return true;
}
}
return false;
}

/**
* Check if there is a constraint on call count
*
Expand Down Expand Up @@ -358,7 +368,7 @@ protected function _matchArg($expected, &$actual)
* Expected argument setter for the expectation
*
* @param mixed ...
* @return self
* @return Expectation
*/
public function with()
{
Expand All @@ -369,7 +379,7 @@ public function with()
* Expected arguments for the expectation passed as an array
*
* @param array $arguments
* @return self
* @return Expectation
*/
private function withArgsInArray(array $arguments)
{
Expand All @@ -385,7 +395,7 @@ private function withArgsInArray(array $arguments)
* Expected arguments have to be matched by the given closure.
*
* @param Closure $closure
* @return self
* @return Expectation
*/
private function withArgsMatchedByClosure(Closure $closure)
{
Expand All @@ -399,7 +409,7 @@ private function withArgsMatchedByClosure(Closure $closure)
* each function call.
*
* @param array|Closure $argsOrClosure
* @return self
* @return Expectation
*/
public function withArgs($argsOrClosure)
{
Expand All @@ -417,7 +427,7 @@ public function withArgs($argsOrClosure)
/**
* Set with() as no arguments expected
*
* @return self
* @return Expectation
*/
public function withNoArgs()
{
Expand All @@ -429,7 +439,7 @@ public function withNoArgs()
/**
* Set expectation that any arguments are acceptable
*
* @return self
* @return Expectation
*/
public function withAnyArgs()
{
Expand All @@ -441,7 +451,7 @@ public function withAnyArgs()
* Set a return value, or sequential queue of return values
*
* @param mixed ...
* @return self
* @return Expectation
*/
public function andReturn()
{
Expand All @@ -452,7 +462,7 @@ public function andReturn()
/**
* Return this mock, like a fluent interface
*
* @return self
* @return Expectation
*/
public function andReturnSelf()
{
Expand All @@ -463,7 +473,7 @@ public function andReturnSelf()
* Set a sequential queue of return values with an array
*
* @param array $values
* @return self
* @return Expectation
*/
public function andReturnValues(array $values)
{
Expand All @@ -477,7 +487,7 @@ public function andReturnValues(array $values)
* closures as parameters.
*
* @param callable ...
* @return self
* @return Expectation
*/
public function andReturnUsing()
{
Expand All @@ -488,7 +498,7 @@ public function andReturnUsing()
/**
* Return a self-returning black hole object.
*
* @return self
* @return Expectation
*/
public function andReturnUndefined()
{
Expand All @@ -499,7 +509,7 @@ public function andReturnUndefined()
/**
* Return null. This is merely a language construct for Mock describing.
*
* @return self
* @return Expectation
*/
public function andReturnNull()
{
Expand All @@ -523,7 +533,7 @@ public function andReturnTrue()
* @param string $message
* @param int $code
* @param \Exception $previous
* @return self
* @return Expectation
*/
public function andThrow($exception, $message = '', $code = 0, \Exception $previous = null)
{
Expand All @@ -540,7 +550,7 @@ public function andThrow($exception, $message = '', $code = 0, \Exception $previ
* Set Exception classes to be thrown
*
* @param array $exceptions
* @return self
* @return Expectation
*/
public function andThrowExceptions(array $exceptions)
{
Expand All @@ -558,7 +568,7 @@ public function andThrowExceptions(array $exceptions)
*
* @param string $name
* @param mixed $value
* @return self
* @return Expectation
*/
public function andSet($name, $value)
{
Expand All @@ -574,7 +584,7 @@ public function andSet($name, $value)
*
* @param string $name
* @param mixed $value
* @return self
* @return Expectation
*/
public function set($name, $value)
{
Expand All @@ -584,7 +594,7 @@ public function set($name, $value)
/**
* Indicates this expectation should occur zero or more times
*
* @return self
* @return Expectation
*/
public function zeroOrMoreTimes()
{
Expand All @@ -596,7 +606,7 @@ public function zeroOrMoreTimes()
*
* @param int $limit
* @throws InvalidArgumentException
* @return self
* @return Expectation
*/
public function times($limit = null)
{
Expand All @@ -614,7 +624,7 @@ public function times($limit = null)
/**
* Indicates that this expectation is never expected to be called
*
* @return self
* @return Expectation
*/
public function never()
{
Expand All @@ -624,7 +634,7 @@ public function never()
/**
* Indicates that this expectation is expected exactly once
*
* @return self
* @return Expectation
*/
public function once()
{
Expand All @@ -634,7 +644,7 @@ public function once()
/**
* Indicates that this expectation is expected exactly twice
*
* @return self
* @return Expectation
*/
public function twice()
{
Expand All @@ -644,7 +654,7 @@ public function twice()
/**
* Sets next count validator to the AtLeast instance
*
* @return self
* @return Expectation
*/
public function atLeast()
{
Expand All @@ -655,7 +665,7 @@ public function atLeast()
/**
* Sets next count validator to the AtMost instance
*
* @return self
* @return Expectation
*/
public function atMost()
{
Expand All @@ -678,7 +688,7 @@ public function between($minimum, $maximum)
* Indicates that this expectation must be called in a specific given order
*
* @param string $group Name of the ordered group
* @return self
* @return Expectation
*/
public function ordered($group = null)
{
Expand All @@ -694,7 +704,7 @@ public function ordered($group = null)
/**
* Indicates call order should apply globally
*
* @return self
* @return Expectation
*/
public function globally()
{
Expand Down Expand Up @@ -736,7 +746,7 @@ public function getOrderNumber()
/**
* Mark this expectation as being a default
*
* @return self
* @return Expectation
*/
public function byDefault()
{
Expand All @@ -761,7 +771,7 @@ public function getMock()
* Flag this expectation as calling the original class method with the
* any provided arguments instead of using a return value queue.
*
* @return self
* @return Expectation
*/
public function passthru()
{
Expand Down
13 changes: 11 additions & 2 deletions library/Mockery/ExpectationDirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ExpectationDirector
/**
* Stores an array of all expectations for this mock
*
* @var array
* @var Expectation[]
*/
protected $_expectations = array();

Expand Down Expand Up @@ -134,7 +134,16 @@ public function verify()
public function findExpectation(array $args)
{
if (!empty($this->_expectations)) {
return $this->_findExpectationIn($this->_expectations, $args);
$orderedExpectations = array();
$reversedUnordered = array();
foreach ($this->_expectations as $exp) {
if ($exp->getOrderNumber() || $exp->isExpected()) {
array_push($orderedExpectations, $exp);
} else {
array_unshift($reversedUnordered, $exp);
}
}
return $this->_findExpectationIn(array_merge($orderedExpectations, $reversedUnordered), $args);
} else {
return $this->_findExpectationIn($this->_defaults, $args);
}
Expand Down
4 changes: 2 additions & 2 deletions library/Mockery/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Mock implements MockInterface
/**
* Stores an array of all expectation directors for this mock
*
* @var array
* @var ExpectationDirector[]
*/
protected $_mockery_expectations = array();

Expand Down Expand Up @@ -321,7 +321,7 @@ public function shouldExpect(\Closure $closure)
* this method will switch them from normal expectations to default
* expectations
*
* @return self
* @return Mock
*/
public function byDefault()
{
Expand Down
16 changes: 16 additions & 0 deletions tests/Mockery/ExpectationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,22 @@ public function testTimesExpectationForbidsFloatNumbers()
$this->mock->shouldReceive('foo')->times(1.3);
}


public function testFirstInLastOut()
{
// assign
$this->mock->shouldReceive('foo')->andReturn('anything');
$this->mock->shouldReceive('foo')->once()->with('bar')->andReturn('something');

// act
$withBar = $this->mock->foo('bar');
$withoutArgs = $this->mock->foo();

// assert
$this->assertEquals('something', $withBar);
$this->assertEquals('anything', $withoutArgs);
}

public function testIfExceptionIndicatesAbsenceOfMethodAndExpectationsOnMock()
{
$mock = $this->container->mock('Mockery_Duck');
Expand Down

0 comments on commit 5e04fad

Please sign in to comment.