|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2016 LINE Corporation |
| 5 | + * |
| 6 | + * LINE Corporation licenses this file to you under the Apache License, |
| 7 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at: |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | + * License for the specific language governing permissions and limitations |
| 16 | + * under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +namespace LINE\Tests\LINEBot; |
| 20 | + |
| 21 | +use LINE\LINEBot; |
| 22 | +use LINE\Tests\LINEBot\Util\DummyHttpClient; |
| 23 | + |
| 24 | +class GetMemberIdsTest extends \PHPUnit_Framework_TestCase |
| 25 | +{ |
| 26 | + public function testGetGroupMemberIds() |
| 27 | + { |
| 28 | + $mock = function ($testRunner, $httpMethod, $url, $data) { |
| 29 | + /** @var \PHPUnit_Framework_TestCase $testRunner */ |
| 30 | + $testRunner->assertEquals('GET', $httpMethod); |
| 31 | + $testRunner->assertEquals('https://api.line.me/v2/bot/group/GROUP_ID/members/ids', $url); |
| 32 | + |
| 33 | + if (!$data) { |
| 34 | + return [ |
| 35 | + 'memberIds' => ['Uxxxxxxxxxxxxxx1', 'Uxxxxxxxxxxxxxx2', 'Uxxxxxxxxxxxxxx3'], |
| 36 | + 'next' => 'testContinuationToken' |
| 37 | + ]; |
| 38 | + } else { |
| 39 | + $testRunner->assertEquals(['start' => 'testContinuationToken'], $data); |
| 40 | + return ['memberIds' => ['Uxxxxxxxxxxxxxx4', 'Uxxxxxxxxxxxxxx5']]; |
| 41 | + } |
| 42 | + }; |
| 43 | + $bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']); |
| 44 | + |
| 45 | + // First time call |
| 46 | + $res = $bot->getGroupMemberIds('GROUP_ID'); |
| 47 | + |
| 48 | + $this->assertEquals(200, $res->getHTTPStatus()); |
| 49 | + $this->assertTrue($res->isSucceeded()); |
| 50 | + |
| 51 | + $data = $res->getJSONDecodedBody(); |
| 52 | + $this->assertEquals(['Uxxxxxxxxxxxxxx1', 'Uxxxxxxxxxxxxxx2', 'Uxxxxxxxxxxxxxx3'], $data['memberIds']); |
| 53 | + $this->assertEquals('testContinuationToken', $data['next']); |
| 54 | + |
| 55 | + // Second time call |
| 56 | + $res = $bot->getGroupMemberIds('GROUP_ID', 'testContinuationToken'); |
| 57 | + |
| 58 | + $this->assertEquals(200, $res->getHTTPStatus()); |
| 59 | + $this->assertTrue($res->isSucceeded()); |
| 60 | + |
| 61 | + $data = $res->getJSONDecodedBody(); |
| 62 | + $this->assertEquals(['Uxxxxxxxxxxxxxx4', 'Uxxxxxxxxxxxxxx5'], $data['memberIds']); |
| 63 | + $this->assertFalse(array_key_exists('next', $data)); |
| 64 | + |
| 65 | + // test getAllGroupMemberIds() |
| 66 | + $memberIds = $bot->getAllGroupMemberIds('GROUP_ID'); |
| 67 | + $this->assertEquals( |
| 68 | + ['Uxxxxxxxxxxxxxx1', 'Uxxxxxxxxxxxxxx2', 'Uxxxxxxxxxxxxxx3', 'Uxxxxxxxxxxxxxx4', 'Uxxxxxxxxxxxxxx5'], |
| 69 | + $memberIds |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + public function testGetRoomMemberIds() |
| 74 | + { |
| 75 | + $mock = function ($testRunner, $httpMethod, $url, $data) { |
| 76 | + /** @var \PHPUnit_Framework_TestCase $testRunner */ |
| 77 | + $testRunner->assertEquals('GET', $httpMethod); |
| 78 | + $testRunner->assertEquals('https://api.line.me/v2/bot/room/ROOM_ID/members/ids', $url); |
| 79 | + |
| 80 | + if (!$data) { |
| 81 | + return [ |
| 82 | + 'memberIds' => ['Uxxxxxxxxxxxxxx1', 'Uxxxxxxxxxxxxxx2', 'Uxxxxxxxxxxxxxx3'], |
| 83 | + 'next' => 'testContinuationToken' |
| 84 | + ]; |
| 85 | + } else { |
| 86 | + $testRunner->assertEquals(['start' => 'testContinuationToken'], $data); |
| 87 | + return ['memberIds' => ['Uxxxxxxxxxxxxxx4', 'Uxxxxxxxxxxxxxx5']]; |
| 88 | + } |
| 89 | + }; |
| 90 | + $bot = new LINEBot(new DummyHttpClient($this, $mock), ['channelSecret' => 'CHANNEL-SECRET']); |
| 91 | + |
| 92 | + // First time call |
| 93 | + $res = $bot->getRoomMemberIds('ROOM_ID'); |
| 94 | + |
| 95 | + $this->assertEquals(200, $res->getHTTPStatus()); |
| 96 | + $this->assertTrue($res->isSucceeded()); |
| 97 | + |
| 98 | + $data = $res->getJSONDecodedBody(); |
| 99 | + $this->assertEquals(['Uxxxxxxxxxxxxxx1', 'Uxxxxxxxxxxxxxx2', 'Uxxxxxxxxxxxxxx3'], $data['memberIds']); |
| 100 | + $this->assertEquals('testContinuationToken', $data['next']); |
| 101 | + |
| 102 | + // Second time call |
| 103 | + $res = $bot->getRoomMemberIds('ROOM_ID', 'testContinuationToken'); |
| 104 | + |
| 105 | + $this->assertEquals(200, $res->getHTTPStatus()); |
| 106 | + $this->assertTrue($res->isSucceeded()); |
| 107 | + |
| 108 | + $data = $res->getJSONDecodedBody(); |
| 109 | + $this->assertEquals(['Uxxxxxxxxxxxxxx4', 'Uxxxxxxxxxxxxxx5'], $data['memberIds']); |
| 110 | + $this->assertFalse(array_key_exists('next', $data)); |
| 111 | + |
| 112 | + // test getAllGroupMemberIds() |
| 113 | + $memberIds = $bot->getAllRoomMemberIds('ROOM_ID'); |
| 114 | + $this->assertEquals( |
| 115 | + ['Uxxxxxxxxxxxxxx1', 'Uxxxxxxxxxxxxxx2', 'Uxxxxxxxxxxxxxx3', 'Uxxxxxxxxxxxxxx4', 'Uxxxxxxxxxxxxxx5'], |
| 116 | + $memberIds |
| 117 | + ); |
| 118 | + } |
| 119 | +} |
0 commit comments