Skip to content

Commit

Permalink
Merge pull request #598 from davedevelopment/alternative-syntax
Browse files Browse the repository at this point in the history
Alternative shouldReceive syntax
  • Loading branch information
davedevelopment committed Jan 17, 2017
2 parents b4617ef + 2e4373e commit 2ff6984
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
29 changes: 29 additions & 0 deletions library/Mockery/HigherOrderMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Mockery;

class HigherOrderMessage
{
private $mock;
private $method;

public function __construct(MockInterface $mock, $method)
{
$this->mock = $mock;
$this->method = $method;
}

/**
* @return Mockery\Expectation
*/
public function __call($method, $args)
{
$expectation = $this->mock->{$this->method}($method);

if ($this->method !== "shouldNotHaveReceived") {
return $expectation->withArgs($args);
}

return $expectation;
}
}
33 changes: 23 additions & 10 deletions library/Mockery/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace Mockery;

use Mockery\HigherOrderMessage;
use Mockery\MockInterface;

class Mock implements MockInterface
Expand Down Expand Up @@ -175,13 +176,13 @@ public function mockery_init(\Mockery\Container $container = null, $partialObjec
/**
* Set expected method calls
*
* @param string $methodName,... one or many methods that are expected to be called in this mock
* @return \Mockery\ExpectationInterface
* @param null|string $methodName,... one or many methods that are expected to be called in this mock
* @return \Mockery\ExpectationInterface|\Mockery\HigherOrderMessage
*/
public function shouldReceive($methodName)
public function shouldReceive($methodName = null)
{
if (func_num_args() < 1) {
throw new \InvalidArgumentException("At least one method name is required");
if ($methodName === null) {
return new HigherOrderMessage($this, "shouldReceive");
}

foreach (func_get_args() as $method) {
Expand Down Expand Up @@ -224,11 +225,15 @@ public function shouldReceive($methodName)
/**
* Shortcut method for setting an expectation that a method should not be called.
*
* @param string $methodName,... one or many methods that are expected not to be called in this mock
* @return \Mockery\Expectation
* @param null|string $methodName,... one or many methods that are expected not to be called in this mock
* @return \Mockery\Expectation|\Mockery\HigherOrderMessage
*/
public function shouldNotReceive($methodName)
public function shouldNotReceive($methodName = null)
{
if ($methodName === null) {
return new HigherOrderMessage($this, "shouldNotReceive");
}

$expectation = call_user_func_array(array($this, 'shouldReceive'), func_get_args());
$expectation->never();
return $expectation;
Expand Down Expand Up @@ -666,8 +671,12 @@ public function mockery_returnValueForMethod($name)
}
}

public function shouldHaveReceived($method, $args = null)
public function shouldHaveReceived($method = null, $args = null)
{
if ($method === null) {
return new HigherOrderMessage($this, "shouldHaveReceived");
}

$expectation = new \Mockery\VerificationExpectation($this, $method);
if (null !== $args) {
$expectation->withArgs($args);
Expand All @@ -679,8 +688,12 @@ public function shouldHaveReceived($method, $args = null)
return $director;
}

public function shouldNotHaveReceived($method, $args = null)
public function shouldNotHaveReceived($method = null, $args = null)
{
if ($method === null) {
return new HigherOrderMessage($this, "shouldNotHaveReceived");
}

$expectation = new \Mockery\VerificationExpectation($this, $method);
if (null !== $args) {
$expectation->withArgs($args);
Expand Down
18 changes: 9 additions & 9 deletions library/Mockery/MockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ public function mockery_init(\Mockery\Container $container = null, $partialObjec
/**
* Set expected method calls
*
* @param string $methodName,... one or many methods that are expected to be called in this mock
* @return \Mockery\Expectation
* @param null|string $methodName,... one or many methods that are expected to be called in this mock
* @return mixed
*/
public function shouldReceive($methodName);
public function shouldReceive($methodName = null);

/**
* Shortcut method for setting an expectation that a method should not be called.
*
* @param string $methodName,... one or many methods that are expected not to be called in this mock
* @return \Mockery\Expectation
* @param null|string $methodName,... one or many methods that are expected not to be called in this mock
* @return mixed
*/
public function shouldNotReceive($methodName);

Expand Down Expand Up @@ -80,16 +80,16 @@ public function shouldDeferMissing();
public function makePartial();

/**
* @param $method
* @param null|string $method
* @param null $args
* @return \Mockery\Expectation
* @return mixed
*/
public function shouldHaveReceived($method, $args = null);

/**
* @param $method
* @param null|string $method
* @param null $args
* @return null
* @return mixed
*/
public function shouldNotHaveReceived($method, $args = null);

Expand Down

0 comments on commit 2ff6984

Please sign in to comment.