TableTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <?php
  2. namespace fphammerle\helpers\tests\table;
  3. use \fphammerle\helpers\table\Row;
  4. use \fphammerle\helpers\table\Table;
  5. class TableTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function setCellValueProvider()
  8. {
  9. return [
  10. [0, 0, 1],
  11. [0, 0, 1.23],
  12. [0, 0, 'string'],
  13. [0, 0, true],
  14. [2, 3, 1],
  15. [2, 3, 1.23],
  16. [2, 3, 'string'],
  17. [2, 3, true],
  18. ];
  19. }
  20. /**
  21. * @dataProvider setCellValueProvider
  22. */
  23. public function testSetCellValue($r, $c, $v)
  24. {
  25. $t = new Table;
  26. $t->setCellValue($r, $c, $v);
  27. $this->assertSame($v, $t->getCell($r, $c)->value);
  28. }
  29. public function setCellValueMultipleProvider()
  30. {
  31. return [
  32. [[[0, 0, 'r0c0']]],
  33. [[[0, 0, 'r0c0'], [0, 1, 'r0c1']]],
  34. [[[1, 2, 'r1c2'], [2, 2, 'r2c2'], [0, 4, 'r0c4']]],
  35. ];
  36. }
  37. /**
  38. * @dataProvider setCellValueMultipleProvider
  39. */
  40. public function testSetCellMultipleValue($vv)
  41. {
  42. $t = new Table;
  43. foreach($vv as $cv) {
  44. $t->setCellValue($cv[0], $cv[1], $cv[2]);
  45. }
  46. foreach($vv as $cv) {
  47. $this->assertSame($cv[2], $t->getCell($cv[0], $cv[1])->value);
  48. }
  49. }
  50. public function setRowProvider()
  51. {
  52. return [
  53. [[]],
  54. [[0 => new Row]],
  55. [[2 => new Row, 4 => new Row(['aaa', 'bbb'])]],
  56. ];
  57. }
  58. /**
  59. * @dataProvider setRowProvider
  60. */
  61. public function testSetRow($rr)
  62. {
  63. $t = new Table;
  64. foreach($rr as $ri => $r) {
  65. $t->setRow($ri, $r);
  66. }
  67. foreach($rr as $ri => $r) {
  68. $this->assertSame($r, $t->getRow($ri));
  69. }
  70. }
  71. public function constructProvider()
  72. {
  73. return [
  74. [[]],
  75. [[[]]],
  76. [[['r0c0', 'r0c1']]],
  77. [[['r0c0', 'r0c1'], ['r1c0']]],
  78. [[['r0c0', 'r0c1'], ['r1c0'], 4 => ['r4c0', 3 => 'r4c3']]],
  79. ];
  80. }
  81. /**
  82. * @dataProvider constructProvider
  83. */
  84. public function testConstruct($rr)
  85. {
  86. $t = new Table($rr);
  87. foreach($rr as $row_index => $row_values) {
  88. foreach($row_values as $column_index => $cell_value) {
  89. $this->assertSame($cell_value, $t->getCell($row_index, $column_index)->value);
  90. }
  91. }
  92. }
  93. public function getColumnsCountProvider()
  94. {
  95. return [
  96. [[], 0],
  97. [[[]], 0],
  98. [[['r0c0', 'r0c1']], 2],
  99. [[['r0c0', 'r0c1'], ['r1c0']], 2],
  100. [[['r0c0', 'r0c1'], [3 => 'r1c0']], 4],
  101. [[2 => [0 => 'r2c0'], 1 => [2 => 'r1c2']], 3],
  102. ];
  103. }
  104. /**
  105. * @dataProvider getColumnsCountProvider
  106. */
  107. public function testGetColumnsCountValue($rr, $count)
  108. {
  109. $t = new Table($rr);
  110. $this->assertSame($count, $t->columnsCount);
  111. }
  112. public function toCSVProvider()
  113. {
  114. return [
  115. [[], ""],
  116. [[[]], "\r\n"],
  117. [[['r0c0', 'r0c1']], "r0c0,r0c1\r\n"],
  118. [[['r0c0', 'r"0c1"']], "r0c0,\"r\"\"0c1\"\"\"\r\n"],
  119. [[['r0c0', 'r0c1'], ['r1c0']], "r0c0,r0c1\r\nr1c0,\r\n"],
  120. [[2 => [0 => 'r2c0'], 1 => [2 => 'r1c2']], ",,\r\n,,r1c2\r\nr2c0,,\r\n"],
  121. [
  122. [['r0c0', 'r0c1'], ['r1c0'], 4 => ['r4c0', 3 => 'r4c3']],
  123. "r0c0,r0c1,,\r\nr1c0,,,\r\n,,,\r\n,,,\r\nr4c0,,,r4c3\r\n",
  124. ],
  125. [
  126. [
  127. [1, 2, 3, 4],
  128. ['a', 'b', null, 'd'],
  129. ['A,B', 'CD', "EF\n", 'G'],
  130. [3.14, '$#"%'],
  131. ],
  132. "1,2,3,4\r\na,b,,d\r\n\"A,B\",CD,\"EF\n\",G\r\n3.14,\"$#\"\"%\",,\r\n",
  133. ],
  134. ];
  135. }
  136. /**
  137. * @dataProvider toCSVProvider
  138. */
  139. public function testToCSVValue($rr, $csv)
  140. {
  141. $t = new Table($rr);
  142. $this->assertSame($csv, $t->toCSV());
  143. }
  144. public function toCSVDelimiterProvider()
  145. {
  146. return [
  147. [[], '#', ""],
  148. [[[]], '#', "\r\n"],
  149. [[['r0c0', 'r0c1']], '#', "r0c0#r0c1\r\n"],
  150. [[['r0c0', 'r"0c1"']], '#', "r0c0#\"r\"\"0c1\"\"\"\r\n"],
  151. [[['r0c0', 'r0c1'], ['r1c0']], '#', "r0c0#r0c1\r\nr1c0#\r\n"],
  152. [[2 => [0 => 'r2c0'], 1 => [2 => 'r1c2']], '@', "@@\r\n@@r1c2\r\nr2c0@@\r\n"],
  153. ];
  154. }
  155. /**
  156. * @dataProvider toCSVDelimiterProvider
  157. */
  158. public function testToCSVDelimiter($rr, $d, $csv)
  159. {
  160. $t = new Table($rr);
  161. $this->assertSame($csv, $t->toCSV($d));
  162. }
  163. public function getRowsCountProvider()
  164. {
  165. return [
  166. [[], 0],
  167. [[[]], 1],
  168. [[['r0c1']], 1],
  169. [[['r0c1'], []], 2],
  170. [[['r0c1'], [], 4 => []], 5],
  171. [[['r0c1'], [], 4 => ['r4c0']], 5],
  172. [[7 => [4 => 'r7c4']], 8],
  173. [[11 => []], 12],
  174. ];
  175. }
  176. /**
  177. * @dataProvider getRowsCountProvider
  178. */
  179. public function testGetRowsCount($vv, $rc)
  180. {
  181. $t = new Table($vv);
  182. $this->assertSame($rc, $t->rowsCount);
  183. }
  184. public function fromAssociativeArrayProvider()
  185. {
  186. return [
  187. [
  188. [],
  189. [
  190. [],
  191. ],
  192. ],
  193. [
  194. [
  195. [],
  196. ],
  197. [
  198. [],
  199. [],
  200. ],
  201. ],
  202. [
  203. [
  204. [],
  205. [],
  206. ],
  207. [
  208. [],
  209. [],
  210. [],
  211. ],
  212. ],
  213. [
  214. [
  215. ['a' => 'A'],
  216. ],
  217. [
  218. ['a'],
  219. ['A'],
  220. ],
  221. ],
  222. [
  223. [
  224. ['a' => null],
  225. ],
  226. [
  227. ['a'],
  228. [null],
  229. ],
  230. ],
  231. [
  232. [
  233. ['a' => 'A', 'b' => 'B'],
  234. ['c' => 'C'],
  235. ],
  236. [
  237. ['a', 'b', 'c'],
  238. ['A', 'B', null],
  239. [null, null, 'C'],
  240. ],
  241. ],
  242. [
  243. [
  244. ['a' => 'A', 'b' => 'B1'],
  245. ['b' => 'B2', 'c' => 'C'],
  246. ],
  247. [
  248. ['a', 'b', 'c'],
  249. ['A', 'B1', null],
  250. [null, 'B2', 'C'],
  251. ],
  252. ],
  253. [
  254. [
  255. ['a' => 'A1', 'b' => 'B1', 'c' => 'C1'],
  256. ['a' => 'A2', 'b' => 'B2', 'c' => 'C2'],
  257. ['a' => 'A3', 'b' => 'B3', 'c' => 'C3'],
  258. ],
  259. [
  260. ['a', 'b', 'c'],
  261. ['A1', 'B1', 'C1'],
  262. ['A2', 'B2', 'C2'],
  263. ['A3', 'B3', 'C3'],
  264. ],
  265. ],
  266. [
  267. [
  268. ['b' => 'B1'],
  269. [],
  270. ['a' => 'A3', 'b' => 'B3', 'c' => 'C3'],
  271. ['d' => 'D4', 'e' => 'E4'],
  272. ['a' => null, 'c' => 'C5'],
  273. ],
  274. [
  275. ['b', 'a', 'c', 'd', 'e' ],
  276. ['B1', null, null, null, null],
  277. [null, null, null, null, null],
  278. ['B3', 'A3', 'C3', null, null],
  279. [null, null, null, 'D4', 'E4'],
  280. [null, null, 'C5', null, null],
  281. ],
  282. ],
  283. [
  284. [
  285. ['a' => null, 'd' => 1.23],
  286. ['b' => ''],
  287. ['a' => 3, 'b' => 'ok', 'd' => null],
  288. ],
  289. [
  290. ['a', 'd', 'b' ],
  291. [null, 1.23, null],
  292. [null, null, '' ],
  293. [3, null, 'ok'],
  294. ],
  295. ],
  296. ];
  297. }
  298. /**
  299. * @dataProvider fromAssociativeArrayProvider
  300. */
  301. public function testFromAssociativeArray($dict, $cells)
  302. {
  303. $t = Table::fromAssociativeArray($dict);
  304. $this->assertSame(sizeof($cells), $t->rowsCount);
  305. for($r = 0; $r < sizeof($cells); $r++) {
  306. $this->assertSame(
  307. sizeof($cells[$r]),
  308. $t->getRow($r)->columnsCount,
  309. sprintf(
  310. '$cells[%d] = %s, $t->getRow(%d) = %s',
  311. $r,
  312. print_r($cells[$r], true),
  313. $r,
  314. print_r($t->getRow($r), true)
  315. )
  316. );
  317. for($c = 0; $c < sizeof($cells[$r]); $c++) {
  318. if(isset($cells[$r][$c])) {
  319. $this->assertSame($cells[$r][$c], $t->getCell($r, $c)->value);
  320. } else {
  321. $this->assertNull($t->getCell($r, $c)->value);
  322. }
  323. }
  324. }
  325. }
  326. public function associativeArrayToCSVProvider()
  327. {
  328. return [
  329. [
  330. [
  331. ['a' => 'A', 'b' => 'B1'],
  332. ['b' => 'B2', 'c' => 'C'],
  333. ],
  334. "a,b,c\r\nA,B1,\r\n,B2,C\r\n",
  335. ],
  336. [
  337. [
  338. ['b' => 'B1'],
  339. [],
  340. ['a' => 'A3', 'b' => 'B3', 'c' => 'C3'],
  341. ['d' => 'D4', 'e' => 'E4'],
  342. ['a' => null, 'c' => 'C5'],
  343. ],
  344. "b,a,c,d,e\r\nB1,,,,\r\n,,,,\r\nB3,A3,C3,,\r\n,,,D4,E4\r\n,,C5,,\r\n",
  345. ],
  346. ];
  347. }
  348. /**
  349. * @dataProvider associativeArrayToCSVProvider
  350. */
  351. public function testAssociativeArrayToCSV($rows_assoc, $csv)
  352. {
  353. $t = Table::fromAssociativeArray($rows_assoc);
  354. $this->assertSame($csv, $t->toCSV());
  355. }
  356. public function testAppendRow()
  357. {
  358. $t = new Table([['r0c0'], [1 => 'r1c1']]);
  359. $this->assertSame(2, $t->rowsCount);
  360. $this->assertSame(2, $t->columnsCount);
  361. $t->appendRow(new Row(['r2c0', 3 => 'r2c3']));
  362. $this->assertSame(3, $t->rowsCount);
  363. $this->assertSame(4, $t->columnsCount);
  364. $this->assertSame('r2c0', $t->getCell(2, 0)->value);
  365. $this->assertNull($t->getCell(2, 1)->value);
  366. $this->assertSame('r2c3', $t->getCell(2, 3)->value);
  367. }
  368. public function toTextProvider()
  369. {
  370. return [
  371. [
  372. new Table([]),
  373. '',
  374. ],
  375. [
  376. new Table([
  377. [],
  378. ]),
  379. "\n",
  380. ],
  381. [
  382. new Table([
  383. [],
  384. [],
  385. ]),
  386. "\n\n",
  387. ],
  388. [
  389. new Table([
  390. ['22'],
  391. ]),
  392. "22\n",
  393. ],
  394. [
  395. new Table([
  396. [null],
  397. ]),
  398. "\n",
  399. ],
  400. [
  401. new Table([
  402. ['1', '', '22', '333'],
  403. ]),
  404. "1 22 333\n",
  405. ],
  406. [
  407. new Table([
  408. [],
  409. ['2', '', '22', '333'],
  410. ['22'],
  411. ]),
  412. implode("\n", [
  413. ' ',
  414. '2 22 333',
  415. '22 ',
  416. ]) . "\n",
  417. ],
  418. [
  419. new Table([
  420. ['22', '1', '2', '3' ],
  421. ['2', '', '22', '333'],
  422. ]),
  423. implode("\n", [
  424. '22 1 2 3 ',
  425. '2 22 333',
  426. ]) . "\n",
  427. ],
  428. [
  429. new Table([
  430. ['33', '', '', '22', ''],
  431. ['3', '1', '', '2', ''],
  432. ['333', '', '', '2', ''],
  433. ]),
  434. implode("\n", [
  435. '33 22 ',
  436. '3 1 2 ',
  437. '333 2 ',
  438. ]) . "\n",
  439. ],
  440. [
  441. new Table([
  442. [true, false, null, '', 1 ],
  443. ['__', null, '***', 3.33, ' '],
  444. ]),
  445. implode("\n", [
  446. '1 0 1 ',
  447. '__ *** 3.33 ',
  448. ]) . "\n",
  449. ],
  450. [
  451. new Table([
  452. ['r0c0', "r0\nc1", 'r0c2' ],
  453. ["r1\n\nc0", 'r1c1', "\nr1c2"],
  454. ["r2c0\n", 'r2c1', 'r2c2' ],
  455. ]),
  456. implode("\n", [
  457. 'r0c0 r0 r0c2',
  458. ' c1 ',
  459. 'r1 r1c1 ',
  460. ' r1c2',
  461. 'c0 ',
  462. 'r2c0 r2c1 r2c2',
  463. ' ',
  464. ]) . "\n",
  465. ],
  466. ];
  467. }
  468. /**
  469. * @dataProvider toTextProvider
  470. */
  471. public function testToText($table, $expected_text)
  472. {
  473. $this->assertSame($expected_text, $table->toText());
  474. }
  475. public function associativeArrayToTextProvider()
  476. {
  477. return [
  478. [
  479. [
  480. ['A' => 'a', 'B' => 'bb'],
  481. ['B' => 'bbb', 'CCC' => 'c'],
  482. ['D' => 'd'],
  483. ],
  484. implode("\n", [
  485. 'A B CCC D',
  486. 'a bb ',
  487. ' bbb c ',
  488. ' d',
  489. ]) . "\n",
  490. ],
  491. [
  492. [
  493. ['a' => 'laaang', 'b' => 'kurz'],
  494. ['a' => 'kurz', 'c' => "new\nline"],
  495. ['a' => '1st', 'b' => "\n2nd", 'c' => "\n\n3rd"],
  496. ['b' => "\ncenter\n"],
  497. ],
  498. implode("\n", [
  499. 'a b c ',
  500. 'laaang kurz ',
  501. 'kurz new ',
  502. ' line',
  503. '1st ',
  504. ' 2nd ',
  505. ' 3rd ',
  506. ' ',
  507. ' center ',
  508. ' ',
  509. ]) . "\n",
  510. ],
  511. ];
  512. }
  513. /**
  514. * @dataProvider associativeArrayToTextProvider
  515. */
  516. public function testAssociativeArrayToText($rows, $expected_text)
  517. {
  518. $t = Table::fromAssociativeArray($rows);
  519. $this->assertSame($expected_text, $t->toText());
  520. }
  521. }