Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"description": "Chatwork SDK for Laravel",
"type": "library",
"require": {
"sun-asterisk/chatwork-php": "^0.1.1",
"illuminate/support": "^5.0"
"ext-json": "*",
"illuminate/http": "^5.0",
"illuminate/support": "^5.0",
"mockery/mockery": "^1.2",
"sun-asterisk/chatwork-php": "^0.1.1"
},
"require-dev": {
"phpunit/phpunit": "^8.3"
Expand Down
56 changes: 56 additions & 0 deletions src/ChatworkRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace SunAsterisk\Chatwork\Laravel;

use SunAsterisk\Chatwork\Helpers\Webhook;
use Illuminate\Http\Request;

class ChatworkRequest extends Request
{
/**
* @return string
*/
public function type()
{
$body = json_decode($this->getContent(), true);
return $body['webhook_event_type'];
}

/**
* @return string
*/
public function id()
{
$body = json_decode($this->getContent(), true);
return $body['webhook_setting_id'];
}

/**
* @return int
*/
public function timestamp()
{
$body = json_decode($this->getContent(), true);
return $body['webhook_event_time'];
}

/**
* @return array
*/
public function event()
{
$body = json_decode($this->getContent(), true);
return $body['webhook_event'];
}

/**
* @param string $token
* @return bool
*/
public function verifySignature(string $token)
{
$signature = $this->header('X-ChatWorkWebhookSignature');
$body = $this->getContent();
return Webhook::verifySignature($token, $body, $signature);
}
}
68 changes: 68 additions & 0 deletions tests/ChatworkRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

use SunAsterisk\Chatwork\Laravel\ChatworkRequest;

class ChatworkRequestTest extends TestCase
{
/**
* @return ChatworkRequest
*/
protected function getRequest()
{
$body = require "Fixtures/request.php";
$request = new ChatworkRequest([], [], [], [], [], [], $body);
return $request;
}

public function testType()
{
$except = "mention_to_me";
$request = $this->getRequest();
$type = $request->type();
$this->assertEquals($type, $except);
}

public function testId()
{
$except = 12345;
$request = $this->getRequest();
$id = $request->id();
$this->assertEquals($id, $except);
}

public function testTimestamp()
{
$except = 1498028130;
$request = $this->getRequest();
$timestamp = $request->timestamp();
$this->assertEquals($timestamp, $except);
}

public function testEvent()
{
$body = require "Fixtures/request.php";
$body = json_decode($body, true);
$except = $body['webhook_event'];
$request = $this->getRequest();
$event = $request->event();
$this->assertEquals($event, $except);
}

public function testVerifySignatureReturnTrue()
{
$token = 'test';
$signature = 'cJ9U13bNloO3GyDI4F2PSrSqSqjSndvyxLmaQSpkA1E=';
$request = $this->getRequest();
$request->headers->set('X-ChatWorkWebhookSignature', $signature);
$this->assertTrue($request->verifySignature($token));
}

public function testVerifySignatureReturnFalse()
{
$token = 'test';
$signature = 'test';
$request = $this->getRequest();
$request->headers->set('X-ChatWorkWebhookSignature', $signature);
$this->assertFalse($request->verifySignature($token));
}
}
18 changes: 18 additions & 0 deletions tests/Fixtures/request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

$body = [
"webhook_setting_id" => "12345",
"webhook_event_type" => "mention_to_me",
"webhook_event_time" => 1498028130,
"webhook_event" => [
"from_account_id" => 123456,
"to_account_id" => 1484814,
"room_id" => 567890123,
"message_id" => "789012345",
"body" => "[To=>1484814]Okazu là gì?",
"send_time" => 1498028125,
"update_time" => 0,
],
];

return json_encode($body, true);
8 changes: 8 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use PHPUnit\Framework\TestCase as BaseTestCase;

class TestCase extends BaseTestCase
{

}