PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/simpletest/test/visual_test.php

https://github.com/justinburger/Light-Weight-MVC
PHP | 566 lines | 453 code | 103 blank | 10 comment | 13 complexity | eeaef14e87da9e1a9097a7986cfdd88c MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. // $Id: visual_test.php,v 1.1 2007/12/12 15:06:30 jburger Exp $
  3. // NOTE:
  4. // Some of these tests are designed to fail! Do not be alarmed.
  5. // ----------------
  6. // The following tests are a bit hacky. Whilst Kent Beck tried to
  7. // build a unit tester with a unit tester, I am not that brave.
  8. // Instead I have just hacked together odd test scripts until
  9. // I have enough of a tester to procede more formally.
  10. //
  11. // The proper tests start in all_tests.php
  12. require_once('../unit_tester.php');
  13. require_once('../shell_tester.php');
  14. require_once('../mock_objects.php');
  15. require_once('../reporter.php');
  16. require_once('../xml.php');
  17. class TestDisplayClass {
  18. var $_a;
  19. function TestDisplayClass($a) {
  20. $this->_a = $a;
  21. }
  22. }
  23. class PassingUnitTestCaseOutput extends UnitTestCase {
  24. function testOfResults() {
  25. $this->pass('Pass');
  26. }
  27. function testTrue() {
  28. $this->assertTrue(true);
  29. }
  30. function testFalse() {
  31. $this->assertFalse(false);
  32. }
  33. function testExpectation() {
  34. $expectation = &new EqualExpectation(25, 'My expectation message: %s');
  35. $this->assert($expectation, 25, 'My assert message : %s');
  36. }
  37. function testNull() {
  38. $this->assertNull(null, "%s -> Pass");
  39. $this->assertNotNull(false, "%s -> Pass");
  40. }
  41. function testType() {
  42. $this->assertIsA("hello", "string", "%s -> Pass");
  43. $this->assertIsA($this, "PassingUnitTestCaseOutput", "%s -> Pass");
  44. $this->assertIsA($this, "UnitTestCase", "%s -> Pass");
  45. }
  46. function testTypeEquality() {
  47. $this->assertEqual("0", 0, "%s -> Pass");
  48. }
  49. function testNullEquality() {
  50. $this->assertNotEqual(null, 1, "%s -> Pass");
  51. $this->assertNotEqual(1, null, "%s -> Pass");
  52. }
  53. function testIntegerEquality() {
  54. $this->assertNotEqual(1, 2, "%s -> Pass");
  55. }
  56. function testStringEquality() {
  57. $this->assertEqual("a", "a", "%s -> Pass");
  58. $this->assertNotEqual("aa", "ab", "%s -> Pass");
  59. }
  60. function testHashEquality() {
  61. $this->assertEqual(array("a" => "A", "b" => "B"), array("b" => "B", "a" => "A"), "%s -> Pass");
  62. }
  63. function testWithin() {
  64. $this->assertWithinMargin(5, 5.4, 0.5, "%s -> Pass");
  65. }
  66. function testOutside() {
  67. $this->assertOutsideMargin(5, 5.6, 0.5, "%s -> Pass");
  68. }
  69. function testStringIdentity() {
  70. $a = "fred";
  71. $b = $a;
  72. $this->assertIdentical($a, $b, "%s -> Pass");
  73. }
  74. function testTypeIdentity() {
  75. $a = "0";
  76. $b = 0;
  77. $this->assertNotIdentical($a, $b, "%s -> Pass");
  78. }
  79. function testNullIdentity() {
  80. $this->assertNotIdentical(null, 1, "%s -> Pass");
  81. $this->assertNotIdentical(1, null, "%s -> Pass");
  82. }
  83. function testHashIdentity() {
  84. }
  85. function testObjectEquality() {
  86. $this->assertEqual(new TestDisplayClass(4), new TestDisplayClass(4), "%s -> Pass");
  87. $this->assertNotEqual(new TestDisplayClass(4), new TestDisplayClass(5), "%s -> Pass");
  88. }
  89. function testObjectIndentity() {
  90. $this->assertIdentical(new TestDisplayClass(false), new TestDisplayClass(false), "%s -> Pass");
  91. $this->assertNotIdentical(new TestDisplayClass(false), new TestDisplayClass(0), "%s -> Pass");
  92. }
  93. function testReference() {
  94. $a = "fred";
  95. $b = &$a;
  96. $this->assertReference($a, $b, "%s -> Pass");
  97. }
  98. function testCloneOnDifferentObjects() {
  99. $a = "fred";
  100. $b = $a;
  101. $c = "Hello";
  102. $this->assertClone($a, $b, "%s -> Pass");
  103. }
  104. function testPatterns() {
  105. $this->assertPattern('/hello/i', "Hello there", "%s -> Pass");
  106. $this->assertNoPattern('/hello/', "Hello there", "%s -> Pass");
  107. }
  108. function testLongStrings() {
  109. $text = "";
  110. for ($i = 0; $i < 10; $i++) {
  111. $text .= "0123456789";
  112. }
  113. $this->assertEqual($text, $text);
  114. }
  115. }
  116. class FailingUnitTestCaseOutput extends UnitTestCase {
  117. function testOfResults() {
  118. $this->fail('Fail'); // Fail.
  119. }
  120. function testTrue() {
  121. $this->assertTrue(false); // Fail.
  122. }
  123. function testFalse() {
  124. $this->assertFalse(true); // Fail.
  125. }
  126. function testExpectation() {
  127. $expectation = &new EqualExpectation(25, 'My expectation message: %s');
  128. $this->assert($expectation, 24, 'My assert message : %s'); // Fail.
  129. }
  130. function testNull() {
  131. $this->assertNull(false, "%s -> Fail"); // Fail.
  132. $this->assertNotNull(null, "%s -> Fail"); // Fail.
  133. }
  134. function testType() {
  135. $this->assertIsA(14, "string", "%s -> Fail"); // Fail.
  136. $this->assertIsA(14, "TestOfUnitTestCaseOutput", "%s -> Fail"); // Fail.
  137. $this->assertIsA($this, "TestReporter", "%s -> Fail"); // Fail.
  138. }
  139. function testTypeEquality() {
  140. $this->assertNotEqual("0", 0, "%s -> Fail"); // Fail.
  141. }
  142. function testNullEquality() {
  143. $this->assertEqual(null, 1, "%s -> Fail"); // Fail.
  144. $this->assertEqual(1, null, "%s -> Fail"); // Fail.
  145. }
  146. function testIntegerEquality() {
  147. $this->assertEqual(1, 2, "%s -> Fail"); // Fail.
  148. }
  149. function testStringEquality() {
  150. $this->assertNotEqual("a", "a", "%s -> Fail"); // Fail.
  151. $this->assertEqual("aa", "ab", "%s -> Fail"); // Fail.
  152. }
  153. function testHashEquality() {
  154. $this->assertEqual(array("a" => "A", "b" => "B"), array("b" => "B", "a" => "Z"), "%s -> Fail");
  155. }
  156. function testWithin() {
  157. $this->assertWithinMargin(5, 5.6, 0.5, "%s -> Fail"); // Fail.
  158. }
  159. function testOutside() {
  160. $this->assertOutsideMargin(5, 5.4, 0.5, "%s -> Fail"); // Fail.
  161. }
  162. function testStringIdentity() {
  163. $a = "fred";
  164. $b = $a;
  165. $this->assertNotIdentical($a, $b, "%s -> Fail"); // Fail.
  166. }
  167. function testTypeIdentity() {
  168. $a = "0";
  169. $b = 0;
  170. $this->assertIdentical($a, $b, "%s -> Fail"); // Fail.
  171. }
  172. function testNullIdentity() {
  173. $this->assertIdentical(null, 1, "%s -> Fail"); // Fail.
  174. $this->assertIdentical(1, null, "%s -> Fail"); // Fail.
  175. }
  176. function testHashIdentity() {
  177. $this->assertIdentical(array("a" => "A", "b" => "B"), array("b" => "B", "a" => "A"), "%s -> fail"); // Fail.
  178. }
  179. function testObjectEquality() {
  180. $this->assertNotEqual(new TestDisplayClass(4), new TestDisplayClass(4), "%s -> Fail"); // Fail.
  181. $this->assertEqual(new TestDisplayClass(4), new TestDisplayClass(5), "%s -> Fail"); // Fail.
  182. }
  183. function testObjectIndentity() {
  184. $this->assertNotIdentical(new TestDisplayClass(false), new TestDisplayClass(false), "%s -> Fail"); // Fail.
  185. $this->assertIdentical(new TestDisplayClass(false), new TestDisplayClass(0), "%s -> Fail"); // Fail.
  186. }
  187. function testReference() {
  188. $a = "fred";
  189. $b = &$a;
  190. $this->assertClone($a, $b, "%s -> Fail"); // Fail.
  191. }
  192. function testCloneOnDifferentObjects() {
  193. $a = "fred";
  194. $b = $a;
  195. $c = "Hello";
  196. $this->assertClone($a, $c, "%s -> Fail"); // Fail.
  197. }
  198. function testPatterns() {
  199. $this->assertPattern('/hello/', "Hello there", "%s -> Fail"); // Fail.
  200. $this->assertNoPattern('/hello/i', "Hello there", "%s -> Fail"); // Fail.
  201. }
  202. function testLongStrings() {
  203. $text = "";
  204. for ($i = 0; $i < 10; $i++) {
  205. $text .= "0123456789";
  206. }
  207. $this->assertEqual($text . $text, $text . "a" . $text); // Fail.
  208. }
  209. }
  210. class VisualTestOfErrors extends UnitTestCase {
  211. function testDumping() {
  212. $this->dump(array('Hello'), 'Displaying a variable');
  213. }
  214. function testErrorDisplay() {
  215. trigger_error('Default'); // Exception.
  216. trigger_error('Error', E_USER_ERROR); // Exception.
  217. trigger_error('Warning', E_USER_WARNING); // Exception.
  218. trigger_error('Notice', E_USER_NOTICE); // Exception.
  219. }
  220. function testErrorTrap() {
  221. $this->expectError(); // Pass.
  222. trigger_error('Error 1');
  223. }
  224. function testErrorText() {
  225. $this->expectError('Error 2', "%s -> Pass");
  226. $this->expectError('Error 2b', "%s -> Fail"); // Fail.
  227. trigger_error('Error 2');
  228. $this->dump('This should lie between the two errors');
  229. trigger_error('Error 3');
  230. }
  231. function testErrorPatterns() {
  232. $this->expectError(new PatternExpectation('/Error 2/'), "%s -> Pass");
  233. $this->expectError(new PatternExpectation('/Error 2/'), "%s -> Fail"); // Fail.
  234. trigger_error('Error 2');
  235. $this->dump('This should lie between the two errors');
  236. trigger_error('Error 3');
  237. }
  238. function testExceptionTrap() {
  239. if (version_compare(phpversion(), '5') >= 0) {
  240. eval('throw new Exception("Ouch!");');
  241. $this->message('Should not be here');
  242. } else {
  243. trigger_error('No exceptions in PHP4');
  244. }
  245. }
  246. function testExceptionExpectationShowsErrorWhenNoException() {
  247. if (version_compare(phpversion(), '5') >= 0) {
  248. $this->expectException();
  249. } else {
  250. trigger_error('No exceptions in PHP4');
  251. }
  252. }
  253. function testExceptionExpectationShowsPassWhenException() {
  254. if (version_compare(phpversion(), '5') >= 0) {
  255. $this->expectException();
  256. eval('throw new Exception("Ouch!");');
  257. } else {
  258. trigger_error('No exceptions in PHP4');
  259. }
  260. }
  261. function testSignal() {
  262. $fred = "signal as a string";
  263. $this->signal("Signal", $fred); // Signal.
  264. }
  265. }
  266. class Dummy {
  267. function Dummy() {
  268. }
  269. function a() {
  270. }
  271. }
  272. Mock::generate('Dummy');
  273. class TestOfMockObjectsOutput extends UnitTestCase {
  274. function testCallCounts() {
  275. $dummy = &new MockDummy();
  276. $dummy->expectCallCount('a', 1, 'My message: %s');
  277. $dummy->a();
  278. $dummy->a();
  279. }
  280. function testMinimumCallCounts() {
  281. $dummy = &new MockDummy();
  282. $dummy->expectMinimumCallCount('a', 2, 'My message: %s');
  283. $dummy->a();
  284. $dummy->a();
  285. }
  286. function testEmptyMatching() {
  287. $dummy = &new MockDummy();
  288. $dummy->expectArguments('a', array());
  289. $dummy->a();
  290. $dummy->a(null); // Fail.
  291. }
  292. function testEmptyMatchingWithCustomMessage() {
  293. $dummy = &new MockDummy();
  294. $dummy->expectArguments('a', array(), 'My expectation message: %s');
  295. $dummy->a();
  296. $dummy->a(null); // Fail.
  297. }
  298. function testNullMatching() {
  299. $dummy = &new MockDummy();
  300. $dummy->expectArguments('a', array(null));
  301. $dummy->a(null);
  302. $dummy->a(); // Fail.
  303. }
  304. function testBooleanMatching() {
  305. $dummy = &new MockDummy();
  306. $dummy->expectArguments('a', array(true, false));
  307. $dummy->a(true, false);
  308. $dummy->a(true, true); // Fail.
  309. }
  310. function testIntegerMatching() {
  311. $dummy = &new MockDummy();
  312. $dummy->expectArguments('a', array(32, 33));
  313. $dummy->a(32, 33);
  314. $dummy->a(32, 34); // Fail.
  315. }
  316. function testFloatMatching() {
  317. $dummy = &new MockDummy();
  318. $dummy->expectArguments('a', array(3.2, 3.3));
  319. $dummy->a(3.2, 3.3);
  320. $dummy->a(3.2, 3.4); // Fail.
  321. }
  322. function testStringMatching() {
  323. $dummy = &new MockDummy();
  324. $dummy->expectArguments('a', array('32', '33'));
  325. $dummy->a('32', '33');
  326. $dummy->a('32', '34'); // Fail.
  327. }
  328. function testEmptyMatchingWithCustomExpectationMessage() {
  329. $dummy = &new MockDummy();
  330. $dummy->expectArguments(
  331. 'a',
  332. array(new EqualExpectation('A', 'My part expectation message: %s')),
  333. 'My expectation message: %s');
  334. $dummy->a('A');
  335. $dummy->a('B'); // Fail.
  336. }
  337. function testArrayMatching() {
  338. $dummy = &new MockDummy();
  339. $dummy->expectArguments('a', array(array(32), array(33)));
  340. $dummy->a(array(32), array(33));
  341. $dummy->a(array(32), array('33')); // Fail.
  342. }
  343. function testObjectMatching() {
  344. $a = new Dummy();
  345. $a->a = 'a';
  346. $b = new Dummy();
  347. $b->b = 'b';
  348. $dummy = &new MockDummy();
  349. $dummy->expectArguments('a', array($a, $b));
  350. $dummy->a($a, $b);
  351. $dummy->a($a, $a); // Fail.
  352. }
  353. function testBigList() {
  354. $dummy = &new MockDummy();
  355. $dummy->expectArguments('a', array(false, 0, 1, 1.0));
  356. $dummy->a(false, 0, 1, 1.0);
  357. $dummy->a(true, false, 2, 2.0); // Fail.
  358. }
  359. }
  360. class TestOfPastBugs extends UnitTestCase {
  361. function testMixedTypes() {
  362. $this->assertEqual(array(), null, "%s -> Pass");
  363. $this->assertIdentical(array(), null, "%s -> Fail"); // Fail.
  364. }
  365. function testMockWildcards() {
  366. $dummy = &new MockDummy();
  367. $dummy->expectArguments('a', array('*', array(33)));
  368. $dummy->a(array(32), array(33));
  369. $dummy->a(array(32), array('33')); // Fail.
  370. }
  371. }
  372. class TestOfVisualShell extends ShellTestCase {
  373. function testDump() {
  374. $this->execute('ls');
  375. $this->dumpOutput();
  376. $this->execute('dir');
  377. $this->dumpOutput();
  378. }
  379. function testDumpOfList() {
  380. $this->execute('ls');
  381. $this->dump($this->getOutputAsList());
  382. }
  383. }
  384. class PassesAsWellReporter extends HtmlReporter {
  385. function _getCss() {
  386. return parent::_getCss() . ' .pass { color: darkgreen; }';
  387. }
  388. function paintPass($message) {
  389. parent::paintPass($message);
  390. print "<span class=\"pass\">Pass</span>: ";
  391. $breadcrumb = $this->getTestList();
  392. array_shift($breadcrumb);
  393. print implode(" -&gt; ", $breadcrumb);
  394. print " -&gt; " . htmlentities($message) . "<br />\n";
  395. }
  396. function paintSignal($type, &$payload) {
  397. print "<span class=\"fail\">$type</span>: ";
  398. $breadcrumb = $this->getTestList();
  399. array_shift($breadcrumb);
  400. print implode(" -&gt; ", $breadcrumb);
  401. print " -&gt; " . htmlentities(serialize($payload)) . "<br />\n";
  402. }
  403. }
  404. class TestOfSkippingNoMatterWhat extends UnitTestCase {
  405. function skip() {
  406. $this->skipIf(true, 'Always skipped -> %s');
  407. }
  408. function testFail() {
  409. $this->fail('This really shouldn\'t have happened');
  410. }
  411. }
  412. class TestOfSkippingOrElse extends UnitTestCase {
  413. function skip() {
  414. $this->skipUnless(false, 'Always skipped -> %s');
  415. }
  416. function testFail() {
  417. $this->fail('This really shouldn\'t have happened');
  418. }
  419. }
  420. class TestOfSkippingTwiceOver extends UnitTestCase {
  421. function skip() {
  422. $this->skipIf(true, 'First reason -> %s');
  423. $this->skipIf(true, 'Second reason -> %s');
  424. }
  425. function testFail() {
  426. $this->fail('This really shouldn\'t have happened');
  427. }
  428. }
  429. class TestThatShouldNotBeSkipped extends UnitTestCase {
  430. function skip() {
  431. $this->skipIf(false);
  432. $this->skipUnless(true);
  433. }
  434. function testFail() {
  435. $this->fail('We should see this message');
  436. }
  437. function testPass() {
  438. $this->pass('We should see this message');
  439. }
  440. }
  441. $test = &new TestSuite('Visual test with 50 passes, 50 fails and 7 exceptions');
  442. $test->addTestCase(new PassingUnitTestCaseOutput());
  443. $test->addTestCase(new FailingUnitTestCaseOutput());
  444. $test->addTestCase(new VisualTestOfErrors());
  445. $test->addTestCase(new TestOfMockObjectsOutput());
  446. $test->addTestCase(new TestOfPastBugs());
  447. $test->addTestCase(new TestOfVisualShell());
  448. $test->addTestCase(new TestOfSkippingNoMatterWhat());
  449. $test->addTestCase(new TestOfSkippingOrElse());
  450. $test->addTestCase(new TestOfSkippingTwiceOver());
  451. $test->addTestCase(new TestThatShouldNotBeSkipped());
  452. if (isset($_GET['xml']) || in_array('xml', (isset($argv) ? $argv : array()))) {
  453. $reporter = &new XmlReporter();
  454. } elseif (TextReporter::inCli()) {
  455. $reporter = &new TextReporter();
  456. } else {
  457. $reporter = &new PassesAsWellReporter();
  458. }
  459. if (isset($_GET['dry']) || in_array('dry', (isset($argv) ? $argv : array()))) {
  460. $reporter->makeDry();
  461. }
  462. exit ($test->run($reporter) ? 0 : 1);
  463. ?>