Yay.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /*
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. //require 'Yay/Expectations.php';
  15. //require 'Yay/Matchers/OptionalMatcher.php';
  16. //require 'Yay/Matchers/AnyMatcher.php';
  17. //require 'Yay/Matchers/IdenticalMatcher.php';
  18. //require 'Yay/Matchers/EqualMatcher.php';
  19. //require 'Yay/Matchers/PatternMatcher.php';
  20. //require 'Yay/Matchers/ReferenceMatcher.php';
  21. //require 'Yay/Matchers/BoundsMatcher.php';
  22. //require 'Yay/Actions/ReturnValueAction.php';
  23. //require 'Yay/Actions/ReturnReferenceAction.php';
  24. //require 'Yay/Actions/ThrowAction.php';
  25. //require 'Yay/Actions/CallbackAction.php';
  26. /**
  27. * A convenience factory class.
  28. * @author Chris Corbyn <chris@w3style.co.uk>
  29. * @package Yay
  30. */
  31. class Yay
  32. {
  33. /**
  34. * The classpath used for autoloading.
  35. * @var string
  36. * @access private
  37. */
  38. private static $CLASSPATH = '.';
  39. // -- Expectations
  40. /**
  41. * Create a new Expectations builder instance.
  42. * @return Yay_Expectations
  43. */
  44. public static function expectations()
  45. {
  46. return new Yay_Expectations();
  47. }
  48. // -- Matchers
  49. /**
  50. * Create a new Optional matcher, optionally wrapping $value.
  51. * @param string $value, optional
  52. * @return Yay_Matchers_OptionalMatcher
  53. */
  54. public static function optional($value = null)
  55. {
  56. return new Yay_Matchers_OptionalMatcher($value);
  57. }
  58. /**
  59. * Create a new Any matcher, optionally constrained to $type.
  60. * @param string $type, optional
  61. * @return Yay_Matchers_AnyMatcher
  62. */
  63. public static function any($type = null)
  64. {
  65. return new Yay_Matchers_AnyMatcher($type, true);
  66. }
  67. /**
  68. * Create a negated Any matcher, optionally constrained to $type.
  69. * @param string $type, optional
  70. * @return Yay_Matchers_AnyMatcher
  71. */
  72. public static function none($type = null)
  73. {
  74. return new Yay_Matchers_AnyMatcher($type, false);
  75. }
  76. /**
  77. * Create a new Identical matcher for $value.
  78. * @param mixed $value
  79. * @return Yay_Matchers_IdenticalMatcher
  80. */
  81. public static function identical($value)
  82. {
  83. return new Yay_Matchers_IdenticalMatcher($value, true);
  84. }
  85. /**
  86. * Create a negated Identical matcher for $value.
  87. * @param mixed $value
  88. * @return Yay_Matchers_IdenticalMatcher
  89. */
  90. public static function notIdentical($value)
  91. {
  92. return new Yay_Matchers_IdenticalMatcher($value, false);
  93. }
  94. /**
  95. * Create a new Equal matcher for $value.
  96. * @param mixed $value
  97. * @return Yay_Matchers_EqualMatcher
  98. */
  99. public static function equal($value)
  100. {
  101. return new Yay_Matchers_EqualMatcher($value, true);
  102. }
  103. /**
  104. * Create a negated Equal matcher for $value.
  105. * @param mixed $value
  106. * @return Yay_Matchers_EqualMatcher
  107. */
  108. public static function notEqual($value)
  109. {
  110. return new Yay_Matchers_EqualMatcher($value, false);
  111. }
  112. /**
  113. * Create a new Pattern matcher for $pattern.
  114. * @param string $pattern
  115. * @return Yay_Matchers_IsAMatcher
  116. */
  117. public static function pattern($pattern)
  118. {
  119. return new Yay_Matchers_PatternMatcher($pattern, true);
  120. }
  121. /**
  122. * Create a negated Pattern matcher for $pattern.
  123. * @param string $pattern
  124. * @return Yay_Matchers_IsAMatcher
  125. */
  126. public static function noPattern($pattern)
  127. {
  128. return new Yay_Matchers_PatternMatcher($pattern, false);
  129. }
  130. /**
  131. * Create a new Reference matcher for $ref.
  132. * @param mixed $ref
  133. * @return Yay_Matchers_ReferenceMatcher
  134. */
  135. public static function reference(&$ref)
  136. {
  137. return new Yay_Matchers_ReferenceMatcher($ref, true);
  138. }
  139. /**
  140. * Create a negated Reference matcher for $ref.
  141. * @param mixed $ref
  142. * @return Yay_Matchers_ReferenceMatcher
  143. */
  144. public static function noReference(&$ref)
  145. {
  146. return new Yay_Matchers_ReferenceMatcher($ref, false);
  147. }
  148. /**
  149. * Create a new Bounds matcher for boundaries between $lower and $upper.
  150. * @param mixed $lower
  151. * @param mixed $upper
  152. * @return Yay_Matchers_BoundsMatcher
  153. */
  154. public static function bounds($lower, $upper)
  155. {
  156. return new Yay_Matchers_BoundsMatcher($lower, $upper, true);
  157. }
  158. /**
  159. * Create a negated Bounds matcher for boundaries outside $lower and $upper.
  160. * @param mixed $lower
  161. * @param mixed $upper
  162. * @return Yay_Matchers_BoundsMatcher
  163. */
  164. public static function outside($lower, $upper)
  165. {
  166. return new Yay_Matchers_BoundsMatcher($lower, $upper, false);
  167. }
  168. // -- Actions
  169. /**
  170. * Create a new ReturnValueAction with $value.
  171. * @param mixed $value
  172. * @return Yay_Actions_ReturnValueAction
  173. */
  174. public static function returnValue($value)
  175. {
  176. return new Yay_Actions_ReturnValueAction($value);
  177. }
  178. /**
  179. * Create a new ReturnReferenceAction with &$ref.
  180. * @param mixed $ref
  181. * @return Yay_Actions_ReturnReferenceAction
  182. */
  183. public static function returnReference(&$ref)
  184. {
  185. return new Yay_Actions_ReturnReferenceAction($ref);
  186. }
  187. /**
  188. * Create a new ThrowAction with $e.
  189. * @param Exception $ref
  190. * @return Yay_Actions_ThrowAction
  191. */
  192. public static function throwException(Exception $e)
  193. {
  194. return new Yay_Actions_ThrowAction($e);
  195. }
  196. /**
  197. * Create a new CallbackAction with $callback.
  198. * @param callback $callback
  199. * @return Yay_Actions_CallbackAction
  200. */
  201. public static function call($callback)
  202. {
  203. return new Yay_Actions_CallbackAction($callback);
  204. }
  205. /**
  206. * Set the classpath for autoloading.
  207. * @param string $path
  208. */
  209. public static function setClassPath($path)
  210. {
  211. self::$CLASSPATH = $path;
  212. }
  213. /**
  214. * Static autoloader registered in bootstrap file.
  215. * @param string $class
  216. */
  217. public static function autoload($class)
  218. {
  219. if (substr($class, 0, 3) != 'Yay')
  220. {
  221. return;
  222. }
  223. $file = str_replace('_', '/', $class) . '.php';
  224. $path = self::$CLASSPATH . '/' . $file;
  225. if (is_file($path))
  226. {
  227. require_once $path;
  228. }
  229. }
  230. }