PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZFToolTest/Diagnostics/Reporter/VerboseConsoleTest.php

https://github.com/maximecorbeau/ZFTool
PHP | 423 lines | 349 code | 65 blank | 9 comment | 13 complexity | 03d920e2eb4b60b7a904da2a43bf5639 MD5 | raw file
  1. <?php
  2. namespace ZFToolTest\Diagnostics\Reporter;
  3. use ZFTool\Diagnostics\Reporter\VerboseConsole;
  4. use ZFTool\Diagnostics\Result\Collection;
  5. use ZFTool\Diagnostics\Result\Failure;
  6. use ZFTool\Diagnostics\Result\Success;
  7. use ZFTool\Diagnostics\Result\Warning;
  8. use ZFTool\Diagnostics\Result\Unknown;
  9. use ZFTool\Diagnostics\RunEvent;
  10. use ZFToolTest\Diagnostics\TestAsset\AlwaysSuccessTest;
  11. use ZFToolTest\Diagnostics\TestAssets\ConsoleAdapter;
  12. use ZFToolTest\Diagnostics\TestAssets\DummyReporter;
  13. use Zend\Console\Charset\Ascii;
  14. use Zend\EventManager\EventManager;
  15. require_once __DIR__.'/../TestAsset/AlwaysSuccessTest.php';
  16. require_once __DIR__.'/../TestAsset/ConsoleAdapter.php';
  17. class VerboseConsoleTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var \ZFToolTest\Diagnostics\TestAssets\ConsoleAdapter
  21. */
  22. protected $console;
  23. /**
  24. * @var \ZFTool\Diagnostics\Reporter\VerboseConsole
  25. */
  26. protected $reporter;
  27. /**
  28. * @var \Zend\EventManager\EventManager;
  29. */
  30. protected $em;
  31. public function setUp()
  32. {
  33. $this->em = new EventManager();
  34. $this->console = new ConsoleAdapter();
  35. $this->console->setCharset(new Ascii());
  36. $this->reporter = new VerboseConsole($this->console);
  37. $this->em->attachAggregate($this->reporter);
  38. }
  39. public function testConsoleSettingGetting()
  40. {
  41. $this->assertSame($this->console, $this->reporter->getConsole());
  42. $newConsole = new ConsoleAdapter();
  43. $this->reporter->setConsole($newConsole);
  44. $this->assertSame($newConsole, $this->reporter->getConsole());
  45. }
  46. public function testStartMessage()
  47. {
  48. $e = new RunEvent();
  49. $tests = array(
  50. new AlwaysSuccessTest()
  51. );
  52. $e->setParam('tests',$tests);
  53. ob_start();
  54. $this->em->trigger(RunEvent::EVENT_START, $e);
  55. $this->assertStringMatchesFormat('Starting diagnostics%A', ob_get_clean());
  56. }
  57. public function testSuccessProgress()
  58. {
  59. $e = new RunEvent();
  60. $tests = array(
  61. new AlwaysSuccessTest(),
  62. new AlwaysSuccessTest(),
  63. );
  64. $e->setParam('tests', $tests);
  65. ob_start();
  66. $this->em->trigger(RunEvent::EVENT_START, $e);
  67. ob_clean();
  68. $result = new Success();
  69. $e->setTarget($tests[0]);
  70. $e->setLastResult($result);
  71. $this->em->trigger(RunEvent::EVENT_AFTER_RUN, $e);
  72. $this->assertEquals(' OK Always Successful Test' . PHP_EOL, ob_get_clean());
  73. ob_start();
  74. $result = new Success('this is a message');
  75. $e->setTarget($tests[1]);
  76. $e->setLastResult($result);
  77. $this->em->trigger(RunEvent::EVENT_AFTER_RUN, $e);
  78. $this->assertEquals(' OK Always Successful Test: this is a message' . PHP_EOL, ob_get_clean());
  79. }
  80. public function testWarningProgress()
  81. {
  82. $e = new RunEvent();
  83. $tests = array(
  84. new AlwaysSuccessTest(),
  85. new AlwaysSuccessTest(),
  86. );
  87. $e->setParam('tests', $tests);
  88. ob_start();
  89. $this->em->trigger(RunEvent::EVENT_START, $e);
  90. ob_clean();
  91. $result = new Warning();
  92. $e->setTarget($tests[0]);
  93. $e->setLastResult($result);
  94. $this->em->trigger(RunEvent::EVENT_AFTER_RUN, $e);
  95. $this->assertEquals(' WARN Always Successful Test' . PHP_EOL, ob_get_clean());
  96. ob_start();
  97. $result = new Warning('this is a message');
  98. $e->setTarget($tests[1]);
  99. $e->setLastResult($result);
  100. $this->em->trigger(RunEvent::EVENT_AFTER_RUN, $e);
  101. $this->assertEquals(' WARN Always Successful Test: this is a message' . PHP_EOL, ob_get_clean());
  102. }
  103. public function testFailureProgress()
  104. {
  105. $e = new RunEvent();
  106. $tests = array(
  107. new AlwaysSuccessTest(),
  108. new AlwaysSuccessTest(),
  109. );
  110. $e->setParam('tests', $tests);
  111. ob_start();
  112. $this->em->trigger(RunEvent::EVENT_START, $e);
  113. ob_clean();
  114. $result = new Failure();
  115. $e->setTarget($tests[0]);
  116. $e->setLastResult($result);
  117. $this->em->trigger(RunEvent::EVENT_AFTER_RUN, $e);
  118. $this->assertEquals(' FAIL Always Successful Test' . PHP_EOL, ob_get_clean());
  119. ob_start();
  120. $result = new Failure('this is a message');
  121. $e->setTarget($tests[1]);
  122. $e->setLastResult($result);
  123. $this->em->trigger(RunEvent::EVENT_AFTER_RUN, $e);
  124. $this->assertEquals(' FAIL Always Successful Test: this is a message' . PHP_EOL, ob_get_clean());
  125. }
  126. public function testUnknownSymbols()
  127. {
  128. $e = new RunEvent();
  129. $tests = array(
  130. new AlwaysSuccessTest(),
  131. new AlwaysSuccessTest(),
  132. );
  133. $e->setParam('tests', $tests);
  134. ob_start();
  135. $this->em->trigger(RunEvent::EVENT_START, $e);
  136. ob_clean();
  137. $result = new Unknown();
  138. $e->setTarget($tests[0]);
  139. $e->setLastResult($result);
  140. $this->em->trigger(RunEvent::EVENT_AFTER_RUN, $e);
  141. $this->assertEquals(' ???? Always Successful Test' . PHP_EOL, ob_get_clean());
  142. ob_start();
  143. $result = new Unknown('this is a message');
  144. $e->setTarget($tests[1]);
  145. $e->setLastResult($result);
  146. $this->em->trigger(RunEvent::EVENT_AFTER_RUN, $e);
  147. $this->assertEquals(' ???? Always Successful Test: this is a message' . PHP_EOL, ob_get_clean());
  148. }
  149. public function testInfoOverflow()
  150. {
  151. $this->console->setTestWidth(40);
  152. $e = new RunEvent();
  153. $tests = array(
  154. new AlwaysSuccessTest(),
  155. new AlwaysSuccessTest(),
  156. );
  157. $e->setParam('tests', $tests);
  158. ob_start();
  159. $this->em->trigger(RunEvent::EVENT_START, $e);
  160. ob_clean();
  161. $result = new Success(
  162. 'foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo'
  163. );
  164. $e->setTarget($tests[0]);
  165. $e->setLastResult($result);
  166. $this->em->trigger(RunEvent::EVENT_AFTER_RUN, $e);
  167. $this->assertEquals(
  168. ' OK Always Successful Test: foo foo' . PHP_EOL .
  169. ' foo foo foo foo foo foo foo foo' . PHP_EOL .
  170. ' foo foo foo foo foo foo foo' . PHP_EOL
  171. , ob_get_clean()
  172. );
  173. ob_start();
  174. $result = new Failure(
  175. 'foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo'
  176. );
  177. $e->setTarget($tests[1]);
  178. $e->setLastResult($result);
  179. $this->em->trigger(RunEvent::EVENT_AFTER_RUN, $e);
  180. $this->assertEquals(
  181. ' FAIL Always Successful Test:' . PHP_EOL .
  182. ' foofoofoofoofoofoofoofoofoofoofoo' . PHP_EOL .
  183. ' foofoofoofoofoofoofoofoofoofoofoo' . PHP_EOL .
  184. ' foo' . PHP_EOL
  185. , ob_get_clean()
  186. );
  187. }
  188. public function testDataDump()
  189. {
  190. $this->console->setTestWidth(40);
  191. $this->reporter->getDisplayData();
  192. $this->reporter->setDisplayData(true);
  193. $e = new RunEvent();
  194. $tests = array(
  195. new AlwaysSuccessTest(),
  196. new AlwaysSuccessTest(),
  197. );
  198. $e->setParam('tests', $tests);
  199. ob_start();
  200. $this->em->trigger(RunEvent::EVENT_START, $e);
  201. ob_clean();
  202. $result = new Success('foo', array('1',2,3));
  203. $e->setTarget($tests[0]);
  204. $e->setLastResult($result);
  205. $this->em->trigger(RunEvent::EVENT_AFTER_RUN, $e);
  206. $this->assertEquals(
  207. ' OK Always Successful Test: foo' . PHP_EOL .
  208. ' ---------------------------------' . PHP_EOL .
  209. ' array (' . PHP_EOL .
  210. ' 0 => \'1\',' . PHP_EOL .
  211. ' 1 => 2,' . PHP_EOL .
  212. ' 2 => 3,' . PHP_EOL .
  213. ' )' . PHP_EOL .
  214. ' ---------------------------------' . PHP_EOL
  215. , ob_get_clean()
  216. );
  217. ob_start();
  218. }
  219. public function testSummaryAllSuccessful()
  220. {
  221. $e = new RunEvent();
  222. $tests = array();
  223. $test = null;
  224. $results = new Collection();
  225. for($x = 0; $x < 20; $x++){
  226. $tests[] = $test = new AlwaysSuccessTest();
  227. $results[$test] = new Success();
  228. }
  229. $e->setParam('tests', $tests);
  230. $e->setResults($results);
  231. ob_start();
  232. $this->em->trigger(RunEvent::EVENT_START, $e);
  233. ob_clean();
  234. $this->em->trigger(RunEvent::EVENT_FINISH, $e);
  235. $this->assertStringStartsWith('OK (20 diagnostic tests)', trim(ob_get_clean()));
  236. }
  237. public function testSummaryWithWarnings()
  238. {
  239. $e = new RunEvent();
  240. $tests = array();
  241. $test = null;
  242. $results = new Collection();
  243. for ($x = 0; $x < 15; $x++) {
  244. $tests[] = $test = new AlwaysSuccessTest();
  245. $results[$test] = new Success();
  246. }
  247. for ($x = 0; $x < 5; $x++) {
  248. $tests[] = $test = new AlwaysSuccessTest();
  249. $results[$test] = new Warning();
  250. }
  251. $e->setParam('tests', $tests);
  252. $e->setResults($results);
  253. ob_start();
  254. $this->em->trigger(RunEvent::EVENT_START, $e);
  255. ob_clean();
  256. $this->em->trigger(RunEvent::EVENT_FINISH, $e);
  257. $this->assertStringStartsWith('5 warnings, 15 successful tests', trim(ob_get_clean()));
  258. }
  259. public function testSummaryWithFailures()
  260. {
  261. $e = new RunEvent();
  262. $tests = array();
  263. $test = null;
  264. $results = new Collection();
  265. for ($x = 0; $x < 15; $x++) {
  266. $tests[] = $test = new AlwaysSuccessTest();
  267. $results[$test] = new Success();
  268. }
  269. for ($x = 0; $x < 5; $x++) {
  270. $tests[] = $test = new AlwaysSuccessTest();
  271. $results[$test] = new Warning();
  272. }
  273. for ($x = 0; $x < 5; $x++) {
  274. $tests[] = $test = new AlwaysSuccessTest();
  275. $results[$test] = new Failure();
  276. }
  277. $e->setParam('tests', $tests);
  278. $e->setResults($results);
  279. ob_start();
  280. $this->em->trigger(RunEvent::EVENT_START, $e);
  281. ob_clean();
  282. $this->em->trigger(RunEvent::EVENT_FINISH, $e);
  283. $this->assertStringStartsWith('5 failures, 5 warnings, 15 successful tests', trim(ob_get_clean()));
  284. }
  285. public function testSummaryWithUnknowns()
  286. {
  287. $e = new RunEvent();
  288. $tests = array();
  289. $test = null;
  290. $results = new Collection();
  291. for ($x = 0; $x < 15; $x++) {
  292. $tests[] = $test = new AlwaysSuccessTest();
  293. $results[$test] = new Success();
  294. }
  295. for ($x = 0; $x < 5; $x++) {
  296. $tests[] = $test = new AlwaysSuccessTest();
  297. $results[$test] = new Warning();
  298. }
  299. for ($x = 0; $x < 5; $x++) {
  300. $tests[] = $test = new AlwaysSuccessTest();
  301. $results[$test] = new Unknown();
  302. }
  303. $e->setParam('tests', $tests);
  304. $e->setResults($results);
  305. ob_start();
  306. $this->em->trigger(RunEvent::EVENT_START, $e);
  307. ob_clean();
  308. $this->em->trigger(RunEvent::EVENT_FINISH, $e);
  309. $this->assertStringMatchesFormat('%A5 unknown test results%A', trim(ob_get_clean()));
  310. }
  311. public function testSummaryWithUnknownsAndFailures()
  312. {
  313. $e = new RunEvent();
  314. $tests = array();
  315. $test = null;
  316. $results = new Collection();
  317. for ($x = 0; $x < 15; $x++) {
  318. $tests[] = $test = new AlwaysSuccessTest();
  319. $results[$test] = new Success();
  320. }
  321. for ($x = 0; $x < 5; $x++) {
  322. $tests[] = $test = new AlwaysSuccessTest();
  323. $results[$test] = new Failure();
  324. }
  325. for ($x = 0; $x < 5; $x++) {
  326. $tests[] = $test = new AlwaysSuccessTest();
  327. $results[$test] = new Unknown();
  328. }
  329. $e->setParam('tests', $tests);
  330. $e->setResults($results);
  331. ob_start();
  332. $this->em->trigger(RunEvent::EVENT_START, $e);
  333. ob_clean();
  334. $this->em->trigger(RunEvent::EVENT_FINISH, $e);
  335. $result = trim(ob_get_clean());
  336. $this->assertStringMatchesFormat('%A5 failures%A', $result);
  337. $this->assertStringMatchesFormat('%A5 unknown test results%A', $result);
  338. }
  339. public function testStoppedNotice()
  340. {
  341. $e = new RunEvent();
  342. $tests = array();
  343. $test = null;
  344. $results = new Collection();
  345. for ($x = 0; $x < 15; $x++) {
  346. $tests[] = $test = new AlwaysSuccessTest();
  347. $results[$test] = new Success();
  348. }
  349. $e->setParam('tests', $tests);
  350. $e->setResults($results);
  351. ob_start();
  352. $this->em->trigger(RunEvent::EVENT_START, $e);
  353. ob_clean();
  354. $this->em->trigger(RunEvent::EVENT_STOP, $e);
  355. $this->em->trigger(RunEvent::EVENT_FINISH, $e);
  356. $this->assertStringMatchesFormat('%ADiagnostics aborted%A', trim(ob_get_clean()));
  357. }
  358. }