Skip to content

Commit

Permalink
Mockery doubles can be created using expectation array as second para…
Browse files Browse the repository at this point in the history
…meter
  • Loading branch information
MarcelloDuarte committed Jul 29, 2012
1 parent 51f0d2a commit f74e901
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions src/PHPSpec/Specification/ExampleGroup.php
Expand Up @@ -150,9 +150,9 @@ public function fail($message = '')
*/
public function double($class = 'stdClass')
{
$args = func_num_args() ? func_get_args() : array($class);
if (class_exists('Mockery')) {
$double = \Mockery::mock($class);
return $double;
return call_user_func_array(array('\Mockery', 'mock'), $args);
}
throw new \PHPSpec\Exception('Mockery is not installed');
}
Expand All @@ -167,7 +167,8 @@ public function double($class = 'stdClass')
*/
public function mock($class = 'stdClass')
{
return $this->double($class);
$args = func_num_args() ? func_get_args() : array($class);
return call_user_func_array(array($this, 'double'), $args);
}

/**
Expand All @@ -180,7 +181,8 @@ public function mock($class = 'stdClass')
*/
public function stub($class = 'stdClass')
{
return $this->double($class, $stubs, $arguments);
$args = func_num_args() ? func_get_args() : array($class);
return call_user_func_array(array($this, 'double'), $args);
}

/**
Expand All @@ -206,11 +208,21 @@ public function getMatcherFactory()
return $this->_matcherFactory;
}

/**
* Sets the macros path
*
* @param string $macros
*/
public function setMacros($macros)
{
$this->_macros = $macros;
}

/**
* Gets the macros path
*
* @return string
*/
public function getMacros()
{
return $this->_macros;
Expand Down Expand Up @@ -303,21 +315,45 @@ public function runSharedExample($example)
}
}

/**
* Proxies calls to macro
*
* @param string $method
* @param string $args
* @return mixed
*/
public function __call($method, $args)
{
if ($this->interceptedHasAMacro($method)) {
return $this->runMacro($method, $args);
}
}

/**
* Checks weather there are macros to be made available and that the
* macro with the name of the method exists
*
* @param string $method
* @return boolean
*/
protected function interceptedHasAMacro($method)
{
if (!file_exists($this->_macros) || !is_readable($this->_macros)) {
return false;
}
include_once ($this->_macros);
$macro = basename($this->_macros, '.php');
$macro = new $macro;
return method_exists($macro, $method);
}

/**
* Runs the macro
*
* @param string $method
* @param string $args
* @return mixed
*/
protected function runMacro($method, $args)
{
include_once ($this->_macros);
Expand Down

0 comments on commit f74e901

Please sign in to comment.