Skip to content

Commit

Permalink
feat(line): parse follow webhook event
Browse files Browse the repository at this point in the history
  • Loading branch information
GimmyHchs committed Jun 8, 2018
1 parent 71bfa4d commit 586e9f2
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 16 deletions.
16 changes: 0 additions & 16 deletions .editorconfig

This file was deleted.

33 changes: 33 additions & 0 deletions app/Eloquents/Line/FollowEvent.php
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',
];
}
34 changes: 34 additions & 0 deletions app/Services/Line/WebhookParsers/FollowParser.php
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;
}
}
2 changes: 2 additions & 0 deletions app/Services/Line/WebhookParsers/ParserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public static function make(string $type): IWebhookParser
return app(JoinParser::class);
case 'leave':
return app(LeaveParser::class);
case 'follow':
return app(FollowParser::class);
default:
throw new UndefinedEventTypeException("undefined webhook type [{$type}]");
}
Expand Down
12 changes: 12 additions & 0 deletions database/factories/LineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use App\Eloquents\Line\Hookevent;
use App\Eloquents\Line\JoinEvent;
use App\Eloquents\Line\LeaveEvent;
use App\Eloquents\Line\FollowEvent;
use App\Eloquents\Line\LineText;
use App\Eloquents\Line\LineUser;
use Carbon\Carbon;
Expand Down Expand Up @@ -64,3 +65,14 @@
'origin_data' => str_random(20),
];
});

$factory->define(FollowEvent::class, function (Faker $faker) {
return [
'type' => $faker->word,
'reply_token' => str_random(20),
'timestamp' => Carbon::now(),
'source_type' => $faker->word,
'source_id' => str_random(20),
'origin_data' => str_random(20),
];
});
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');
}
}
40 changes: 40 additions & 0 deletions tests/Unit/Eloquents/Line/FollowEventTest.php
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 tests/Unit/Services/Line/WebhookParsers/FollowParserTest.php
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());
}
}
2 changes: 2 additions & 0 deletions tests/Unit/Services/Line/WebhookParsers/ParserFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Exceptions\Line\UndefinedEventTypeException;
use App\Services\Line\WebhookParsers\JoinParser;
use App\Services\Line\WebhookParsers\LeaveParser;
use App\Services\Line\WebhookParsers\FollowParser;
use App\Services\Line\WebhookParsers\ParserFactory;
use Exception;
use Tests\TestCase;
Expand All @@ -15,6 +16,7 @@ public function testMake()
{
$this->assertInstanceOf(JoinParser::class, ParserFactory::make('join'));
$this->assertInstanceOf(LeaveParser::class, ParserFactory::make('leave'));
$this->assertInstanceOf(FollowParser::class, ParserFactory::make('follow'));
}

public function testMakeWillThrowException()
Expand Down

0 comments on commit 586e9f2

Please sign in to comment.