AdvancedValueBinderTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Cell;
  3. use PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder;
  4. use PhpOffice\PhpSpreadsheet\Cell\Cell;
  5. use PhpOffice\PhpSpreadsheet\Cell\IValueBinder;
  6. use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
  7. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  8. use PHPUnit\Framework\TestCase;
  9. class AdvancedValueBinderTest extends TestCase
  10. {
  11. const AVB_PRECISION = 1.0E-8;
  12. /**
  13. * @var string
  14. */
  15. private $currencyCode;
  16. /**
  17. * @var string
  18. */
  19. private $decimalSeparator;
  20. /**
  21. * @var string
  22. */
  23. private $thousandsSeparator;
  24. /**
  25. * @var IValueBinder
  26. */
  27. private $valueBinder;
  28. protected function setUp(): void
  29. {
  30. $this->currencyCode = StringHelper::getCurrencyCode();
  31. $this->decimalSeparator = StringHelper::getDecimalSeparator();
  32. $this->thousandsSeparator = StringHelper::getThousandsSeparator();
  33. $this->valueBinder = Cell::getValueBinder();
  34. Cell::setValueBinder(new AdvancedValueBinder());
  35. }
  36. protected function tearDown(): void
  37. {
  38. StringHelper::setCurrencyCode($this->currencyCode);
  39. StringHelper::setDecimalSeparator($this->decimalSeparator);
  40. StringHelper::setThousandsSeparator($this->thousandsSeparator);
  41. Cell::setValueBinder($this->valueBinder);
  42. }
  43. public function testNullValue(): void
  44. {
  45. $spreadsheet = new Spreadsheet();
  46. $sheet = $spreadsheet->getActiveSheet();
  47. $sheet->getCell('A1')->setValue(null);
  48. self::assertNull($sheet->getCell('A1')->getValue());
  49. $spreadsheet->disconnectWorksheets();
  50. }
  51. /**
  52. * @dataProvider currencyProvider
  53. *
  54. * @param mixed $value
  55. * @param mixed $valueBinded
  56. * @param mixed $thousandsSeparator
  57. * @param mixed $decimalSeparator
  58. * @param mixed $currencyCode
  59. */
  60. public function testCurrency($value, $valueBinded, $thousandsSeparator, $decimalSeparator, $currencyCode): void
  61. {
  62. StringHelper::setCurrencyCode($currencyCode);
  63. StringHelper::setDecimalSeparator($decimalSeparator);
  64. StringHelper::setThousandsSeparator($thousandsSeparator);
  65. $spreadsheet = new Spreadsheet();
  66. $sheet = $spreadsheet->getActiveSheet();
  67. $sheet->getCell('A1')->setValue($value);
  68. self::assertEquals($valueBinded, $sheet->getCell('A1')->getValue());
  69. $spreadsheet->disconnectWorksheets();
  70. }
  71. public function currencyProvider(): array
  72. {
  73. return [
  74. ['$10.11', 10.11, ',', '.', '$'],
  75. ['$1,010.12', 1010.12, ',', '.', '$'],
  76. ['$20,20', 20.2, '.', ',', '$'],
  77. ['$2.020,20', 2020.2, '.', ',', '$'],
  78. ['€2.020,20', 2020.2, '.', ',', '€'],
  79. ['€ 2.020,20', 2020.2, '.', ',', '€'],
  80. ['€2,020.22', 2020.22, ',', '.', '€'],
  81. ['$10.11', 10.11, ',', '.', '€'],
  82. ];
  83. }
  84. /**
  85. * @dataProvider fractionProvider
  86. *
  87. * @param mixed $value
  88. * @param mixed $valueBinded
  89. */
  90. public function testFractions($value, $valueBinded): void
  91. {
  92. $spreadsheet = new Spreadsheet();
  93. $sheet = $spreadsheet->getActiveSheet();
  94. $sheet->getCell('A1')->setValue($value);
  95. self::assertEquals($valueBinded, $sheet->getCell('A1')->getValue());
  96. $spreadsheet->disconnectWorksheets();
  97. }
  98. public function fractionProvider(): array
  99. {
  100. return [
  101. ['1/5', 0.2],
  102. ['-1/5', -0.2],
  103. ['12/5', 2.4],
  104. ['2/100', 0.02],
  105. ['15/12', 1.25],
  106. ['20/100', 0.2],
  107. ['1 3/5', 1.6],
  108. ['-1 3/5', -1.6],
  109. ['1 4/20', 1.2],
  110. ['1 16/20', 1.8],
  111. ['12 20/100', 12.2],
  112. ];
  113. }
  114. /**
  115. * @dataProvider percentageProvider
  116. *
  117. * @param mixed $value
  118. * @param mixed $valueBinded
  119. */
  120. public function testPercentages($value, $valueBinded): void
  121. {
  122. $spreadsheet = new Spreadsheet();
  123. $sheet = $spreadsheet->getActiveSheet();
  124. $sheet->getCell('A1')->setValue($value);
  125. self::assertEquals($valueBinded, $sheet->getCell('A1')->getValue());
  126. $spreadsheet->disconnectWorksheets();
  127. }
  128. public function percentageProvider(): array
  129. {
  130. return [
  131. ['10%', 0.1],
  132. ['-12%', -0.12],
  133. ['120%', 1.2],
  134. ['12.5%', 0.125],
  135. ];
  136. }
  137. /**
  138. * @dataProvider timeProvider
  139. *
  140. * @param mixed $value
  141. * @param mixed $valueBinded
  142. */
  143. public function testTimes($value, $valueBinded): void
  144. {
  145. $spreadsheet = new Spreadsheet();
  146. $sheet = $spreadsheet->getActiveSheet();
  147. $sheet->getCell('A1')->setValue($value);
  148. self::assertEqualsWithDelta($valueBinded, $sheet->getCell('A1')->getValue(), self::AVB_PRECISION);
  149. $spreadsheet->disconnectWorksheets();
  150. }
  151. public function timeProvider(): array
  152. {
  153. return [
  154. ['1:20', 0.05555555556],
  155. ['09:17', 0.386805555556],
  156. ['15:00', 0.625],
  157. ['17:12:35', 0.71707175926],
  158. ['23:58:20', 0.99884259259],
  159. ];
  160. }
  161. /**
  162. * @dataProvider stringProvider
  163. */
  164. public function testStringWrapping(string $value): void
  165. {
  166. $spreadsheet = new Spreadsheet();
  167. $sheet = $spreadsheet->getActiveSheet();
  168. $sheet->getCell('A1')->setValue($value);
  169. self::assertEquals($value, $sheet->getCell('A1')->getValue());
  170. $spreadsheet->disconnectWorksheets();
  171. }
  172. public function stringProvider(): array
  173. {
  174. return [
  175. ['Hello World', false],
  176. ["Hello\nWorld", true],
  177. ];
  178. }
  179. }