Overview

Namespaces

  • phpmock
    • environment
    • functions
    • spy

Classes

  • phpmock\environment\MockEnvironment
  • phpmock\environment\SleepEnvironmentBuilder
  • phpmock\functions\AbstractSleepFunction
  • phpmock\functions\FixedDateFunction
  • phpmock\functions\FixedMicrotimeFunction
  • phpmock\functions\FixedValueFunction
  • phpmock\functions\SleepFunction
  • phpmock\functions\UsleepFunction
  • phpmock\Mock
  • phpmock\MockBuilder
  • phpmock\spy\Invocation
  • phpmock\spy\Spy

Interfaces

  • phpmock\Deactivatable
  • phpmock\functions\FunctionProvider
  • phpmock\functions\Incrementable

Exceptions

  • phpmock\MockEnabledException
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 
<?php

namespace phpmock;

use phpmock\functions\FunctionProvider;

/**
 * Fluent API mock builder.
 *
 * Example:
 * <code>
 * namespace foo;
 *
 * use phpmock\MockBuilder;
 * use phpmock\functions\FixedValueFunction;
 *
 * $builder = new MockBuilder();
 * $builder->setNamespace(__NAMESPACE__)
 *         ->setName("time")
 *         ->setFunctionProvider(new FixedValueFunction(1417011228));
 *
 * $mock = $builder->build();
 *
 * // The mock is not enabled yet.
 * assert (time() != 1417011228);
 *
 * $mock->enable();
 * assert (time() == 1417011228);
 *
 * // The mock is disabled and PHP's built-in time() is called.
 * $mock->disable();
 * assert (time() != 1417011228);
 * </code>
 *
 * @author Markus Malkusch <markus@malkusch.de>
 * @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
 * @license http://www.wtfpl.net/txt/copying/ WTFPL
 * @see Mock
 */
class MockBuilder
{
    
    /**
     * @var string namespace for the mock function.
     */
    private $namespace;
    
    /**
     * @var string function name of the mocked function.
     */
    private $name;
    
    /**
     * @var callable The function mock.
     */
    private $function;
    
    /**
     * Sets the mock namespace.
     *
     * @param string $namespace The function namespace.
     * @return MockBuilder
     */
    public function setNamespace($namespace)
    {
        $this->namespace = $namespace;
        return $this;
    }
    
    /**
     * Sets the mocked function name.
     *
     * @param string $name The function name.
     * @return MockBuilder
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }
    
    /**
     * Sets the mock function.
     *
     * Use this method if you want to set the mocked behaviour with
     * a callable. Alternatively, you can use {@link setFunctionProvider()}
     * to set it with a {@link FunctionProvider}.
     *
     * @param callable $function The mock function.
     * @return MockBuilder
     * @see setFunctionProvider()
     */
    public function setFunction(callable $function)
    {
        $this->function = $function;
        return $this;
    }
    
    /**
     * Sets the mock function.
     *
     * Use this method if you want to set the mocked behaviour with
     * a {@link FunctionProvider}. Alternatively, you can use
     * {@link setFunction()} to set it with a callable.
     *
     * @param FunctionProvider $provider The mock function provider.
     * @return MockBuilder
     * @see setFunction()
     */
    public function setFunctionProvider(FunctionProvider $provider)
    {
        return $this->setFunction($provider->getCallable());
    }
    
    /**
     * Builds a mock.
     *
     * @return Mock The mock.
     */
    public function build()
    {
        return new Mock($this->namespace, $this->name, $this->function);
    }
}
API documentation generated by ApiGen