|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Plugin\DataMigration43\Tests\Service; |
| 4 | + |
| 5 | +use Eccube\Tests\EccubeTestCase; |
| 6 | +use Plugin\DataMigration43\Service\DataMigrationService; |
| 7 | +use Symfony\Component\Filesystem\Filesystem; |
| 8 | + |
| 9 | +class ArchiveExtractionTest extends EccubeTestCase |
| 10 | +{ |
| 11 | + /** @var DataMigrationService */ |
| 12 | + private $service; |
| 13 | + |
| 14 | + /** @var string */ |
| 15 | + private $fixturesDir; |
| 16 | + |
| 17 | + /** @var string */ |
| 18 | + private $tmpDir; |
| 19 | + |
| 20 | + public function setUp(): void |
| 21 | + { |
| 22 | + parent::setUp(); |
| 23 | + $this->service = self::getContainer()->get(DataMigrationService::class); |
| 24 | + $this->fixturesDir = __DIR__ . '/../Fixtures/'; |
| 25 | + $this->tmpDir = sys_get_temp_dir() . '/datamigration_test_' . uniqid(); |
| 26 | + mkdir($this->tmpDir, 0777, true); |
| 27 | + } |
| 28 | + |
| 29 | + public function tearDown(): void |
| 30 | + { |
| 31 | + $fs = new Filesystem(); |
| 32 | + if (is_dir($this->tmpDir)) { |
| 33 | + $fs->remove($this->tmpDir); |
| 34 | + } |
| 35 | + parent::tearDown(); |
| 36 | + } |
| 37 | + |
| 38 | + public function tarGzProvider() |
| 39 | + { |
| 40 | + return [ |
| 41 | + '2.11系' => ['2_11_5.tar.gz', ['bkup_data.csv', 'autoinc_data.csv']], |
| 42 | + '2.12系' => ['2_12_6.tar.gz', ['dtb_customer.csv', 'dtb_order.csv', 'dtb_member.csv']], |
| 43 | + '2.13系' => ['2_13_5.tar.gz', ['dtb_customer.csv', 'dtb_order.csv', 'dtb_member.csv']], |
| 44 | + '3.0.9' => ['3_0_9.tar.gz', ['dtb_product.csv', 'dtb_customer.csv']], |
| 45 | + '3.0.18' => ['3_0_18.tar.gz', ['dtb_product.csv', 'dtb_customer.csv']], |
| 46 | + '4.0系' => ['4_0_6.tar.gz', ['dtb_order_item.csv', 'dtb_customer.csv']], |
| 47 | + '4.1系' => ['4_1_2.tar.gz', ['dtb_order_item.csv', 'dtb_customer.csv']], |
| 48 | + 'member_test' => ['member_test.tar.gz', ['dtb_member.csv', 'mtb_authority.csv']], |
| 49 | + ]; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @dataProvider tarGzProvider |
| 54 | + */ |
| 55 | + public function testTarGz解凍(string $filename, array $expectedFiles) |
| 56 | + { |
| 57 | + $archivePath = $this->fixturesDir . $filename; |
| 58 | + $fileNames = $this->service->extractArchive($archivePath, $this->tmpDir); |
| 59 | + |
| 60 | + self::assertNotEmpty($fileNames, $filename . ' のファイル一覧が空'); |
| 61 | + |
| 62 | + // 期待するファイルが解凍されているか確認 |
| 63 | + foreach ($expectedFiles as $expected) { |
| 64 | + $found = false; |
| 65 | + // サブディレクトリ内も検索 |
| 66 | + $iterator = new \RecursiveIteratorIterator( |
| 67 | + new \RecursiveDirectoryIterator($this->tmpDir, \FilesystemIterator::SKIP_DOTS) |
| 68 | + ); |
| 69 | + foreach ($iterator as $file) { |
| 70 | + if ($file->getFilename() === $expected) { |
| 71 | + $found = true; |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + self::assertTrue($found, $filename . ' から ' . $expected . ' が解凍されていること'); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public function testZip解凍() |
| 80 | + { |
| 81 | + // テスト用ZIPを作成 |
| 82 | + $zipPath = $this->tmpDir . '/test.zip'; |
| 83 | + $zip = new \ZipArchive(); |
| 84 | + $zip->open($zipPath, \ZipArchive::CREATE); |
| 85 | + $zip->addFromString('dtb_customer.csv', "id,name\n1,test\n"); |
| 86 | + $zip->addFromString('dtb_product.csv', "id,name\n1,product\n"); |
| 87 | + $zip->close(); |
| 88 | + |
| 89 | + $outputDir = $this->tmpDir . '/output'; |
| 90 | + mkdir($outputDir, 0777, true); |
| 91 | + |
| 92 | + $fileNames = $this->service->extractArchive($zipPath, $outputDir); |
| 93 | + |
| 94 | + self::assertContains('dtb_customer.csv', $fileNames); |
| 95 | + self::assertContains('dtb_product.csv', $fileNames); |
| 96 | + self::assertFileExists($outputDir . '/dtb_customer.csv'); |
| 97 | + self::assertFileExists($outputDir . '/dtb_product.csv'); |
| 98 | + } |
| 99 | + |
| 100 | + public function test不正なファイルで例外() |
| 101 | + { |
| 102 | + $badFile = $this->tmpDir . '/bad.tar.gz'; |
| 103 | + file_put_contents($badFile, 'not an archive'); |
| 104 | + |
| 105 | + $this->expectException(\RuntimeException::class); |
| 106 | + $this->service->extractArchive($badFile, $this->tmpDir); |
| 107 | + } |
| 108 | +} |
0 commit comments