ImportCommonCatalogServiceTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. declare(strict_types=1);
  3. namespace Tests\Unit\Services\Import;
  4. use App\Models\Import;
  5. use App\Models\Role;
  6. use App\Models\User;
  7. use App\Services\Export\ExportCommonCatalogService;
  8. use App\Services\Import\ImportCommonCatalogService;
  9. use Illuminate\Foundation\Testing\RefreshDatabase;
  10. use Illuminate\Http\UploadedFile;
  11. use Illuminate\Support\Facades\Storage;
  12. use PhpOffice\PhpSpreadsheet\Cell\DataType;
  13. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  14. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  15. use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
  16. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  17. use RuntimeException;
  18. use Tests\TestCase;
  19. class ImportCommonCatalogServiceTest extends TestCase
  20. {
  21. use RefreshDatabase;
  22. protected bool $seed = true;
  23. public function test_import_creates_and_updates_by_article_while_preserving_leading_zeroes(): void
  24. {
  25. Storage::fake('upload');
  26. Storage::fake('public');
  27. $user = User::factory()->create(['role' => Role::ADMIN]);
  28. $import = $this->createImport($user, 'Оборудование для благоустройства', withImage: true);
  29. $this->assertTrue((new ImportCommonCatalogService($import, $user->id))->handle());
  30. $this->assertDatabaseHas('common_catalog_items', [
  31. 'article' => '0254',
  32. 'calculator_name' => 'Оборудование для благоустройства',
  33. 'places' => 38,
  34. 'builders_price' => 141850000,
  35. 'recommended_plus_10_price' => 188802350,
  36. ]);
  37. $item = \App\Models\CommonCatalogItem::query()->firstOrFail();
  38. $this->assertNotNull($item->imageFile);
  39. Storage::disk('public')->assertExists($item->imageFile->path);
  40. $secondImport = $this->createImport($user, 'Обновлённое наименование');
  41. $this->assertTrue((new ImportCommonCatalogService($secondImport, $user->id))->handle());
  42. $this->assertDatabaseCount('common_catalog_items', 1);
  43. $this->assertDatabaseHas('common_catalog_items', [
  44. 'article' => '0254',
  45. 'calculator_name' => 'Обновлённое наименование',
  46. ]);
  47. }
  48. public function test_import_accepts_template_header_with_line_break(): void
  49. {
  50. Storage::fake('upload');
  51. $user = User::factory()->create(['role' => Role::ADMIN]);
  52. $import = $this->createImport($user, 'Тест', "Единица измерения\nгабаритов");
  53. $this->assertTrue((new ImportCommonCatalogService($import, $user->id))->handle());
  54. $this->assertSame('DONE', $import->refresh()->status);
  55. }
  56. public function test_import_accepts_real_catalog_row_without_calculator_name(): void
  57. {
  58. Storage::fake('upload');
  59. $user = User::factory()->create(['role' => Role::ADMIN]);
  60. $import = $this->createImport($user, null);
  61. $this->assertTrue((new ImportCommonCatalogService($import, $user->id))->handle());
  62. $this->assertDatabaseHas('common_catalog_items', [
  63. 'article' => '0254',
  64. 'calculator_name' => null,
  65. ]);
  66. $this->assertStringContainsString('ошибок: 0', (string) $import->refresh()->result);
  67. }
  68. public function test_import_does_not_change_prices_without_field_permission(): void
  69. {
  70. Storage::fake('upload');
  71. $user = User::factory()->create(['role' => Role::ASSISTANT_HEAD]);
  72. $item = \App\Models\CommonCatalogItem::factory()->create([
  73. 'article' => '0254',
  74. 'calculator_name' => 'Старое наименование',
  75. 'builders_price' => 100,
  76. ]);
  77. $import = $this->createImport($user, 'Новое наименование');
  78. $this->assertTrue((new ImportCommonCatalogService($import, $user->id))->handle());
  79. $item->refresh();
  80. $this->assertSame('Новое наименование', $item->calculator_name);
  81. $this->assertSame(100.0, $item->builders_price);
  82. }
  83. public function test_import_preserves_rows_with_same_article_as_named_variants(): void
  84. {
  85. Storage::fake('upload');
  86. $user = User::factory()->create(['role' => Role::ADMIN]);
  87. $import = $this->createImport(
  88. $user,
  89. 'Оборудование игровой площадки',
  90. duplicateVariant: true,
  91. );
  92. $this->assertTrue((new ImportCommonCatalogService($import, $user->id))->handle());
  93. $this->assertDatabaseCount('common_catalog_items', 2);
  94. $this->assertDatabaseHas('common_catalog_items', [
  95. 'article' => '0254',
  96. 'calculator_name' => 'Оборудование игровой площадки',
  97. 'builders_price' => 141850000,
  98. ]);
  99. $this->assertDatabaseHas('common_catalog_items', [
  100. 'article' => '0254',
  101. 'calculator_name' => 'Оборудование игровой площадки (Цинк)',
  102. 'builders_price' => 162280000,
  103. ]);
  104. }
  105. public function test_import_rejects_wrong_headers_with_clear_error(): void
  106. {
  107. Storage::fake('upload');
  108. $user = User::factory()->create(['role' => Role::ADMIN]);
  109. $import = $this->createImport($user, 'Тест', 'Неверный заголовок');
  110. $this->expectException(RuntimeException::class);
  111. $this->expectExceptionMessage('Некорректные заголовки');
  112. try {
  113. (new ImportCommonCatalogService($import, $user->id))->handle();
  114. } finally {
  115. $this->assertSame('ERROR', $import->refresh()->status);
  116. }
  117. }
  118. private function createImport(
  119. User $user,
  120. ?string $calculatorName,
  121. string $dimensionHeader = "Единица измерения\nгабаритов",
  122. bool $withImage = false,
  123. bool $duplicateVariant = false,
  124. ): Import {
  125. $spreadsheet = new Spreadsheet;
  126. $sheet = $spreadsheet->getActiveSheet();
  127. $headers = ExportCommonCatalogService::HEADERS;
  128. $headers[10] = $dimensionHeader;
  129. $sheet->fromArray($headers, null, 'A1');
  130. $sheet->fromArray(ExportCommonCatalogService::SUBHEADERS, null, 'A2');
  131. $sheet->setCellValueExplicit('A3', '0254', DataType::TYPE_STRING);
  132. $sheet->setCellValue('B3', $calculatorName);
  133. $sheet->setCellValue('C3', 'Теневой навес');
  134. $sheet->setCellValue('D3', 'от 180');
  135. $sheet->setCellValue('E3', '1,37-1,55');
  136. $sheet->setCellValue('F3', 3.43);
  137. $sheet->setCellValue('G3', 9.15);
  138. $sheet->setCellValue('H3', 3.46);
  139. $sheet->setCellValue('I3', 0);
  140. $sheet->setCellValue('K3', 'м');
  141. $sheet->setCellValue('L3', 2153.84);
  142. $sheet->setCellValue('M3', 7.93);
  143. $sheet->setCellValue('N3', 38);
  144. $sheet->setCellValue('P3', '0+');
  145. $sheet->setCellValue('Q3', 10);
  146. $sheet->setCellValue('R3', 'шт.');
  147. $sheet->setCellValue('T3', 'Теневые навесы');
  148. $sheet->setCellValue('U3', 'Наш Двор');
  149. $sheet->setCellValue('V3', 'да');
  150. $sheet->setCellValue('W3', 1418500);
  151. $sheet->setCellValue('X3', 1489425);
  152. $sheet->setCellValue('Y3', 1560350);
  153. $sheet->setCellValue('Z3', 2127750);
  154. $sheet->setCellValue('AA3', 2837000);
  155. $sheet->setCellValue('AB3', 3404400);
  156. $sheet->setCellValue('AC3', 1560350);
  157. $sheet->setCellValue('AD3', 1888023.50);
  158. if ($duplicateVariant) {
  159. for ($index = 1; $index <= 30; $index++) {
  160. $column = Coordinate::stringFromColumnIndex($index);
  161. $sheet->setCellValue("{$column}4", $sheet->getCell("{$column}3")->getValue());
  162. }
  163. $sheet->setCellValue('B4', $calculatorName.' (Цинк)');
  164. $sheet->setCellValue('V4', 'нет');
  165. $sheet->setCellValue('W4', 1622800);
  166. }
  167. if ($withImage) {
  168. $image = UploadedFile::fake()->image('catalog-item.png', 100, 100);
  169. $drawing = new Drawing;
  170. $drawing->setPath($image->getPathname());
  171. $drawing->setCoordinates('S3');
  172. $drawing->setWorksheet($sheet);
  173. }
  174. $filename = 'common_catalog_'.uniqid().'.xlsx';
  175. $path = Storage::disk('upload')->path($filename);
  176. (new Xlsx($spreadsheet))->save($path);
  177. return Import::query()->create([
  178. 'type' => 'common_catalog',
  179. 'filename' => $filename,
  180. 'status' => 'new',
  181. 'user_id' => $user->id,
  182. ]);
  183. }
  184. }