lime.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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
  145. {
  146. const
  147. ERROR = 'ERROR',
  148. INFO = 'INFO',
  149. PARAMETER = 'PARAMETER',
  150. COMMENT = 'COMMENT',
  151. GREEN_BAR = 'GREEN_BAR',
  152. RED_BAR = 'RED_BAR',
  153. INFO_BAR = 'INFO_BAR';
  154. protected static
  155. $styles = array(self::ERROR, self::INFO, self::PARAMETER, self::COMMENT, self::GREEN_BAR, self::RED_BAR, self::INFO_BAR);
  156. protected
  157. $colorizer = null;
  158. /**
  159. * Constructor.
  160. *
  161. * @param boolean $forceColors If set to TRUE, colorization will be enforced
  162. * whether or not the current console supports it
  163. */
  164. public function __construct($forceColors = false)
  165. {
  166. if (LimeColorizer::isSupported() || $forceColors)
  167. {
  168. $colorizer = new LimeColorizer();
  169. $colorizer->setStyle(self::ERROR, array('bg' => 'red', 'fg' => 'white', 'bold' => true));
  170. $colorizer->setStyle(self::INFO, array('fg' => 'green', 'bold' => true));
  171. $colorizer->setStyle(self::PARAMETER, array('fg' => 'cyan'));
  172. $colorizer->setStyle(self::COMMENT, array('fg' => 'yellow'));
  173. $colorizer->setStyle(self::GREEN_BAR, array('fg' => 'white', 'bg' => 'green', 'bold' => true));
  174. $colorizer->setStyle(self::RED_BAR, array('fg' => 'white', 'bg' => 'red', 'bold' => true));
  175. $colorizer->setStyle(self::INFO_BAR, array('fg' => 'cyan', 'bold' => true));
  176. $this->colorizer = $colorizer;
  177. }
  178. }
  179. /**
  180. * Colorizes the given text with the given style.
  181. *
  182. * @param string $text Some text
  183. * @param string $style One of the predefined style constants
  184. * @return string The formatted text
  185. */
  186. protected function colorize($text, $style)
  187. {
  188. if (!in_array($style, self::$styles))
  189. {
  190. throw new InvalidArgumentException(sprintf('The style "%s" does not exist', $style));
  191. }
  192. return is_null($this->colorizer) ? $text : $this->colorizer->colorize($text, $style);
  193. }
  194. /**
  195. * ?
  196. */
  197. public function diag()
  198. {
  199. $messages = func_get_args();
  200. foreach ($messages as $message)
  201. {
  202. echo $this->colorize('# '.join("\n# ", (array) $message), self::COMMENT)."\n";
  203. }
  204. }
  205. /**
  206. * Prints a comment.
  207. *
  208. * @param string $message
  209. */
  210. public function comment($message)
  211. {
  212. echo $this->colorize(sprintf('# %s', $message), self::COMMENT)."\n";
  213. }
  214. /**
  215. * Prints an informational message.
  216. *
  217. * @param string $message
  218. */
  219. public function info($message)
  220. {
  221. echo $this->colorize(sprintf('> %s', $message), self::INFO_BAR)."\n";
  222. }
  223. /**
  224. * Prints an error.
  225. *
  226. * @param string $message
  227. */
  228. public function error($message)
  229. {
  230. echo $this->colorize(sprintf(' %s ', $message), self::RED_BAR)."\n";
  231. }
  232. /**
  233. * Prints and automatically colorizes a line.
  234. *
  235. * You can wrap the whole line into a specific predefined style by passing
  236. * the style constant in the second parameter.
  237. *
  238. * @param string $message The message to colorize
  239. * @param string $style The desired style constant
  240. * @param boolean $colorize Whether to automatically colorize parts of the
  241. * line
  242. */
  243. public function echoln($message, $style = null, $colorize = true)
  244. {
  245. if ($colorize)
  246. {
  247. $message = preg_replace('/(?:^|\.)((?:not ok|dubious) *\d*)\b/e', '$this->colorize(\'$1\', self::ERROR)', $message);
  248. $message = preg_replace('/(?:^|\.)(ok *\d*)\b/e', '$this->colorize(\'$1\', self::INFO)', $message);
  249. $message = preg_replace('/"(.+?)"/e', '$this->colorize(\'$1\', self::PARAMETER)', $message);
  250. $message = preg_replace('/(\->|\:\:)?([a-zA-Z0-9_]+?)\(\)/e', '$this->colorize(\'$1$2()\', self::PARAMETER)', $message);
  251. }
  252. echo ($style ? $this->colorize($message, $style) : $message)."\n";
  253. }
  254. /**
  255. * Prints a message in a green box.
  256. *
  257. * @param string $message
  258. */
  259. public function greenBar($message)
  260. {
  261. echo $this->colorize($message.str_repeat(' ', 71 - min(71, strlen($message))), self::GREEN_BAR)."\n";
  262. }
  263. /**
  264. * Prints a message a in a red box.
  265. *
  266. * @param string $message
  267. */
  268. public function redBar($message)
  269. {
  270. echo $this->colorize($message.str_repeat(' ', 71 - min(71, strlen($message))), self::RED_BAR)."\n";
  271. }
  272. }
  273. class lime_output_color extends lime_output
  274. {
  275. }
  276. class lime_colorizer extends LimeColorizer
  277. {
  278. protected static
  279. $instances = array(),
  280. $staticStyles = array();
  281. public function __construct()
  282. {
  283. self::$instances[] = $this;
  284. $this->styles = self::$staticStyles;
  285. }
  286. public static function style($name, $options = array())
  287. {
  288. foreach (self::$instances as $instance)
  289. {
  290. $instance->setStyle($name, $options);
  291. }
  292. self::$staticStyles[$name] = $options;
  293. }
  294. }
  295. class lime_harness extends LimeTestSuite
  296. {
  297. public function __construct($options = array())
  298. {
  299. // for BC
  300. if (!is_array($options))
  301. {
  302. $options = array(); // drop the old output because it is not compatible with LimeTest
  303. }
  304. else if (array_key_exists('php_cli', $options))
  305. {
  306. $options['executable'] = $options['php_cli'];
  307. unset($options['php_cli']);
  308. }
  309. parent::__construct($options);
  310. }
  311. public function to_array()
  312. {
  313. return $this->toArray();
  314. }
  315. public function to_xml()
  316. {
  317. return $this->toXml();
  318. }
  319. public function get_failed_files()
  320. {
  321. return $this->output->getFailedFiles();
  322. }
  323. }
  324. class lime_coverage extends LimeCoverage
  325. {
  326. public static function get_php_lines($content)
  327. {
  328. return self::getPhpLines($content);
  329. }
  330. public function format_range($lines)
  331. {
  332. return $this->formatRange($lines);
  333. }
  334. }
  335. class lime_registration extends LimeRegistration
  336. {
  337. public function register_glob($glob)
  338. {
  339. return $this->registerGlob($glob);
  340. }
  341. public function register_dir($directory)
  342. {
  343. return $this->registerDir($directory);
  344. }
  345. }