TableTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Text
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Text_FigletTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Text_TableTest::main");
  25. }
  26. require_once 'Zend/Text/Table.php';
  27. require_once 'Zend/Text/Table/Row.php';
  28. require_once 'Zend/Text/Table/Column.php';
  29. require_once 'Zend/Text/Table/Decorator/Unicode.php';
  30. require_once 'Zend/Text/Table/Decorator/Ascii.php';
  31. /**
  32. * @category Zend
  33. * @package Zend_Text
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Text
  38. */
  39. class Zend_Text_TableTest extends PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * Runs the test methods of this class.
  43. *
  44. * @return void
  45. */
  46. public static function main()
  47. {
  48. $suite = new PHPUnit_Framework_TestSuite("Zend_Text_TableTest");
  49. $result = PHPUnit_TextUI_TestRunner::run($suite);
  50. }
  51. public function tearDown()
  52. {
  53. Zend_Text_Table::setInputCharset('utf-8');
  54. Zend_Text_Table::setOutputCharset('utf-8');
  55. }
  56. public function testColumnAlignLeft()
  57. {
  58. $column = new Zend_Text_Table_Column("foobar\nfoo");
  59. $this->assertEquals($column->render(10), "foobar \nfoo ");
  60. }
  61. public function testColumnPadding()
  62. {
  63. $column = new Zend_Text_Table_Column("foobar\nfoo");
  64. $this->assertEquals($column->render(10, 1), " foobar \n foo ");
  65. }
  66. public function testColumnWordwrap()
  67. {
  68. $column = new Zend_Text_Table_Column("foobar");
  69. $this->assertEquals($column->render(3), "foo\nbar");
  70. }
  71. public function testColumnUnicodeWordwrap()
  72. {
  73. $column = new Zend_Text_Table_Column("Ömläüt");
  74. $this->assertEquals($column->render(3), "Öml\näüt");
  75. }
  76. public function testColumnAlignCenter()
  77. {
  78. $column = new Zend_Text_Table_Column("foobar\nfoo", Zend_Text_Table_Column::ALIGN_CENTER);
  79. $this->assertEquals($column->render(10), " foobar \n foo ");
  80. }
  81. public function testColumnAlignRight()
  82. {
  83. $column = new Zend_Text_Table_Column("foobar\nfoo", Zend_Text_Table_Column::ALIGN_RIGHT);
  84. $this->assertEquals($column->render(10), " foobar\n foo");
  85. }
  86. public function testColumnForcedEncoding()
  87. {
  88. if (PHP_OS == 'AIX') {
  89. // AIX cannot handle these charsets
  90. $this->markTestSkipped('Test case cannot run on AIX');
  91. }
  92. $iso885915 = iconv('utf-8', 'iso-8859-15', 'Ömläüt');
  93. $column = new Zend_Text_Table_Column($iso885915, null, null, 'iso-8859-15');
  94. $this->assertEquals($column->render(6), 'Ömläüt');
  95. }
  96. public function testColumnDefaultInputEncoding()
  97. {
  98. if (PHP_OS == 'AIX') {
  99. // AIX cannot handle these charsets
  100. $this->markTestSkipped('Test case cannot run on AIX');
  101. }
  102. $iso885915 = iconv('utf-8', 'iso-8859-15', 'Ömläüt');
  103. Zend_Text_Table::setInputCharset('iso-8859-15');
  104. $column = new Zend_Text_Table_Column($iso885915);
  105. $this->assertEquals($column->render(6), 'Ömläüt');
  106. }
  107. public function testColumnDefaultOutputEncoding()
  108. {
  109. if (PHP_OS == 'AIX') {
  110. // AIX cannot handle these charsets
  111. $this->markTestSkipped('Test case cannot run on AIX');
  112. }
  113. $iso885915 = iconv('utf-8', 'iso-8859-15', 'Ömläüt');
  114. Zend_Text_Table::setOutputCharset('iso-8859-15');
  115. $column = new Zend_Text_Table_Column('Ömläüt');
  116. $this->assertEquals($column->render(6), $iso885915);
  117. }
  118. public function testColumnSetContentInvalidArgument()
  119. {
  120. try {
  121. $column = new Zend_Text_Table_Column(1);
  122. $this->fail('An expected InvalidArgumentException has not been raised');
  123. } catch (Zend_Text_Table_Exception $expected) {
  124. $this->assertContains('$content must be a string', $expected->getMessage());
  125. }
  126. }
  127. public function testColumnSetAlignInvalidArgument()
  128. {
  129. try {
  130. $column = new Zend_Text_Table_Column(null, false);
  131. $this->fail('An expected InvalidArgumentException has not been raised');
  132. } catch (Zend_Text_Table_Exception $expected) {
  133. $this->assertContains('Invalid align supplied', $expected->getMessage());
  134. }
  135. }
  136. public function testColumnSetColSpanInvalidArgument()
  137. {
  138. try {
  139. $column = new Zend_Text_Table_Column(null, null, 0);
  140. $this->fail('An expected InvalidArgumentException has not been raised');
  141. } catch (Zend_Text_Table_Exception $expected) {
  142. $this->assertContains('$colSpan must be an integer and greater than 0', $expected->getMessage());
  143. }
  144. }
  145. public function testColumnRenderInvalidArgument()
  146. {
  147. try {
  148. $column = new Zend_Text_Table_Column();
  149. $column->render(0);
  150. $this->fail('An expected InvalidArgumentException has not been raised');
  151. } catch (Zend_Text_Table_Exception $expected) {
  152. $this->assertContains('$columnWidth must be an integer and greater than 0', $expected->getMessage());
  153. }
  154. }
  155. public function testUnicodeStringPadding()
  156. {
  157. $decorator = new Zend_Text_Table_Decorator_Unicode();
  158. $row = new Zend_Text_Table_Row();
  159. $row->appendColumn(new Zend_Text_Table_Column('Eté'));
  160. $row->appendColumn(new Zend_Text_Table_Column('Ete'));
  161. $this->assertEquals($row->render(array(10, 10), $decorator), "│Eté │Ete │\n");
  162. }
  163. public function testRowColumnsWithColSpan()
  164. {
  165. $decorator = new Zend_Text_Table_Decorator_Unicode();
  166. $row = new Zend_Text_Table_Row();
  167. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  168. $row->appendColumn(new Zend_Text_Table_Column('foobar', null, 2));
  169. $this->assertEquals($row->render(array(10, 10, 10), $decorator), "│foobar │foobar │\n");
  170. }
  171. public function testRowWithNoColumns()
  172. {
  173. $decorator = new Zend_Text_Table_Decorator_Unicode();
  174. $row = new Zend_Text_Table_Row();
  175. $this->assertEquals($row->render(array(10, 10, 10), $decorator), "│ │\n");
  176. }
  177. public function testRowNotEnoughColumnWidths()
  178. {
  179. $decorator = new Zend_Text_Table_Decorator_Unicode();
  180. $row = new Zend_Text_Table_Row();
  181. $row->appendColumn(new Zend_Text_Table_Column());
  182. $row->appendColumn(new Zend_Text_Table_Column());
  183. try {
  184. $row->render(array(10), $decorator);
  185. $this->fail('An expected Zend_Text_Table_Exception has not been raised');
  186. } catch (Zend_Text_Table_Exception $expected) {
  187. $this->assertContains('Too many columns', $expected->getMessage());
  188. }
  189. }
  190. public function testRowGetColumnWidthsBeforeRendering()
  191. {
  192. $row = new Zend_Text_Table_Row();
  193. try {
  194. $row->getColumnWidths();
  195. $this->fail('An expected Zend_Text_Table_Exception has not been raised');
  196. } catch (Zend_Text_Table_Exception $expected) {
  197. $this->assertContains('No columns were rendered yet', $expected->getMessage());
  198. }
  199. }
  200. public function testRowAutoInsertColumns()
  201. {
  202. $decorator = new Zend_Text_Table_Decorator_Unicode();
  203. $row = new Zend_Text_Table_Row();
  204. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  205. $this->assertEquals($row->render(array(10, 10, 10), $decorator), "│foobar │ │\n");
  206. }
  207. public function testRowMultiLine()
  208. {
  209. $decorator = new Zend_Text_Table_Decorator_Unicode();
  210. $row = new Zend_Text_Table_Row();
  211. $row->appendColumn(new Zend_Text_Table_Column("foo\nbar"));
  212. $row->appendColumn(new Zend_Text_Table_Column("foobar"));
  213. $this->assertEquals($row->render(array(10, 10), $decorator), "│foo │foobar │\n│bar │ │\n");
  214. }
  215. public function testUnicodeRowMultiLine()
  216. {
  217. $decorator = new Zend_Text_Table_Decorator_Unicode();
  218. $row = new Zend_Text_Table_Row();
  219. $row->appendColumn(new Zend_Text_Table_Column("föö\nbär"));
  220. $row->appendColumn(new Zend_Text_Table_Column("fööbär"));
  221. $this->assertEquals($row->render(array(3, 10), $decorator), "│föö│fööbär │\n│bär│ │\n");
  222. }
  223. public function testTableConstructInvalidColumnWidths()
  224. {
  225. try {
  226. $table = new Zend_Text_Table(array('columnWidths' => array()));
  227. $this->fail('An expected Zend_Text_Table_Exception has not been raised');
  228. } catch (Zend_Text_Table_Exception $expected) {
  229. $this->assertContains('You must supply at least one column', $expected->getMessage());
  230. }
  231. }
  232. public function testTableConstructInvalidColumnWidthsItem()
  233. {
  234. try {
  235. $table = new Zend_Text_Table(array('columnWidths' => array('foo')));
  236. $this->fail('An expected Zend_Text_Table_Exception has not been raised');
  237. } catch (Zend_Text_Table_Exception $expected) {
  238. $this->assertContains('Column 0 has an invalid column width', $expected->getMessage());
  239. }
  240. }
  241. public function testTableDecoratorLoaderSimple()
  242. {
  243. $table = new Zend_Text_Table(array('columnWidths' => array(10), 'decorator' => 'ascii'));
  244. $row = new Zend_Text_Table_Row();
  245. $row->createColumn('foobar');
  246. $table->appendRow($row);
  247. $this->assertEquals($table->render(), "+----------+\n|foobar |\n+----------+\n");
  248. }
  249. public function testTableDecoratorEncodingDefault()
  250. {
  251. Zend_Text_Table::setOutputCharset('iso-8859-15');
  252. $table = new Zend_Text_Table(array('columnWidths' => array(10)));
  253. $row = new Zend_Text_Table_Row();
  254. $row->createColumn('foobar');
  255. $table->appendRow($row);
  256. $this->assertEquals($table->render(), "+----------+\n|foobar |\n+----------+\n");
  257. }
  258. public function testTableDecoratorLoaderAdvanced()
  259. {
  260. $table = new Zend_Text_Table(array('columnWidths' => array(10), 'decorator' => new Zend_Text_Table_Decorator_Ascii()));
  261. $row = new Zend_Text_Table_Row();
  262. $row->createColumn('foobar');
  263. $table->appendRow($row);
  264. $this->assertEquals($table->render(), "+----------+\n|foobar |\n+----------+\n");
  265. }
  266. public function testTableSimpleRow()
  267. {
  268. $table = new Zend_Text_Table(array('columnWidths' => array(10)));
  269. $row = new Zend_Text_Table_Row();
  270. $row->createColumn('foobar');
  271. $table->appendRow($row);
  272. $this->assertEquals($table->render(), "┌──────────┐\n│foobar │\n└──────────┘\n");
  273. }
  274. public function testDefaultColumnAlign()
  275. {
  276. $table = new Zend_Text_Table(array('columnWidths' => array(10)));
  277. $table->setDefaultColumnAlign(0, Zend_Text_Table_Column::ALIGN_CENTER);
  278. $table->appendRow(array('foobar'));
  279. $this->assertEquals($table->render(), "┌──────────┐\n│ foobar │\n└──────────┘\n");
  280. }
  281. public function testRowGetColumns()
  282. {
  283. $row = new Zend_Text_Table_Row();
  284. $row->createColumn('foo')
  285. ->createColumn('bar');
  286. $this->assertEquals(2, count($row->getColumns()));
  287. }
  288. public function testRowGetColumn()
  289. {
  290. $row = new Zend_Text_Table_Row();
  291. $row->createColumn('foo');
  292. $this->assertTrue($row->getColumn(0) instanceof Zend_Text_Table_Column);
  293. }
  294. public function testRowGetInvalidColumn()
  295. {
  296. $row = new Zend_Text_Table_Row();
  297. $row->createColumn('foo');
  298. $this->assertEquals(null, $row->getColumn(1));
  299. }
  300. public function testTableWithoutRows()
  301. {
  302. $table = new Zend_Text_Table(array('columnWidths' => array(10)));
  303. try {
  304. $table->render();
  305. $this->fail('An expected Zend_Text_Table_Exception has not been raised');
  306. } catch (Zend_Text_Table_Exception $expected) {
  307. $this->assertContains('No rows were added to the table yet', $expected->getMessage());
  308. }
  309. }
  310. public function testTableColSpanWithMultipleRows()
  311. {
  312. $table = new Zend_Text_Table(array('columnWidths' => array(10, 10)));
  313. $row = new Zend_Text_Table_Row();
  314. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  315. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  316. $table->appendRow($row);
  317. $row = new Zend_Text_Table_Row();
  318. $row->appendColumn(new Zend_Text_Table_Column('foobar', null, 2));
  319. $table->appendRow($row);
  320. $this->assertEquals($table->render(), "┌──────────┬──────────┐\n"
  321. . "│foobar │foobar │\n"
  322. . "├──────────┴──────────┤\n"
  323. . "│foobar │\n"
  324. . "└─────────────────────┘\n");
  325. }
  326. public function testTableComplex()
  327. {
  328. $table = new Zend_Text_Table(array('columnWidths' => array(10, 10, 10)));
  329. $row = new Zend_Text_Table_Row();
  330. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  331. $row->appendColumn(new Zend_Text_Table_Column('foobar', null, 2));
  332. $table->appendRow($row);
  333. $row = new Zend_Text_Table_Row();
  334. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  335. $row->appendColumn(new Zend_Text_Table_Column('foobar', null, 2));
  336. $table->appendRow($row);
  337. $row = new Zend_Text_Table_Row();
  338. $row->appendColumn(new Zend_Text_Table_Column('foobar', null, 3));
  339. $table->appendRow($row);
  340. $row = new Zend_Text_Table_Row();
  341. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  342. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  343. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  344. $table->appendRow($row);
  345. $this->assertEquals($table->render(), "┌──────────┬─────────────────────┐\n"
  346. . "│foobar │foobar │\n"
  347. . "├──────────┼─────────────────────┤\n"
  348. . "│foobar │foobar │\n"
  349. . "├──────────┴─────────────────────┤\n"
  350. . "│foobar │\n"
  351. . "├──────────┬──────────┬──────────┤\n"
  352. . "│foobar │foobar │foobar │\n"
  353. . "└──────────┴──────────┴──────────┘\n");
  354. }
  355. public function testTableMagicToString()
  356. {
  357. $table = new Zend_Text_Table(array('columnWidths' => array(10)));
  358. $row = new Zend_Text_Table_Row();
  359. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  360. $table->appendRow($row);
  361. $this->assertEquals((string) $table, "┌──────────┐\n│foobar │\n└──────────┘\n");
  362. }
  363. public function testDecoratorUnicode()
  364. {
  365. $decorator = new Zend_Text_Table_Decorator_Unicode();
  366. $chars = $decorator->getBottomLeft()
  367. . $decorator->getBottomRight()
  368. . $decorator->getCross()
  369. . $decorator->getHorizontal()
  370. . $decorator->getHorizontalDown()
  371. . $decorator->getHorizontalUp()
  372. . $decorator->getTopLeft()
  373. . $decorator->getTopRight()
  374. . $decorator->getVertical()
  375. . $decorator->getVerticalLeft()
  376. . $decorator->getVerticalRight();
  377. $this->assertEquals($chars, '└┘┼─┬┴┌┐│┤├');
  378. }
  379. public function testDecoratorAscii()
  380. {
  381. $decorator = new Zend_Text_Table_Decorator_Ascii();
  382. $chars = $decorator->getBottomLeft()
  383. . $decorator->getBottomRight()
  384. . $decorator->getCross()
  385. . $decorator->getHorizontal()
  386. . $decorator->getHorizontalDown()
  387. . $decorator->getHorizontalUp()
  388. . $decorator->getTopLeft()
  389. . $decorator->getTopRight()
  390. . $decorator->getVertical()
  391. . $decorator->getVerticalLeft()
  392. . $decorator->getVerticalRight();
  393. $this->assertEquals($chars, '+++-++++|++');
  394. }
  395. }
  396. // Call Zend_Text_TableTest::main() if this source file is executed directly.
  397. if (PHPUnit_MAIN_METHOD == "Zend_Text_TableTest::main") {
  398. Zend_Text_TableTest::main();
  399. }