LimeTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. /**
  12. * Unit test library.
  13. *
  14. * @package lime
  15. * @author Fabien Potencier <fabien.potencier@gmail.com>
  16. * @version SVN: $Id: LimeTest.php 23880 2009-11-14 10:14:34Z bschussek $
  17. */
  18. class LimeTest
  19. {
  20. protected
  21. $output = null,
  22. $options = array(),
  23. $errorReporting = true,
  24. $exception = null,
  25. $exceptionExpectation = null;
  26. public function __construct($plan = null, array $options = array())
  27. {
  28. $this->options = array(
  29. 'base_dir' => null,
  30. 'output' => 'tap',
  31. 'force_colors' => false,
  32. 'verbose' => false,
  33. 'serialize' => false,
  34. 'coverage' => false,
  35. );
  36. foreach (LimeShell::parseArguments($GLOBALS['argv']) as $argument => $value)
  37. {
  38. $this->options[str_replace('-', '_', $argument)] = $value;
  39. }
  40. $this->options = array_merge($this->options, $options);
  41. $this->options['base_dir'] = realpath($this->options['base_dir']);
  42. list ($file, $line) = LimeTrace::findCaller('LimeTest');
  43. if ($this->options['coverage'])
  44. {
  45. $this->output = new LimeOutputCoverage();
  46. }
  47. elseif (is_string($this->options['output']))
  48. {
  49. $factory = new LimeOutputFactory($this->options);
  50. $this->output = $factory->create($this->options['output']);
  51. }
  52. else
  53. {
  54. $this->output = $this->options['output'];
  55. }
  56. $this->output->focus($file);
  57. if (!is_null($plan))
  58. {
  59. $this->output->plan($plan);
  60. }
  61. set_error_handler(array($this, 'handleError'));
  62. // make sure that exceptions that are not caught by the test runner are
  63. // caught and formatted in an appropriate way
  64. set_exception_handler(array($this, 'handleException'));
  65. }
  66. public function setErrorReporting($enabled)
  67. {
  68. $this->errorReporting = $enabled;
  69. }
  70. public function __destruct()
  71. {
  72. $this->output->close();
  73. $this->output->flush();
  74. restore_error_handler();
  75. restore_exception_handler();
  76. }
  77. public function getOutput()
  78. {
  79. return $this->output;
  80. }
  81. private function test(LimeConstraintInterface $constraint, $value, $message)
  82. {
  83. try
  84. {
  85. $constraint->evaluate($value);
  86. return $this->pass($message);
  87. }
  88. catch (LimeConstraintException $e)
  89. {
  90. return $this->fail($message, $e->getMessage());
  91. }
  92. }
  93. /**
  94. * Tests a condition and passes if it is true
  95. *
  96. * @param mixed $exp condition to test
  97. * @param string $message display output message when the test passes
  98. *
  99. * @return boolean
  100. */
  101. public function ok($exp, $message = '')
  102. {
  103. if ((boolean)$exp)
  104. {
  105. return $this->pass($message);
  106. }
  107. else
  108. {
  109. return $this->fail($message);
  110. }
  111. }
  112. /**
  113. * Compares two values and passes if they are equal (==)
  114. *
  115. * @param mixed $exp1 left value
  116. * @param mixed $exp2 right value
  117. * @param string $message display output message when the test passes
  118. *
  119. * @return boolean
  120. */
  121. public function is($exp1, $exp2, $message = '')
  122. {
  123. return $this->test(new LimeConstraintIs($exp2), $exp1, $message);
  124. }
  125. /**
  126. * Compares two values and passes if they are identical (===)
  127. *
  128. * @param mixed $exp1 left value
  129. * @param mixed $exp2 right value
  130. * @param string $message display output message when the test passes
  131. *
  132. * @return boolean
  133. */
  134. public function same($exp1, $exp2, $message = '')
  135. {
  136. return $this->test(new LimeConstraintSame($exp2), $exp1, $message);
  137. }
  138. /**
  139. * Compares two values and passes if they are not equal
  140. *
  141. * @param mixed $exp1 left value
  142. * @param mixed $exp2 right value
  143. * @param string $message display output message when the test passes
  144. *
  145. * @return boolean
  146. */
  147. public function isnt($exp1, $exp2, $message = '')
  148. {
  149. return $this->test(new LimeConstraintIsNot($exp2), $exp1, $message);
  150. }
  151. /**
  152. * Compares two values and passes if they are not identical (!==)
  153. *
  154. * @param mixed $exp1 left value
  155. * @param mixed $exp2 right value
  156. * @param string $message display output message when the test passes
  157. *
  158. * @return boolean
  159. */
  160. public function isntSame($exp1, $exp2, $message = '')
  161. {
  162. return $this->test(new LimeConstraintNotSame($exp2), $exp1, $message);
  163. }
  164. /**
  165. * Tests a string against a regular expression
  166. *
  167. * @param string $exp value to test
  168. * @param string $regex the pattern to search for, as a string
  169. * @param string $message display output message when the test passes
  170. *
  171. * @return boolean
  172. */
  173. public function like($exp1, $exp2, $message = '')
  174. {
  175. return $this->test(new LimeConstraintLike($exp2), $exp1, $message);
  176. }
  177. /**
  178. * Checks that a string doesn't match a regular expression
  179. *
  180. * @param string $exp value to test
  181. * @param string $regex the pattern to search for, as a string
  182. * @param string $message display output message when the test passes
  183. *
  184. * @return boolean
  185. */
  186. public function unlike($exp1, $exp2, $message = '')
  187. {
  188. return $this->test(new LimeConstraintUnlike($exp2), $exp1, $message);
  189. }
  190. public function greaterThan($exp1, $exp2, $message = '')
  191. {
  192. return $this->test(new LimeConstraintGreaterThan($exp2), $exp1, $message);
  193. }
  194. public function greaterThanEqual($exp1, $exp2, $message = '')
  195. {
  196. return $this->test(new LimeConstraintGreaterThanEqual($exp2), $exp1, $message);
  197. }
  198. public function lessThan($exp1, $exp2, $message = '')
  199. {
  200. return $this->test(new LimeConstraintLessThan($exp2), $exp1, $message);
  201. }
  202. public function lessThanEqual($exp1, $exp2, $message = '')
  203. {
  204. return $this->test(new LimeConstraintLessThanEqual($exp2), $exp1, $message);
  205. }
  206. public function contains($exp1, $exp2, $message = '')
  207. {
  208. return $this->test(new LimeConstraintContains($exp2), $exp1, $message);
  209. }
  210. public function containsNot($exp1, $exp2, $message = '')
  211. {
  212. return $this->test(new LimeConstraintContainsNot($exp2), $exp1, $message);
  213. }
  214. /**
  215. * Always passes--useful for testing exceptions
  216. *
  217. * @param string $message display output message
  218. *
  219. * @return true
  220. */
  221. public function pass($message = '')
  222. {
  223. list ($file, $line) = LimeTrace::findCaller('LimeTest');
  224. $this->output->pass($message, $file, $line);
  225. return true;
  226. }
  227. /**
  228. * Always fails--useful for testing exceptions
  229. *
  230. * @param string $message display output message
  231. *
  232. * @return false
  233. */
  234. public function fail($message = '', $error = null)
  235. {
  236. list ($file, $line) = LimeTrace::findCaller('LimeTest');
  237. $this->output->fail($message, $file, $line, $error);
  238. return false;
  239. }
  240. /**
  241. * Outputs a diag message but runs no test
  242. *
  243. * @param string $message display output message
  244. *
  245. * @return void
  246. */
  247. public function diag($message)
  248. {
  249. $this->output->comment($message);
  250. }
  251. /**
  252. * Counts as $nbTests tests--useful for conditional tests
  253. *
  254. * @param string $message display output message
  255. * @param integer $nbTests number of tests to skip
  256. *
  257. * @return void
  258. */
  259. public function skip($message = '', $nbTests = 1)
  260. {
  261. list ($file, $line) = LimeTrace::findCaller('LimeTest');
  262. for ($i = 0; $i < $nbTests; $i++)
  263. {
  264. $this->output->skip($message, $file, $line);
  265. }
  266. }
  267. /**
  268. * Counts as a test--useful for tests yet to be written
  269. *
  270. * @param string $message display output message
  271. *
  272. * @return void
  273. */
  274. public function todo($message = '')
  275. {
  276. list ($file, $line) = LimeTrace::findCaller('LimeTest');
  277. $this->output->todo($message, $file, $line);
  278. }
  279. public function comment($message)
  280. {
  281. $this->output->comment($message);
  282. }
  283. public function mock($class, array $options = array())
  284. {
  285. return LimeMock::create($class, $this->output, $options);
  286. }
  287. public function stub($class, array $options = array())
  288. {
  289. $options = array_merge(array(
  290. 'nice' => true,
  291. 'no_exceptions' => true,
  292. ), $options);
  293. return LimeMock::create($class, new LimeOutputNone(), $options);
  294. }
  295. public function extendMock($class, array $options = array())
  296. {
  297. $options['stub_methods'] = false;
  298. return $this->mock($class, $options);
  299. }
  300. public function extendStub($class, array $options = array())
  301. {
  302. $options['stub_methods'] = false;
  303. return $this->stub($class, $options);
  304. }
  305. public function expect($exception, $code = null)
  306. {
  307. list ($file, $line) = LimeTrace::findCaller('LimeTest');
  308. $this->exceptionExpectation = new LimeExceptionExpectation($exception, $file, $line);
  309. $this->exception = null;
  310. }
  311. public function handleError($code, $message, $file, $line, $context)
  312. {
  313. if (!$this->errorReporting || ($code & error_reporting()) == 0)
  314. {
  315. return false;
  316. }
  317. switch ($code)
  318. {
  319. case E_WARNING:
  320. $message = 'Warning: '.$message;
  321. break;
  322. case E_NOTICE:
  323. $message = 'Notice: '.$message;
  324. break;
  325. }
  326. $this->output->warning($message, $file, $line);
  327. }
  328. public function handleException(Exception $exception)
  329. {
  330. if (!is_null($this->exceptionExpectation))
  331. {
  332. $this->exception = $exception;
  333. }
  334. else
  335. {
  336. $this->output->error(LimeError::fromException($exception));
  337. }
  338. return true;
  339. }
  340. public function verifyException()
  341. {
  342. if (!is_null($this->exceptionExpectation))
  343. {
  344. $expected = $this->exceptionExpectation->getException();
  345. $file = $this->exceptionExpectation->getFile();
  346. $line = $this->exceptionExpectation->getLine();
  347. if (is_string($expected))
  348. {
  349. $actual = is_object($this->exception) ? get_class($this->exception) : 'none';
  350. $message = sprintf('A "%s" was thrown', $expected);
  351. }
  352. else
  353. {
  354. $actual = $this->exception;
  355. $message = sprintf('A "%s" was thrown', get_class($expected));
  356. }
  357. // can't use ->is() here because the custom file and line need to be
  358. // passed to the output
  359. try
  360. {
  361. $constraint = new LimeConstraintIs($expected);
  362. $constraint->evaluate($actual);
  363. $this->output->pass($message, $file, $line);
  364. }
  365. catch (LimeConstraintException $e)
  366. {
  367. $this->output->fail($message, $file, $line, $e->getMessage());
  368. }
  369. }
  370. $this->exceptionExpectation = null;
  371. }
  372. }