Skip to content

Commit

Permalink
Merge 2a04ef3 into 2ff6984
Browse files Browse the repository at this point in the history
  • Loading branch information
davedevelopment committed Jan 17, 2017
2 parents 2ff6984 + 2a04ef3 commit 512a510
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 0 deletions.
2 changes: 2 additions & 0 deletions library/Mockery.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Mockery\Generator\StringManipulation\Pass\MethodDefinitionPass;
use Mockery\Generator\StringManipulation\Pass\RemoveBuiltinMethodsThatAreFinalPass;
use Mockery\Generator\StringManipulation\Pass\RemoveUnserializeForInternalSerializableClassesPass;
use Mockery\Generator\StringManipulation\Pass\AvoidMethodClashPass;
use Mockery\Loader\EvalLoader;
use Mockery\Loader\Loader;

Expand Down Expand Up @@ -228,6 +229,7 @@ public static function getDefaultGenerator()
new ClassNamePass(),
new InstanceMockPass(),
new InterfacePass(),
new AvoidMethodClashPass(),
new MethodDefinitionPass(),
new RemoveUnserializeForInternalSerializableClassesPass(),
new RemoveBuiltinMethodsThatAreFinalPass(),
Expand Down
11 changes: 11 additions & 0 deletions library/Mockery/CompositeExpectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ public function andReturn()
return $this->__call(__FUNCTION__, func_get_args());
}

/**
* Set a return value, or sequential queue of return values
*
* @param mixed ...
* @return self
*/
public function andReturns()
{
return call_user_func_array([$this, 'andReturn'], func_get_args());
}

/**
* Intercept any expectation calls and direct against all expectations
*
Expand Down
11 changes: 11 additions & 0 deletions library/Mockery/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,17 @@ public function andReturn()
return $this;
}

/**
* Set a return value, or sequential queue of return values
*
* @param mixed ...
* @return self
*/
public function andReturns()
{
return call_user_func_array([$this, 'andReturn'], func_get_args());
}

/**
* Return this mock, like a fluent interface
*
Expand Down
5 changes: 5 additions & 0 deletions library/Mockery/ExpectationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public function getMock();
* @return self
*/
public function andReturn();

/**
* @return self
*/
public function andReturns();
}
38 changes: 38 additions & 0 deletions library/Mockery/ExpectsHigherOrderMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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;

class ExpectsHigherOrderMessage extends HigherOrderMessage
{
public function __construct(MockInterface $mock)
{
return parent::__construct($mock, "shouldReceive");
}
/**
* @return Mockery\Expectation
*/
public function __call($method, $args)
{
$expectation = parent::__call($method, $args);

return $expectation->once();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\Generator\StringManipulation\Pass;

use Mockery\Generator\Method;
use Mockery\Generator\Parameter;
use Mockery\Generator\MockConfiguration;

class AvoidMethodClashPass implements Pass
{
public function apply($code, MockConfiguration $config)
{
$names = array_map(function ($method) {
return $method->getName();
}, $config->getMethodsToMock());

foreach (["allows", "expects"] as $method) {
if (in_array($method, $names)) {

$code = preg_replace(
"#// start method {$method}.*// end method {$method}#ms",
"",
$code
);
}

}

return $code;
}
}
18 changes: 18 additions & 0 deletions library/Mockery/HigherOrderMessage.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
<?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;

Expand Down
22 changes: 22 additions & 0 deletions library/Mockery/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use Mockery\HigherOrderMessage;
use Mockery\MockInterface;
use Mockery\ExpectsHigherOrderMessage;

class Mock implements MockInterface
{
Expand Down Expand Up @@ -222,6 +223,27 @@ public function shouldReceive($methodName = null)
return $lastExpectation;
}

// start method allows
/**
* @return HigherOrderMessage
*/
public function allows()
{
return $this->shouldReceive();
}
// end method allows

// start method expects
/**
* @return ExpectsHigherOrderMessage
*/
public function expects()
{
return new ExpectsHigherOrderMessage($this);
}
// end method expects


/**
* Shortcut method for setting an expectation that a method should not be called.
*
Expand Down
84 changes: 84 additions & 0 deletions tests/Mockery/AllowsExpectsSyntaxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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/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
* @subpackage UnitTests
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/

namespace test\Mockery;

use Mockery as m;
use Mockery\Spy;
use Mockery\Exception\InvalidCountException;

class ClassWithAllowsMethod
{
public function allows()
{
return 123;
}
}

class ClassWithExpectsMethod
{
public function expects()
{
return 123;
}
}

class AllowsExpectsSyntaxTest extends \PHPUnit_Framework_TestCase
{
/** @test */
public function allowsSetsUpMethodStub()
{
$stub = m::mock();
$stub->allows()->foo(123)->andReturns(456);

$this->assertEquals(456, $stub->foo(123));
}

/** @test */
public function generateSkipsAllowsMethodIfAlreadyExists()
{
$stub = m::mock("test\Mockery\ClassWithAllowsMethod");

$stub->shouldReceive('allows')->andReturn(123);

$this->assertEquals(123, $stub->allows());
}

/** @test */
public function expectsSetsUpExpectationOfOneCall()
{
$mock = m::mock();
$mock->expects()->foo(123);

$this->setExpectedException("Mockery\Exception\InvalidCountException");
m::close();
}

/** @test */
public function generateSkipsExpectsMethodIfAlreadyExists()
{
$stub = m::mock("test\Mockery\ClassWithExpectsMethod");

$stub->shouldReceive('expects')->andReturn(123);

$this->assertEquals(123, $stub->expects());
}
}

0 comments on commit 512a510

Please sign in to comment.