94% of 657OPs |
93% of 106Lines |
70% of 97Branches |
69% of 39Paths |
| # | |
|---|---|
| 1 |
<?php |
| 2 |
|
| 3 |
namespace mageekguy\atoum\asserters; |
| 4 |
|
| 5 |
use |
| 6 |
mageekguy\atoum, |
| 7 |
mageekguy\atoum\exceptions |
| 8 |
; |
| 9 |
|
| 10 |
class phpClass extends atoum\asserter |
| 11 |
{
|
| 12 |
protected $class = null; |
| 13 |
protected $reflectionClassInjector = null; |
| 14 |
|
| 15 |
public function __get($property)63% |
| 16 |
{
|
| 17 |
switch (strtolower($property)) |
| 18 |
{
|
| 19 |
case 'isabstract': |
| 20 |
case 'isfinal': |
| 21 |
case 'hasnoparent': |
| 22 |
return $this->{$property}();
|
| 23 |
|
| 24 |
default: |
| 25 |
return parent::__get($property); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
public function __call($method, $arguments)63% |
| 30 |
{
|
| 31 |
switch (strtolower($method)) |
| 32 |
{
|
| 33 |
case 'extends': |
| 34 |
return call_user_func_array(array($this, 'isSubClassOf'), $arguments); |
| 35 |
|
| 36 |
case 'implements': |
| 37 |
return call_user_func_array(array($this, 'hasInterface'), $arguments); |
| 38 |
|
| 39 |
default: |
| 40 |
return parent::__call($method, $arguments); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
public function __toString()100% |
| 45 |
{
|
| 46 |
return (string) $this->getClass(); |
| 47 |
} |
| 48 |
|
| 49 |
public function getReflectionClass($class)100% |
| 50 |
{
|
| 51 |
if ($this->reflectionClassInjector === null) |
| 52 |
{
|
| 53 |
$reflectionClass = new \reflectionClass($class); |
| 54 |
} |
| 55 |
else |
| 56 |
{
|
| 57 |
$reflectionClass = $this->reflectionClassInjector->__invoke($class); |
| 58 | |
| 59 |
if ($reflectionClass instanceof \reflectionClass === false) |
| 60 |
{
|
| 61 |
throw new exceptions\runtime\unexpectedValue('Reflection class injector must return a \reflectionClass instance');
|
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return $reflectionClass; |
| 66 |
} |
| 67 |
|
| 68 |
public function setReflectionClassInjector(\closure $reflectionClassInjector)100% |
| 69 |
{
|
| 70 |
$closure = new \reflectionMethod($reflectionClassInjector, '__invoke'); |
| 71 |
|
| 72 |
if ($closure->getNumberOfParameters() != 1) |
| 73 |
{
|
| 74 |
throw new exceptions\logic\invalidArgument('Reflection class injector must take one argument');
|
| 75 |
} |
| 76 | |
| 77 |
$this->reflectionClassInjector = $reflectionClassInjector; |
| 78 |
|
| 79 |
return $this; |
| 80 |
} |
| 81 |
|
| 82 |
public function getClass()100% |
| 83 |
{
|
| 84 |
return ($this->class === null ? null : $this->class->getName()); |
| 85 |
} |
| 86 |
|
| 87 |
public function setWith($class)100% |
| 88 |
{
|
| 89 |
parent::setWith($class); |
| 90 |
|
| 91 |
try |
| 92 |
{
|
| 93 |
$this->class = $this->getReflectionClass($class); |
| 94 |
} |
| 95 |
catch (\exception $exception) |
| 96 |
{
|
| 97 |
$this->fail($this->_('Class \'%s\' does not exist', $class));
|
| 98 |
} |
| 99 |
|
| 100 |
$this->pass(); |
| 101 |
|
| 102 |
return $this; |
| 103 |
} |
| 104 |
|
| 105 |
public function hasParent($parent, $failMessage = null)100% |
| 106 |
{
|
| 107 |
$parentClass = $this->classIsSet()->class->getParentClass(); |
| 108 |
|
| 109 |
if ($parentClass !== false && strtolower($parentClass->getName()) == strtolower($parent)) |
| 110 |
{
|
| 111 |
$this->pass(); |
| 112 |
} |
| 113 |
else |
| 114 |
{
|
| 115 |
$this->fail($failMessage ?: $this->_('%s is not the parent of class %s', $parent, $this));
|
| 116 |
} |
| 117 |
|
| 118 |
return $this; |
| 119 |
} |
| 120 |
|
| 121 |
public function hasNoParent($failMessage = null)100% |
| 122 |
{
|
| 123 |
if (($parentClass = $this->classIsSet()->class->getParentClass()) === false) |
| 124 |
{
|
| 125 |
$this->pass(); |
| 126 |
} |
| 127 |
else |
| 128 |
{
|
| 129 |
$this->fail($failMessage ?: $this->_('%s has parent %s', $this, $parentClass));
|
| 130 |
} |
| 131 |
|
| 132 |
return $this; |
| 133 |
} |
| 134 |
|
| 135 |
public function isSubClassOf($parent, $failMessage = null)100% |
| 136 |
{
|
| 137 |
try |
| 138 |
{
|
| 139 |
if ($this->classIsSet()->class->isSubClassOf($parent) == true) |
| 140 |
{
|
| 141 |
$this->pass(); |
| 142 |
} |
| 143 |
else |
| 144 |
{
|
| 145 |
$this->fail($failMessage ?: $this->_('%s does not extend %s', $this, $parent));
|
| 146 |
} |
| 147 |
} |
| 148 |
catch (\reflectionException $exception) |
| 149 |
{
|
| 150 |
throw new exceptions\logic('Argument of ' . __METHOD__ . '() must be a class name', null, $exception);
|
| 151 |
} |
| 152 |
|
| 153 |
return $this; |
| 154 |
} |
| 155 |
|
| 156 |
public function hasInterface($interface, $failMessage = null)100% |
| 157 |
{
|
| 158 |
try |
| 159 |
{
|
| 160 |
if ($this->classIsSet()->class->implementsInterface($interface) === true) |
| 161 |
{
|
| 162 |
$this->pass(); |
| 163 |
} |
| 164 |
else |
| 165 |
{
|
| 166 |
$this->fail($failMessage ?: $this->_('%s does not implement %s', $this, $interface));
|
| 167 |
} |
| 168 |
} |
| 169 |
catch (\reflectionException $exception) |
| 170 |
{
|
| 171 |
throw new exceptions\logic('Argument of ' . __METHOD__ . '() must be an interface name', null, $exception);
|
| 172 |
} |
| 173 | |
| 174 |
return $this; |
| 175 |
} |
| 176 |
|
| 177 |
public function isAbstract($failMessage = null)100% |
| 178 |
{
|
| 179 |
if ($this->classIsSet()->class->isAbstract() === true) |
| 180 |
{
|
| 181 |
$this->pass(); |
| 182 |
} |
| 183 |
else |
| 184 |
{
|
| 185 |
$this->fail($failMessage ?: $this->_('%s is not abstract', $this));
|
| 186 |
} |
| 187 |
|
| 188 |
return $this; |
| 189 |
} |
| 190 |
|
| 191 |
public function isFinal($failMessage = null)100% |
| 192 |
{
|
| 193 |
if ($this->classIsSet()->class->isFinal() === true) |
| 194 |
{
|
| 195 |
$this->pass(); |
| 196 |
} |
| 197 |
else |
| 198 |
{
|
| 199 |
$this->fail($failMessage ?: $this->_('%s is not final', $this));
|
| 200 |
} |
| 201 |
|
| 202 |
return $this; |
| 203 |
} |
| 204 |
|
| 205 |
public function hasMethod($method, $failMessage = null)100% |
| 206 |
{
|
| 207 |
if ($this->classIsSet()->class->hasMethod($method) === true) |
| 208 |
{
|
| 209 |
$this->pass(); |
| 210 |
} |
| 211 |
else |
| 212 |
{
|
| 213 |
$this->fail($failMessage ?: $this->_('%s::%s() does not exist', $this, $method));
|
| 214 |
} |
| 215 |
|
| 216 |
return $this; |
| 217 |
} |
| 218 |
|
| 219 |
public function hasConstant($constant, $failMessage = null)83% |
| 220 |
{
|
| 221 |
if ($this->classIsSet()->class->hasConstant($constant) === false) |
| 222 |
{
|
| 223 |
$this->fail($failMessage ?: $this->_('%s::%s does not exist', $this, $constant));
|
| 224 |
|
| 225 |
return $this; |
| 226 |
} |
| 227 |
else |
| 228 |
{
|
| 229 |
$this->pass(); |
| 230 |
|
| 231 |
return $this->generator->constant($this->class->getConstant($constant)); |
| 232 |
} |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
protected function classIsSet()100% |
| 237 |
{
|
| 238 |
if ($this->class === null) |
| 239 |
{
|
| 240 |
throw new exceptions\logic('Class is undefined');
|
| 241 |
} |
| 242 |
|
| 243 |
return $this; |
| 244 |
} |
| 245 |
} |