Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds a method to defer all calls by default to mock's parent

  • Loading branch information...
commit 0d72d5852295613a8ab34c17b249da5160f6f454 1 parent ef0446e
@davedevelopment davedevelopment authored
View
2  .gitignore
@@ -2,3 +2,5 @@
pearfarm.spec
*.sublime-project
library/Hamcrest/*
+composer.lock
+vendor/
View
10 library/Mockery/Generator.php
@@ -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;
@@ -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;
@@ -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;
View
24 library/Mockery/Mock.php
@@ -46,6 +46,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
*
* @var bool
@@ -170,6 +178,20 @@ public function shouldIgnoreMissing()
}
/**
+ * 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
* interactions of a concrete object as a set of expectations on the
@@ -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;
View
7 library/Mockery/MockInterface.php
@@ -49,6 +49,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
* expectations
View
19 tests/Mockery/ContainerTest.php
@@ -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');
Please sign in to comment.
Something went wrong with that request. Please try again.