/classes/asserters/variable.php

https://github.com/Yacodo/atoum · PHP · 219 lines · 180 code · 39 blank · 0 comment · 15 complexity · 5af2b1b97857e43cd61af4464e148f49 MD5 · raw file

  1. <?php
  2. namespace mageekguy\atoum\asserters;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\exceptions,
  6. mageekguy\atoum\tools\diffs
  7. ;
  8. class variable extends atoum\asserter
  9. {
  10. protected $isSet = false;
  11. protected $value = null;
  12. protected $isSetByReference = false;
  13. public function __toString()
  14. {
  15. return $this->getTypeOf($this->value);
  16. }
  17. public function wasSet()
  18. {
  19. return ($this->isSet === true);
  20. }
  21. public function setWith($value)
  22. {
  23. $this->value = $value;
  24. $this->isSet = true;
  25. $this->isSetByReference = false;
  26. return $this;
  27. }
  28. public function setByReferenceWith(& $value)
  29. {
  30. $this->value = & $value;
  31. $this->isSet = true;
  32. $this->isSetByReference = true;
  33. return $this;
  34. }
  35. public function reset()
  36. {
  37. $this->value = null;
  38. $this->isSet = false;
  39. $this->isSetByReference = false;
  40. return $this;
  41. }
  42. public function getValue()
  43. {
  44. return $this->value;
  45. }
  46. public function isSetByReference()
  47. {
  48. return ($this->isSet === true && $this->isSetByReference === true);
  49. }
  50. public function isEqualTo($value, $failMessage = null)
  51. {
  52. self::check($value, __METHOD__);
  53. if ($this->valueIsSet()->value == $value)
  54. {
  55. $this->pass();
  56. }
  57. else
  58. {
  59. $diff = new diffs\variable();
  60. $this->fail(
  61. ($failMessage !== null ? $failMessage : sprintf($this->getLocale()->_('%s is not equal to %s'), $this, $this->getTypeOf($value))) .
  62. PHP_EOL .
  63. $diff->setReference($value)->setData($this->value)
  64. );
  65. }
  66. return $this;
  67. }
  68. public function isNotEqualTo($value, $failMessage = null)
  69. {
  70. self::check($value, __METHOD__);
  71. if ($this->valueIsSet()->value != $value)
  72. {
  73. $this->pass();
  74. }
  75. else
  76. {
  77. $this->fail($failMessage !== null ? $failMessage : sprintf($this->getLocale()->_('%s is equal to %s'), $this, $this->getTypeOf($value)));
  78. }
  79. return $this;
  80. }
  81. public function isIdenticalTo($value, $failMessage = null)
  82. {
  83. self::check($value, __METHOD__);
  84. if ($this->valueIsSet()->value === $value)
  85. {
  86. $this->pass();
  87. }
  88. else
  89. {
  90. $diff = new diffs\variable();
  91. $this->fail(
  92. ($failMessage !== null ? $failMessage : sprintf($this->getLocale()->_('%s is not identical to %s'), $this, $this->getTypeOf($value))) .
  93. PHP_EOL .
  94. $diff->setReference($value)->setData($this->value)
  95. );
  96. }
  97. return $this;
  98. }
  99. public function isNotIdenticalTo($value, $failMessage = null)
  100. {
  101. self::check($value, __METHOD__);
  102. if ($this->valueIsSet()->value !== $value)
  103. {
  104. $this->pass();
  105. }
  106. else
  107. {
  108. $this->fail($failMessage !== null ? $failMessage : sprintf($this->getLocale()->_('%s is identical to %s'), $this, $this->getTypeOf($value)));
  109. }
  110. return $this;
  111. }
  112. public function isNull($failMessage = null)
  113. {
  114. if ($this->valueIsSet()->value === null)
  115. {
  116. $this->pass();
  117. }
  118. else
  119. {
  120. $this->fail($failMessage !== null ? $failMessage : sprintf($this->getLocale()->_('%s is not null'), $this));
  121. }
  122. return $this;
  123. }
  124. public function isNotNull($failMessage = null)
  125. {
  126. if ($this->valueIsSet()->value !== null)
  127. {
  128. $this->pass();
  129. }
  130. else
  131. {
  132. $this->fail($failMessage !== null ? $failMessage : sprintf($this->getLocale()->_('%s is null'), $this));
  133. }
  134. return $this;
  135. }
  136. public function isReferenceTo(& $reference, $failMessage = null)
  137. {
  138. if ($this->valueIsSet()->isSetByReference() === false)
  139. {
  140. throw new exceptions\logic('Value is not set by reference');
  141. }
  142. if (is_object($this->value) === true && is_object($reference) === true)
  143. {
  144. if ($this->value === $reference)
  145. {
  146. $this->pass();
  147. }
  148. else
  149. {
  150. $this->fail($failMessage !== null ? $failMessage : sprintf($this->getLocale()->_('%s is not a reference to %s'), $this, $this->getTypeOf($reference)));
  151. }
  152. }
  153. else
  154. {
  155. $tmp = $reference;
  156. $reference = uniqid(mt_rand());
  157. $isReference = ($this->value === $reference);
  158. $reference = $tmp;
  159. if ($isReference === true)
  160. {
  161. $this->pass();
  162. }
  163. else
  164. {
  165. $this->fail($failMessage !== null ? $failMessage : sprintf($this->getLocale()->_('%s is not a reference to %s'), $this, $this->getTypeOf($reference)));
  166. }
  167. }
  168. return $this;
  169. }
  170. protected function valueIsSet($message = 'Value is undefined')
  171. {
  172. if ($this->isSet === false)
  173. {
  174. throw new exceptions\logic($message);
  175. }
  176. return $this;
  177. }
  178. protected static function check($value, $method) {}
  179. }
  180. ?>