Skip to content

Commit

Permalink
Added argument matchers: Not, AnyOf and NotAnyOf
Browse files Browse the repository at this point in the history
  • Loading branch information
Padraic Brady committed May 21, 2010
1 parent a18387c commit 5e1dde8
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 0 deletions.
33 changes: 33 additions & 0 deletions library/Mockery.php
Expand Up @@ -120,6 +120,39 @@ public static function mustBe($expected)
return $return;
}

/**
* Return instance of NOT matcher
*
* @return
*/
public static function not($expected)
{
$return = new \Mockery\Matcher\Not($expected);
return $return;
}

/**
* Return instance of ANYOF matcher
*
* @return
*/
public static function anyOf()
{
$return = new \Mockery\Matcher\AnyOf(func_get_args());
return $return;
}

/**
* Return instance of NOTANYOF matcher
*
* @return
*/
public static function notAnyOf()
{
$return = new \Mockery\Matcher\NotAnyOf(func_get_args());
return $return;
}

/**
* Utility method to format method name and args into a string
*
Expand Down
63 changes: 63 additions & 0 deletions library/Mockery/Matcher/AnyOf.php
@@ -0,0 +1,63 @@
<?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) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/

namespace Mockery\Matcher;

class AnyOf extends MatcherAbstract
{

/**
* Set the expected value
*
* @param mixed $expected
*/
public function __construct($expected = null)
{
$this->_expected = $expected;
}

/**
* Check if the actual value does not match the expected (in this
* case it's specifically NOT expected).
*
* @param mixed $actual
* @return bool
*/
public function match($actual)
{
foreach ($this->_expected as $exp) {
if ($actual === $exp || $actual == $exp) {
return true;
}
}
return false;
}

/**
* Return a string representation of this Matcher
*
* @return string
*/
public function __toString()
{
return '<AnyOf>';
}

}
48 changes: 48 additions & 0 deletions library/Mockery/Matcher/Not.php
@@ -0,0 +1,48 @@
<?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) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/

namespace Mockery\Matcher;

class Not extends MatcherAbstract
{

/**
* Check if the actual value does not match the expected (in this
* case it's specifically NOT expected).
*
* @param mixed $actual
* @return bool
*/
public function match($actual)
{
return $actual !== $this->_expected;
}

/**
* Return a string representation of this Matcher
*
* @return string
*/
public function __toString()
{
return '<Not>';
}

}
63 changes: 63 additions & 0 deletions library/Mockery/Matcher/NotAnyOf.php
@@ -0,0 +1,63 @@
<?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) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/

namespace Mockery\Matcher;

class NotAnyOf extends MatcherAbstract
{

/**
* Set the expected value
*
* @param mixed $expected
*/
public function __construct($expected = null)
{
$this->_expected = $expected;
}

/**
* Check if the actual value does not match the expected (in this
* case it's specifically NOT expected).
*
* @param mixed $actual
* @return bool
*/
public function match($actual)
{
foreach ($this->_expected as $exp) {
if ($actual === $exp || $actual == $exp) {
return false;
}
}
return true;
}

/**
* Return a string representation of this Matcher
*
* @return string
*/
public function __toString()
{
return '<AnyOf>';
}

}
82 changes: 82 additions & 0 deletions tests/Mockery/ExpectationTest.php
Expand Up @@ -1317,6 +1317,88 @@ public function testOptionalMockRetrieval()
$this->assertTrue($m instanceof \Mockery\MockInterface);
}

public function testNotConstraintMatchesArgument()
{
$this->mock->shouldReceive('foo')->with(Mockery::not(1))->once();
$this->mock->foo(2);
$this->container->mockery_verify();
}

public function testNotConstraintNonMatchingCase()
{
$this->mock->shouldReceive('foo')->times(3);
$this->mock->shouldReceive('foo')->with(1, Mockery::not(2))->never();
$this->mock->foo();
$this->mock->foo(1);
$this->mock->foo(1, 2, 3);
$this->container->mockery_verify();
}

/**
* @expectedException \Mockery\Exception
*/
public function testNotConstraintThrowsExceptionWhenConstraintUnmatched()
{
$this->mock->shouldReceive('foo')->with(Mockery::not(2))->once();
$this->mock->foo(2);
$this->container->mockery_verify();
}

public function testAnyOfConstraintMatchesArgument()
{
$this->mock->shouldReceive('foo')->with(Mockery::anyOf(1, 2))->twice();
$this->mock->foo(2);
$this->mock->foo(1);
$this->container->mockery_verify();
}

public function testAnyOfConstraintNonMatchingCase()
{
$this->mock->shouldReceive('foo')->times(3);
$this->mock->shouldReceive('foo')->with(1, Mockery::anyOf(1, 2))->never();
$this->mock->foo();
$this->mock->foo(1);
$this->mock->foo(1, 2, 3);
$this->container->mockery_verify();
}

/**
* @expectedException \Mockery\Exception
*/
public function testAnyOfConstraintThrowsExceptionWhenConstraintUnmatched()
{
$this->mock->shouldReceive('foo')->with(Mockery::anyOf(1, 2))->once();
$this->mock->foo(3);
$this->container->mockery_verify();
}

public function testNotAnyOfConstraintMatchesArgument()
{
$this->mock->shouldReceive('foo')->with(Mockery::notAnyOf(1, 2))->once();
$this->mock->foo(3);
$this->container->mockery_verify();
}

public function testNotAnyOfConstraintNonMatchingCase()
{
$this->mock->shouldReceive('foo')->times(3);
$this->mock->shouldReceive('foo')->with(1, Mockery::notAnyOf(1, 2))->never();
$this->mock->foo();
$this->mock->foo(1);
$this->mock->foo(1, 4, 3);
$this->container->mockery_verify();
}

/**
* @expectedException \Mockery\Exception
*/
public function testNotAnyOfConstraintThrowsExceptionWhenConstraintUnmatched()
{
$this->mock->shouldReceive('foo')->with(Mockery::notAnyOf(1, 2))->once();
$this->mock->foo(2);
$this->container->mockery_verify();
}

}

class Mockery_Duck {
Expand Down

0 comments on commit 5e1dde8

Please sign in to comment.