-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(line): parse follow webhook event
- Loading branch information
Showing
9 changed files
with
211 additions
and
16 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Eloquents\Line; | ||
|
||
use App\Contracts\Line\IWebhookEvent; | ||
use App\Eloquents\Eloquent; | ||
use App\Traits\Line\WebhookEventEloquent; | ||
|
||
class FollowEvent extends Eloquent implements IWebhookEvent | ||
{ | ||
use WebhookEventEloquent; | ||
|
||
protected $table = 'line_follow_events'; | ||
|
||
protected $fillable = [ | ||
'type', | ||
'reply_token', | ||
'timestamp', | ||
'source_type', | ||
'source_id', | ||
'origin_data', | ||
]; | ||
|
||
protected $dates = [ | ||
'timestamp', | ||
'created_at', | ||
'updated_at', | ||
]; | ||
|
||
protected $casts = [ | ||
'origin_data' => 'object', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace App\Services\Line\WebhookParsers; | ||
|
||
use App\Contracts\Line\IWebhookEvent; | ||
use App\Contracts\Line\IWebhookParser; | ||
use App\Eloquents\Line\FollowEvent; | ||
|
||
class FollowParser implements IWebhookParser | ||
{ | ||
/** | ||
* @param array $event | ||
* @param bool $is_auto_save | ||
*/ | ||
public function parse(array $event, bool $is_auto_save = false): IWebhookEvent | ||
{ | ||
$source_type = array_get($event, 'source.type'); | ||
$source_id = array_get($event, "source.{$source_type}Id"); | ||
$follow_event = new FollowEvent([ | ||
'type' => array_get($event, 'type'), | ||
'reply_token' => array_get($event, 'replyToken'), | ||
'timestamp' => intval(array_get($event, 'timestamp') / 1000), | ||
'source_type' => $source_type, | ||
'source_id' => $source_id, | ||
'origin_data' => $event, | ||
]); | ||
|
||
if ($is_auto_save) { | ||
$follow_event->save(); | ||
} | ||
|
||
return $follow_event; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
database/migrations/2018_06_07_124605_create_line_follow_events_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateLineFollowEventsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('line_follow_events', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('type', 15); | ||
$table->string('reply_token', 50); | ||
$table->timestamp('timestamp'); | ||
$table->string('source_type', 15); | ||
$table->string('source_id', 50); | ||
$table->text('origin_data'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('line_follow_events'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Eloquents\Line; | ||
|
||
use App\Eloquents\Line\FollowEvent; | ||
use Carbon\Carbon; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Tests\TestCase; | ||
|
||
class FollowEventTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function testFactory() | ||
{ | ||
$events = factory(FollowEvent::class, 5)->create(); | ||
|
||
$this->assertEquals($events->count(), 5); | ||
} | ||
|
||
public function testOriginDataCast() | ||
{ | ||
$event = factory(FollowEvent::class)->create(); | ||
|
||
$json_array = ['a' => 'test', 'b' => 1]; | ||
|
||
$event->origin_data = $json_array; | ||
|
||
$this->assertEquals('test', $event->origin_data->a); | ||
$this->assertEquals(1, $event->origin_data->b); | ||
} | ||
|
||
public function testTimestampDate() | ||
{ | ||
$event = factory(FollowEvent::class)->create(); | ||
$event->timestamp = 1528383950; | ||
|
||
$this->assertInstanceOf(Carbon::class, $event->timestamp); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
tests/Unit/Services/Line/WebhookParsers/FollowParserTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Services\Line\WebhookParsers; | ||
|
||
use App\Eloquents\Line\FollowEvent; | ||
use App\Services\Line\WebhookParsers\FollowParser; | ||
use Carbon\Carbon; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use stdClass; | ||
use Tests\TestCase; | ||
|
||
class FollowParserTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
protected $mock_event; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
$event_json = ' | ||
{ | ||
"replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", | ||
"type": "follow", | ||
"timestamp": 1462629479859, | ||
"source": { | ||
"type": "user", | ||
"userId": "C8900d40ace9ee5d64f93120330ad8872" | ||
} | ||
}'; | ||
$this->mock_event = json_decode($event_json, true); | ||
} | ||
|
||
public function testParse() | ||
{ | ||
$parser = app(FollowParser::class); | ||
$event = $parser->parse($this->mock_event); | ||
$expectCarbon = Carbon::createFromTimestamp(intval(1462629479859 / 1000)); | ||
$this->assertInstanceOf(FollowEvent::class, $event); | ||
$this->assertInstanceOf(stdClass::class, $event->origin_data); | ||
$this->assertEquals('nHuyWiB7yP5Zw52FIkcQobQuGDXCTA', $event->reply_token); | ||
$this->assertEquals('follow', $event->type); | ||
$this->assertEquals($expectCarbon->toDateTimeString(), $event->timestamp->toDateTimeString()); | ||
$this->assertEquals('user', $event->source_type); | ||
$this->assertEquals('C8900d40ace9ee5d64f93120330ad8872', $event->source_id); | ||
} | ||
|
||
public function testParseAndAutoSave() | ||
{ | ||
$parser = app(FollowParser::class); | ||
$parser->parse($this->mock_event, true); | ||
|
||
$this->assertEquals(1, FollowEvent::all()->count()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters