1
0

RowTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace fphammerle\helpers\tests\table;
  3. use \fphammerle\helpers\table\Row;
  4. class RowTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function setCellValueProvider()
  7. {
  8. return [
  9. [0, 1],
  10. [0, 1.23],
  11. [0, 'string'],
  12. [0, true],
  13. [3, 1],
  14. [3, 1.23],
  15. [3, 'string'],
  16. [3, true],
  17. ];
  18. }
  19. /**
  20. * @dataProvider setCellValueProvider
  21. */
  22. public function testSetCellValue($c, $v)
  23. {
  24. $r = new Row;
  25. $r->setCellValue($c, $v);
  26. $this->assertSame($v, $r->getCell($c)->value);
  27. }
  28. public function setCellValueMultipleProvider()
  29. {
  30. return [
  31. [[[0, 'c0']]],
  32. [[[0, 'c0'], [1, 'c1']]],
  33. [[[0, 'c0'], [1, 'c1'], [3, 'c3']]],
  34. [[[2, 'c2'], [3, 'c3']]],
  35. ];
  36. }
  37. /**
  38. * @dataProvider setCellValueMultipleProvider
  39. */
  40. public function testSetCellMultipleValue($vv)
  41. {
  42. $r = new Row;
  43. foreach($vv as $cv) {
  44. $r->setCellValue($cv[0], $cv[1]);
  45. }
  46. foreach($vv as $cv) {
  47. $this->assertSame($cv[1], $r->getCell($cv[0])->value);
  48. }
  49. }
  50. public function constructProvider()
  51. {
  52. return [
  53. [[]],
  54. [['c0']],
  55. [['c0', 'c1']],
  56. [['c0', 'c1', 'c2']],
  57. ];
  58. }
  59. /**
  60. * @dataProvider constructProvider
  61. */
  62. public function testConstruct($vv)
  63. {
  64. $r = new Row($vv);
  65. foreach($vv as $c => $v) {
  66. $this->assertSame($v, $r->getCell($c)->value);
  67. }
  68. }
  69. public function getColumnsCountProvider()
  70. {
  71. return [
  72. [[], 0],
  73. [[[0, 'c0']], 1],
  74. [[[0, 'c0'], [1, 'c1']], 2],
  75. [[[0, 'c0'], [1, 'c1'], [3, 'c3']], 4],
  76. [[[5, 'c5']], 6],
  77. ];
  78. }
  79. /**
  80. * @dataProvider getColumnsCountProvider
  81. */
  82. public function testGetColumnsCountValue($vv, $count)
  83. {
  84. $r = new Row;
  85. foreach($vv as $cv) {
  86. $r->setCellValue($cv[0], $cv[1]);
  87. }
  88. $this->assertSame($count, $r->columnsCount);
  89. }
  90. public function toCSVProvider()
  91. {
  92. return [
  93. [[], "\r\n"],
  94. [[[0, 'c0']], "c0\r\n"],
  95. [[[0, 'c0'], [1, 'c1']], "c0,c1\r\n"],
  96. [[[0, 'c0'], [1, 'c1'], [3, 'c3']], "c0,c1,,c3\r\n"],
  97. [[[2, 'c2'], [3, 'c3']], ",,c2,c3\r\n"],
  98. [[[2, 'c,2'], [3, 'c3']], ",,\"c,2\",c3\r\n"],
  99. [[[2, "c2\n"], [3, 'c"3"']], ",,\"c2\n\",\"c\"\"3\"\"\"\r\n"],
  100. ];
  101. }
  102. /**
  103. * @dataProvider toCSVProvider
  104. */
  105. public function testToCSVValue($vv, $csv)
  106. {
  107. $r = new Row;
  108. foreach($vv as $cv) {
  109. $r->setCellValue($cv[0], $cv[1]);
  110. }
  111. $this->assertSame($csv, $r->toCSV());
  112. }
  113. public function toCSVAfterConstructProvider()
  114. {
  115. return [
  116. [[], "\r\n"],
  117. [['c0'], "c0\r\n"],
  118. [['c0', 'c1'], "c0,c1\r\n"],
  119. [['c0', 'c1', null, 'c3'], "c0,c1,,c3\r\n"],
  120. ];
  121. }
  122. /**
  123. * @dataProvider toCSVAfterConstructProvider
  124. */
  125. public function testToCSVAfterConstructValue($vv, $csv)
  126. {
  127. $r = new Row($vv);
  128. $this->assertSame($csv, $r->toCSV());
  129. }
  130. public function toCSVDelimiterProvider()
  131. {
  132. return [
  133. [[], "\t", "\r\n"],
  134. [['c0'], "\t", "c0\r\n"],
  135. [['c0', 'c1'], "**", "c0**c1\r\n"],
  136. [['c0', 'c1', null, 'c3'], "\t", "c0\tc1\t\tc3\r\n"],
  137. [['c0', 'c1', null, "c\t3", 'c4'], "\t", "c0\tc1\t\t\"c\t3\"\tc4\r\n"],
  138. ];
  139. }
  140. /**
  141. * @dataProvider toCSVDelimiterProvider
  142. */
  143. public function testToCSVDelimiterValue($vv, $d, $csv)
  144. {
  145. $r = new Row($vv);
  146. $this->assertSame($csv, $r->toCSV($d));
  147. }
  148. public function toCSVColumnsNumberProvider()
  149. {
  150. return [
  151. [[], 0, "\r\n"],
  152. [[], 1, "\r\n"],
  153. [[], 2, "|\r\n"],
  154. [[], 4, "|||\r\n"],
  155. [['a', 'b'], null, "a|b\r\n"],
  156. [['a', 'b'], 1, "a\r\n"],
  157. [['a', 'b'], 2, "a|b\r\n"],
  158. [['a', 'b'], 3, "a|b|\r\n"],
  159. [['a', 'b'], 5, "a|b|||\r\n"],
  160. ];
  161. }
  162. /**
  163. * @dataProvider toCSVColumnsNumberProvider
  164. */
  165. public function testToCSVColumnsNumberValue($vv, $n, $csv)
  166. {
  167. $r = new Row($vv);
  168. $this->assertSame($csv, $r->toCSV('|', $n));
  169. }
  170. /**
  171. * @expectedException InvalidArgumentException
  172. */
  173. public function testToCsvDelimiterEmpty()
  174. {
  175. $c = new Row;
  176. $c->toCSV('');
  177. }
  178. }