WithCalculatedFormulasTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Maatwebsite\Excel\Concerns\HasReferencesToOtherSheets;
  5. use Maatwebsite\Excel\Concerns\Importable;
  6. use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
  7. use Maatwebsite\Excel\Concerns\ToArray;
  8. use Maatwebsite\Excel\Concerns\ToModel;
  9. use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
  10. use Maatwebsite\Excel\Concerns\WithMultipleSheets;
  11. use Maatwebsite\Excel\Concerns\WithStartRow;
  12. use Maatwebsite\Excel\Tests\TestCase;
  13. use PHPUnit\Framework\Assert;
  14. class WithCalculatedFormulasTest extends TestCase
  15. {
  16. /**
  17. * @test
  18. */
  19. public function by_default_does_not_calculate_formulas()
  20. {
  21. $import = new class implements ToArray
  22. {
  23. use Importable;
  24. public $called = false;
  25. /**
  26. * @param array $array
  27. */
  28. public function array(array $array)
  29. {
  30. $this->called = true;
  31. Assert::assertSame('=1+1', $array[0][0]);
  32. }
  33. };
  34. $import->import('import-formulas.xlsx');
  35. $this->assertTrue($import->called);
  36. }
  37. /**
  38. * @test
  39. */
  40. public function can_import_to_array_with_calculated_formulas()
  41. {
  42. $import = new class implements ToArray, WithCalculatedFormulas
  43. {
  44. use Importable;
  45. public $called = false;
  46. /**
  47. * @param array $array
  48. */
  49. public function array(array $array)
  50. {
  51. $this->called = true;
  52. Assert::assertSame(2, $array[0][0]);
  53. }
  54. };
  55. $import->import('import-formulas.xlsx');
  56. $this->assertTrue($import->called);
  57. }
  58. /**
  59. * @test
  60. */
  61. public function can_import_to_model_with_calculated_formulas()
  62. {
  63. $import = new class implements ToModel, WithCalculatedFormulas
  64. {
  65. use Importable;
  66. public $called = false;
  67. /**
  68. * @param array $row
  69. * @return Model|null
  70. */
  71. public function model(array $row)
  72. {
  73. $this->called = true;
  74. Assert::assertSame(2, $row[0]);
  75. return null;
  76. }
  77. };
  78. $import->import('import-formulas.xlsx');
  79. $this->assertTrue($import->called);
  80. }
  81. public function can_import_with_formulas_and_reference()
  82. {
  83. $import = new class implements ToModel, WithCalculatedFormulas, WithStartRow
  84. {
  85. use Importable;
  86. public $called = false;
  87. /**
  88. * @param array $row
  89. * @return Model|null
  90. */
  91. public function model(array $row)
  92. {
  93. $this->called = true;
  94. Assert::assertSame('julien', $row[1]);
  95. return null;
  96. }
  97. public function startRow(): int
  98. {
  99. return 2;
  100. }
  101. };
  102. $import->import('import-external-reference.xls');
  103. $this->assertTrue($import->called);
  104. }
  105. /**
  106. * @test
  107. */
  108. public function can_import_to_array_with_calculated_formulas_and_multi_sheet_references()
  109. {
  110. $import = new class implements WithMultipleSheets, HasReferencesToOtherSheets
  111. {
  112. use Importable;
  113. public $test = 'test1';
  114. public function sheets(): array
  115. {
  116. return [
  117. new class implements ToArray, HasReferencesToOtherSheets
  118. {
  119. public $test = 'test2';
  120. public function array(array $array)
  121. {
  122. Assert::assertEquals([
  123. ['1', '1'],
  124. ], $array);
  125. }
  126. },
  127. new class implements ToArray, WithCalculatedFormulas, HasReferencesToOtherSheets
  128. {
  129. public $test = 'test2';
  130. public function array(array $array)
  131. {
  132. Assert::assertEquals([
  133. ['2'],
  134. ], $array);
  135. }
  136. },
  137. ];
  138. }
  139. };
  140. $import->import('import-formulas-multiple-sheets.xlsx');
  141. }
  142. /**
  143. * @test
  144. */
  145. public function can_import_to_array_with_calculated_formulas_and_skips_empty()
  146. {
  147. $import = new class implements ToArray, WithCalculatedFormulas, SkipsEmptyRows
  148. {
  149. use Importable;
  150. public $called = false;
  151. /**
  152. * @param array $array
  153. */
  154. public function array(array $array)
  155. {
  156. $this->called = true;
  157. Assert::assertSame(2, $array[0][0]);
  158. }
  159. };
  160. $import->import('import-formulas.xlsx');
  161. $this->assertTrue($import->called);
  162. }
  163. /**
  164. * @test
  165. */
  166. public function can_import_to_model_with_calculated_formulas_and_skips_empty()
  167. {
  168. $import = new class implements ToModel, WithCalculatedFormulas, SkipsEmptyRows
  169. {
  170. use Importable;
  171. public $called = false;
  172. /**
  173. * @param array $row
  174. * @return Model|null
  175. */
  176. public function model(array $row)
  177. {
  178. $this->called = true;
  179. Assert::assertSame(2, $row[0]);
  180. return null;
  181. }
  182. };
  183. $import->import('import-formulas.xlsx');
  184. $this->assertTrue($import->called);
  185. }
  186. }