WithMappedCellsTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Illuminate\Support\Str;
  4. use Maatwebsite\Excel\Concerns\Importable;
  5. use Maatwebsite\Excel\Concerns\ToArray;
  6. use Maatwebsite\Excel\Concerns\ToModel;
  7. use Maatwebsite\Excel\Concerns\WithMappedCells;
  8. use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
  9. use Maatwebsite\Excel\Tests\TestCase;
  10. use PHPUnit\Framework\Assert;
  11. class WithMappedCellsTest extends TestCase
  12. {
  13. /**
  14. * Setup the test environment.
  15. */
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->loadLaravelMigrations(['--database' => 'testing']);
  20. }
  21. /**
  22. * @test
  23. */
  24. public function can_import_with_references_to_cells()
  25. {
  26. $import = new class implements WithMappedCells, ToArray
  27. {
  28. use Importable;
  29. /**
  30. * @return array
  31. */
  32. public function mapping(): array
  33. {
  34. return [
  35. 'name' => 'B1',
  36. 'email' => 'B2',
  37. ];
  38. }
  39. /**
  40. * @param array $array
  41. */
  42. public function array(array $array)
  43. {
  44. Assert::assertEquals([
  45. 'name' => 'Patrick Brouwers',
  46. 'email' => 'patrick@maatwebsite.nl',
  47. ], $array);
  48. }
  49. };
  50. $import->import('mapped-import.xlsx');
  51. }
  52. /**
  53. * @test
  54. */
  55. public function can_import_with_nested_references_to_cells()
  56. {
  57. $import = new class implements WithMappedCells, ToArray
  58. {
  59. use Importable;
  60. /**
  61. * @return array
  62. */
  63. public function mapping(): array
  64. {
  65. return [
  66. [
  67. 'name' => 'B1',
  68. 'email' => 'B2',
  69. ],
  70. [
  71. 'name' => 'D1',
  72. 'email' => 'D2',
  73. ],
  74. ];
  75. }
  76. /**
  77. * @param array $array
  78. */
  79. public function array(array $array)
  80. {
  81. Assert::assertEquals([
  82. [
  83. 'name' => 'Patrick Brouwers',
  84. 'email' => 'patrick@maatwebsite.nl',
  85. ],
  86. [
  87. 'name' => 'Typingbeaver',
  88. 'email' => 'typingbeaver@mailbox.org',
  89. ],
  90. ], $array);
  91. }
  92. };
  93. $import->import('mapped-import.xlsx');
  94. }
  95. /**
  96. * @test
  97. */
  98. public function can_import_with_references_to_cells_to_model()
  99. {
  100. $import = new class implements WithMappedCells, ToModel
  101. {
  102. use Importable;
  103. /**
  104. * @return array
  105. */
  106. public function mapping(): array
  107. {
  108. return [
  109. 'name' => 'B1',
  110. 'email' => 'B2',
  111. ];
  112. }
  113. /**
  114. * @param array $array
  115. * @return User
  116. */
  117. public function model(array $array)
  118. {
  119. Assert::assertEquals([
  120. 'name' => 'Patrick Brouwers',
  121. 'email' => 'patrick@maatwebsite.nl',
  122. ], $array);
  123. $array['password'] = Str::random();
  124. return new User($array);
  125. }
  126. };
  127. $import->import('mapped-import.xlsx');
  128. $this->assertDatabaseHas('users', [
  129. 'name' => 'Patrick Brouwers',
  130. 'email' => 'patrick@maatwebsite.nl',
  131. ]);
  132. }
  133. }