setName('e2e-tmp-exchange-'.time()); $body->setType('translation'); $body->setLang('zh-Hans'); $channel = request(fn () => api(StudiosApi::class, $token)->postApiV3StudiosStudioChannelsWithHttpInfo($me['body']['data']['id'], $body)); assert_status(201, $channel, '建临时 channel'); $channelId = $channel['body']['data']['id']; $term = new PostApiV3ChannelsChannelTermsRequest(); $term->setWord('apiexchange'); $term->setMeaning('导入导出测试'); $created = request(fn () => api(ChannelsApi::class, $token)->postApiV3ChannelsChannelTermsWithHttpInfo($channelId, $term)); assert_status(201, $created, '建术语'); // 导出:生成客户端只认 JSON,这里直接拿二进制 $http = new Client(['timeout' => config()['timeout'], 'http_errors' => false]); $export = $http->get(config()['base_url']."/v3/channels/{$channelId}/terms", [ 'headers' => ['Accept' => TERM_XLSX, 'Authorization' => "Bearer {$token}"], ]); assert_eq(200, $export->getStatusCode(), '导出状态码'); assert_eq(TERM_XLSX, $export->getHeaderLine('Content-Type'), '导出的 Content-Type'); $content = (string) $export->getBody(); assert_eq('PK', substr($content, 0, 2), '导出的是 xlsx(zip)'); $path = sys_get_temp_dir().'/api-test-terms-'.time().'.xlsx'; file_put_contents($path, $content); $import = request(fn () => api(ChannelsApi::class, $token)->postApiV3ChannelsChannelTermsImportsWithHttpInfo($channelId, new \SplFileObject($path))); assert_status(200, $import, '原样导入'); assert_eq(['created' => 0, 'updated' => 0, 'unchanged' => 1], array_intersect_key($import['body']['data'], ['created' => 0, 'updated' => 0, 'unchanged' => 0]), '原样导入全部未变'); assert_eq([], $import['body']['data']['errors'], '没有跳过的行'); file_put_contents($path, 'not a spreadsheet'); $bad = request(fn () => api(ChannelsApi::class, $token)->postApiV3ChannelsChannelTermsImportsWithHttpInfo($channelId, new \SplFileObject($path))); assert_status(422, $bad, '坏文件'); unlink($path); // channel 里有术语不能删:先删术语 assert_status(200, raw('DELETE', '/v3/terms/'.$created['body']['data']['id'], null, null, $token), '删术语'); assert_status(200, request(fn () => api(ChannelsApi::class, $token)->deleteApiV3ChannelsChannelWithHttpInfo($channelId)), '删临时 channel'); });