TableTest.php 18 KB

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