unit_tester.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id: unit_tester.php 1794 2008-06-28 12:55:41Z pp11 $
  7. */
  8. /**#@+
  9. * include other SimpleTest class files
  10. */
  11. require_once(dirname(__FILE__) . '/test_case.php');
  12. require_once(dirname(__FILE__) . '/dumper.php');
  13. /**#@-*/
  14. /**
  15. * Standard unit test class for day to day testing
  16. * of PHP code XP style. Adds some useful standard
  17. * assertions.
  18. * @package SimpleTest
  19. * @subpackage UnitTester
  20. */
  21. class UnitTestCase extends SimpleTestCase {
  22. /**
  23. * Creates an empty test case. Should be subclassed
  24. * with test methods for a functional test case.
  25. * @param string $label Name of test case. Will use
  26. * the class name if none specified.
  27. * @access public
  28. */
  29. function __construct($label = false) {
  30. if (! $label) {
  31. $label = get_class($this);
  32. }
  33. parent::__construct($label);
  34. }
  35. /**
  36. * Called from within the test methods to register
  37. * passes and failures.
  38. * @param boolean $result Pass on true.
  39. * @param string $message Message to display describing
  40. * the test state.
  41. * @return boolean True on pass
  42. * @access public
  43. */
  44. function assertTrue($result, $message = '%s') {
  45. return $this->assert(new TrueExpectation(), $result, $message);
  46. }
  47. /**
  48. * Will be true on false and vice versa. False
  49. * is the PHP definition of false, so that null,
  50. * empty strings, zero and an empty array all count
  51. * as false.
  52. * @param boolean $result Pass on false.
  53. * @param string $message Message to display.
  54. * @return boolean True on pass
  55. * @access public
  56. */
  57. function assertFalse($result, $message = '%s') {
  58. return $this->assert(new FalseExpectation(), $result, $message);
  59. }
  60. /**
  61. * Will be true if the value is null.
  62. * @param null $value Supposedly null value.
  63. * @param string $message Message to display.
  64. * @return boolean True on pass
  65. * @access public
  66. */
  67. function assertNull($value, $message = '%s') {
  68. $dumper = new SimpleDumper();
  69. $message = sprintf(
  70. $message,
  71. '[' . $dumper->describeValue($value) . '] should be null');
  72. return $this->assertTrue(! isset($value), $message);
  73. }
  74. /**
  75. * Will be true if the value is set.
  76. * @param mixed $value Supposedly set value.
  77. * @param string $message Message to display.
  78. * @return boolean True on pass.
  79. * @access public
  80. */
  81. function assertNotNull($value, $message = '%s') {
  82. $dumper = new SimpleDumper();
  83. $message = sprintf(
  84. $message,
  85. '[' . $dumper->describeValue($value) . '] should not be null');
  86. return $this->assertTrue(isset($value), $message);
  87. }
  88. /**
  89. * Type and class test. Will pass if class
  90. * matches the type name or is a subclass or
  91. * if not an object, but the type is correct.
  92. * @param mixed $object Object to test.
  93. * @param string $type Type name as string.
  94. * @param string $message Message to display.
  95. * @return boolean True on pass.
  96. * @access public
  97. */
  98. function assertIsA($object, $type, $message = '%s') {
  99. return $this->assert(
  100. new IsAExpectation($type),
  101. $object,
  102. $message);
  103. }
  104. /**
  105. * Type and class mismatch test. Will pass if class
  106. * name or underling type does not match the one
  107. * specified.
  108. * @param mixed $object Object to test.
  109. * @param string $type Type name as string.
  110. * @param string $message Message to display.
  111. * @return boolean True on pass.
  112. * @access public
  113. */
  114. function assertNotA($object, $type, $message = '%s') {
  115. return $this->assert(
  116. new NotAExpectation($type),
  117. $object,
  118. $message);
  119. }
  120. /**
  121. * Will trigger a pass if the two parameters have
  122. * the same value only. Otherwise a fail.
  123. * @param mixed $first Value to compare.
  124. * @param mixed $second Value to compare.
  125. * @param string $message Message to display.
  126. * @return boolean True on pass
  127. * @access public
  128. */
  129. function assertEqual($first, $second, $message = '%s') {
  130. return $this->assert(
  131. new EqualExpectation($first),
  132. $second,
  133. $message);
  134. }
  135. /**
  136. * Will trigger a pass if the two parameters have
  137. * a different value. Otherwise a fail.
  138. * @param mixed $first Value to compare.
  139. * @param mixed $second Value to compare.
  140. * @param string $message Message to display.
  141. * @return boolean True on pass
  142. * @access public
  143. */
  144. function assertNotEqual($first, $second, $message = '%s') {
  145. return $this->assert(
  146. new NotEqualExpectation($first),
  147. $second,
  148. $message);
  149. }
  150. /**
  151. * Will trigger a pass if the if the first parameter
  152. * is near enough to the second by the margin.
  153. * @param mixed $first Value to compare.
  154. * @param mixed $second Value to compare.
  155. * @param mixed $margin Fuzziness of match.
  156. * @param string $message Message to display.
  157. * @return boolean True on pass
  158. * @access public
  159. */
  160. function assertWithinMargin($first, $second, $margin, $message = '%s') {
  161. return $this->assert(
  162. new WithinMarginExpectation($first, $margin),
  163. $second,
  164. $message);
  165. }
  166. /**
  167. * Will trigger a pass if the two parameters differ
  168. * by more than the margin.
  169. * @param mixed $first Value to compare.
  170. * @param mixed $second Value to compare.
  171. * @param mixed $margin Fuzziness of match.
  172. * @param string $message Message to display.
  173. * @return boolean True on pass
  174. * @access public
  175. */
  176. function assertOutsideMargin($first, $second, $margin, $message = '%s') {
  177. return $this->assert(
  178. new OutsideMarginExpectation($first, $margin),
  179. $second,
  180. $message);
  181. }
  182. /**
  183. * Will trigger a pass if the two parameters have
  184. * the same value and same type. Otherwise a fail.
  185. * @param mixed $first Value to compare.
  186. * @param mixed $second Value to compare.
  187. * @param string $message Message to display.
  188. * @return boolean True on pass
  189. * @access public
  190. */
  191. function assertIdentical($first, $second, $message = '%s') {
  192. return $this->assert(
  193. new IdenticalExpectation($first),
  194. $second,
  195. $message);
  196. }
  197. /**
  198. * Will trigger a pass if the two parameters have
  199. * the different value or different type.
  200. * @param mixed $first Value to compare.
  201. * @param mixed $second Value to compare.
  202. * @param string $message Message to display.
  203. * @return boolean True on pass
  204. * @access public
  205. */
  206. function assertNotIdentical($first, $second, $message = '%s') {
  207. return $this->assert(
  208. new NotIdenticalExpectation($first),
  209. $second,
  210. $message);
  211. }
  212. /**
  213. * Will trigger a pass if both parameters refer
  214. * to the same object or value. Fail otherwise.
  215. * This will cause problems testing objects under
  216. * E_STRICT.
  217. * TODO: Replace with expectation.
  218. * @param mixed $first Reference to check.
  219. * @param mixed $second Hopefully the same variable.
  220. * @param string $message Message to display.
  221. * @return boolean True on pass
  222. * @access public
  223. */
  224. function assertReference(&$first, &$second, $message = '%s') {
  225. $dumper = new SimpleDumper();
  226. $message = sprintf(
  227. $message,
  228. '[' . $dumper->describeValue($first) .
  229. '] and [' . $dumper->describeValue($second) .
  230. '] should reference the same object');
  231. return $this->assertTrue(
  232. SimpleTestCompatibility::isReference($first, $second),
  233. $message);
  234. }
  235. /**
  236. * Will trigger a pass if both parameters refer
  237. * to the same object. Fail otherwise. This has
  238. * the same semantics at the PHPUnit assertSame.
  239. * That is, if values are passed in it has roughly
  240. * the same affect as assertIdentical.
  241. * TODO: Replace with expectation.
  242. * @param mixed $first Object reference to check.
  243. * @param mixed $second Hopefully the same object.
  244. * @param string $message Message to display.
  245. * @return boolean True on pass
  246. * @access public
  247. */
  248. function assertSame($first, $second, $message = '%s') {
  249. $dumper = new SimpleDumper();
  250. $message = sprintf(
  251. $message,
  252. '[' . $dumper->describeValue($first) .
  253. '] and [' . $dumper->describeValue($second) .
  254. '] should reference the same object');
  255. return $this->assertTrue($first === $second, $message);
  256. }
  257. /**
  258. * Will trigger a pass if both parameters refer
  259. * to different objects. Fail otherwise. The objects
  260. * have to be identical though.
  261. * @param mixed $first Object reference to check.
  262. * @param mixed $second Hopefully not the same object.
  263. * @param string $message Message to display.
  264. * @return boolean True on pass
  265. * @access public
  266. */
  267. function assertClone($first, $second, $message = '%s') {
  268. $dumper = new SimpleDumper();
  269. $message = sprintf(
  270. $message,
  271. '[' . $dumper->describeValue($first) .
  272. '] and [' . $dumper->describeValue($second) .
  273. '] should not be the same object');
  274. $identical = new IdenticalExpectation($first);
  275. return $this->assertTrue(
  276. $identical->test($second) && ! ($first === $second),
  277. $message);
  278. }
  279. /**
  280. * Will trigger a pass if both parameters refer
  281. * to different variables. Fail otherwise. The objects
  282. * have to be identical references though.
  283. * This will fail under E_STRICT with objects. Use
  284. * assertClone() for this.
  285. * @param mixed $first Object reference to check.
  286. * @param mixed $second Hopefully not the same object.
  287. * @param string $message Message to display.
  288. * @return boolean True on pass
  289. * @access public
  290. */
  291. function assertCopy(&$first, &$second, $message = "%s") {
  292. $dumper = new SimpleDumper();
  293. $message = sprintf(
  294. $message,
  295. "[" . $dumper->describeValue($first) .
  296. "] and [" . $dumper->describeValue($second) .
  297. "] should not be the same object");
  298. return $this->assertFalse(
  299. SimpleTestCompatibility::isReference($first, $second),
  300. $message);
  301. }
  302. /**
  303. * Will trigger a pass if the Perl regex pattern
  304. * is found in the subject. Fail otherwise.
  305. * @param string $pattern Perl regex to look for including
  306. * the regex delimiters.
  307. * @param string $subject String to search in.
  308. * @param string $message Message to display.
  309. * @return boolean True on pass
  310. * @access public
  311. */
  312. function assertPattern($pattern, $subject, $message = '%s') {
  313. return $this->assert(
  314. new PatternExpectation($pattern),
  315. $subject,
  316. $message);
  317. }
  318. /**
  319. * Will trigger a pass if the perl regex pattern
  320. * is not present in subject. Fail if found.
  321. * @param string $pattern Perl regex to look for including
  322. * the regex delimiters.
  323. * @param string $subject String to search in.
  324. * @param string $message Message to display.
  325. * @return boolean True on pass
  326. * @access public
  327. */
  328. function assertNoPattern($pattern, $subject, $message = '%s') {
  329. return $this->assert(
  330. new NoPatternExpectation($pattern),
  331. $subject,
  332. $message);
  333. }
  334. /**
  335. * Prepares for an error. If the error mismatches it
  336. * passes through, otherwise it is swallowed. Any
  337. * left over errors trigger failures.
  338. * @param SimpleExpectation/string $expected The error to match.
  339. * @param string $message Message on failure.
  340. * @access public
  341. */
  342. function expectError($expected = false, $message = '%s') {
  343. $queue = SimpleTest::getContext()->get('SimpleErrorQueue');
  344. $queue->expectError($this->coerceExpectation($expected), $message);
  345. }
  346. /**
  347. * Prepares for an exception. If the error mismatches it
  348. * passes through, otherwise it is swallowed. Any
  349. * left over errors trigger failures.
  350. * @param SimpleExpectation/Exception $expected The error to match.
  351. * @param string $message Message on failure.
  352. * @access public
  353. */
  354. function expectException($expected = false, $message = '%s') {
  355. $queue = SimpleTest::getContext()->get('SimpleExceptionTrap');
  356. $line = $this->getAssertionLine();
  357. $queue->expectException($expected, $message . $line);
  358. }
  359. /**
  360. * Creates an equality expectation if the
  361. * object/value is not already some type
  362. * of expectation.
  363. * @param mixed $expected Expected value.
  364. * @return SimpleExpectation Expectation object.
  365. * @access private
  366. */
  367. protected function coerceExpectation($expected) {
  368. if ($expected == false) {
  369. return new TrueExpectation();
  370. }
  371. if (SimpleTestCompatibility::isA($expected, 'SimpleExpectation')) {
  372. return $expected;
  373. }
  374. return new EqualExpectation(
  375. is_string($expected) ? str_replace('%', '%%', $expected) : $expected);
  376. }
  377. }
  378. ?>