95% of 778OPs |
92% of 100Lines |
75% of 89Branches |
59% of 39Paths |
| # | |
|---|---|
| 1 |
<?php |
| 2 |
|
| 3 |
namespace mageekguy\atoum\asserters; |
| 4 |
|
| 5 |
use |
| 6 |
mageekguy\atoum, |
| 7 |
mageekguy\atoum\asserter, |
| 8 |
mageekguy\atoum\asserters, |
| 9 |
mageekguy\atoum\exceptions |
| 10 |
; |
| 11 |
|
| 12 |
class phpString extends asserters\variable |
| 13 |
{
|
| 14 |
protected $charlist = null; |
| 15 |
|
| 16 |
public function __get($asserter)70% |
| 17 |
{
|
| 18 |
switch (strtolower($asserter)) |
| 19 |
{
|
| 20 |
case 'length': |
| 21 |
return $this->getLengthAsserter(); |
| 22 |
|
| 23 |
case 'isempty': |
| 24 |
return $this->isEmpty(); |
| 25 |
|
| 26 |
case 'isnotempty': |
| 27 |
return $this->isNotEmpty(); |
| 28 |
|
| 29 |
default: |
| 30 |
return $this->generator->__get($asserter); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
public function __toString()100% |
| 35 |
{
|
| 36 |
return (is_string($this->value) === false ? parent::__toString() : $this->_('string(%s) \'%s\'', strlen($this->value), addcslashes($this->value, $this->charlist)));
|
| 37 |
} |
| 38 |
|
| 39 |
public function getCharlist()100% |
| 40 |
{
|
| 41 |
return $this->charlist; |
| 42 |
} |
| 43 |
|
| 44 |
public function setWith($value, $charlist = null, $checkType = true)100% |
| 45 |
{
|
| 46 |
parent::setWith($value); |
| 47 |
|
| 48 |
$this->charlist = $charlist; |
| 49 |
|
| 50 |
if ($checkType === true) |
| 51 |
{
|
| 52 |
if ($this->analyzer->isString($this->value) === true) |
| 53 |
{
|
| 54 |
$this->pass(); |
| 55 |
} |
| 56 |
else |
| 57 |
{
|
| 58 |
$this->fail($this->_('%s is not a string', $this));
|
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
return $this; |
| 63 |
} |
| 64 |
|
| 65 |
public function isEmpty($failMessage = null)100% |
| 66 |
{
|
| 67 |
return $this->isEqualTo('', $failMessage ?: $this->_('string is not empty'));
|
| 68 |
} |
| 69 |
|
| 70 |
public function isNotEmpty($failMessage = null)100% |
| 71 |
{
|
| 72 |
return $this->isNotEqualTo('', $failMessage ?: $this->_('string is empty'));
|
| 73 |
} |
| 74 |
|
| 75 |
public function match($pattern, $failMessage = null)0% |
| 76 |
{
|
| 77 |
return $this->matches($pattern, $failMessage = null); |
| 78 |
} |
| 79 |
|
| 80 |
public function matches($pattern, $failMessage = null)100% |
| 81 |
{
|
| 82 |
if (preg_match($pattern, $this->valueIsSet()->value) === 1) |
| 83 |
{
|
| 84 |
$this->pass(); |
| 85 |
} |
| 86 |
else |
| 87 |
{
|
| 88 |
$this->fail($failMessage ?: $this->_('%s does not match %s', $this, $pattern));
|
| 89 |
} |
| 90 |
|
| 91 |
return $this; |
| 92 |
} |
| 93 |
|
| 94 |
public function isEqualTo($value, $failMessage = null)100% |
| 95 |
{
|
| 96 |
return parent::isEqualTo($value, $failMessage ?: $this->_('strings are not equal'));
|
| 97 |
} |
| 98 |
|
| 99 |
public function isEqualToContentsOfFile($path, $failMessage = null)75% |
| 100 |
{
|
| 101 |
$this->valueIsSet(); |
| 102 |
|
| 103 |
$fileContents = @file_get_contents($path); |
| 104 |
|
| 105 |
if ($fileContents === false) |
| 106 |
{
|
| 107 |
$this->fail($this->_('Unable to get contents of file %s', $path));
|
| 108 |
} |
| 109 |
else |
| 110 |
{
|
| 111 |
return parent::isEqualTo($fileContents, $failMessage ?: $this->_('string is not equal to contents of file %s', $path));
|
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
public function hasLength($length, $failMessage = null) |
| 116 |
{
|
| 117 |
if (strlen($this->valueIsSet()->value) == $length) |
| 118 |
{
|
| 119 |
$this->pass(); |
| 120 |
} |
| 121 |
else |
| 122 |
{
|
| 123 |
$this->fail($failMessage ?: $this->_('length of %s is not %d', $this, $length));
|
| 124 |
} |
| 125 |
|
| 126 |
return $this; |
| 127 |
} |
| 128 |
|
| 129 |
public function hasLengthGreaterThan($length, $failMessage = null)100% |
| 130 |
{
|
| 131 |
if (strlen($this->valueIsSet()->value) > $length) |
| 132 |
{
|
| 133 |
$this->pass(); |
| 134 |
} |
| 135 |
else |
| 136 |
{
|
| 137 |
$this->fail($failMessage ?: $this->_('length of %s is not greater than %d', $this, $length));
|
| 138 |
} |
| 139 |
|
| 140 |
return $this; |
| 141 |
} |
| 142 |
|
| 143 |
public function hasLengthLessThan($length, $failMessage = null)100% |
| 144 |
{
|
| 145 |
if (strlen($this->valueIsSet()->value) < $length) |
| 146 |
{
|
| 147 |
$this->pass(); |
| 148 |
} |
| 149 |
else |
| 150 |
{
|
| 151 |
$this->fail($failMessage ?: $this->_('length of %s is not less than %d', $this, $length));
|
| 152 |
} |
| 153 |
|
| 154 |
return $this; |
| 155 |
} |
| 156 |
|
| 157 |
public function contains($fragment, $failMessage = null)100% |
| 158 |
{
|
| 159 |
if (strpos($this->valueIsSet()->value, $fragment) !== false) |
| 160 |
{
|
| 161 |
$this->pass(); |
| 162 |
} |
| 163 |
else |
| 164 |
{
|
| 165 |
$this->fail($failMessage ?: $this->_('%s does not contain %s', $this, $fragment));
|
| 166 |
} |
| 167 |
|
| 168 |
return $this; |
| 169 |
} |
| 170 |
|
| 171 |
public function notContains($fragment, $failMessage = null)83% |
| 172 |
{
|
| 173 |
if (strpos($this->valueIsSet()->value, $fragment) !== false) |
| 174 |
{
|
| 175 |
$this->fail($failMessage ?: $this->_('%s contains %s', $this, $fragment));
|
| 176 |
} |
| 177 |
else |
| 178 |
{
|
| 179 |
$this->pass(); |
| 180 |
} |
| 181 |
|
| 182 |
return $this; |
| 183 |
} |
| 184 |
|
| 185 |
public function startWith($fragment, $failMessage = null)100% |
| 186 |
{
|
| 187 |
if (strpos($this->valueIsSet()->value, $fragment) === 0) |
| 188 |
{
|
| 189 |
$this->pass(); |
| 190 |
} |
| 191 |
else |
| 192 |
{
|
| 193 |
$this->fail($failMessage ?: $this->_('%s does not start with %s', $this, $fragment));
|
| 194 |
} |
| 195 |
|
| 196 |
return $this; |
| 197 |
} |
| 198 |
|
| 199 |
public function notStartWith($fragment, $failMessage = null)100% |
| 200 |
{
|
| 201 |
$fragmentPosition = strpos($this->valueIsSet()->value, $fragment); |
| 202 |
|
| 203 |
if ($fragmentPosition === false || $fragmentPosition > 0) |
| 204 |
{
|
| 205 |
$this->pass(); |
| 206 |
} |
| 207 |
else |
| 208 |
{
|
| 209 |
$this->fail($failMessage ?: $this->_('%s start with %s', $this, $fragment));
|
| 210 |
} |
| 211 |
|
| 212 |
return $this; |
| 213 |
} |
| 214 |
|
| 215 |
public function endWith($fragment, $failMessage = null)100% |
| 216 |
{
|
| 217 |
if (strpos($this->valueIsSet()->value, $fragment) === (strlen($this->valueIsSet()->value) - strlen($fragment))) |
| 218 |
{
|
| 219 |
$this->pass(); |
| 220 |
} |
| 221 |
else |
| 222 |
{
|
| 223 |
$this->fail($failMessage ?: $this->_('%s does not end with %s', $this, $fragment));
|
| 224 |
} |
| 225 |
|
| 226 |
return $this; |
| 227 |
} |
| 228 |
|
| 229 |
public function notEndWith($fragment, $failMessage = null)83% |
| 230 |
{
|
| 231 |
if (strpos($this->valueIsSet()->value, $fragment) === (strlen($this->valueIsSet()->value) - strlen($fragment))) |
| 232 |
{
|
| 233 |
$this->fail($failMessage ?: $this->_('%s end with %s', $this, $fragment));
|
| 234 |
} |
| 235 |
else |
| 236 |
{
|
| 237 |
$this->pass(); |
| 238 |
} |
| 239 |
|
| 240 |
return $this; |
| 241 |
} |
| 242 |
|
| 243 |
protected function getLengthAsserter()100% |
| 244 |
{
|
| 245 |
return $this->generator->__call('integer', array(strlen($this->valueIsSet()->value)));
|
| 246 |
} |
| 247 |
} |