2020-02-04 01:44:16 +09:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Tests\Unit\Io;
|
|
|
|
|
|
|
|
|
|
use App\Exceptions\CsvImportException;
|
|
|
|
|
use App\Io\CheckinCsvImporter;
|
2020-02-13 01:03:54 +09:00
|
|
|
|
use App\User;
|
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
|
use Illuminate\Support\Carbon;
|
2020-02-04 01:44:16 +09:00
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
|
|
class CheckinCsvImporterTest extends TestCase
|
|
|
|
|
{
|
2020-02-13 01:03:54 +09:00
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
2020-02-04 01:44:16 +09:00
|
|
|
|
public function testIncompatibleCharsetEUCJP()
|
|
|
|
|
{
|
2020-02-13 01:03:54 +09:00
|
|
|
|
$user = factory(User::class)->create();
|
2020-02-04 01:44:16 +09:00
|
|
|
|
$this->expectException(CsvImportException::class);
|
|
|
|
|
$this->expectExceptionMessage('文字コード判定に失敗しました。UTF-8 (BOM無し) または Shift_JIS をお使いください。');
|
|
|
|
|
|
2020-02-13 01:03:54 +09:00
|
|
|
|
$importer = new CheckinCsvImporter($user, __DIR__ . '/../../fixture/Csv/incompatible-charset.eucjp.csv');
|
2020-02-04 01:44:16 +09:00
|
|
|
|
$importer->execute();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 22:53:10 +09:00
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideMissingTime
|
|
|
|
|
*/
|
|
|
|
|
public function testMissingTime($filename)
|
2020-02-04 01:44:16 +09:00
|
|
|
|
{
|
2020-02-13 01:03:54 +09:00
|
|
|
|
$user = factory(User::class)->create();
|
2020-02-04 01:44:16 +09:00
|
|
|
|
$this->expectException(CsvImportException::class);
|
|
|
|
|
$this->expectExceptionMessage('日時列は必須です。');
|
|
|
|
|
|
2020-02-15 22:53:10 +09:00
|
|
|
|
$importer = new CheckinCsvImporter($user, $filename);
|
2020-02-04 01:44:16 +09:00
|
|
|
|
$importer->execute();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 22:53:10 +09:00
|
|
|
|
public function provideMissingTime()
|
2020-02-04 01:44:16 +09:00
|
|
|
|
{
|
2020-02-15 22:53:10 +09:00
|
|
|
|
return [
|
|
|
|
|
'UTF8' => [__DIR__ . '/../../fixture/Csv/missing-time.utf8.csv'],
|
|
|
|
|
'SJIS' => [__DIR__ . '/../../fixture/Csv/missing-time.sjis.csv'],
|
|
|
|
|
];
|
2020-02-04 01:44:16 +09:00
|
|
|
|
}
|
2020-02-13 01:03:54 +09:00
|
|
|
|
|
2020-02-15 22:53:10 +09:00
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideDate
|
|
|
|
|
*/
|
|
|
|
|
public function testDate($expectedDate, $filename)
|
2020-02-13 01:03:54 +09:00
|
|
|
|
{
|
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
|
|
2020-02-15 22:53:10 +09:00
|
|
|
|
$importer = new CheckinCsvImporter($user, $filename);
|
2020-02-13 01:03:54 +09:00
|
|
|
|
$importer->execute();
|
|
|
|
|
$ejaculation = $user->ejaculations()->first();
|
|
|
|
|
|
|
|
|
|
$this->assertSame(1, $user->ejaculations()->count());
|
2020-02-15 22:53:10 +09:00
|
|
|
|
$this->assertEquals($expectedDate, $ejaculation->ejaculated_date);
|
2020-02-13 01:03:54 +09:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 22:53:10 +09:00
|
|
|
|
public function provideDate()
|
2020-02-13 01:03:54 +09:00
|
|
|
|
{
|
2020-02-15 22:53:10 +09:00
|
|
|
|
$date = Carbon::create(2020, 1, 23, 6, 1, 0, 'Asia/Tokyo');
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'Zero, Second, UTF8' => [$date, __DIR__ . '/../../fixture/Csv/date.utf8.csv'],
|
|
|
|
|
'NoZero, Second, UTF8' => [$date, __DIR__ . '/../../fixture/Csv/date-nozero.utf8.csv'],
|
|
|
|
|
'Zero, NoSecond, UTF8' => [$date, __DIR__ . '/../../fixture/Csv/date-nosecond.utf8.csv'],
|
|
|
|
|
'NoZero, NoSecond, UTF8' => [$date, __DIR__ . '/../../fixture/Csv/date-nozero-nosecond.utf8.csv'],
|
|
|
|
|
];
|
2020-02-13 01:03:54 +09:00
|
|
|
|
}
|
2020-02-16 14:28:30 +09:00
|
|
|
|
|
|
|
|
|
public function testInvalidDate()
|
|
|
|
|
{
|
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$importer = new CheckinCsvImporter($user, __DIR__ . '/../../fixture/Csv/invalid-date.utf8.csv');
|
|
|
|
|
$importer->execute();
|
|
|
|
|
} catch (CsvImportException $e) {
|
2020-02-16 22:06:12 +09:00
|
|
|
|
$this->assertSame('2 行 : 日時は 2000/01/01 00:00 〜 2099/12/31 23:59 の間のみ対応しています。', $e->getErrors()[0]);
|
|
|
|
|
$this->assertSame('3 行 : 日時は 2000/01/01 00:00 〜 2099/12/31 23:59 の間のみ対応しています。', $e->getErrors()[1]);
|
|
|
|
|
$this->assertSame('4 行 : 日時の形式は "年/月/日 時:分" にしてください。', $e->getErrors()[2]);
|
|
|
|
|
$this->assertSame('5 行 : 日時の形式は "年/月/日 時:分" にしてください。', $e->getErrors()[3]);
|
|
|
|
|
$this->assertSame('6 行 : 日時の形式は "年/月/日 時:分" にしてください。', $e->getErrors()[4]);
|
|
|
|
|
$this->assertSame('7 行 : 日時の形式は "年/月/日 時:分" にしてください。', $e->getErrors()[5]);
|
|
|
|
|
$this->assertSame('8 行 : 日時の形式は "年/月/日 時:分" にしてください。', $e->getErrors()[6]);
|
|
|
|
|
$this->assertSame('9 行 : 日時の形式は "年/月/日 時:分" にしてください。', $e->getErrors()[7]);
|
|
|
|
|
$this->assertSame('10 行 : 日時の形式は "年/月/日 時:分" にしてください。', $e->getErrors()[8]);
|
|
|
|
|
$this->assertSame('11 行 : 日時の形式は "年/月/日 時:分" にしてください。', $e->getErrors()[9]);
|
|
|
|
|
$this->assertSame('12 行 : 日時の形式は "年/月/日 時:分" にしてください。', $e->getErrors()[10]);
|
|
|
|
|
$this->assertSame('13 行 : 日時の形式は "年/月/日 時:分" にしてください。', $e->getErrors()[11]);
|
|
|
|
|
$this->assertSame('14 行 : 日時の形式は "年/月/日 時:分" にしてください。', $e->getErrors()[12]);
|
|
|
|
|
$this->assertSame('15 行 : 日時の形式は "年/月/日 時:分" にしてください。', $e->getErrors()[13]);
|
|
|
|
|
$this->assertSame('16 行 : 日時の形式は "年/月/日 時:分" にしてください。', $e->getErrors()[14]);
|
|
|
|
|
$this->assertSame('17 行 : 日時の形式は "年/月/日 時:分" にしてください。', $e->getErrors()[15]);
|
2020-02-16 14:28:30 +09:00
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->fail('期待する例外が発生していません');
|
|
|
|
|
}
|
2020-02-16 14:49:55 +09:00
|
|
|
|
|
|
|
|
|
public function testNoteUTF8()
|
|
|
|
|
{
|
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
|
|
|
|
|
|
$importer = new CheckinCsvImporter($user, __DIR__ . '/../../fixture/Csv/note.utf8.csv');
|
|
|
|
|
$importer->execute();
|
|
|
|
|
$ejaculations = $user->ejaculations()->orderBy('ejaculated_date')->get();
|
|
|
|
|
|
|
|
|
|
$this->assertCount(3, $ejaculations);
|
|
|
|
|
$this->assertEquals('The quick brown fox jumps over the lazy dog. 素早い茶色の狐はのろまな犬を飛び越える', $ejaculations[0]->note);
|
|
|
|
|
$this->assertEquals("The quick brown fox jumps over the lazy dog.\n素早い茶色の狐はのろまな犬を飛び越える", $ejaculations[1]->note);
|
|
|
|
|
$this->assertEquals('The quick brown fox jumps over the "lazy" dog.', $ejaculations[2]->note);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideNoteOverLength
|
|
|
|
|
*/
|
|
|
|
|
public function testNoteOverLength($filename)
|
|
|
|
|
{
|
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
|
$this->expectException(CsvImportException::class);
|
|
|
|
|
$this->expectExceptionMessage('2 行 : ノートには500文字以下の文字列を指定してください。');
|
|
|
|
|
|
|
|
|
|
$importer = new CheckinCsvImporter($user, $filename);
|
|
|
|
|
$importer->execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideNoteOverLength()
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'ASCII Only, UTF8' => [__DIR__ . '/../../fixture/Csv/note-over-length.ascii.utf8.csv'],
|
|
|
|
|
'JP, UTF8' => [__DIR__ . '/../../fixture/Csv/note-over-length.jp.utf8.csv'],
|
|
|
|
|
];
|
|
|
|
|
}
|
2020-02-16 22:05:03 +09:00
|
|
|
|
|
|
|
|
|
public function testLinkUTF8()
|
|
|
|
|
{
|
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
|
|
|
|
|
|
$importer = new CheckinCsvImporter($user, __DIR__ . '/../../fixture/Csv/link.utf8.csv');
|
|
|
|
|
$importer->execute();
|
|
|
|
|
$ejaculations = $user->ejaculations()->orderBy('ejaculated_date')->get();
|
|
|
|
|
|
|
|
|
|
$this->assertCount(1, $ejaculations);
|
|
|
|
|
$this->assertEquals('http://example.com', $ejaculations[0]->link);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testLinkOverLengthUTF8()
|
|
|
|
|
{
|
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
|
$this->expectException(CsvImportException::class);
|
|
|
|
|
$this->expectExceptionMessage('3 行 : オカズリンクには2000文字以下の文字列を指定してください。');
|
|
|
|
|
|
|
|
|
|
$importer = new CheckinCsvImporter($user, __DIR__ . '/../../fixture/Csv/link-over-length.utf8.csv');
|
|
|
|
|
$importer->execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testLinkIsNotUrlUTF8()
|
|
|
|
|
{
|
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
|
$this->expectException(CsvImportException::class);
|
|
|
|
|
$this->expectExceptionMessage('2 行 : オカズリンクには正しい形式のURLを指定してください。');
|
|
|
|
|
|
|
|
|
|
$importer = new CheckinCsvImporter($user, __DIR__ . '/../../fixture/Csv/link-not-url.utf8.csv');
|
|
|
|
|
$importer->execute();
|
|
|
|
|
}
|
2020-02-04 01:44:16 +09:00
|
|
|
|
}
|