TableTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\r\",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. }