/** * 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); }; }
/** * Checks if the called method has been stubbed. * * @param string $lsb Late state binding class name. * @param object|string $self The object instance or a fully-namespaces class name. * @param string $class The class name. * @param string $name The method name. * @param string $args The passed arguments. * @return boolean Returns `true` if the method has been stubbed. */ protected static function _stubbedMethod($lsb, $self, $class, $name, $args) { if (is_object($self)) { $list = $lsb === $class ? [$self, $lsb] : [$self, $lsb, $class]; } else { $list = $lsb === $class ? [$lsb] : [$lsb, $class]; $name = '::' . $name; } $stub = static::$_classes['stub']; $method = $stub::find($list, $name, $args); Calls::log($list, compact('name', 'args', 'method')); return $method ?: false; }
<?php namespace Kahlan\Spec\Suite\Plugin\Call; use Kahlan\Plugin\Call\Calls; describe("Calls", function () { beforeEach(function () { Calls::reset(); }); describe("::log()", function () { it("logs a dynamic call", function () { Calls::log('my\\name\\space\\Class', ['name' => 'methodName']); $logs = Calls::logs(); expect($logs[0][0])->toEqual(['class' => 'my\\name\\space\\Class', 'name' => 'methodName', 'instance' => null, 'static' => false, 'method' => null]); }); it("logs a static call", function () { Calls::log('my\\name\\space\\Class', ['name' => '::methodName']); $logs = Calls::logs(); expect($logs[0][0])->toEqual(['class' => 'my\\name\\space\\Class', 'name' => 'methodName', 'instance' => null, 'static' => true, 'method' => null]); }); }); describe("::lastFindIndex()", function () { it("gets/sets the last find index", function () { $index = Calls::lastFindIndex(100); expect($index)->toBe(100); $index = Calls::lastFindIndex(); expect($index)->toBe(100); }); }); });