Skip to content

Commit aaf2d40

Browse files
authored
Merge pull request #334 from hinoguma/feature/add-activate-audience-api
Add Activate Audience API
2 parents ff0ac2f + 49abb92 commit aaf2d40

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

src/LINEBot.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,18 @@ public function updateAuthorityLevel($authorityLevel)
11601160
]);
11611161
}
11621162

1163+
/**
1164+
* Activate the audience
1165+
*
1166+
* @param int $audienceGroupId The audience ID.
1167+
* @return Response
1168+
*/
1169+
public function activateAudience($audienceGroupId)
1170+
{
1171+
$url = sprintf($this->endpointBase . '/v2/bot/audienceGroup/%s/activate', urlencode($audienceGroupId));
1172+
return $this->httpClient->put($url, []);
1173+
}
1174+
11631175
/**
11641176
* Get webhook endpoint information
11651177
*

tests/LINEBot/ManagementAudienceTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,4 +572,43 @@ public function testUpdateAuthorityLevel200()
572572
$this->assertEquals(200, $res->getHTTPStatus());
573573
$this->assertTrue($res->isSucceeded());
574574
}
575+
576+
public function testActivateAudience202()
577+
{
578+
$audienceGroupId = "4389303728991";
579+
$mock = function ($testRunner, $httpMethod, $url, $data) use ($audienceGroupId) {
580+
/** @var \PHPUnit\Framework\TestCase $testRunner */
581+
$testRunner->assertEquals('PUT', $httpMethod);
582+
$testRunner->assertEquals("https://api.line.me/v2/bot/audienceGroup/{$audienceGroupId}/activate", $url);
583+
return [];
584+
};
585+
$bot = new LINEBot(new DummyHttpClient($this, $mock, 202), ['channelSecret' => 'CHANNEL-SECRET']);
586+
$res = $bot->activateAudience($audienceGroupId);
587+
588+
$this->assertEquals(202, $res->getHTTPStatus());
589+
$this->assertTrue($res->isSucceeded());
590+
}
591+
592+
public function testActivateAudience400()
593+
{
594+
$audienceGroupId = "4389303728991";
595+
$mock = function ($testRunner, $httpMethod, $url, $data) use ($audienceGroupId) {
596+
/** @var \PHPUnit\Framework\TestCase $testRunner */
597+
$testRunner->assertEquals('PUT', $httpMethod);
598+
$testRunner->assertEquals("https://api.line.me/v2/bot/audienceGroup/{$audienceGroupId}/activate", $url);
599+
return [
600+
'status' => 400,
601+
'message' => 'ERROR MESSAGE.',
602+
'details' => 'ALREADY_ACTIVE',
603+
];
604+
};
605+
$bot = new LINEBot(new DummyHttpClient($this, $mock, 400), ['channelSecret' => 'CHANNEL-SECRET']);
606+
$res = $bot->activateAudience($audienceGroupId);
607+
608+
$this->assertEquals(400, $res->getHTTPStatus());
609+
$this->assertFalse($res->isSucceeded());
610+
$data = $res->getJSONDecodedBody();
611+
$this->assertEquals('ERROR MESSAGE.', $data['message']);
612+
$this->assertEquals('ALREADY_ACTIVE', $data['details']);
613+
}
575614
}

tests/LINEBot/Util/DummyHttpClient.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ class DummyHttpClient implements HTTPClient
2828
private $testRunner;
2929
/** @var \Closure */
3030
private $mock;
31+
/** @var int */
32+
private $statusCode;
3133

32-
public function __construct(TestCase $testRunner, \Closure $mock)
34+
public function __construct(TestCase $testRunner, \Closure $mock, $statusCode = 200)
3335
{
3436
$this->testRunner = $testRunner;
3537
$this->mock = $mock;
38+
$this->statusCode = $statusCode;
3639
}
3740

3841
/**
@@ -44,7 +47,7 @@ public function __construct(TestCase $testRunner, \Closure $mock)
4447
public function get($url, array $data = [], array $headers = [])
4548
{
4649
$ret = call_user_func($this->mock, $this->testRunner, 'GET', $url, is_null($data) ? [] : $data);
47-
return new Response(200, json_encode($ret));
50+
return new Response($this->statusCode, json_encode($ret));
4851
}
4952

5053
/**
@@ -56,7 +59,7 @@ public function get($url, array $data = [], array $headers = [])
5659
public function post($url, array $data, array $headers = null)
5760
{
5861
$ret = call_user_func($this->mock, $this->testRunner, 'POST', $url, $data, $headers);
59-
return new Response(200, json_encode($ret));
62+
return new Response($this->statusCode, json_encode($ret));
6063
}
6164

6265
/**
@@ -70,7 +73,7 @@ public function post($url, array $data, array $headers = null)
7073
public function put($url, array $data, array $headers = null)
7174
{
7275
$ret = call_user_func($this->mock, $this->testRunner, 'PUT', $url, $data, $headers);
73-
return new Response(200, json_encode($ret));
76+
return new Response($this->statusCode, json_encode($ret));
7477
}
7578

7679
/**
@@ -81,6 +84,6 @@ public function put($url, array $data, array $headers = null)
8184
public function delete($url, $data = null)
8285
{
8386
$ret = call_user_func($this->mock, $this->testRunner, 'DELETE', $url, is_null($data) ? [] : $data);
84-
return new Response(200, json_encode($ret));
87+
return new Response($this->statusCode, json_encode($ret));
8588
}
8689
}

0 commit comments

Comments
 (0)