CellTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace fphammerle\helpers\tests\table;
  3. use \fphammerle\helpers\table\Cell;
  4. class CellTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function setValueProvider()
  7. {
  8. return [
  9. [1],
  10. [1.23],
  11. ['string'],
  12. [true],
  13. ];
  14. }
  15. /**
  16. * @dataProvider setValueProvider
  17. */
  18. public function testSetValue($v)
  19. {
  20. $c = new Cell;
  21. $c->setValue($v);
  22. $this->assertSame($c->value, $v);
  23. }
  24. /**
  25. * @dataProvider setValueProvider
  26. */
  27. public function testConstruct($v)
  28. {
  29. $this->assertSame((new Cell($v))->value, $v);
  30. }
  31. public function testConstructDefault()
  32. {
  33. $this->assertNull((new Cell)->value);
  34. }
  35. public function toCSVProvider()
  36. {
  37. return [
  38. [0, '0'],
  39. [1, '1'],
  40. [1.23, '1.23'],
  41. [true, '1'],
  42. [false, '0'],
  43. [null, ''],
  44. ['', ''],
  45. ['string', 'string'],
  46. ['str"ing', '"str""ing"'],
  47. ['str"ing"', '"str""ing"""'],
  48. ['"string"', '"""string"""'],
  49. ['str,ing', '"str,ing"'],
  50. ['str,ing,', '"str,ing,"'],
  51. [',string,', '",string,"'],
  52. ["str\ning", "\"str\ning\""],
  53. ["str\ning\n", "\"str\ning\n\""],
  54. ["string\n", "\"string\n\""],
  55. ["\nstring", "\"\nstring\""],
  56. ["str\ring", "\"str\ring\""],
  57. ["str\r\ning", "\"str\r\ning\""],
  58. ["str\rin\ng", "\"str\rin\ng\""],
  59. ];
  60. }
  61. /**
  62. * @dataProvider toCSVProvider
  63. */
  64. public function testToCSV($v, $csv)
  65. {
  66. $c = new Cell($v);
  67. $this->assertSame($c->toCSV(), $csv);
  68. }
  69. public function toCSVDelimiterProvider()
  70. {
  71. return [
  72. [1, "\t", "1"],
  73. ["1\t2", "\t", "\"1\t2\""],
  74. ["1\t2", "\t", "\"1\t2\""],
  75. ["12\t", "\t", "\"12\t\""],
  76. ['a#$b', '#$', '"a#$b"'],
  77. ['a#$$b', '#$', '"a#$$b"'],
  78. ['1.23', '.', '"1.23"'],
  79. ];
  80. }
  81. /**
  82. * @dataProvider toCSVDelimiterProvider
  83. */
  84. public function testToCSVDelimiter($v, $d, $csv)
  85. {
  86. $c = new Cell($v);
  87. $this->assertSame($c->toCSV($d), $csv);
  88. }
  89. public function toCSVQuotesProvider()
  90. {
  91. return [
  92. [1, '*', '1'],
  93. ['1*2', '*', '*1**2*'],
  94. ['12*', '*', '*12***'],
  95. ['1"2"', '*', '1"2"'],
  96. ['1*"2"', '*', '*1**"2"*'],
  97. ['', '*', ''],
  98. ];
  99. }
  100. /**
  101. * @dataProvider toCSVQuotesProvider
  102. */
  103. public function testToCSVQuotes($v, $q, $csv)
  104. {
  105. $c = new Cell($v);
  106. $this->assertSame($c->toCSV(',', $q), $csv);
  107. }
  108. /**
  109. * @expectedException InvalidArgumentException
  110. */
  111. public function testToCsvDelimiterEqualsQuotes()
  112. {
  113. $c = new Cell;
  114. $c->toCSV('*', '*');
  115. }
  116. /**
  117. * @expectedException InvalidArgumentException
  118. */
  119. public function testToCsvDelimiterEmpty()
  120. {
  121. $c = new Cell;
  122. $c->toCSV('', '*');
  123. }
  124. /**
  125. * @expectedException InvalidArgumentException
  126. */
  127. public function testToCsvQuotesEmpty()
  128. {
  129. $c = new Cell;
  130. $c->toCSV('*', '');
  131. }
  132. }