94% of 608OPs |
94% of 70Lines |
74% of 93Branches |
58% of 43Paths |
| # | |
|---|---|
| 1 |
<?php |
| 2 |
|
| 3 |
namespace mageekguy\atoum\asserters; |
| 4 |
|
| 5 |
use |
| 6 |
mageekguy\atoum\asserters, |
| 7 |
mageekguy\atoum\exceptions |
| 8 |
; |
| 9 |
|
| 10 |
class object extends asserters\variable |
| 11 |
{
|
| 12 |
public function __get($property)70% |
| 13 |
{
|
| 14 |
switch (strtolower($property)) |
| 15 |
{
|
| 16 |
case 'tostring': |
| 17 |
case 'isempty': |
| 18 |
case 'istestedinstance': |
| 19 |
case 'isnottestedinstance': |
| 20 |
case 'isinstanceoftestedclass': |
| 21 |
return $this->{$property}();
|
| 22 |
|
| 23 |
default: |
| 24 |
return parent::__get($property); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
public function setWith($value, $checkType = true)100% |
| 29 |
{
|
| 30 |
parent::setWith($value); |
| 31 |
|
| 32 |
if ($checkType === true) |
| 33 |
{
|
| 34 |
if ($this->analyzer->isObject($this->value) === true) |
| 35 |
{
|
| 36 |
$this->pass(); |
| 37 |
} |
| 38 |
else |
| 39 |
{
|
| 40 |
$this->fail($this->_('%s is not an object', $this));
|
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
return $this; |
| 45 |
} |
| 46 |
|
| 47 |
public function isInstanceOf($value, $failMessage = null)100% |
| 48 |
{
|
| 49 |
try |
| 50 |
{
|
| 51 |
self::check($value, __FUNCTION__); |
| 52 |
} |
| 53 |
catch (\logicException $exception) |
| 54 |
{
|
| 55 |
if (self::classExists($value) === false) |
| 56 |
{
|
| 57 |
throw new exceptions\logic('Argument of ' . __METHOD__ . '() must be a class instance or a class name');
|
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
$this->valueIsSet()->value instanceof $value ? $this->pass() : $this->fail($failMessage ?: $this->_('%s is not an instance of %s', $this, is_string($value) === true ? $value : $this->getTypeOf($value)));
|
| 62 |
|
| 63 |
return $this; |
| 64 |
} |
| 65 |
|
| 66 |
public function isNotInstanceOf($value, $failMessage = null)100% |
| 67 |
{
|
| 68 |
try |
| 69 |
{
|
| 70 |
self::check($value, __FUNCTION__); |
| 71 |
} |
| 72 |
catch (\logicException $exception) |
| 73 |
{
|
| 74 |
if (self::classExists($value) === false) |
| 75 |
{
|
| 76 |
throw new exceptions\logic('Argument of ' . __METHOD__ . '() must be a class instance or a class name');
|
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
$this->valueIsSet()->value instanceof $value === false ? $this->pass() : $this->fail($failMessage ?: $this->_('%s is an instance of %s', $this, is_string($value) === true ? $value : $this->getTypeOf($value)));
|
| 81 |
|
| 82 |
return $this; |
| 83 |
} |
| 84 |
|
| 85 |
public function hasSize($size, $failMessage = null)100% |
| 86 |
{
|
| 87 |
if (sizeof($this->valueIsSet()->value) == $size) |
| 88 |
{
|
| 89 |
$this->pass(); |
| 90 |
} |
| 91 |
else |
| 92 |
{
|
| 93 |
$this->fail($failMessage ?: $this->_('%s has size %d, expected size %d', $this, sizeof($this->valueIsSet()->value), $size));
|
| 94 |
} |
| 95 |
|
| 96 |
return $this; |
| 97 |
} |
| 98 |
|
| 99 |
public function isCloneOf($object, $failMessage = null)100% |
| 100 |
{
|
| 101 |
if ($failMessage === null) |
| 102 |
{
|
| 103 |
$failMessage = $this->_('%s is not a clone of %s', $this, $this->getTypeOf($object));
|
| 104 |
} |
| 105 |
|
| 106 |
return $this->isEqualTo($object, $failMessage)->isNotIdenticalTo($object, $failMessage); |
| 107 |
} |
| 108 |
|
| 109 |
public function isEmpty($failMessage = null)100% |
| 110 |
{
|
| 111 |
if (sizeof($this->valueIsSet()->value) == 0) |
| 112 |
{
|
| 113 |
$this->pass(); |
| 114 |
} |
| 115 |
else |
| 116 |
{
|
| 117 |
$this->fail($failMessage ?: $this->_('%s has size %d', $this, sizeof($this->value)));
|
| 118 |
} |
| 119 |
|
| 120 |
return $this; |
| 121 |
} |
| 122 |
|
| 123 |
public function isTestedInstance($failMessage = null)100% |
| 124 |
{
|
| 125 |
return $this->valueIsSet()->testedInstanceIsSet()->isIdenticalTo($this->test->testedInstance, $failMessage); |
| 126 |
} |
| 127 |
|
| 128 |
public function isNotTestedInstance($failMessage = null)100% |
| 129 |
{
|
| 130 |
return $this->valueIsSet()->testedInstanceIsSet()->isNotIdenticalTo($this->test->testedInstance, $failMessage); |
| 131 |
} |
| 132 |
|
| 133 |
public function isInstanceOfTestedClass($failMessage = null)100% |
| 134 |
{
|
| 135 |
return $this->valueIsSet()->testedInstanceIsSet()->isInstanceOf($this->test->getTestedClassName(), $failMessage); |
| 136 |
} |
| 137 |
|
| 138 |
public function toString()100% |
| 139 |
{
|
| 140 |
return $this->generator->castToString($this->valueIsSet()->value); |
| 141 |
} |
| 142 |
|
| 143 |
protected function valueIsSet($message = 'Object is undefined') |
| 144 |
{
|
| 145 |
if ($this->analyzer->isObject(parent::valueIsSet($message)->value) === false) |
| 146 |
{
|
| 147 |
throw new exceptions\logic($message); |
| 148 |
} |
| 149 |
|
| 150 |
return $this; |
| 151 |
} |
| 152 |
|
| 153 |
protected function testedInstanceIsSet()100% |
| 154 |
{
|
| 155 |
if ($this->test === null || $this->test->testedInstance === null) |
| 156 |
{
|
| 157 |
throw new exceptions\logic('Tested instance is undefined in the test');
|
| 158 |
} |
| 159 |
|
| 160 |
return $this; |
| 161 |
} |
| 162 |
|
| 163 |
protected function check($value, $method)100% |
| 164 |
{
|
| 165 |
if ($this->analyzer->isObject($value) === false) |
| 166 |
{
|
| 167 |
throw new exceptions\logic('Argument of ' . __CLASS__ . '::' . $method . '() must be a class instance');
|
| 168 |
} |
| 169 |
|
| 170 |
return $this; |
| 171 |
} |
| 172 |
|
| 173 |
protected static function classExists($value)100% |
| 174 |
{
|
| 175 |
return (class_exists($value) === true || interface_exists($value) === true); |
| 176 |
} |
| 177 |
} |