Skip to content

Commit 23e113a

Browse files
authored
Merge branch 'master' into master
2 parents fafbea1 + 334ae57 commit 23e113a

File tree

5 files changed

+145
-7
lines changed

5 files changed

+145
-7
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
*

src/LINEBot/MessageBuilder/TemplateBuilder/CarouselColumnTemplateBuilder.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class CarouselColumnTemplateBuilder implements TemplateBuilder
4242
/** @var array */
4343
private $template;
4444

45+
/**
46+
* @var TemplateActionBuilder|null
47+
*/
48+
private $defaultAction;
49+
4550
/**
4651
* CarouselColumnTemplateBuilder constructor.
4752
*
@@ -50,14 +55,22 @@ class CarouselColumnTemplateBuilder implements TemplateBuilder
5055
* @param string $thumbnailImageUrl
5156
* @param TemplateActionBuilder[] $actionBuilders
5257
* @param string|null $imageBackgroundColor
58+
* @param TemplateActionBuilder|null $defaultAction
5359
*/
54-
public function __construct($title, $text, $thumbnailImageUrl, array $actionBuilders, $imageBackgroundColor = null)
55-
{
60+
public function __construct(
61+
$title,
62+
$text,
63+
$thumbnailImageUrl,
64+
array $actionBuilders,
65+
$imageBackgroundColor = null,
66+
TemplateActionBuilder $defaultAction = null
67+
) {
5668
$this->title = $title;
5769
$this->text = $text;
5870
$this->thumbnailImageUrl = $thumbnailImageUrl;
5971
$this->actionBuilders = $actionBuilders;
6072
$this->imageBackgroundColor = $imageBackgroundColor;
73+
$this->defaultAction = $defaultAction;
6174
}
6275

6376
/**
@@ -86,6 +99,9 @@ public function buildTemplate()
8699
if ($this->imageBackgroundColor) {
87100
$this->template['imageBackgroundColor'] = $this->imageBackgroundColor;
88101
}
102+
if ($this->defaultAction) {
103+
$this->template['defaultAction'] = $this->defaultAction->buildTemplateAction();
104+
}
89105

90106
return $this->template;
91107
}

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/MessageBuilder/TemplateBuilder/CarouselColumnTemplateBuilderTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,51 @@ class CarouselColumnTemplateBuilderTest extends TestCase
7171
],
7272
"imageBackgroundColor":"ddd"
7373
}
74+
JSON
75+
],
76+
[
77+
'param' => ['aaa', 'bbb', 'ccc', ['postback'], 'ddd', 'uri'],
78+
'json' => <<<JSON
79+
{
80+
"thumbnailImageUrl":"ccc",
81+
"title":"aaa",
82+
"text":"bbb",
83+
"actions":[
84+
{"type":"postback","label":"AAA","data":"BBB"}
85+
],
86+
"imageBackgroundColor":"ddd",
87+
"defaultAction":{"type":"uri","label":"EEE","uri":"FFF"}
88+
}
89+
JSON
90+
],
91+
[
92+
'param' => ['aaa', 'bbb', 'ccc', ['postback'], 'ddd', 'message'],
93+
'json' => <<<JSON
94+
{
95+
"thumbnailImageUrl":"ccc",
96+
"title":"aaa",
97+
"text":"bbb",
98+
"actions":[
99+
{"type":"postback","label":"AAA","data":"BBB"}
100+
],
101+
"imageBackgroundColor":"ddd",
102+
"defaultAction":{"type":"message","label":"CCC","text":"DDD"}
103+
}
104+
JSON
105+
],
106+
[
107+
'param' => ['aaa', 'bbb', 'ccc', ['postback'], 'ddd', 'postback'],
108+
'json' => <<<JSON
109+
{
110+
"thumbnailImageUrl":"ccc",
111+
"title":"aaa",
112+
"text":"bbb",
113+
"actions":[
114+
{"type":"postback","label":"AAA","data":"BBB"}
115+
],
116+
"imageBackgroundColor":"ddd",
117+
"defaultAction":{"type":"postback","label":"AAA","data":"BBB"}
118+
}
74119
JSON
75120
],
76121
];
@@ -100,6 +145,20 @@ public function test()
100145
$actionBuilders = null;
101146
}
102147
$imageBackgroundColor = isset($t['param'][4]) ? $t['param'][4] : null;
148+
$defaultAction = null;
149+
if (isset($t['param'][5])) {
150+
switch ($t['param'][5]) {
151+
case 'postback':
152+
$defaultAction = $postbackActionBuilder;
153+
break;
154+
case 'message':
155+
$defaultAction = $messageTemplateActionBuilder;
156+
break;
157+
case 'uri':
158+
$defaultAction = $uriTemplateActionBuilder;
159+
break;
160+
}
161+
}
103162

104163
if (count($t['param']) == 5) {
105164
$builder = new CarouselColumnTemplateBuilder(
@@ -109,6 +168,15 @@ public function test()
109168
$actionBuilders,
110169
$imageBackgroundColor
111170
);
171+
} elseif (count($t['param']) == 6) {
172+
$builder = new CarouselColumnTemplateBuilder(
173+
$title,
174+
$text,
175+
$thumbnailImageUrl,
176+
$actionBuilders,
177+
$imageBackgroundColor,
178+
$defaultAction
179+
);
112180
} else {
113181
$builder = new CarouselColumnTemplateBuilder(
114182
$title,

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)