WithGroupedHeadingRowTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\OnEachRow;
  7. use Maatwebsite\Excel\Concerns\ToArray;
  8. use Maatwebsite\Excel\Concerns\ToCollection;
  9. use Maatwebsite\Excel\Concerns\ToModel;
  10. use Maatwebsite\Excel\Concerns\WithGroupedHeadingRow;
  11. use Maatwebsite\Excel\Row;
  12. use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
  13. use Maatwebsite\Excel\Tests\TestCase;
  14. use PHPUnit\Framework\Assert;
  15. class WithGroupedHeadingRowTest extends TestCase
  16. {
  17. /**
  18. * Setup the test environment.
  19. */
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. $this->loadLaravelMigrations(['--database' => 'testing']);
  24. $this->loadMigrationsFrom(dirname(__DIR__) . '/Data/Stubs/Database/Migrations');
  25. }
  26. /**
  27. * @test
  28. */
  29. public function can_import_to_array_with_grouped_headers()
  30. {
  31. $import = new class implements ToArray, WithGroupedHeadingRow
  32. {
  33. use Importable;
  34. /**
  35. * @param array $array
  36. */
  37. public function array(array $array)
  38. {
  39. Assert::assertEquals([
  40. [
  41. 'name' => 'Patrick Brouwers',
  42. 'email' => 'patrick@maatwebsite.nl',
  43. 'options' => [
  44. 'laravel',
  45. 'excel',
  46. ],
  47. ],
  48. ], $array);
  49. }
  50. };
  51. $import->import('import-users-with-grouped-headers.xlsx');
  52. }
  53. /**
  54. * @test
  55. */
  56. public function can_import_oneachrow_with_grouped_headers()
  57. {
  58. $import = new class implements OnEachRow, WithGroupedHeadingRow
  59. {
  60. use Importable;
  61. /**
  62. * @param \Maatwebsite\Excel\Row $row
  63. * @return void
  64. */
  65. public function onRow(Row $row)
  66. {
  67. Assert::assertEquals(
  68. [
  69. 'name' => 'Patrick Brouwers',
  70. 'email' => 'patrick@maatwebsite.nl',
  71. 'options' => [
  72. 'laravel',
  73. 'excel',
  74. ],
  75. ], $row->toArray());
  76. }
  77. };
  78. $import->import('import-users-with-grouped-headers.xlsx');
  79. }
  80. /**
  81. * @test
  82. */
  83. public function can_import_to_collection_with_grouped_headers()
  84. {
  85. $import = new class implements ToCollection, WithGroupedHeadingRow
  86. {
  87. use Importable;
  88. public $called = false;
  89. /**
  90. * @param Collection $collection
  91. */
  92. public function collection(Collection $collection)
  93. {
  94. $this->called = true;
  95. Assert::assertEquals([
  96. [
  97. 'name' => 'Patrick Brouwers',
  98. 'email' => 'patrick@maatwebsite.nl',
  99. 'options' => [
  100. 'laravel',
  101. 'excel',
  102. ],
  103. ],
  104. ], $collection->toArray());
  105. }
  106. };
  107. $import->import('import-users-with-grouped-headers.xlsx');
  108. $this->assertTrue($import->called);
  109. }
  110. /**
  111. * @test
  112. */
  113. public function can_import_each_row_to_model_with_grouped_headers()
  114. {
  115. $import = new class implements ToModel, WithGroupedHeadingRow
  116. {
  117. use Importable;
  118. /**
  119. * @param array $row
  120. * @return Model
  121. */
  122. public function model(array $row): Model
  123. {
  124. return new User([
  125. 'name' => $row['name'],
  126. 'email' => $row['email'],
  127. 'password' => 'secret',
  128. 'options' => $row['options'],
  129. ]);
  130. }
  131. };
  132. $import->import('import-users-with-grouped-headers.xlsx');
  133. $this->assertDatabaseHas('users', [
  134. 'name' => 'Patrick Brouwers',
  135. 'email' => 'patrick@maatwebsite.nl',
  136. 'options' => '["laravel","excel"]',
  137. ]);
  138. }
  139. }