PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/ConsoleTools/tests/progressbar_test.php

https://github.com/Yannix/zetacomponents
PHP | 526 lines | 408 code | 82 blank | 36 comment | 14 complexity | e2cf48e8890b700e0d3ca5ce92496d65 MD5 | raw file
  1. <?php
  2. /**
  3. * ezcConsoleOutputTest class
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package ConsoleTools
  23. * @subpackage Tests
  24. * @version //autogentag//
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  26. */
  27. /**
  28. * Test suite for ezcConsoleProgressbar class.
  29. *
  30. * @package ConsoleTools
  31. * @subpackage Tests
  32. */
  33. class ezcConsoleProgressbarTest extends ezcTestCase
  34. {
  35. public static function suite()
  36. {
  37. return new PHPUnit_Framework_TestSuite( "ezcConsoleProgressbarTest" );
  38. }
  39. public function testProgress1()
  40. {
  41. $this->commonProgressbarTest( __FUNCTION__, 42, 13, array () );
  42. }
  43. public function testProgress2()
  44. {
  45. $this->commonProgressbarTest( __FUNCTION__, 20, 32, array () );
  46. }
  47. public function testProgress3()
  48. {
  49. $this->commonProgressbarTest( __FUNCTION__, 42, 13, array ( 'barChar' => '#', 'emptyChar' => '*' ) );
  50. }
  51. public function testProgress4()
  52. {
  53. $this->commonProgressbarTest( __FUNCTION__, 55, 19, array ( 'progressChar' => '&' ) );
  54. }
  55. public function testProgress5()
  56. {
  57. $this->commonProgressbarTest( __FUNCTION__, 42, 13, array ( 'progressChar' => '&', 'width' => 55 ) );
  58. }
  59. public function testProgress6()
  60. {
  61. $this->commonProgressbarTest( __FUNCTION__, 22, 3, array ( 'barChar' => '#', 'emptyChar' => '*', 'progressChar' => '&', 'width' => 81 ) );
  62. }
  63. public function testProgress7()
  64. {
  65. $this->commonProgressbarTest( __FUNCTION__, 42, 7, array ( 'barChar' => '1234', 'emptyChar' => '9876' ) );
  66. }
  67. public function testProgress8()
  68. {
  69. $this->commonProgressbarTest( __FUNCTION__, 42, 7, array ( 'barChar' => '123', 'emptyChar' => '987', 'progressChar' => '---' ) );
  70. }
  71. public function testProgress9()
  72. {
  73. $out = new ezcConsoleOutput();
  74. $formatString = ''
  75. . $out->formatText( 'Actual progress', 'success' )
  76. . ': <'
  77. . $out->formatText( '%bar%', 'failure' )
  78. . '> '
  79. . $out->formatText( '%fraction%', 'success' );
  80. $this->commonProgressbarTest(
  81. __FUNCTION__,
  82. 1073,
  83. 123,
  84. array(
  85. 'barChar' => '123',
  86. 'emptyChar' => '987',
  87. 'progressChar' => '---',
  88. 'width' => 97,
  89. 'formatString' => $formatString,
  90. 'fractionFormat' => '%o'
  91. )
  92. );
  93. }
  94. public function testProgress10()
  95. {
  96. $this->commonProgressbarTest( __FUNCTION__, 100, 1, array ( 'redrawFrequency' => 10 ) );
  97. }
  98. public function testProgress11()
  99. {
  100. $this->commonProgressbarTest( __FUNCTION__, 100, 2.5, array ( 'actFormat' => '%01.2f', 'maxFormat' => '%01.2f' ) );
  101. }
  102. public function testProgress12()
  103. {
  104. $this->commonProgressbarTest( __FUNCTION__, 100, 2.5, array ( 'actFormat' => '%01.2f', 'maxFormat' => '%01.8f' ) );
  105. }
  106. public function testProgress13()
  107. {
  108. $this->commonProgressbarTest( __FUNCTION__, 100, 2.5, array ( 'actFormat' => '%01.8f', 'maxFormat' => '%01.2f' ) );
  109. }
  110. public function testProgressUtfInBar()
  111. {
  112. $this->commonProgressbarTest(
  113. __FUNCTION__,
  114. 10,
  115. 2,
  116. array(
  117. 'barChar' => 'ö'
  118. )
  119. );
  120. }
  121. public function testProgressUtfInText()
  122. {
  123. $this->commonProgressbarTest(
  124. __FUNCTION__,
  125. 10,
  126. 2,
  127. array(
  128. 'formatString' => '%act%ö/ä%max%ü[%bar%]ß%fraction%%'
  129. )
  130. );
  131. }
  132. public function testProgressUtfInBoth()
  133. {
  134. $this->commonProgressbarTest(
  135. __FUNCTION__,
  136. 10,
  137. 2,
  138. array(
  139. 'formatString' => '%act%ö/ä%max%ü[%bar%]ß%fraction%%',
  140. 'barChar' => 'ö'
  141. )
  142. );
  143. }
  144. public function testProgressNoPrintMinVerbosity()
  145. {
  146. $out = new ezcConsoleOutput();
  147. $out->options->verbosityLevel = 0;
  148. $progress = new ezcConsoleProgressbar( $out, 10 );
  149. ob_start();
  150. for ( $i = 0; $i < 10; ++$i )
  151. {
  152. $progress->advance();
  153. }
  154. $progress->finish();
  155. $content = ob_get_clean();
  156. $this->assertEquals(
  157. '',
  158. $content,
  159. 'Progress bar printed although verbosity level to low.'
  160. );
  161. }
  162. public function testProgressNoPrintMinVerbosityNoDefault()
  163. {
  164. $out = new ezcConsoleOutput();
  165. $out->options->verbosityLevel = 10;
  166. $progress = new ezcConsoleProgressbar( $out, 10 );
  167. $progress->options->minVerbosity = 20;
  168. ob_start();
  169. for ( $i = 0; $i < 10; ++$i )
  170. {
  171. $progress->advance();
  172. }
  173. $progress->finish();
  174. $content = ob_get_clean();
  175. $this->assertEquals(
  176. '',
  177. $content,
  178. 'Progress bar printed although verbosity level to low.'
  179. );
  180. }
  181. public function testProgressNoPrintMaxVerbosity()
  182. {
  183. $out = new ezcConsoleOutput();
  184. $progress = new ezcConsoleProgressbar( $out, 10 );
  185. $progress->options->maxVerbosity = 0;
  186. ob_start();
  187. for ( $i = 0; $i < 10; ++$i )
  188. {
  189. $progress->advance();
  190. }
  191. $progress->finish();
  192. $content = ob_get_clean();
  193. $this->assertEquals(
  194. '',
  195. $content,
  196. 'Progress bar printed although verbosity level to low.'
  197. );
  198. }
  199. public function testProgressNoPrintMaxVerbosityNoDefault()
  200. {
  201. $out = new ezcConsoleOutput();
  202. $out->options->verbosityLevel = 10;
  203. $progress = new ezcConsoleProgressbar( $out, 10 );
  204. $progress->options->maxVerbosity = 9;
  205. ob_start();
  206. for ( $i = 0; $i < 10; ++$i )
  207. {
  208. $progress->advance();
  209. }
  210. $progress->finish();
  211. $content = ob_get_clean();
  212. $this->assertEquals(
  213. '',
  214. $content,
  215. 'Progress bar printed although verbosity level to low.'
  216. );
  217. }
  218. public function testProgressPrintMinMaxVerbosity()
  219. {
  220. $out = new ezcConsoleOutput();
  221. $out->options->verbosityLevel = 10;
  222. $progress = new ezcConsoleProgressbar( $out, 10 );
  223. $progress->options->maxVerbosity = 10;
  224. $progress->options->minVerbosity = 10;
  225. ob_start();
  226. for ( $i = 0; $i < 10; ++$i )
  227. {
  228. $progress->advance();
  229. }
  230. $progress->finish();
  231. $content = ob_get_clean();
  232. $this->assertNotEquals(
  233. '',
  234. $content,
  235. 'Progress bar printed although verbosity level to low.'
  236. );
  237. }
  238. public function testSetOptionsSuccess()
  239. {
  240. $out = new ezcConsoleOutput();
  241. $opArr = array();
  242. $optArr['barChar'] = "*";
  243. $optArr['emptyChar'] = "#";
  244. $optArr['formatString'] = "-- %act% / %max% [%bar%] %fraction%% --";
  245. $optArr['fractionFormat'] = "%f";
  246. $optArr['progressChar'] = "<";
  247. $optArr['redrawFrequency'] = 42;
  248. $optArr['step'] = 23;
  249. $optArr['width'] = 42;
  250. $optArr['actFormat'] = '%f';
  251. $optArr['maxFormat'] = '%f';
  252. $optObj = new ezcConsoleProgressbarOptions();
  253. $optObj->barChar = "*";
  254. $optObj->emptyChar = "#";
  255. $optObj->formatString = "-- %act% / %max% [%bar%] %fraction%% --";
  256. $optObj->fractionFormat = "%f";
  257. $optObj->progressChar = "<";
  258. $optObj->redrawFrequency = 42;
  259. $optObj->step = 23;
  260. $optObj->width = 42;
  261. $optObj->actFormat = '%f';
  262. $optObj->maxFormat = '%f';
  263. $bar = new ezcConsoleProgressbar( $out, 10 );
  264. $bar->setOptions( $optArr );
  265. $this->assertEquals( $optObj, $bar->options, "Options not set correctly from array." );
  266. $bar = new ezcConsoleProgressbar( $out, 10 );
  267. $bar->setOptions( $optObj );
  268. $this->assertEquals( $optObj, $bar->options, "Options not set correctly from object." );
  269. }
  270. public function testSetOptionsFailure()
  271. {
  272. $out = new ezcConsoleOutput();
  273. $bar = new ezcConsoleProgressbar( $out, 10 );
  274. try
  275. {
  276. $bar->setOptions( 23 );
  277. }
  278. catch ( ezcBaseValueException $e )
  279. {
  280. return;
  281. }
  282. $this->fail( "Exeception not thrown on invalid parameter for setOptions()." );
  283. }
  284. public function testGetOptions()
  285. {
  286. $out = new ezcConsoleOutput();
  287. $bar = new ezcConsoleProgressbar( $out, 10 );
  288. $opt = new ezcConsoleProgressbarOptions();
  289. $bar->options = $opt;
  290. $this->assertSame( $opt, $bar->getOptions(), "Options not correctly returned from getOptions()." );
  291. }
  292. public function testGetAccessSuccess()
  293. {
  294. $out = new ezcConsoleOutput();
  295. $bar = new ezcConsoleProgressbar( $out, 10 );
  296. $this->assertEquals( new ezcConsoleProgressbarOptions(), $bar->options );
  297. $this->assertEquals( 10, $bar->max );
  298. $this->assertEquals( 1, $bar->step );
  299. }
  300. public function testGetAccessFailure()
  301. {
  302. $out = new ezcConsoleOutput();
  303. $bar = new ezcConsoleProgressbar( $out, 10 );
  304. try
  305. {
  306. echo $bar->foo;
  307. }
  308. catch ( ezcBasePropertyNotFoundException $e )
  309. {
  310. return;
  311. }
  312. $this->fail( "ezcBasePropertyNotFoundException not thrown on get access of invalid property ezcConsoleProgressbar->foo." );
  313. }
  314. public function testSetAccessSuccess()
  315. {
  316. $out = new ezcConsoleOutput();
  317. $bar = new ezcConsoleProgressbar( $out, 10 );
  318. $this->genericSetAccessTestSuccess( $bar, "options", new ezcConsoleProgressbarOptions() );
  319. $this->genericSetAccessTestSuccess( $bar, "step", 10 );
  320. $this->genericSetAccessTestSuccess( $bar, "max", 100 );
  321. }
  322. public function testSetAccessFailure()
  323. {
  324. $out = new ezcConsoleOutput();
  325. $bar = new ezcConsoleProgressbar( $out, 10 );
  326. $this->genericSetAccessTestFailureValue( $bar, "options", 23 );
  327. $this->genericSetAccessTestFailureValue( $bar, "max", true );
  328. $this->genericSetAccessTestFailureValue( $bar, "max", -23 );
  329. $this->genericSetAccessTestFailureValue( $bar, "step", true );
  330. $this->genericSetAccessTestFailureValue( $bar, "step", -23 );
  331. $this->genericSetAccessTestFailureNonexistent( $bar );
  332. }
  333. public function testIssetAccess()
  334. {
  335. $out = new ezcConsoleOutput();
  336. $bar = new ezcConsoleProgressbar( $out, 10 );
  337. $this->assertTrue( isset( $bar->options ) );
  338. $this->assertTrue( isset( $bar->max ) );
  339. $this->assertTrue( isset( $bar->step ) );
  340. $this->assertFalse( isset( $bar->foo ) );
  341. }
  342. public function testFinish()
  343. {
  344. $out = new ezcConsoleOutput();
  345. $bar = new ezcConsoleProgressbar( $out, 10 );
  346. $res = array();
  347. for ( $i = 0; $i < 5; ++$i )
  348. {
  349. ob_start();
  350. $bar->advance();
  351. $resTmp = ob_get_clean();
  352. if ( trim( $resTmp ) !== '')
  353. {
  354. $res[] = $resTmp;
  355. }
  356. }
  357. ob_start();
  358. $bar->finish();
  359. $res[] = ob_get_clean();
  360. $refFile = dirname( __FILE__ ) . '/data/' . ( ezcBaseFeatures::os() === "Windows" ? "windows/" : "posix/" ) . __FUNCTION__ . '.dat';
  361. // Use the following line to regenerate test reference files
  362. // file_put_contents( $refFile, implode( PHP_EOL, $res ) );
  363. $this->assertEquals(
  364. file_get_contents( $refFile ),
  365. implode( PHP_EOL, $res ),
  366. 'Progressbar not correctly generated for ' . $refFile . '.'
  367. );
  368. }
  369. protected function genericSetAccessTestSuccess( $object, $propertyName, $value )
  370. {
  371. $object->$propertyName = $value;
  372. if ( is_object( $value ) )
  373. {
  374. $this->assertSame(
  375. $value,
  376. $object->$propertyName, "Property " . get_class( $object ) . "->$propertyName not set correctly on object of type " . get_class( $object ) . "."
  377. );
  378. }
  379. else
  380. {
  381. $this->assertEquals(
  382. $value,
  383. $object->$propertyName, "Property " . get_class( $object ) . "->$propertyName not set correctly on object of type " . get_class( $object ) . "."
  384. );
  385. }
  386. }
  387. protected function genericSetAccessTestFailureValue( $object, $propertyName, $value )
  388. {
  389. try
  390. {
  391. $object->$propertyName = $value;
  392. }
  393. catch ( ezcBaseValueException $e )
  394. {
  395. return;
  396. }
  397. $this->fail(
  398. "ezcBaseValueException not thrown on value '" . ( is_object( $value ) ) ? get_class( $value ) : $value . "' for property " . get_class( $object ) . "->$propertyName."
  399. );
  400. }
  401. protected function genericSetAccessTestFailureNonexistent( $object )
  402. {
  403. try
  404. {
  405. $object->foo = 23;
  406. }
  407. catch ( ezcBasePropertyNotFoundException $e )
  408. {
  409. return;
  410. }
  411. $this->fail( "ezcBasePropertyNotFoundException not thrown on access of not existent property " . get_class( $object ) . "->foo." );
  412. }
  413. private function commonProgressbarTest( $refFile, $max, $step, $options )
  414. {
  415. $out = new ezcConsoleOutput();
  416. $bar = new ezcConsoleProgressbar( $out, $max, $options );
  417. if ( $step != 1 )
  418. {
  419. $bar->options->step = $step;
  420. }
  421. $res = array();
  422. for ( $i = 0; $i < $max; $i+= $step)
  423. {
  424. ob_start();
  425. $bar->advance();
  426. // sleep( 1 );
  427. $resTmp = ob_get_clean();
  428. if ( trim( $resTmp ) !== '' )
  429. {
  430. $res[] = $resTmp;
  431. }
  432. }
  433. $refFile = dirname( __FILE__ ) . '/data/' . ( ezcBaseFeatures::os() === "Windows" ? "windows/" : "posix/" ) . $refFile . '.dat';
  434. // Use the following line to regenerate test reference files
  435. // file_put_contents( $refFile, implode( PHP_EOL, $res ) );
  436. $expected = ( file_exists( $refFile ) ? file_get_contents( $refFile ) : '' );
  437. $this->assertEquals(
  438. $expected,
  439. implode( PHP_EOL, $res ),
  440. 'Progressbar not correctly generated for ' . $refFile . '.'
  441. );
  442. }
  443. }
  444. ?>