/** * 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; }
/** * Resolves the matching. * * @return boolean Returns `true` if successfully resolved, `false` otherwise. */ public function resolve() { $startIndex = $this->_ordered ? Calls::lastFindIndex() : 0; $report = Calls::find($this->_message, $startIndex, $this->times()); $this->_report = $report; $this->_buildDescription($startIndex); return $report['success']; }
<?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); }); }); });
/** * Build the description of the runned `::match()` call. * * @param mixed $startIndex The startIndex in calls log. */ public function _buildDescription($startIndex = 0) { $times = $this->times(); $report = $this->_report; $reference = $report['message']->reference(); $expected = $report['message']->name(); $with = $report['message']->args(); $expectedTimes = $times ? ' the expected times' : ''; $expectedParameters = $with ? ' with expected parameters' : ''; $this->_description['description'] = "receive the expected method{$expectedParameters}{$expectedTimes}."; $calledTimes = count($report['args']); if (!$calledTimes) { $logged = []; foreach (Calls::logs($reference, $startIndex) as $log) { $logged[] = $log['static'] ? '::' . $log['name'] : $log['name']; } $this->_description['data']['actual received calls'] = $logged; } elseif ($calledTimes) { $this->_description['data']['actual received'] = $expected; $this->_description['data']['actual received times'] = $calledTimes; if ($with !== null) { $this->_description['data']['actual received parameters list'] = $report['args']; } } $this->_description['data']['expected to receive'] = $expected; if ($with !== null) { $this->_description['data']['expected parameters'] = $with; } if ($times) { $this->_description['data']['expected received times'] = $times; } }