88% of 766OPs |
79% of 124Lines |
63% of 138Branches |
58% of 71Paths |
| # | |
|---|---|
| 1 |
<?php |
| 2 |
|
| 3 |
namespace mageekguy\atoum\test; |
| 4 |
|
| 5 |
use |
| 6 |
mageekguy\atoum, |
| 7 |
mageekguy\atoum\exceptions, |
| 8 |
mageekguy\atoum\test\adapter\invoker |
| 9 |
; |
| 10 |
|
| 11 |
class adapter extends atoum\adapter |
| 12 |
{
|
| 13 |
protected $calls = null; |
| 14 |
protected $invokers = array(); |
| 15 |
|
| 16 |
private static $storage = null; |
| 17 |
|
| 18 |
public function __construct()100% |
| 19 |
{
|
| 20 |
$this->setCalls(); |
| 21 |
|
| 22 |
if (self::$storage !== null) |
| 23 |
{
|
| 24 |
self::$storage->add($this); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
public function __clone()100% |
| 29 |
{
|
| 30 |
$this->calls = clone $this->calls; |
| 31 |
|
| 32 |
if (self::$storage !== null) |
| 33 |
{
|
| 34 |
self::$storage->add($this); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
public function __set($functionName, $mixed)100% |
| 39 |
{
|
| 40 |
$this->{$functionName}->return = $mixed;
|
| 41 |
|
| 42 |
return $this; |
| 43 |
} |
| 44 |
|
| 45 |
public function __get($functionName)100% |
| 46 |
{
|
| 47 |
return $this->setInvoker($functionName); |
| 48 |
} |
| 49 |
|
| 50 |
public function __isset($functionName)100% |
| 51 |
{
|
| 52 |
return $this->nextCallIsOverloaded($functionName); |
| 53 |
} |
| 54 |
|
| 55 |
public function __unset($functionName)100% |
| 56 |
{
|
| 57 |
if (isset($this->{$functionName}) === true)
|
| 58 |
{
|
| 59 |
$functionName = static::getKey($functionName); |
| 60 |
|
| 61 |
unset($this->invokers[$functionName]); |
| 62 |
unset($this->calls[$functionName]); |
| 63 |
} |
| 64 |
|
| 65 |
return $this; |
| 66 |
} |
| 67 |
|
| 68 |
public function __sleep()100% |
| 69 |
{
|
| 70 |
return array(); |
| 71 |
} |
| 72 |
|
| 73 |
public function __toString()100% |
| 74 |
{
|
| 75 |
return (string) $this->calls; |
| 76 |
} |
| 77 |
|
| 78 |
public function getInvokers()100% |
| 79 |
{
|
| 80 |
return $this->invokers; |
| 81 |
} |
| 82 |
|
| 83 |
public function setCalls(adapter\calls $calls = null)100% |
| 84 |
{
|
| 85 |
$this->calls = $calls ?: new adapter\calls(); |
| 86 |
|
| 87 |
return $this->resetCalls(); |
| 88 |
} |
| 89 |
|
| 90 |
public function getCalls(adapter\call $call = null, $identical = false)100% |
| 91 |
{
|
| 92 |
return ($call === null ? $this->calls : $this->calls->get($call, $identical)); |
| 93 |
} |
| 94 |
|
| 95 |
public function getCallsNumber(adapter\call $call = null, $identical = false)100% |
| 96 |
{
|
| 97 |
return sizeof($this->getCalls($call, $identical)); |
| 98 |
} |
| 99 |
|
| 100 |
public function getCallsEqualTo(adapter\call $call)100% |
| 101 |
{
|
| 102 |
return $this->calls->getEqualTo($call); |
| 103 |
} |
| 104 |
|
| 105 |
public function getCallsNumberEqualTo(adapter\call $call)100% |
| 106 |
{
|
| 107 |
return sizeof($this->calls->getEqualTo($call)); |
| 108 |
} |
| 109 |
|
| 110 |
public function getCallsIdenticalTo(adapter\call $call)100% |
| 111 |
{
|
| 112 |
return $this->calls->getIdenticalTo($call); |
| 113 |
} |
| 114 |
|
| 115 |
public function getPreviousCalls(adapter\call $call, $position, $identical = false)100% |
| 116 |
{
|
| 117 |
return $this->calls->getPrevious($call, $position, $identical); |
| 118 |
} |
| 119 |
|
| 120 |
public function hasPreviousCalls(adapter\call $call, $position, $identical = false)0% |
| 121 |
{
|
| 122 |
return $this->calls->hasPrevious($call, $position, $identical); |
| 123 |
} |
| 124 |
|
| 125 |
public function getAfterCalls(adapter\call $call, $position, $identical = false)100% |
| 126 |
{
|
| 127 |
return $this->calls->getAfter($call, $position, $identical); |
| 128 |
} |
| 129 |
|
| 130 |
public function hasAfterCalls(adapter\call $call, $position, $identical = false)100% |
| 131 |
{
|
| 132 |
return $this->calls->hasAfter($call, $position, $identical); |
| 133 |
} |
| 134 |
|
| 135 |
public function getCallNumber(adapter\call $call = null, $identical = false)100% |
| 136 |
{
|
| 137 |
return sizeof($this->getCalls($call, $identical)); |
| 138 |
} |
| 139 |
|
| 140 |
public function getTimeline(adapter\call $call = null, $identical = false)100% |
| 141 |
{
|
| 142 |
return $this->calls->getTimeline($call, $identical); |
| 143 |
} |
| 144 |
|
| 145 |
public function resetCalls($functionName = null)100% |
| 146 |
{
|
| 147 |
if ($functionName === null) |
| 148 |
{
|
| 149 |
$this->calls->reset(); |
| 150 |
} |
| 151 |
else |
| 152 |
{
|
| 153 |
unset($this->calls[$functionName]); |
| 154 |
} |
| 155 |
|
| 156 |
return $this; |
| 157 |
} |
| 158 |
|
| 159 |
public function reset()100% |
| 160 |
{
|
| 161 |
$this->invokers = array(); |
| 162 |
|
| 163 |
return $this->resetCalls(); |
| 164 |
} |
| 165 |
|
| 166 |
public function addCall($functionName, array $arguments = array()) |
| 167 |
{
|
| 168 |
$unreferencedArguments = array(); |
| 169 |
|
| 170 |
foreach ($arguments as $argument) |
| 171 |
{
|
| 172 |
$unreferencedArguments[] = $argument; |
| 173 |
} |
| 174 |
|
| 175 |
$this->calls[] = $this->buildCall($functionName, $unreferencedArguments); |
| 176 |
|
| 177 |
return $this; |
| 178 |
} |
| 179 |
|
| 180 |
public function invoke($functionName, array $arguments = array())71% |
| 181 |
{
|
| 182 |
if (self::isLanguageConstruct($functionName) || (function_exists($functionName) === true && is_callable($functionName) === false)) |
| 183 |
{
|
| 184 |
throw new exceptions\logic\invalidArgument('Function \'' . $functionName . '()\' is not invokable by an adapter');
|
| 185 |
} |
| 186 |
|
| 187 |
$call = sizeof($this->addCall($functionName, $arguments)->getCallsEqualTo(new adapter\call($functionName))); |
| 188 |
|
| 189 |
try |
| 190 |
{
|
| 191 |
return ($this->callIsOverloaded($functionName, $call) === false ? parent::invoke($functionName, $arguments) : $this->{$functionName}->invoke($arguments, $call));
|
| 192 |
} |
| 193 |
catch (exceptions\logic\invalidArgument $exception) |
| 194 |
{
|
| 195 |
throw new exceptions\logic('There is no return value defined for \'' . $functionName . '() at call ' . $call);
|
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
public static function setStorage(adapter\storage $storage = null)100% |
| 200 |
{
|
| 201 |
self::$storage = $storage ?: new adapter\storage(); |
| 202 |
} |
| 203 |
|
| 204 |
protected function buildInvoker($functionName, \closure $factory = null)100% |
| 205 |
{
|
| 206 |
if ($factory === null) |
| 207 |
{
|
| 208 |
$factory = function($functionName) { return new invoker($functionName); };
|
| 209 |
} |
| 210 |
|
| 211 |
return $factory($functionName); |
| 212 |
} |
| 213 |
|
| 214 |
protected function setInvoker($functionName, \closure $factory = null)100% |
| 215 |
{
|
| 216 |
$key = static::getKey($functionName); |
| 217 |
|
| 218 |
if (isset($this->invokers[$key]) === false) |
| 219 |
{
|
| 220 |
$this->invokers[$key] = $this->buildInvoker($functionName, $factory); |
| 221 |
} |
| 222 |
|
| 223 |
return $this->invokers[$key]; |
| 224 |
} |
| 225 |
|
| 226 |
protected function callIsOverloaded($functionName, $call)100% |
| 227 |
{
|
| 228 |
$functionName = static::getKey($functionName); |
| 229 |
|
| 230 |
return (isset($this->invokers[$functionName]) === true && $this->invokers[$functionName]->closureIsSetForCall($call) === true); |
| 231 |
} |
| 232 |
|
| 233 |
protected function nextCallIsOverloaded($functionName)100% |
| 234 |
{
|
| 235 |
return ($this->callIsOverloaded($functionName, $this->getCallNumber(new adapter\call($functionName)) + 1) === true); |
| 236 |
} |
| 237 |
|
| 238 |
protected function buildCall($function, array $arguments)100% |
| 239 |
{
|
| 240 |
return new adapter\call($function, $arguments); |
| 241 |
} |
| 242 |
|
| 243 |
protected static function getKey($functionName)100% |
| 244 |
{
|
| 245 |
return strtolower($functionName); |
| 246 |
} |
| 247 |
|
| 248 |
protected static function isLanguageConstruct($functionName)100% |
| 249 |
{
|
| 250 |
switch (strtolower($functionName)) |
| 251 |
{
|
| 252 |
case 'array': |
| 253 |
case 'declare': |
| 254 |
case 'echo': |
| 255 |
case 'empty': |
| 256 |
case 'eval': |
| 257 |
case 'exit': |
| 258 |
case 'die': |
| 259 |
case 'isset': |
| 260 |
case 'list': |
| 261 |
case 'print': |
| 262 |
case 'unset': |
| 263 |
case 'require': |
| 264 |
case 'require_once': |
| 265 |
case 'include': |
| 266 |
case 'include_once': |
| 267 |
return true; |
| 268 |
|
| 269 |
default: |
| 270 |
return false; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
protected static function getArgumentsFilter($arguments, $identicalArguments)0% |
| 275 |
{
|
| 276 |
$filter = null; |
| 277 |
|
| 278 |
if (is_array($arguments) === true) |
| 279 |
{
|
| 280 |
if ($arguments === array()) |
| 281 |
{
|
| 282 |
$filter = function($callArguments) use ($arguments) {
|
| 283 |
return ($arguments === $callArguments); |
| 284 |
}; |
| 285 |
} |
| 286 |
else |
| 287 |
{
|
| 288 |
$callback = function($a, $b) {
|
| 289 |
return ($a == $b ? 0 : -1); |
| 290 |
}; |
| 291 |
|
| 292 |
if ($identicalArguments === false) |
| 293 |
{
|
| 294 |
$filter = function($callArguments) use ($arguments, $callback) {
|
| 295 |
return ($arguments == array_uintersect_uassoc($callArguments, $arguments, $callback, $callback)); |
| 296 |
}; |
| 297 |
} |
| 298 |
else |
| 299 |
{
|
| 300 |
$filter = function($callArguments) use ($arguments, $callback) {
|
| 301 |
return ($arguments === array_uintersect_uassoc($callArguments, $arguments, $callback, $callback)); |
| 302 |
}; |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
return $filter; |
| 308 |
} |
| 309 |
} |