|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2026 LINE Corporation |
| 4 | + * |
| 5 | + * LINE Corporation licenses this file to you under the Apache License, |
| 6 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 7 | + * with the License. You may obtain a copy of the License at: |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | + * License for the specific language governing permissions and limitations |
| 15 | + * under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace LINE\Tests\Clients\ManageAudience\Api; |
| 19 | + |
| 20 | +use GuzzleHttp\ClientInterface; |
| 21 | +use GuzzleHttp\Psr7\MultipartStream; |
| 22 | +use GuzzleHttp\Psr7\Request; |
| 23 | +use GuzzleHttp\Psr7\Response; |
| 24 | +use LINE\Clients\ManageAudience\Api\ManageAudienceBlobApi; |
| 25 | +use LINE\Clients\ManageAudience\Model\CreateAudienceGroupResponse; |
| 26 | +use Mockery; |
| 27 | +use PHPUnit\Framework\TestCase; |
| 28 | +use SplFileObject; |
| 29 | + |
| 30 | +class ManageAudienceBlobApiTest extends TestCase |
| 31 | +{ |
| 32 | + private string $tempFile; |
| 33 | + |
| 34 | + protected function setUp(): void |
| 35 | + { |
| 36 | + parent::setUp(); |
| 37 | + $this->tempFile = tempnam(sys_get_temp_dir(), 'audience-test-'); |
| 38 | + file_put_contents($this->tempFile, "U1234567890abcdef\nU0987654321fedcba\n"); |
| 39 | + } |
| 40 | + |
| 41 | + protected function tearDown(): void |
| 42 | + { |
| 43 | + @unlink($this->tempFile); |
| 44 | + Mockery::close(); |
| 45 | + parent::tearDown(); |
| 46 | + } |
| 47 | + |
| 48 | + public function testCreateAudienceForUploadingUserIds(): void |
| 49 | + { |
| 50 | + $client = Mockery::mock(ClientInterface::class); |
| 51 | + $client->shouldReceive('send') |
| 52 | + ->with( |
| 53 | + Mockery::on(function (Request $request) { |
| 54 | + $this->assertEquals('POST', $request->getMethod()); |
| 55 | + $this->assertEquals( |
| 56 | + 'https://api-data.line.me/v2/bot/audienceGroup/upload/byFile', |
| 57 | + (string)$request->getUri() |
| 58 | + ); |
| 59 | + $this->assertInstanceOf(MultipartStream::class, $request->getBody()); |
| 60 | + $body = (string)$request->getBody(); |
| 61 | + // form param keys must follow the OpenAPI spec (camelCase here) |
| 62 | + $this->assertStringContainsString('name="description"', $body); |
| 63 | + $this->assertStringContainsString('name="isIfaAudience"', $body); |
| 64 | + $this->assertStringContainsString('name="uploadDescription"', $body); |
| 65 | + $this->assertStringContainsString('name="file"', $body); |
| 66 | + // file content is included |
| 67 | + $this->assertStringContainsString('U1234567890abcdef', $body); |
| 68 | + return true; |
| 69 | + }), |
| 70 | + [] |
| 71 | + ) |
| 72 | + ->once() |
| 73 | + ->andReturn(new Response( |
| 74 | + status: 200, |
| 75 | + headers: [], |
| 76 | + body: json_encode([ |
| 77 | + 'audienceGroupId' => 1234567890, |
| 78 | + 'createRoute' => 'MESSAGING_API', |
| 79 | + 'type' => 'UPLOAD', |
| 80 | + 'description' => 'audience-test', |
| 81 | + 'created' => 1700000000, |
| 82 | + 'permission' => 'READ_WRITE', |
| 83 | + 'expireTimestamp' => 1800000000, |
| 84 | + 'isIfaAudience' => false, |
| 85 | + ]), |
| 86 | + )); |
| 87 | + $api = new ManageAudienceBlobApi($client); |
| 88 | + $response = $api->createAudienceForUploadingUserIds( |
| 89 | + file: new SplFileObject($this->tempFile), |
| 90 | + description: 'audience-test', |
| 91 | + isIfaAudience: false, |
| 92 | + uploadDescription: 'upload-1', |
| 93 | + ); |
| 94 | + $this->assertInstanceOf(CreateAudienceGroupResponse::class, $response); |
| 95 | + $this->assertEquals(1234567890, $response->getAudienceGroupId()); |
| 96 | + $this->assertEquals('MESSAGING_API', $response->getCreateRoute()); |
| 97 | + } |
| 98 | + |
| 99 | + public function testAddUserIdsToAudience(): void |
| 100 | + { |
| 101 | + $client = Mockery::mock(ClientInterface::class); |
| 102 | + $client->shouldReceive('send') |
| 103 | + ->with( |
| 104 | + Mockery::on(function (Request $request) { |
| 105 | + $this->assertEquals('PUT', $request->getMethod()); |
| 106 | + $this->assertEquals( |
| 107 | + 'https://api-data.line.me/v2/bot/audienceGroup/upload/byFile', |
| 108 | + (string)$request->getUri() |
| 109 | + ); |
| 110 | + $this->assertInstanceOf(MultipartStream::class, $request->getBody()); |
| 111 | + $body = (string)$request->getBody(); |
| 112 | + $this->assertStringContainsString('name="audienceGroupId"', $body); |
| 113 | + $this->assertStringContainsString('1234567890', $body); |
| 114 | + $this->assertStringContainsString('name="uploadDescription"', $body); |
| 115 | + $this->assertStringContainsString('name="file"', $body); |
| 116 | + $this->assertStringContainsString('U1234567890abcdef', $body); |
| 117 | + return true; |
| 118 | + }), |
| 119 | + [] |
| 120 | + ) |
| 121 | + ->once() |
| 122 | + ->andReturn(new Response(status: 202, headers: [], body: '')); |
| 123 | + $api = new ManageAudienceBlobApi($client); |
| 124 | + $api->addUserIdsToAudience( |
| 125 | + file: new SplFileObject($this->tempFile), |
| 126 | + audienceGroupId: 1234567890, |
| 127 | + uploadDescription: 'upload-2', |
| 128 | + ); |
| 129 | + } |
| 130 | +} |
0 commit comments