Skip to content

Commit

Permalink
Merge pull request #692 from davedevelopment/any-args
Browse files Browse the repository at this point in the history
Refactor arguments matching allowing specific AnyArgs matcher
  • Loading branch information
davedevelopment authored Feb 9, 2017
2 parents 0b23f71 + c2164bf commit de5feb9
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 21 deletions.
30 changes: 10 additions & 20 deletions library/Mockery/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

use Closure;
use Mockery\Matcher\MultiArgumentClosure;
use Mockery\Matcher\ArgumentListMatcher;
use Mockery\Matcher\AnyArgs;
use Mockery\Matcher\NoArgs;

class Expectation implements ExpectationInterface
{
Expand Down Expand Up @@ -125,13 +128,6 @@ class Expectation implements ExpectationInterface
*/
protected $_globally = false;

/**
* Flag indicating we expect no arguments
*
* @var bool
*/
protected $_noArgsExpectation = false;

/**
* Flag indicating if the return value should be obtained from the original
* class method instead of returning predefined values from the return queue
Expand All @@ -150,6 +146,7 @@ public function __construct(\Mockery\MockInterface $mock, $name)
{
$this->_mock = $mock;
$this->_name = $name;
$this->withAnyArgs();
}

/**
Expand Down Expand Up @@ -300,12 +297,12 @@ public function verify()
}

/**
* Check if the registered expectation is a MultiArgumentClosureExpectation.
* Check if the registered expectation is an ArgumentListMatcher
* @return bool
*/
private function isMultiArgumentClosureExpectation()
private function isArgumentListMatcher()
{
return (count($this->_expectedArgs) === 1 && ($this->_expectedArgs[0] instanceof \Mockery\Matcher\MultiArgumentClosure));
return (count($this->_expectedArgs) === 1 && ($this->_expectedArgs[0] instanceof ArgumentListMatcher));
}

/**
Expand All @@ -316,10 +313,7 @@ private function isMultiArgumentClosureExpectation()
*/
public function matchArgs(array $args)
{
if (empty($this->_expectedArgs) && !$this->_noArgsExpectation) {
return true;
}
if ($this->isMultiArgumentClosureExpectation()) {
if ($this->isArgumentListMatcher()) {
return $this->_matchArg($this->_expectedArgs[0], $args);
}
$argCount = count($args);
Expand Down Expand Up @@ -400,7 +394,6 @@ private function withArgsInArray(array $arguments)
return $this->withNoArgs();
}
$this->_expectedArgs = $arguments;
$this->_noArgsExpectation = false;
return $this;
}

Expand All @@ -413,7 +406,6 @@ private function withArgsInArray(array $arguments)
private function withArgsMatchedByClosure(Closure $closure)
{
$this->_expectedArgs = [new MultiArgumentClosure($closure)];
$this->_noArgsExpectation = false;
return $this;
}

Expand Down Expand Up @@ -444,8 +436,7 @@ public function withArgs($argsOrClosure)
*/
public function withNoArgs()
{
$this->_noArgsExpectation = true;
$this->_expectedArgs = [];
$this->_expectedArgs = [new NoArgs()];
return $this;
}

Expand All @@ -456,8 +447,7 @@ public function withNoArgs()
*/
public function withAnyArgs()
{
$this->_noArgsExpectation = false;
$this->_expectedArgs = array();
$this->_expectedArgs = [new AnyArgs()];
return $this;
}

Expand Down
40 changes: 40 additions & 0 deletions library/Mockery/Matcher/AnyArgs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/blob/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @copyright Copyright (c) 2017 Dave Marshall
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/

namespace Mockery\Matcher;

class AnyArgs extends MatcherAbstract implements ArgumentListMatcher
{
/**
* @inheritdoc
*/
public function match(&$actual)
{
return true;
}

/**
* @inheritdoc
*/
public function __toString()
{
return '<Any Arguments>';
}
}
26 changes: 26 additions & 0 deletions library/Mockery/Matcher/ArgumentListMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/blob/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @copyright Copyright (c) 2017 Dave Marshall
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/

namespace Mockery\Matcher;

interface ArgumentListMatcher
{

}
2 changes: 1 addition & 1 deletion library/Mockery/Matcher/MultiArgumentClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace Mockery\Matcher;

class MultiArgumentClosure extends MatcherAbstract
class MultiArgumentClosure extends MatcherAbstract implements ArgumentListMatcher
{

/**
Expand Down
40 changes: 40 additions & 0 deletions library/Mockery/Matcher/NoArgs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/blob/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @copyright Copyright (c) 2017 Dave Marshall
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/

namespace Mockery\Matcher;

class NoArgs extends MatcherAbstract implements ArgumentListMatcher
{
/**
* @inheritdoc
*/
public function match(&$actual)
{
return count($actual) == 0;
}

/**
* @inheritdoc
*/
public function __toString()
{
return '<No Arguments>';
}
}
9 changes: 9 additions & 0 deletions library/helpers.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

use Mockery\Matcher\AnyArgs;
use Mockery\Matcher\NoArgs;
/**
* Mockery
*
Expand Down Expand Up @@ -35,3 +38,9 @@ function namedMock() {
return call_user_func_array([Mockery::class, "namedMock"], func_get_args());
}
}

if (!function_exists("anyArgs")) {
function anyArgs() {
return new AnyArgs();
}
}
1 change: 1 addition & 0 deletions tests/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function isAbsolutePath($path)
require_once $hamcrestPath;

Mockery::globalHelpers();

/*
* Unset global variables that are no longer needed.
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/Mockery/SpyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,13 @@ public function itIncrementsExpectationCountWhenShouldNotHaveReceivedIsUsed()
$spy->shouldNotHaveReceived('method');
$this->assertEquals(1, $spy->mockery_getExpectationCount());
}

/** @test */
public function any_args_can_be_used_with_alternative_syntax()
{
$spy = m::spy();
$spy->foo(123, 456);

$spy->shouldHaveReceived()->foo(anyArgs());
}
}

0 comments on commit de5feb9

Please sign in to comment.