TableTest.php 18 KB

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