Skip to content

Commit 9927edb

Browse files
committed
feat: #13 create wiki by zip
1 parent 6f55417 commit 9927edb

File tree

3 files changed

+86
-20
lines changed

3 files changed

+86
-20
lines changed

app/Coding.php

+26
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,32 @@ public function upload(array $uploadToken, string $fileFullPath): bool
9191
return Storage::disk('cos')->put(basename($fileFullPath), File::get($fileFullPath));
9292
}
9393

94+
public function createWikiByZip(string $token, string $projectName, array $uploadToken, array $data)
95+
{
96+
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
97+
'headers' => [
98+
'Accept' => 'application/json',
99+
'Authorization' => "token ${token}",
100+
'Content-Type' => 'application/json'
101+
],
102+
'json' => [
103+
'Action' => 'CreateWikiByZip',
104+
'ProjectName' => $projectName,
105+
'ParentIid' => $data['ParentIid'],
106+
'FileName' => $data['FileName'],
107+
'Key' => $data['FileName'],
108+
'Time' => $uploadToken['Time'],
109+
'AuthToken' => $uploadToken['AuthToken'],
110+
],
111+
]);
112+
$result = json_decode($response->getBody(), true);
113+
if (isset($result['Response']['JobId'])) {
114+
return $result['Response'];
115+
} else {
116+
return new \Exception('createWikiByZip failed');
117+
}
118+
}
119+
94120
/**
95121
* 获取 Wiki 导入任务的进度(API 文档未展示,其实此接口已上线)
96122
*

tests/Unit/CodingTest.php

+54-20
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010

1111
class CodingTest extends TestCase
1212
{
13+
private array $uploadToken = [
14+
'AuthToken' => '65e5968b5e17d5aaa3f5d33200aca2d1911fe2ad2948b47d899d46e6da1e4',
15+
'Provide' => 'TENCENT',
16+
'SecretId' => 'AKIDU-VqQm39vRar-ZrHj1UIE5u2gYJ7gWFcG2ThwFNO9eU1HbyQlZp8vVcQ99',
17+
'SecretKey' => 'clUYSNeg2es16EILsrF6RyCD3ss6uFLX3Xgc=',
18+
'UploadLink' => 'https://coding-net-dev-file-123456.cos.ap-shanghai.myqcloud.com',
19+
'UpToken' => 'EOlMEc2x0xbrFoL9CMy7OqDl5413654938410a360a63207e30dab4655pMKmNJ3t5M-Z8bGt',
20+
'Time' => 1625579588693,
21+
'Bucket' => 'coding-net-dev-file-123456',
22+
'AppId' => '123456',
23+
'Region' => 'ap-shanghai',
24+
];
25+
1326
public function testCreateWiki()
1427
{
1528
$responseBody = file_get_contents($this->dataDir . 'coding/createWikiResponse.json');
@@ -74,18 +87,7 @@ public function testCreateUploadToken()
7487
->willReturn(new Response(200, [], $responseBody));
7588
$coding = new Coding($clientMock);
7689
$result = $coding->createUploadToken($codingToken, $codingProjectUri, $fileName);
77-
$this->assertEquals([
78-
'AuthToken' => '65e5968b5e17d5aaa3f5d33200aca2d1911fe2ad2948b47d899d46e6da1e4',
79-
'Provide' => 'TENCENT',
80-
'SecretId' => 'AKIDU-VqQm39vRar-ZrHj1UIE5u2gYJ7gWFcG2ThwFNO9eU1HbyQlZp8vVcQ99',
81-
'SecretKey' => 'clUYSNeg2es16EILsrF6RyCD3ss6uFLX3Xgc=',
82-
'UploadLink' => 'https://coding-net-dev-file-123456.cos.ap-shanghai.myqcloud.com',
83-
'UpToken' => 'EOlMEc2x0xbrFoL9CMy7OqDl5413654938410a360a63207e30dab4655pMKmNJ3t5M-Z8bGt',
84-
'Time' => 1625579588693,
85-
'Bucket' => 'coding-net-dev-file-123456',
86-
'AppId' => '123456',
87-
'Region' => 'ap-shanghai',
88-
], $result);
90+
$this->assertEquals($this->uploadToken, $result);
8991
}
9092

9193
public function testCreateMarkdownZip()
@@ -107,18 +109,11 @@ public function testCreateMarkdownZip()
107109

108110
public function testUpload()
109111
{
110-
$uploadToken = [
111-
'SecretId' => 'AKIDU-VqQm39vRar-ZrHj1UIE5u2gYJ7gWFcG2ThwFNO9eU1HbyQlZp8vVcQ99',
112-
'SecretKey' => 'clUYSNeg2es16EILsrF6RyCD3ss6uFLX3Xgc=',
113-
'Bucket' => 'coding-net-dev-file-123456',
114-
'AppId' => '123456',
115-
'Region' => 'ap-shanghai',
116-
];
117112
$file = $this->faker->filePath();
118113
Storage::fake('cos');
119114

120115
$coding = new Coding();
121-
$coding->upload($uploadToken, $file);
116+
$coding->upload($this->uploadToken, $file);
122117

123118
Storage::disk('cos')->assertExists(basename($file));
124119
}
@@ -154,4 +149,43 @@ public function testGetImportJobStatus()
154149
$result = $coding->getImportJobStatus($codingToken, $codingProjectUri, $jobId);
155150
$this->assertEquals('success', $result);
156151
}
152+
153+
public function testCreateWikiByZip()
154+
{
155+
$responseBody = file_get_contents($this->dataDir . 'coding/CreateWikiByZipResponse.json');
156+
$codingToken = $this->faker->md5;
157+
$codingProjectUri = $this->faker->slug;
158+
159+
$data = [
160+
'ParentIid' => 0,
161+
'FileName' => $this->faker->word,
162+
];
163+
$clientMock = $this->getMockBuilder(Client::class)->getMock();
164+
$clientMock->expects($this->once())
165+
->method('request')
166+
->with(
167+
'POST',
168+
'https://e.coding.net/open-api',
169+
[
170+
'headers' => [
171+
'Accept' => 'application/json',
172+
'Authorization' => "token ${codingToken}",
173+
'Content-Type' => 'application/json'
174+
],
175+
'json' => [
176+
'Action' => 'CreateWikiByZip',
177+
'ProjectName' => $codingProjectUri,
178+
'ParentIid' => $data['ParentIid'],
179+
'FileName' => $data['FileName'],
180+
'Key' => $data['FileName'],
181+
'Time' => $this->uploadToken['Time'],
182+
'AuthToken' => $this->uploadToken['AuthToken'],
183+
],
184+
]
185+
)
186+
->willReturn(new Response(200, [], $responseBody));
187+
$coding = new Coding($clientMock);
188+
$result = $coding->createWikiByZip($codingToken, $codingProjectUri, $this->uploadToken, $data);
189+
$this->assertArrayHasKey('JobId', $result);
190+
}
157191
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"Response" : {
3+
"JobId" : "a12353fa-f45b-4af2-83db-666bf9f66615",
4+
"RequestId" : "1237f454-321f-5eb6-dec1-08a25f876999"
5+
}
6+
}

0 commit comments

Comments
 (0)