92% of 953OPs |
99% of 149Lines |
77% of 166Branches |
63% of 80Paths |
| # | |
|---|---|
| 1 |
<?php |
| 2 |
|
| 3 |
namespace mageekguy\atoum\test\adapter; |
| 4 |
|
| 5 |
use |
| 6 |
mageekguy\atoum\exceptions, |
| 7 |
mageekguy\atoum\test\adapter |
| 8 |
; |
| 9 |
|
| 10 |
class calls implements \countable, \arrayAccess, \iteratorAggregate |
| 11 |
{
|
| 12 |
protected $calls = array(); |
| 13 |
protected $size = 0; |
| 14 |
protected $decorator = null; |
| 15 |
|
| 16 |
private static $callsNumber = 0; |
| 17 |
|
| 18 |
public function __construct()100% |
| 19 |
{
|
| 20 |
$this->setDecorator(); |
| 21 |
} |
| 22 |
|
| 23 |
public function __invoke()100% |
| 24 |
{
|
| 25 |
return $this->calls; |
| 26 |
} |
| 27 |
|
| 28 |
public function __toString()100% |
| 29 |
{
|
| 30 |
return $this->decorator->decorate($this); |
| 31 |
} |
| 32 |
|
| 33 |
public function count() |
| 34 |
{
|
| 35 |
return $this->size; |
| 36 |
} |
| 37 |
|
| 38 |
public function offsetSet($functionName = null, $call = null)100% |
| 39 |
{
|
| 40 |
if ($functionName !== null) |
| 41 |
{
|
| 42 |
$call->setFunction($functionName); |
| 43 |
} |
| 44 |
|
| 45 |
return $this->addCall($call); |
| 46 |
} |
| 47 |
|
| 48 |
public function offsetGet($mixed)100% |
| 49 |
{
|
| 50 |
return $this->getEqualTo(self::buildCall($mixed)); |
| 51 |
} |
| 52 |
|
| 53 |
public function offsetUnset($mixed)100% |
| 54 |
{
|
| 55 |
$key = self::getKey(self::buildCall($mixed)); |
| 56 |
|
| 57 |
if (isset($this->calls[$key]) === true) |
| 58 |
{
|
| 59 |
$this->size -= sizeof($this->calls[$key]); |
| 60 |
|
| 61 |
unset($this->calls[$key]); |
| 62 |
} |
| 63 |
|
| 64 |
return $this; |
| 65 |
} |
| 66 |
|
| 67 |
public function offsetExists($mixed)100% |
| 68 |
{
|
| 69 |
return (isset($this->calls[self::getKey(self::buildCall($mixed))]) === true); |
| 70 |
} |
| 71 |
|
| 72 |
public function getIterator()100% |
| 73 |
{
|
| 74 |
return new \arrayIterator($this()); |
| 75 |
} |
| 76 |
|
| 77 |
public function reset()100% |
| 78 |
{
|
| 79 |
$this->calls = array(); |
| 80 |
$this->size = 0; |
| 81 |
|
| 82 |
return $this; |
| 83 |
} |
| 84 |
|
| 85 |
public function setDecorator(adapter\calls\decorator $decorator = null)100% |
| 86 |
{
|
| 87 |
$this->decorator = $decorator ?: new adapter\calls\decorator(); |
| 88 |
|
| 89 |
return $this; |
| 90 |
} |
| 91 |
|
| 92 |
public function getDecorator()100% |
| 93 |
{
|
| 94 |
return $this->decorator; |
| 95 |
} |
| 96 |
|
| 97 |
public function addCall(adapter\call $call)100% |
| 98 |
{
|
| 99 |
return $this->setCall($call); |
| 100 |
} |
| 101 |
|
| 102 |
public function removeCall(adapter\call $call, $position)91% |
| 103 |
{
|
| 104 |
$function = $call->getFunction(); |
| 105 |
|
| 106 |
if ($function == '') |
| 107 |
{
|
| 108 |
throw new exceptions\logic\invalidArgument('Function is undefined');
|
| 109 |
} |
| 110 |
|
| 111 |
$key = self::getKey($call); |
| 112 |
|
| 113 |
if (isset($this->calls[$key][$position]) === true) |
| 114 |
{
|
| 115 |
unset($this->calls[$key][$position]); |
| 116 |
$this->size--; |
| 117 |
} |
| 118 |
|
| 119 |
return $this; |
| 120 |
} |
| 121 |
|
| 122 |
public function toArray(adapter\call $call = null)100% |
| 123 |
{
|
| 124 |
if ($call === null) |
| 125 |
{
|
| 126 |
$calls = $this->getTimeline(); |
| 127 |
} |
| 128 |
else |
| 129 |
{
|
| 130 |
$calls = $this->getEqualTo($call)->toArray(); |
| 131 |
} |
| 132 |
|
| 133 |
return $calls; |
| 134 |
} |
| 135 |
|
| 136 |
public function getEqualTo(adapter\call $call)100% |
| 137 |
{
|
| 138 |
$innerCalls = $this->getCalls($call); |
| 139 |
|
| 140 |
if ($call->getArguments() !== null) |
| 141 |
{
|
| 142 |
$innerCalls = array_filter($innerCalls, function($innerCall) use ($call) { return $call->isEqualTo($innerCall); });
|
| 143 |
} |
| 144 |
|
| 145 |
return self::buildCallsForCall($call, $innerCalls); |
| 146 |
} |
| 147 |
|
| 148 |
public function getIdenticalTo(adapter\call $call)100% |
| 149 |
{
|
| 150 |
$innerCalls = $this->getCalls($call); |
| 151 |
|
| 152 |
if ($call->getArguments() !== null) |
| 153 |
{
|
| 154 |
$innerCalls = array_filter($innerCalls, function($innerCall) use ($call) { return $call->isIdenticalTo($innerCall); });
|
| 155 |
} |
| 156 |
|
| 157 |
return self::buildCallsForCall($call, $innerCalls); |
| 158 |
} |
| 159 |
|
| 160 |
public function getPreviousEqualTo(adapter\call $call, $position)100% |
| 161 |
{
|
| 162 |
$calls = new static(); |
| 163 |
|
| 164 |
foreach ($this->getEqualTo($call)->toArray() as $innerPosition => $innerCall) |
| 165 |
{
|
| 166 |
if ($innerPosition < $position) |
| 167 |
{
|
| 168 |
$calls->setCall($innerCall, $innerPosition); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return $calls; |
| 173 |
} |
| 174 |
|
| 175 |
public function getPreviousIdenticalTo(adapter\call $call, $position)100% |
| 176 |
{
|
| 177 |
$calls = new static(); |
| 178 |
|
| 179 |
foreach ($this->getIdenticalTo($call)->toArray() as $innerPosition => $innerCall) |
| 180 |
{
|
| 181 |
if ($innerPosition < $position) |
| 182 |
{
|
| 183 |
$calls->setCall($innerCall, $innerPosition); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return $calls; |
| 188 |
} |
| 189 |
|
| 190 |
public function getPrevious(adapter\call $call, $position, $identical = false)100% |
| 191 |
{
|
| 192 |
return ($identical === false ? $this->getPreviousEqualTo($call, $position) : $this->getPreviousIdenticalTo($call, $position)); |
| 193 |
} |
| 194 |
|
| 195 |
public function hasPreviousEqualTo(adapter\call $call, $position)100% |
| 196 |
{
|
| 197 |
foreach ($this->getEqualTo($call)->toArray() as $innerPosition => $innerCall) |
| 198 |
{
|
| 199 |
if ($innerPosition < $position) |
| 200 |
{
|
| 201 |
return true; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
public function hasPreviousIdenticalTo(adapter\call $call, $position)100% |
| 209 |
{
|
| 210 |
foreach ($this->getIdenticalTo($call)->toArray() as $innerPosition => $innerCall) |
| 211 |
{
|
| 212 |
if ($innerPosition < $position) |
| 213 |
{
|
| 214 |
return true; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
return false; |
| 219 |
} |
| 220 |
|
| 221 |
public function hasPrevious(adapter\call $call, $position, $identical = false)100% |
| 222 |
{
|
| 223 |
return ($identical === false ? $this->hasPreviousEqualTo($call, $position) : $this->hasPreviousIdenticalTo($call, $position)); |
| 224 |
} |
| 225 |
|
| 226 |
public function getAfterEqualTo(adapter\call $call, $position)100% |
| 227 |
{
|
| 228 |
$calls = new static(); |
| 229 |
|
| 230 |
foreach ($this->getEqualTo($call)->toArray() as $innerPosition => $innerCall) |
| 231 |
{
|
| 232 |
if ($innerPosition > $position) |
| 233 |
{
|
| 234 |
$calls->setCall($innerCall, $innerPosition); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
return $calls; |
| 239 |
} |
| 240 |
|
| 241 |
public function getAfterIdenticalTo(adapter\call $call, $position) |
| 242 |
{
|
| 243 |
$calls = new static(); |
| 244 |
|
| 245 |
foreach ($this->getIdenticalTo($call)->toArray() as $innerPosition => $innerCall) |
| 246 |
{
|
| 247 |
if ($innerPosition > $position) |
| 248 |
{
|
| 249 |
$calls->setCall($innerCall, $innerPosition); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
return $calls; |
| 254 |
} |
| 255 |
|
| 256 |
public function getAfter(adapter\call $call, $position, $identical = false) |
| 257 |
{
|
| 258 |
return ($identical === false ? $this->getAfterEqualTo($call, $position) : $this->getAfterIdenticalTo($call, $position)); |
| 259 |
} |
| 260 |
|
| 261 |
public function hasAfterEqualTo(adapter\call $call, $position)100% |
| 262 |
{
|
| 263 |
foreach ($this->getEqualTo($call)->toArray() as $innerPosition => $innerCall) |
| 264 |
{
|
| 265 |
if ($innerPosition > $position) |
| 266 |
{
|
| 267 |
return true; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
public function hasAfterIdenticalTo(adapter\call $call, $position)100% |
| 275 |
{
|
| 276 |
foreach ($this->getIdenticalTo($call)->toArray() as $innerPosition => $innerCall) |
| 277 |
{
|
| 278 |
if ($innerPosition > $position) |
| 279 |
{
|
| 280 |
return true; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
return false; |
| 285 |
} |
| 286 |
|
| 287 |
public function hasAfter(adapter\call $call, $position, $identical = false)100% |
| 288 |
{
|
| 289 |
return ($identical === false ? $this->hasAfterEqualTo($call, $position) : $this->hasAfterIdenticalTo($call, $position)); |
| 290 |
} |
| 291 |
|
| 292 |
public function get(adapter\call $call, $identical = false)100% |
| 293 |
{
|
| 294 |
return ($identical === false ? $this->getEqualTo($call) : $this->getIdenticalTo($call)); |
| 295 |
} |
| 296 |
|
| 297 |
public function getTimeline()100% |
| 298 |
{
|
| 299 |
$timeline = array(); |
| 300 |
|
| 301 |
foreach ($this as $innerCalls) |
| 302 |
{
|
| 303 |
foreach ($innerCalls as $position => $call) |
| 304 |
{
|
| 305 |
$timeline[$position] = $call; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
ksort($timeline, SORT_NUMERIC); |
| 310 |
|
| 311 |
return $timeline; |
| 312 |
} |
| 313 |
|
| 314 |
protected function setCall(adapter\call $call, $position = null)100% |
| 315 |
{
|
| 316 |
$function = $call->getFunction(); |
| 317 |
|
| 318 |
if ($function == '') |
| 319 |
{
|
| 320 |
throw new exceptions\logic\invalidArgument('Function is undefined');
|
| 321 |
} |
| 322 |
|
| 323 |
if ($position === null) |
| 324 |
{
|
| 325 |
$position = ++self::$callsNumber; |
| 326 |
} |
| 327 |
|
| 328 |
$this->calls[self::getKey($call)][$position] = $call; |
| 329 |
$this->size++; |
| 330 |
|
| 331 |
return $this; |
| 332 |
} |
| 333 |
|
| 334 |
protected function getCalls($mixed)100% |
| 335 |
{
|
| 336 |
$key = self::getKey($mixed); |
| 337 |
|
| 338 |
return (isset($this->calls[$key]) === false ? array() : $this->calls[$key]); |
| 339 |
} |
| 340 |
|
| 341 |
protected static function getKey(adapter\call $call)100% |
| 342 |
{
|
| 343 |
return strtolower($call->getFunction()); |
| 344 |
} |
| 345 |
|
| 346 |
private static function buildCallsForCall(adapter\call $call, array $array) |
| 347 |
{
|
| 348 |
$calls = new static(); |
| 349 |
|
| 350 |
$calls->calls[self::getKey($call)] = $array; |
| 351 |
$calls->size = sizeof($array); |
| 352 |
|
| 353 |
return $calls; |
| 354 |
} |
| 355 |
|
| 356 |
private static function buildCall($mixed)100% |
| 357 |
{
|
| 358 |
return ($mixed instanceof adapter\call ? $mixed : new adapter\call($mixed)); |
| 359 |
} |
| 360 |
} |