Skip to content

Commit

Permalink
Adds a method to defer all calls by default to mock's parent
Browse files Browse the repository at this point in the history
  • Loading branch information
davedevelopment committed Jul 30, 2012
1 parent ef0446e commit 0d72d58
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -2,3 +2,5 @@
pearfarm.spec
*.sublime-project
library/Hamcrest/*
composer.lock
vendor/
10 changes: 10 additions & 0 deletions library/Mockery/Generator.php
Expand Up @@ -311,6 +311,8 @@ public static function _getStandardMethods($callTypehint = true, $makeInstanceMo
protected \$_mockery_ignoreMissing = false;
protected \$_mockery_deferMissing = false;
protected \$_mockery_verified = false;
protected \$_mockery_name = null;
Expand Down Expand Up @@ -372,6 +374,12 @@ public function shouldReceive()
return \$lastExpectation;
}
public function shouldDeferMissing()
{
\$this->_mockery_deferMissing = true;
return \$this;
}
public function shouldIgnoreMissing()
{
\$this->_mockery_ignoreMissing = true;
Expand Down Expand Up @@ -406,6 +414,8 @@ public function __call(\$method, $typehint \$args)
return \$handler->call(\$args);
} elseif (!is_null(\$this->_mockery_partial) && method_exists(\$this->_mockery_partial, \$method)) {
return call_user_func_array(array(\$this->_mockery_partial, \$method), \$args);
} elseif (\$this->_mockery_deferMissing && is_callable("parent::\$method")) {
return call_user_func_array("parent::\$method", \$args);
} elseif (\$this->_mockery_ignoreMissing) {
\$return = new \Mockery\Undefined;
return \$return;
Expand Down
24 changes: 24 additions & 0 deletions library/Mockery/Mock.php
Expand Up @@ -45,6 +45,14 @@ class Mock implements MockInterface
*/
protected $_mockery_ignoreMissing = false;

/**
* Flag to indicate whether we can defer method calls missing from our
* expectations
*
* @var bool
*/
protected $_mockery_deferMissing = false;

/**
* Flag to indicate whether this mock was verified
*
Expand Down Expand Up @@ -169,6 +177,20 @@ public function shouldIgnoreMissing()
return $this;
}

/**
* Set mock to defer unexpected methods to it's parent
*
* This is particularly useless for this class, as it doesn't have a parent,
* but included for completeness
*
* @return Mock
*/
public function shouldDeferMissing()
{
$this->_mockery_deferMissing = true;
return $this;
}

/**
* Accepts a closure which is executed with an object recorder which proxies
* to the partial source object. The intent being to record the
Expand Down Expand Up @@ -216,6 +238,8 @@ public function __call($method, array $args)
return $handler->call($args);
} elseif (!is_null($this->_mockery_partial) && method_exists($this->_mockery_partial, $method)) {
return call_user_func_array(array($this->_mockery_partial, $method), $args);
} elseif ($this->_mockery_deferMissing && is_callable("parent::$method")) {
return call_user_func_array("parent::$method", $args);
} elseif ($this->_mockery_ignoreMissing) {
$return = new \Mockery\Undefined;
return $return;
Expand Down
7 changes: 7 additions & 0 deletions library/Mockery/MockInterface.php
Expand Up @@ -48,6 +48,13 @@ public function shouldReceive();
*/
public function shouldIgnoreMissing();

/**
* Set mock to defer unexpected methods to it's parent if possible
*
* @return Mock
*/
public function shouldDeferMissing();

/**
* In the event shouldReceive() accepting an array of methods/returns
* this method will switch them from normal expectations to default
Expand Down
19 changes: 19 additions & 0 deletions tests/Mockery/ContainerTest.php
Expand Up @@ -95,6 +95,25 @@ public function testNamedMockWithConstructorArgsWithInternalCallToMockedMethod()
$this->assertEquals(123, $m->bar());
}

public function testNamedMockWithShouldDeferMissing()
{
$m = $this->container->mock("MockeryTest_ClassConstructor2", array($param1 = new stdClass()));
$m->shouldDeferMissing();
$this->assertEquals('foo', $m->bar());
$m->shouldReceive("bar")->andReturn(123);
$this->assertEquals(123, $m->bar());
}

/**
* @expectedException BadMethodCallException
*/
public function testNamedMockWithShouldDeferMissingThrowsIfNotAvailable()
{
$m = $this->container->mock("MockeryTest_ClassConstructor2", array($param1 = new stdClass()));
$m->shouldDeferMissing();
$m->foorbar123();
}

public function testMockingAKnownConcreteClassSoMockInheritsClassType()
{
$m = $this->container->mock('stdClass');
Expand Down

0 comments on commit 0d72d58

Please sign in to comment.