WithHeadingRowTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Collection;
  5. use Maatwebsite\Excel\Concerns\Importable;
  6. use Maatwebsite\Excel\Concerns\ToArray;
  7. use Maatwebsite\Excel\Concerns\ToCollection;
  8. use Maatwebsite\Excel\Concerns\ToModel;
  9. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  10. use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
  11. use Maatwebsite\Excel\Tests\TestCase;
  12. use PHPUnit\Framework\Assert;
  13. class WithHeadingRowTest extends TestCase
  14. {
  15. /**
  16. * Setup the test environment.
  17. */
  18. protected function setUp(): void
  19. {
  20. parent::setUp();
  21. $this->loadLaravelMigrations(['--database' => 'testing']);
  22. }
  23. /**
  24. * @test
  25. */
  26. public function can_import_each_row_to_model_with_heading_row()
  27. {
  28. $import = new class implements ToModel, WithHeadingRow
  29. {
  30. use Importable;
  31. /**
  32. * @param array $row
  33. * @return Model
  34. */
  35. public function model(array $row): Model
  36. {
  37. return new User([
  38. 'name' => $row['name'],
  39. 'email' => $row['email'],
  40. 'password' => 'secret',
  41. ]);
  42. }
  43. };
  44. $import->import('import-users-with-headings.xlsx');
  45. $this->assertDatabaseHas('users', [
  46. 'name' => 'Patrick Brouwers',
  47. 'email' => 'patrick@maatwebsite.nl',
  48. ]);
  49. $this->assertDatabaseHas('users', [
  50. 'name' => 'Taylor Otwell',
  51. 'email' => 'taylor@laravel.com',
  52. ]);
  53. }
  54. /**
  55. * @test
  56. */
  57. public function can_import_each_row_to_model_with_different_heading_row()
  58. {
  59. $import = new class implements ToModel, WithHeadingRow
  60. {
  61. use Importable;
  62. /**
  63. * @param array $row
  64. * @return Model
  65. */
  66. public function model(array $row): Model
  67. {
  68. return new User([
  69. 'name' => $row['name'],
  70. 'email' => $row['email'],
  71. 'password' => 'secret',
  72. ]);
  73. }
  74. /**
  75. * @return int
  76. */
  77. public function headingRow(): int
  78. {
  79. return 4;
  80. }
  81. };
  82. $import->import('import-users-with-different-heading-row.xlsx');
  83. $this->assertDatabaseHas('users', [
  84. 'name' => 'Patrick Brouwers',
  85. 'email' => 'patrick@maatwebsite.nl',
  86. ]);
  87. $this->assertDatabaseHas('users', [
  88. 'name' => 'Taylor Otwell',
  89. 'email' => 'taylor@laravel.com',
  90. ]);
  91. }
  92. /**
  93. * @test
  94. */
  95. public function can_import_to_array_with_heading_row()
  96. {
  97. $import = new class implements ToArray, WithHeadingRow
  98. {
  99. use Importable;
  100. /**
  101. * @param array $array
  102. */
  103. public function array(array $array)
  104. {
  105. Assert::assertEquals([
  106. [
  107. 'name' => 'Patrick Brouwers',
  108. 'email' => 'patrick@maatwebsite.nl',
  109. ],
  110. [
  111. 'name' => 'Taylor Otwell',
  112. 'email' => 'taylor@laravel.com',
  113. ],
  114. ], $array);
  115. }
  116. };
  117. $import->import('import-users-with-headings.xlsx');
  118. }
  119. /**
  120. * @test
  121. */
  122. public function can_import_empty_rows_with_header()
  123. {
  124. $import = new class() implements ToArray, WithHeadingRow
  125. {
  126. use Importable;
  127. /**
  128. * @param array $array
  129. */
  130. public function array(array $array)
  131. {
  132. Assert::assertEmpty($array);
  133. }
  134. };
  135. $import->import('import-empty-users-with-headings.xlsx');
  136. }
  137. /**
  138. * @test
  139. */
  140. public function can_import_empty_models_with_header()
  141. {
  142. $import = new class() implements ToModel, WithHeadingRow
  143. {
  144. use Importable;
  145. /**
  146. * @param array $row
  147. * @return Model
  148. */
  149. public function model(array $row): Model
  150. {
  151. return new User([
  152. 'name' => $row['name'],
  153. 'email' => $row['email'],
  154. 'password' => 'secret',
  155. ]);
  156. }
  157. };
  158. $import->import('import-empty-users-with-headings.xlsx');
  159. $this->assertEmpty(User::all());
  160. }
  161. /**
  162. * @test
  163. */
  164. public function can_cast_empty_headers_to_indexed_int()
  165. {
  166. $import = new class() implements ToCollection, WithHeadingRow
  167. {
  168. use Importable;
  169. public $called = false;
  170. public function collection(Collection $collection)
  171. {
  172. $this->called = true;
  173. Assert::assertEquals([
  174. 0 => 0,
  175. 1 => 'email',
  176. 2 => 'status',
  177. 3 => 3,
  178. ], $collection->first()->keys()->toArray());
  179. }
  180. };
  181. $import->import('import-users-with-mixed-headings.xlsx');
  182. $this->assertTrue($import->called);
  183. }
  184. }