TableTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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-2011 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-2011 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 testTableConstructInvalidColumnWidths()
  216. {
  217. try {
  218. $table = new Zend_Text_Table(array('columnWidths' => array()));
  219. $this->fail('An expected Zend_Text_Table_Exception has not been raised');
  220. } catch (Zend_Text_Table_Exception $expected) {
  221. $this->assertContains('You must supply at least one column', $expected->getMessage());
  222. }
  223. }
  224. public function testTableConstructInvalidColumnWidthsItem()
  225. {
  226. try {
  227. $table = new Zend_Text_Table(array('columnWidths' => array('foo')));
  228. $this->fail('An expected Zend_Text_Table_Exception has not been raised');
  229. } catch (Zend_Text_Table_Exception $expected) {
  230. $this->assertContains('Column 0 has an invalid column width', $expected->getMessage());
  231. }
  232. }
  233. public function testTableDecoratorLoaderSimple()
  234. {
  235. $table = new Zend_Text_Table(array('columnWidths' => array(10), 'decorator' => 'ascii'));
  236. $row = new Zend_Text_Table_Row();
  237. $row->createColumn('foobar');
  238. $table->appendRow($row);
  239. $this->assertEquals($table->render(), "+----------+\n|foobar |\n+----------+\n");
  240. }
  241. public function testTableDecoratorEncodingDefault()
  242. {
  243. Zend_Text_Table::setOutputCharset('iso-8859-15');
  244. $table = new Zend_Text_Table(array('columnWidths' => array(10)));
  245. $row = new Zend_Text_Table_Row();
  246. $row->createColumn('foobar');
  247. $table->appendRow($row);
  248. $this->assertEquals($table->render(), "+----------+\n|foobar |\n+----------+\n");
  249. }
  250. public function testTableDecoratorLoaderAdvanced()
  251. {
  252. $table = new Zend_Text_Table(array('columnWidths' => array(10), 'decorator' => new Zend_Text_Table_Decorator_Ascii()));
  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 testTableSimpleRow()
  259. {
  260. $table = new Zend_Text_Table(array('columnWidths' => array(10)));
  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 testDefaultColumnAlign()
  267. {
  268. $table = new Zend_Text_Table(array('columnWidths' => array(10)));
  269. $table->setDefaultColumnAlign(0, Zend_Text_Table_Column::ALIGN_CENTER);
  270. $table->appendRow(array('foobar'));
  271. $this->assertEquals($table->render(), "┌──────────┐\n│ foobar │\n└──────────┘\n");
  272. }
  273. public function testRowGetColumns()
  274. {
  275. $row = new Zend_Text_Table_Row();
  276. $row->createColumn('foo')
  277. ->createColumn('bar');
  278. $this->assertEquals(2, count($row->getColumns()));
  279. }
  280. public function testRowGetColumn()
  281. {
  282. $row = new Zend_Text_Table_Row();
  283. $row->createColumn('foo');
  284. $this->assertTrue($row->getColumn(0) instanceof Zend_Text_Table_Column);
  285. }
  286. public function testRowGetInvalidColumn()
  287. {
  288. $row = new Zend_Text_Table_Row();
  289. $row->createColumn('foo');
  290. $this->assertEquals(null, $row->getColumn(1));
  291. }
  292. public function testTableWithoutRows()
  293. {
  294. $table = new Zend_Text_Table(array('columnWidths' => array(10)));
  295. try {
  296. $table->render();
  297. $this->fail('An expected Zend_Text_Table_Exception has not been raised');
  298. } catch (Zend_Text_Table_Exception $expected) {
  299. $this->assertContains('No rows were added to the table yet', $expected->getMessage());
  300. }
  301. }
  302. public function testTableColSpanWithMultipleRows()
  303. {
  304. $table = new Zend_Text_Table(array('columnWidths' => array(10, 10)));
  305. $row = new Zend_Text_Table_Row();
  306. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  307. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  308. $table->appendRow($row);
  309. $row = new Zend_Text_Table_Row();
  310. $row->appendColumn(new Zend_Text_Table_Column('foobar', null, 2));
  311. $table->appendRow($row);
  312. $this->assertEquals($table->render(), "┌──────────┬──────────┐\n"
  313. . "│foobar │foobar │\n"
  314. . "├──────────┴──────────┤\n"
  315. . "│foobar │\n"
  316. . "└─────────────────────┘\n");
  317. }
  318. public function testTableComplex()
  319. {
  320. $table = new Zend_Text_Table(array('columnWidths' => array(10, 10, 10)));
  321. $row = new Zend_Text_Table_Row();
  322. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  323. $row->appendColumn(new Zend_Text_Table_Column('foobar', null, 2));
  324. $table->appendRow($row);
  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', null, 3));
  331. $table->appendRow($row);
  332. $row = new Zend_Text_Table_Row();
  333. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  334. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  335. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  336. $table->appendRow($row);
  337. $this->assertEquals($table->render(), "┌──────────┬─────────────────────┐\n"
  338. . "│foobar │foobar │\n"
  339. . "├──────────┼─────────────────────┤\n"
  340. . "│foobar │foobar │\n"
  341. . "├──────────┴─────────────────────┤\n"
  342. . "│foobar │\n"
  343. . "├──────────┬──────────┬──────────┤\n"
  344. . "│foobar │foobar │foobar │\n"
  345. . "└──────────┴──────────┴──────────┘\n");
  346. }
  347. public function testTableMagicToString()
  348. {
  349. $table = new Zend_Text_Table(array('columnWidths' => array(10)));
  350. $row = new Zend_Text_Table_Row();
  351. $row->appendColumn(new Zend_Text_Table_Column('foobar'));
  352. $table->appendRow($row);
  353. $this->assertEquals((string) $table, "┌──────────┐\n│foobar │\n└──────────┘\n");
  354. }
  355. public function testDecoratorUnicode()
  356. {
  357. $decorator = new Zend_Text_Table_Decorator_Unicode();
  358. $chars = $decorator->getBottomLeft()
  359. . $decorator->getBottomRight()
  360. . $decorator->getCross()
  361. . $decorator->getHorizontal()
  362. . $decorator->getHorizontalDown()
  363. . $decorator->getHorizontalUp()
  364. . $decorator->getTopLeft()
  365. . $decorator->getTopRight()
  366. . $decorator->getVertical()
  367. . $decorator->getVerticalLeft()
  368. . $decorator->getVerticalRight();
  369. $this->assertEquals($chars, '└┘┼─┬┴┌┐│┤├');
  370. }
  371. public function testDecoratorAscii()
  372. {
  373. $decorator = new Zend_Text_Table_Decorator_Ascii();
  374. $chars = $decorator->getBottomLeft()
  375. . $decorator->getBottomRight()
  376. . $decorator->getCross()
  377. . $decorator->getHorizontal()
  378. . $decorator->getHorizontalDown()
  379. . $decorator->getHorizontalUp()
  380. . $decorator->getTopLeft()
  381. . $decorator->getTopRight()
  382. . $decorator->getVertical()
  383. . $decorator->getVerticalLeft()
  384. . $decorator->getVerticalRight();
  385. $this->assertEquals($chars, '+++-++++|++');
  386. }
  387. }
  388. // Call Zend_Text_TableTest::main() if this source file is executed directly.
  389. if (PHPUnit_MAIN_METHOD == "Zend_Text_TableTest::main") {
  390. Zend_Text_TableTest::main();
  391. }