phpmock\generator\MockFunctionGenerator::defineFunction PHP Method

defineFunction() public method

Defines the mock function.
public defineFunction ( )
    public function defineFunction()
    {
        $name = $this->mock->getName();
        $parameterBuilder = new ParameterBuilder();
        $parameterBuilder->build($name);
        $data = ["namespace" => $this->mock->getNamespace(), "name" => $name, "fqfn" => $this->mock->getFQFN(), "signatureParameters" => $parameterBuilder->getSignatureParameters(), "bodyParameters" => $parameterBuilder->getBodyParameters()];
        $this->template->setVar($data, false);
        $definition = $this->template->render();
        eval($definition);
    }

Usage Example

Example #1
0
 /**
  * Defines the mocked function in the given namespace.
  *
  * In most cases you don't have to call this method. enable() is doing this
  * for you. But if the mock is defined after the first call in the
  * tested class, the tested class doesn't resolve to the mock. This is
  * documented in Bug #68541. You therefore have to define the namespaced
  * function before the first call. Defining the function has no side
  * effects as you still have to enable the mock. If the function was
  * already defined this method does nothing.
  *
  * @see enable()
  * @link https://bugs.php.net/bug.php?id=68541 Bug #68541
  */
 public function define()
 {
     $fqfn = $this->getFQFN();
     if (function_exists($fqfn)) {
         return;
     }
     $functionGenerator = new MockFunctionGenerator($this);
     $functionGenerator->defineFunction();
 }