/** * Patches the string. * * @param string $namespace The namespace. * @param string $ref The fully namespaced class/function reference string. * @param boolean $isFunc Boolean indicating if $ref is a function reference. * @return string A fully namespaced reference. */ public static function patched($namespace, $ref, $isFunc = true, &$substitute = null) { $name = $ref; if ($namespace) { if (!$isFunc || function_exists("{$namespace}\\{$ref}")) { $name = "{$namespace}\\{$ref}"; } } $method = isset(static::$_registered[$name]) ? static::$_registered[$name] : null; $fake = $method ? $method->substitute() : null; if (!$isFunc) { if (is_object($fake)) { $substitute = $fake; } return $fake ?: $name; } if (!Suite::registered($name) && !$method) { return $name; } return function () use($name, $method) { $args = func_get_args(); if (Suite::registered($name)) { Calls::log(null, compact('name', 'args')); } if ($method && $method->matchArgs($args)) { return $method($args); } return call_user_func_array($name, $args); }; }
/** * Point cut called before method execution. * * @return boolean If `true` is returned, the normal execution of the method is aborted. */ public static function before($method, $self, &$params) { if (!Suite::registered()) { return false; } list($class, $name) = explode('::', $method); $lsb = is_object($self) ? get_class($self) : $self; if (!Suite::registered($lsb) && !Suite::registered($class)) { return false; } if ($name === '__call' || $name === '__callStatic') { $name = array_shift($params); $params = array_shift($params); } return static::_stubbedMethod($lsb, $self, $class, $name, $params); }
}); describe("::register()", function () { it("return `false` if the hash is not registered", function () { $instance = new stdClass(); $hash = Suite::hash($instance); expect(Suite::registered($hash))->toBe(false); }); }); describe("::reset()", function () { it("clears registered hashes", function () { $instance = new stdClass(); $hash = Suite::hash($instance); Suite::register($hash); expect(Suite::registered($hash))->toBe(true); Suite::reset(); expect(Suite::registered($hash))->toBe(false); }); }); describe("->status()", function () { it("returns `0` if a specs suite passes", function () { $describe = $this->suite->describe("", function () { $this->it("passes", function () { $this->expect(true)->toBe(true); }); }); $this->suite->run(); expect($this->suite->status())->toBe(0); }); it("returns `-1` if a specs suite fails", function () { $describe = $this->suite->describe("", function () { $this->it("fails", function () {