From 0ca7b8b7e1a7e76250c55b9e80306a5eb02b7c90 Mon Sep 17 00:00:00 2001 From: shibafu Date: Thu, 14 May 2020 00:27:09 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=BF=E3=82=B0=E5=88=97=E3=81=AE=E7=95=AA?= =?UTF-8?q?=E5=8F=B7=E9=A3=9B=E3=81=B3=E3=82=92=E8=A8=B1=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/CheckinCsvImporter.php | 12 +++++++----- .../Unit/Services/CheckinCsvImporterTest.php | 19 +++++++++++++++++-- tests/fixture/Csv/tag-33-column.utf8.csv | 2 ++ 3 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 tests/fixture/Csv/tag-33-column.utf8.csv diff --git a/app/Services/CheckinCsvImporter.php b/app/Services/CheckinCsvImporter.php index b4906f5..d99f51a 100644 --- a/app/Services/CheckinCsvImporter.php +++ b/app/Services/CheckinCsvImporter.php @@ -147,15 +147,14 @@ class CheckinCsvImporter private function parseTags(int $line, array $record): array { $tags = []; - for ($i = 1; $i <= 32; $i++) { - $column = 'タグ' . $i; - if (empty($record[$column])) { - break; + foreach (array_keys($record) as $column) { + if (preg_match('/\Aタグ\d{1,2}\z/u', $column) !== 1) { + continue; } $tag = trim($record[$column]); if (empty($tag)) { - break; + continue; } if (mb_strlen($tag) > 255) { throw new CsvImportException("{$line} 行 : {$column}は255文字以内にしてください。"); @@ -165,6 +164,9 @@ class CheckinCsvImporter } $tags[] = Tag::firstOrCreate(['name' => $tag]); + if (count($tags) >= 32) { + break; + } } return $tags; diff --git a/tests/Unit/Services/CheckinCsvImporterTest.php b/tests/Unit/Services/CheckinCsvImporterTest.php index 0128267..51307da 100644 --- a/tests/Unit/Services/CheckinCsvImporterTest.php +++ b/tests/Unit/Services/CheckinCsvImporterTest.php @@ -215,7 +215,7 @@ class CheckinCsvImporterTest extends TestCase $importer->execute(); } - public function testTagCantAcceptJumpedColumnUTF8() + public function testTagCanAcceptJumpedColumnUTF8() { $user = factory(User::class)->create(); @@ -225,8 +225,9 @@ class CheckinCsvImporterTest extends TestCase $tags = $ejaculation->tags()->get(); $this->assertSame(1, $user->ejaculations()->count()); - $this->assertCount(1, $tags); + $this->assertCount(2, $tags); $this->assertEquals('貧乳', $tags[0]->name); + $this->assertEquals('巨乳', $tags[1]->name); } public function testTagCantAcceptMultilineUTF8() @@ -239,6 +240,20 @@ class CheckinCsvImporterTest extends TestCase $importer->execute(); } + public function testTagCanAccept32ColumnsUTF8() + { + $user = factory(User::class)->create(); + + $importer = new CheckinCsvImporter($user, __DIR__ . '/../../fixture/Csv/tag-33-column.utf8.csv'); + $importer->execute(); + $ejaculation = $user->ejaculations()->first(); + $tags = $ejaculation->tags()->get(); + + $this->assertSame(1, $user->ejaculations()->count()); + $this->assertCount(32, $tags); + $this->assertEquals('み', $tags[31]->name); + } + public function testSourceIsCsv() { $user = factory(User::class)->create(); diff --git a/tests/fixture/Csv/tag-33-column.utf8.csv b/tests/fixture/Csv/tag-33-column.utf8.csv new file mode 100644 index 0000000..85670a2 --- /dev/null +++ b/tests/fixture/Csv/tag-33-column.utf8.csv @@ -0,0 +1,2 @@ +日時,タグ1,タグ2,タグ3,タグ4,タグ5,タグ6,タグ7,タグ8,タグ9,タグ10,タグ11,タグ12,タグ13,タグ14,タグ15,タグ16,タグ17,タグ18,タグ19,タグ20,タグ21,タグ22,タグ23,タグ24,タグ25,タグ26,タグ27,タグ28,タグ29,タグ30,タグ31,タグ32,タグ33 +2020/01/23 06:01,あ,い,う,え,お,か,き,く,け,こ,さ,し,す,せ,そ,た,ち,つ,て,と,な,に,ぬ,ね,の,は,ひ,ふ,へ,ほ,ま,み,む