チェックインデータがCSVで投入されたことを記録できるようにした

This commit is contained in:
shibafu 2020-02-18 02:03:49 +09:00
parent 0a53199399
commit 6387d4e853
6 changed files with 81 additions and 1 deletions

View File

@ -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',

View File

@ -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);

View File

@ -0,0 +1,38 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddSourceToEjaculations extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('ejaculation_sources', function (Blueprint $table) {
$table->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');
}
}

View File

@ -11,6 +11,6 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(EjaculationSourcesSeeder::class);
}
}

View File

@ -0,0 +1,19 @@
<?php
use Illuminate\Database\Seeder;
class EjaculationSourcesSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$sources = ['web', 'csv'];
foreach ($sources as $source) {
DB::table('ejaculation_sources')->insert(['name' => $source]);
}
}
}

View File

@ -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);
}
}