Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bb900e8

Browse files
feat: #1 core auth and error
1 parent 6705187 commit bb900e8

File tree

8 files changed

+207
-0
lines changed

8 files changed

+207
-0
lines changed

‎.gitignore‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.phpunit.result.cache
2+
composer.lock
3+
coverage.xml
4+
/.idea/
5+
/vendor/

‎composer.json‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "coding/sdk",
3+
"description": "CODING.net SDK for PHP",
4+
"type": "library",
5+
"require": {
6+
"php": ">=7.4",
7+
"ext-json": "*",
8+
"guzzlehttp/guzzle": "^7.4"
9+
},
10+
"require-dev": {
11+
"fakerphp/faker": "^1.16",
12+
"phpunit/phpunit": "^9.5"
13+
},
14+
"license": "Apache-2.0",
15+
"autoload": {
16+
"psr-4": { "Coding\\": "src/" }
17+
},
18+
"autoload-dev": {
19+
"psr-4": { "Coding\\Tests\\": "tests/" }
20+
}
21+
}

‎src/ApiError.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Coding;
4+
5+
use Exception;
6+
7+
class ApiError extends Exception
8+
{
9+
}

‎src/Core.php‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Coding;
4+
5+
use GuzzleHttp\Client;
6+
7+
class Core
8+
{
9+
protected string $token;
10+
protected Client $client;
11+
12+
public function __construct(string $token, Client $client = null)
13+
{
14+
$this->token = $token;
15+
$this->client = $client ?? new Client();
16+
}
17+
18+
public function request(string $action, $data)
19+
{
20+
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
21+
'headers' => [
22+
'Accept' => 'application/json',
23+
'Authorization' => "token $this->token",
24+
'Content-Type' => 'application/json'
25+
],
26+
'json' => array_merge([
27+
'Action' => $action,
28+
], $data),
29+
]);
30+
$result = json_decode($response->getBody(), true);
31+
if (isset($result['Response']['Error']['Message'])) {
32+
throw new ApiError($result['Response']['Error']['Message']);
33+
}
34+
return $result['Response'];
35+
}
36+
}

‎tests/CoreTest.php‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Coding\Tests;
4+
5+
use Coding\ApiError;
6+
use Coding\Core;
7+
use GuzzleHttp\Psr7\Response;
8+
9+
class CoreTest extends TestCase
10+
{
11+
public function testRequestSuccess()
12+
{
13+
$responseBody = file_get_contents($this->dataPath('CreateIterationResponse.json'));
14+
$action = $this->faker->word;
15+
$number = ($this->faker->randomDigitNotZero());
16+
$data = array_combine($this->faker->words($number), $this->faker->words($number));
17+
18+
$this->clientMock->expects($this->once())
19+
->method('request')
20+
->with(
21+
'POST',
22+
'https://e.coding.net/open-api',
23+
[
24+
'headers' => [
25+
'Accept' => 'application/json',
26+
'Authorization' => "token $this->token",
27+
'Content-Type' => 'application/json'
28+
],
29+
'json' => array_merge([
30+
'Action' => $action,
31+
], $data)
32+
]
33+
)
34+
->willReturn(new Response(200, [], $responseBody));
35+
$core = new Core($this->token, $this->clientMock);
36+
$result = $core->request($action, $data);
37+
$this->assertEquals(json_decode($responseBody, true)['Response'], $result);
38+
}
39+
40+
public function testRequestFailed()
41+
{
42+
$responseBody = file_get_contents($this->dataPath('CreateIterationResponseError.json'));
43+
$action = $this->faker->word;
44+
$number = ($this->faker->randomDigitNotZero());
45+
$data = array_combine($this->faker->words($number), $this->faker->words($number));
46+
47+
$this->clientMock->expects($this->once())
48+
->method('request')
49+
->with(
50+
'POST',
51+
'https://e.coding.net/open-api',
52+
[
53+
'headers' => [
54+
'Accept' => 'application/json',
55+
'Authorization' => "token $this->token",
56+
'Content-Type' => 'application/json'
57+
],
58+
'json' => array_merge([
59+
'Action' => $action,
60+
], $data)
61+
]
62+
)
63+
->willReturn(new Response(200, [], $responseBody));
64+
$core = new Core($this->token, $this->clientMock);
65+
$this->expectException(ApiError::class);
66+
$this->expectExceptionMessage(json_decode($responseBody, true)['Response']['Error']['Message']);
67+
$core->request($action, $data);
68+
}
69+
}

‎tests/TestCase.php‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Coding\Tests;
4+
5+
use Faker\Factory;
6+
use Faker\Generator;
7+
use GuzzleHttp\Client;
8+
use PHPUnit\Framework\TestCase as BaseTestBase;
9+
10+
class TestCase extends BaseTestBase
11+
{
12+
protected Generator $faker;
13+
protected Client $clientMock;
14+
protected string $token;
15+
protected string $projectName;
16+
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
$this->faker = Factory::create();
21+
$this->clientMock = $this->getMockBuilder(Client::class)->getMock();
22+
$this->token = $this->faker->md5;
23+
$this->projectName = $this->faker->slug;
24+
}
25+
26+
protected function dataPath($fileName): string
27+
{
28+
return implode(DIRECTORY_SEPARATOR, [
29+
__DIR__,
30+
'data',
31+
$fileName
32+
]);
33+
}
34+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"Response" : {
3+
"Iteration" : {
4+
"Assignee" : 0,
5+
"Code" : 2746,
6+
"CompletedCount" : 0,
7+
"CompletedPercent" : 0,
8+
"Completer" : 0,
9+
"CreatedAt" : 1634697259529,
10+
"Creator" : 183478,
11+
"Deleter" : 0,
12+
"EndAt" : -28800000,
13+
"Goal" : "",
14+
"Name" : "it by cli",
15+
"ProcessingCount" : 0,
16+
"StartAt" : -28800000,
17+
"Starter" : 0,
18+
"Status" : "WAIT_PROCESS",
19+
"UpdatedAt" : 1634697259529,
20+
"WaitProcessCount" : 0
21+
},
22+
"RequestId" : "58777aa6-e6e4-155a-c99f-415a33615ca6"
23+
}
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Response" : {
3+
"Error" : {
4+
"Code" : "InvalidParameterValue",
5+
"Message" : "Title Duplicated"
6+
},
7+
"RequestId" : "a323676d-51d7-40da-74fb-dea60c063b0e"
8+
}
9+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /