lime.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /*
  3. * This file is part of the Lime framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. require_once dirname(__FILE__).'/LimeAutoloader.php';
  12. LimeAutoloader::enableLegacyMode();
  13. LimeAutoloader::register();
  14. class lime_test extends LimeTest
  15. {
  16. public function __construct($plan = null, $options = array())
  17. {
  18. // for BC
  19. if (!is_array($options))
  20. {
  21. $options = array(); // drop the old output because it is not compatible with LimeTest
  22. }
  23. parent::__construct($plan, $options);
  24. }
  25. static public function to_array()
  26. {
  27. return self::toArray();
  28. }
  29. static public function to_xml($results = null)
  30. {
  31. return self::toXml($results);
  32. }
  33. /**
  34. * Compares two arguments with an operator
  35. *
  36. * @param mixed $exp1 left value
  37. * @param string $op operator
  38. * @param mixed $exp2 right value
  39. * @param string $message display output message when the test passes
  40. *
  41. * @return boolean
  42. */
  43. public function cmp_ok($exp1, $op, $exp2, $message = '')
  44. {
  45. switch ($op)
  46. {
  47. case '===':
  48. return $this->same($exp1, $exp2, $message);
  49. case '!==':
  50. return $this->isntSame($exp1, $exp2, $message);
  51. case '==':
  52. return $this->is($exp1, $exp2, $message);
  53. case '!=':
  54. return $this->isnt($exp1, $exp2, $message);
  55. case '<':
  56. return $this->lessThan($exp1, $exp2, $message);
  57. case '<=':
  58. return $this->lessThanEqual($exp1, $exp2, $message);
  59. case '>':
  60. return $this->greaterThan($exp1, $exp2, $message);
  61. case '>=':
  62. return $this->greaterThanEqual($exp1, $exp2, $message);
  63. default:
  64. throw new InvalidArgumentException(sprintf('Unknown operation "%s"', $op));
  65. }
  66. }
  67. /**
  68. * Checks the availability of a method for an object or a class
  69. *
  70. * @param mixed $object an object instance or a class name
  71. * @param string|array $methods one or more method names
  72. * @param string $message display output message when the test passes
  73. *
  74. * @return boolean
  75. */
  76. public function can_ok($object, $methods, $message = '')
  77. {
  78. $result = true;
  79. $failedMessages = array();
  80. foreach ((array) $methods as $method)
  81. {
  82. if (!method_exists($object, $method))
  83. {
  84. $failedMessages[] = sprintf("method '%s' does not exist", $method);
  85. $result = false;
  86. }
  87. }
  88. return $this->test_ok($result, $message, implode("\n", $failedMessages));
  89. }
  90. /**
  91. * Checks the type of an argument
  92. *
  93. * @param mixed $var variable instance
  94. * @param string $class class or type name
  95. * @param string $message display output message when the test passes
  96. *
  97. * @return boolean
  98. */
  99. public function isa_ok($var, $class, $message = '')
  100. {
  101. $type = is_object($var) ? get_class($var) : gettype($var);
  102. $error = sprintf("variable isn't a '%s' it's a '%s'", $class, $type);
  103. return $this->test_ok($type == $class, $message, $error);
  104. }
  105. public function is_deeply($exp1, $exp2, $message = '')
  106. {
  107. return $this->is($exp1, $exp2, $message);
  108. }
  109. public function include_ok($file, $message = '')
  110. {
  111. return $this->includeOk($file, $message);
  112. }
  113. public function error($message)
  114. {
  115. list($file, $line) = LimeTrace::findCaller('lime_test');
  116. $this->output->error(new LimeError($message, $file, $line));
  117. }
  118. /**
  119. * @deprecated Use comment() instead
  120. * @param $message
  121. * @return unknown_type
  122. */
  123. public function info($message)
  124. {
  125. if ($this->output instanceof LimeOutputTap)
  126. {
  127. $this->output->info($message);
  128. }
  129. }
  130. private function test_ok($condition, $message, $error = null)
  131. {
  132. list ($file, $line) = LimeTrace::findCaller('LimeTest');
  133. if ($result = (boolean) $condition)
  134. {
  135. $this->output->pass($message, $file, $line);
  136. }
  137. else
  138. {
  139. $this->output->fail($message, $file, $line, $error);
  140. }
  141. return $result;
  142. }
  143. }
  144. class lime_output extends LimeOutput
  145. {
  146. public function green_bar($message)
  147. {
  148. return $this->greenBar($message);
  149. }
  150. public function red_bar($message)
  151. {
  152. return $this->redBar($message);
  153. }
  154. }
  155. class lime_output_color extends LimeOutput
  156. {
  157. }
  158. class lime_colorizer extends LimeColorizer
  159. {
  160. protected static
  161. $instances = array(),
  162. $staticStyles = array();
  163. public function __construct()
  164. {
  165. self::$instances[] = $this;
  166. $this->styles = self::$staticStyles;
  167. }
  168. public static function style($name, $options = array())
  169. {
  170. foreach (self::$instances as $instance)
  171. {
  172. $instance->setStyle($name, $options);
  173. }
  174. self::$staticStyles[$name] = $options;
  175. }
  176. }
  177. class lime_harness extends LimeTestSuite
  178. {
  179. public function __construct($options = array())
  180. {
  181. // for BC
  182. if (!is_array($options))
  183. {
  184. $options = array(); // drop the old output because it is not compatible with LimeTest
  185. }
  186. else if (array_key_exists('php_cli', $options))
  187. {
  188. $options['executable'] = $options['php_cli'];
  189. unset($options['php_cli']);
  190. }
  191. parent::__construct($options);
  192. }
  193. public function to_array()
  194. {
  195. return $this->toArray();
  196. }
  197. public function to_xml()
  198. {
  199. return $this->toXml();
  200. }
  201. public function get_failed_files()
  202. {
  203. return $this->output->getFailedFiles();
  204. }
  205. }
  206. class lime_coverage extends LimeCoverage
  207. {
  208. public static function get_php_lines($content)
  209. {
  210. return self::getPhpLines($content);
  211. }
  212. public function format_range($lines)
  213. {
  214. return $this->formatRange($lines);
  215. }
  216. }
  217. class lime_registration extends LimeRegistration
  218. {
  219. public function register_glob($glob)
  220. {
  221. return $this->registerGlob($glob);
  222. }
  223. public function register_dir($directory)
  224. {
  225. return $this->registerDir($directory);
  226. }
  227. }