From 6387d4e8536dc3acaf66b5e8042861aa77953ad9 Mon Sep 17 00:00:00 2001 From: shibafu Date: Tue, 18 Feb 2020 02:03:49 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=81=E3=82=A7=E3=83=83=E3=82=AF=E3=82=A4?= =?UTF-8?q?=E3=83=B3=E3=83=87=E3=83=BC=E3=82=BF=E3=81=8CCSV=E3=81=A7?= =?UTF-8?q?=E6=8A=95=E5=85=A5=E3=81=95=E3=82=8C=E3=81=9F=E3=81=93=E3=81=A8?= =?UTF-8?q?=E3=82=92=E8=A8=98=E9=8C=B2=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Ejaculation.php | 3 ++ app/Io/CheckinCsvImporter.php | 1 + ...2_17_085327_add_source_to_ejaculations.php | 38 +++++++++++++++++++ database/seeds/DatabaseSeeder.php | 2 +- database/seeds/EjaculationSourcesSeeder.php | 19 ++++++++++ tests/Unit/Io/CheckinCsvImporterTest.php | 19 ++++++++++ 6 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 database/migrations/2020_02_17_085327_add_source_to_ejaculations.php create mode 100644 database/seeds/EjaculationSourcesSeeder.php diff --git a/app/Ejaculation.php b/app/Ejaculation.php index 0f3522c..a270f40 100644 --- a/app/Ejaculation.php +++ b/app/Ejaculation.php @@ -12,6 +12,9 @@ class Ejaculation extends Model { use HasEagerLimit; + const SOURCE_WEB = 'web'; + const SOURCE_CSV = 'csv'; + protected $fillable = [ 'user_id', 'ejaculated_date', 'note', 'geo_latitude', 'geo_longitude', 'link', diff --git a/app/Io/CheckinCsvImporter.php b/app/Io/CheckinCsvImporter.php index 30d13d4..1239667 100644 --- a/app/Io/CheckinCsvImporter.php +++ b/app/Io/CheckinCsvImporter.php @@ -69,6 +69,7 @@ class CheckinCsvImporter $ejaculation->ejaculated_date = Carbon::createFromFormat('!Y/m/d H:i+', $record['日時']); $ejaculation->note = str_replace(["\r\n", "\r"], "\n", $record['ノート'] ?? ''); $ejaculation->link = $record['オカズリンク'] ?? ''; + $ejaculation->source = Ejaculation::SOURCE_CSV; try { $tags = $this->parseTags($line, $record); diff --git a/database/migrations/2020_02_17_085327_add_source_to_ejaculations.php b/database/migrations/2020_02_17_085327_add_source_to_ejaculations.php new file mode 100644 index 0000000..965e398 --- /dev/null +++ b/database/migrations/2020_02_17_085327_add_source_to_ejaculations.php @@ -0,0 +1,38 @@ +string('name'); + $table->primary('name'); + }); + Schema::table('ejaculations', function (Blueprint $table) { + $table->string('source')->nullable(); + $table->foreign('source')->references('name')->on('ejaculation_sources'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('ejaculations', function (Blueprint $table) { + $table->dropColumn('source'); + }); + Schema::drop('ejaculation_sources'); + } +} diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index e119db6..3f0fc32 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -11,6 +11,6 @@ class DatabaseSeeder extends Seeder */ public function run() { - // $this->call(UsersTableSeeder::class); + $this->call(EjaculationSourcesSeeder::class); } } diff --git a/database/seeds/EjaculationSourcesSeeder.php b/database/seeds/EjaculationSourcesSeeder.php new file mode 100644 index 0000000..a762a08 --- /dev/null +++ b/database/seeds/EjaculationSourcesSeeder.php @@ -0,0 +1,19 @@ +insert(['name' => $source]); + } + } +} diff --git a/tests/Unit/Io/CheckinCsvImporterTest.php b/tests/Unit/Io/CheckinCsvImporterTest.php index f3ecfb3..a486760 100644 --- a/tests/Unit/Io/CheckinCsvImporterTest.php +++ b/tests/Unit/Io/CheckinCsvImporterTest.php @@ -2,6 +2,7 @@ namespace Tests\Unit\Io; +use App\Ejaculation; use App\Exceptions\CsvImportException; use App\Io\CheckinCsvImporter; use App\User; @@ -13,6 +14,12 @@ class CheckinCsvImporterTest extends TestCase { use RefreshDatabase; + protected function setUp() + { + parent::setUp(); + $this->seed(); + } + public function testIncompatibleCharsetEUCJP() { $user = factory(User::class)->create(); @@ -231,4 +238,16 @@ class CheckinCsvImporterTest extends TestCase $importer = new CheckinCsvImporter($user, __DIR__ . '/../../fixture/Csv/tag-multiline.utf8.csv'); $importer->execute(); } + + public function testSourceIsCsv() + { + $user = factory(User::class)->create(); + + $importer = new CheckinCsvImporter($user, __DIR__ . '/../../fixture/Csv/date.utf8.csv'); + $importer->execute(); + $ejaculation = $user->ejaculations()->first(); + + $this->assertSame(1, $user->ejaculations()->count()); + $this->assertEquals(Ejaculation::SOURCE_CSV, $ejaculation->source); + } }