WithFormatDataTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Illuminate\Support\Collection;
  4. use Maatwebsite\Excel\Concerns\Importable;
  5. use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
  6. use Maatwebsite\Excel\Concerns\ToArray;
  7. use Maatwebsite\Excel\Concerns\ToCollection;
  8. use Maatwebsite\Excel\Concerns\ToModel;
  9. use Maatwebsite\Excel\Concerns\WithFormatData;
  10. use Maatwebsite\Excel\Tests\TestCase;
  11. use PHPUnit\Framework\Assert;
  12. class WithFormatDataTest extends TestCase
  13. {
  14. /**
  15. * @test
  16. */
  17. public function by_default_import_to_array()
  18. {
  19. $import = new class implements ToArray
  20. {
  21. use Importable;
  22. public $called = false;
  23. /**
  24. * @param array $array
  25. */
  26. public function array(array $array)
  27. {
  28. $this->called = true;
  29. Assert::assertSame(44328, $array[0][0]);
  30. }
  31. };
  32. $import->import('import-format-data.xlsx');
  33. $this->assertTrue($import->called);
  34. }
  35. /**
  36. * @test
  37. */
  38. public function can_import_to_array_with_format_data()
  39. {
  40. config()->set('excel.imports.read_only', false);
  41. $import = new class implements ToArray, WithFormatData
  42. {
  43. use Importable;
  44. public $called = false;
  45. /**
  46. * @param array $array
  47. */
  48. public function array(array $array)
  49. {
  50. $this->called = true;
  51. Assert::assertSame('5/12/2021', $array[0][0]);
  52. }
  53. };
  54. $import->import('import-format-data.xlsx');
  55. $this->assertTrue($import->called);
  56. }
  57. /**
  58. * @test
  59. */
  60. public function can_import_to_array_with_format_data_and_skips_empty_rows()
  61. {
  62. config()->set('excel.imports.read_only', false);
  63. $import = new class implements ToArray, WithFormatData, SkipsEmptyRows
  64. {
  65. use Importable;
  66. public $called = false;
  67. /**
  68. * @param array $array
  69. */
  70. public function array(array $array)
  71. {
  72. $this->called = true;
  73. Assert::assertSame('5/12/2021', $array[0][0]);
  74. }
  75. };
  76. $import->import('import-format-data.xlsx');
  77. $this->assertTrue($import->called);
  78. }
  79. /**
  80. * @test
  81. */
  82. public function by_default_import_to_collection()
  83. {
  84. $import = new class implements ToCollection
  85. {
  86. use Importable;
  87. public $called = false;
  88. /**
  89. * @param array $row
  90. * @return Model|null
  91. */
  92. public function collection(collection $collection)
  93. {
  94. $this->called = true;
  95. Assert::assertSame(44328, $collection[0][0]);
  96. return null;
  97. }
  98. };
  99. $import->import('import-format-data.xlsx');
  100. $this->assertTrue($import->called);
  101. }
  102. /**
  103. * @test
  104. */
  105. public function can_import_to_collection_with_format_data()
  106. {
  107. config()->set('excel.imports.read_only', false);
  108. $import = new class implements ToCollection, WithFormatData
  109. {
  110. use Importable;
  111. public $called = false;
  112. /**
  113. * @param array $row
  114. * @return Model|null
  115. */
  116. public function collection(collection $collection)
  117. {
  118. $this->called = true;
  119. Assert::assertSame('5/12/2021', $collection[0][0]);
  120. return null;
  121. }
  122. };
  123. $import->import('import-format-data.xlsx');
  124. $this->assertTrue($import->called);
  125. }
  126. /**
  127. * @test
  128. */
  129. public function by_default_import_to_model()
  130. {
  131. $import = new class implements ToModel
  132. {
  133. use Importable;
  134. public $called = false;
  135. /**
  136. * @param array $row
  137. * @return Model|null
  138. */
  139. public function model(array $row)
  140. {
  141. $this->called = true;
  142. Assert::assertSame(44328, $row[0]);
  143. return null;
  144. }
  145. };
  146. $import->import('import-format-data.xlsx');
  147. $this->assertTrue($import->called);
  148. }
  149. /**
  150. * @test
  151. */
  152. public function can_import_to_model_with_format_data()
  153. {
  154. config()->set('excel.imports.read_only', false);
  155. $import = new class implements ToModel, WithFormatData
  156. {
  157. use Importable;
  158. public $called = false;
  159. /**
  160. * @param array $row
  161. * @return Model|null
  162. */
  163. public function model(array $row)
  164. {
  165. $this->called = true;
  166. Assert::assertSame('5/12/2021', $row[0]);
  167. return null;
  168. }
  169. };
  170. $import->import('import-format-data.xlsx');
  171. $this->assertTrue($import->called);
  172. }
  173. }