From c5e27145611f0dff99e1a5214d821b08682b3838 Mon Sep 17 00:00:00 2001 From: sink Date: Wed, 5 Jan 2022 11:47:34 +0800 Subject: [PATCH 1/3] refactor: #40 set project name once --- README.md | 9 +- src/Base.php | 11 +- src/Client.php | 97 +++++++++++++ src/Core.php | 42 ------ src/GitBranch.php | 2 +- src/Issue.php | 12 +- src/Iteration.php | 12 +- src/ProjectSetting.php | 10 +- tests/Acceptance/ArtifactTest.php | 10 +- tests/Acceptance/GitBranchTest.php | 2 +- tests/Acceptance/IssueTest.php | 7 +- tests/Acceptance/IterationTest.php | 8 +- tests/Acceptance/ProjectSettingTest.php | 3 +- tests/Acceptance/TestCase.php | 26 ++-- tests/TestCase.php | 16 ++- tests/Unit/ClientTest.php | 175 ++++++++++++++++++++++++ tests/Unit/CoreTest.php | 81 ----------- tests/Unit/GitBranchTest.php | 4 +- tests/Unit/IssueTest.php | 39 +++--- tests/Unit/IterationTest.php | 57 +++----- tests/Unit/ProjectSettingTest.php | 17 +-- 21 files changed, 379 insertions(+), 261 deletions(-) create mode 100644 src/Client.php delete mode 100644 src/Core.php create mode 100644 tests/Unit/ClientTest.php delete mode 100644 tests/Unit/CoreTest.php diff --git a/README.md b/README.md index d090538..fe641a8 100644 --- a/README.md +++ b/README.md @@ -20,12 +20,15 @@ composer require coding/sdk require 'vendor/autoload.php'; +use Coding\Client; use Coding\Iteration; -$iteration = new Iteration('c127894e5a851cef22dc317f882dfb9ca6054321'); -$projectName = 'project-foo'; +$client = new Client(); +$client->setProjectName('project-foo'); +$client->setProjectToken('c127894e5a851cef22dc317f882dfb9ca6054321'); + +$iteration = new Iteration($client); $result = $iteration->create([ - 'ProjectName' => $projectName, 'Name' => 'Sprint 1', ]); echo "https://my-team.coding.net/p/{$projectName}/iterations/${result['Code']}/issues\n"; diff --git a/src/Base.php b/src/Base.php index 3ef6b70..c7e11cb 100644 --- a/src/Base.php +++ b/src/Base.php @@ -7,16 +7,11 @@ abstract class Base { - protected Core $core; + protected Client $client; - public function __construct(string $token = '', Core $core = null) + public function __construct(Client $client = null) { - $this->core = $core ?? new Core($token); - } - - public function setToken(string $token) - { - $this->core->setToken($token); + $this->client = $client ?? new Client(); } protected function validate(array $data, array $rules) diff --git a/src/Client.php b/src/Client.php new file mode 100644 index 0000000..24ff60a --- /dev/null +++ b/src/Client.php @@ -0,0 +1,97 @@ +config = array_merge([ + 'api_url' => self::API_URL, + 'personal_token' => '', + 'project_name' => '', + 'project_token' => '', + ], $config); + } + + public function setHttpClient(ClientInterface $http): void + { + $this->http = $http; + } + + public function getHttpClient(): ClientInterface + { + if (null === $this->http) { + $this->http = new GuzzleClient(); + } + + return $this->http; + } + + public function request(string $action, array $data) + { + $params = ['Action' => $action]; + $response = $this->getHttpClient()->request('POST', $this->config['api_url'], [ + 'headers' => [ + 'Accept' => 'application/json', + 'Authorization' => 'token ' . $this->getToken(), + 'Content-Type' => 'application/json' + ], + 'json' => array_merge($params, $data), + ]); + $result = json_decode($response->getBody(), true); + if (isset($result['Response']['Error']['Message'])) { + throw new ApiError($result['Response']['Error']['Message']); + } + return $result['Response']; + } + + public function requestProjectApi(string $action, array $data = []) + { + if (empty($this->config['project_name'])) { + throw new ValidationException('Need set project name first.'); + } + $data['ProjectName'] = $this->config['project_name']; + return $this->request($action, $data); + } + + public function setPersonalToken(string $token) + { + $this->config['personal_token'] = $token; + } + + public function setProjectName(string $projectName) + { + $this->config['project_name'] = $projectName; + } + + public function setProjectToken(string $token) + { + $this->config['project_token'] = $token; + } + + private function getToken() + { + if ($this->usePersonalToken) { + if (empty($this->config['personal_token'])) { + throw new InvalidArgumentException('Need set project token first.'); + } + return $this->config['personal_token']; + } + return !empty($this->config['project_token']) ? + $this->config['project_token'] : $this->config['personal_token']; + } +} diff --git a/src/Core.php b/src/Core.php deleted file mode 100644 index 6001514..0000000 --- a/src/Core.php +++ /dev/null @@ -1,42 +0,0 @@ -token = $token; - $this->client = $client ?? new Client(); - } - - public function request(string $action, $data) - { - $response = $this->client->request('POST', 'https://e.coding.net/open-api', [ - 'headers' => [ - 'Accept' => 'application/json', - 'Authorization' => "token $this->token", - 'Content-Type' => 'application/json' - ], - 'json' => array_merge([ - 'Action' => $action, - ], $data), - ]); - $result = json_decode($response->getBody(), true); - if (isset($result['Response']['Error']['Message'])) { - throw new ApiError($result['Response']['Error']['Message']); - } - return $result['Response']; - } - - public function setToken(string $token) - { - $this->token = $token; - } -} diff --git a/src/GitBranch.php b/src/GitBranch.php index 62074b3..20c3ac1 100644 --- a/src/GitBranch.php +++ b/src/GitBranch.php @@ -15,7 +15,7 @@ public function index(array $data) 'KeyWord' => 'string', // 搜索关键字 ]); $data['DepotId'] = intval($data['DepotId']); - $response = $this->core->request('DescribeGitBranches', $data); + $response = $this->client->requestProjectApi('DescribeGitBranches', $data); return $response['Branches']; } } diff --git a/src/Issue.php b/src/Issue.php index 372a619..8a7a588 100644 --- a/src/Issue.php +++ b/src/Issue.php @@ -17,7 +17,6 @@ class Issue extends Base public function create(array $data) { $this->validate($data, [ - 'ProjectName' => 'string|required', 'Name' => 'string|required', 'Priority' => [ 'string', @@ -58,34 +57,31 @@ public function create(array $data) // 自定义属性值列表 Array of IssueCustomFieldForm 'CustomFieldValues' => 'nullable|array', ]); - $response = $this->core->request('CreateIssue', $data); + $response = $this->client->requestProjectApi('CreateIssue', $data); return $response['Issue']; } public function delete(array $data) { $this->validate($data, [ - 'ProjectName' => 'string|required', 'IssueCode' => 'integer|required', ]); - $this->core->request('DeleteIssue', $data); + $this->client->requestProjectApi('DeleteIssue', $data); return true; } public function get(array $data) { $this->validate($data, [ - 'ProjectName' => 'string|required', 'IssueCode' => 'integer|required', ]); - $response = $this->core->request('DescribeIssue', $data); + $response = $this->client->requestProjectApi('DescribeIssue', $data); return $response['Issue']; } public function update(array $data) { $this->validate($data, [ - 'ProjectName' => 'string|required', 'IssueCode' => 'integer', 'ParentCode' => 'nullable|integer', 'Name' => 'nullable|string', @@ -118,7 +114,7 @@ public function update(array $data) // 自定义属性值列表 Array of IssueCustomFieldForm 'CustomFieldValues' => 'nullable|array', ]); - $response = $this->core->request('ModifyIssue', $data); + $response = $this->client->requestProjectApi('ModifyIssue', $data); return $response['Issue']; } } diff --git a/src/Iteration.php b/src/Iteration.php index f9a23bb..e7dcba5 100644 --- a/src/Iteration.php +++ b/src/Iteration.php @@ -7,31 +7,28 @@ class Iteration extends Base public function create(array $data) { $this->validate($data, [ - 'ProjectName' => 'string|required', 'Name' => 'string|required', 'Goal' => 'nullable|string', 'Assignee' => 'nullable|integer', 'StartAt' => 'nullable|date_format:Y-m-d', 'EndAt' => 'nullable|date_format:Y-m-d|after:StartAt', ]); - $response = $this->core->request('CreateIteration', $data); + $response = $this->client->requestProjectApi('CreateIteration', $data); return $response['Iteration']; } public function get(array $data) { $this->validate($data, [ - 'ProjectName' => 'string|required', 'IterationCode' => 'integer|required', ]); - $response = $this->core->request('DescribeIteration', $data); + $response = $this->client->requestProjectApi('DescribeIteration', $data); return $response['Iteration']; } public function update(array $data) { $this->validate($data, [ - 'ProjectName' => 'string|required', 'IterationCode' => 'integer|required', 'Name' => 'nullable|string', 'Goal' => 'nullable|string', @@ -39,17 +36,16 @@ public function update(array $data) 'StartAt' => 'nullable|date_format:Y-m-d', 'EndAt' => 'nullable|date_format:Y-m-d|after:StartAt', ]); - $response = $this->core->request('ModifyIteration', $data); + $response = $this->client->requestProjectApi('ModifyIteration', $data); return $response['Iteration']; } public function delete(array $data) { $this->validate($data, [ - 'ProjectName' => 'string|required', 'IterationCode' => 'integer|required', ]); - $this->core->request('DeleteIteration', $data); + $this->client->requestProjectApi('DeleteIteration', $data); return true; } } diff --git a/src/ProjectSetting.php b/src/ProjectSetting.php index 38f8fba..a9a2be1 100644 --- a/src/ProjectSetting.php +++ b/src/ProjectSetting.php @@ -6,26 +6,22 @@ class ProjectSetting extends Base { - public function getIssueTypes(array $data) + public function getIssueTypes() { - $this->validate($data, [ - 'ProjectName' => 'string|required', - ]); - $response = $this->core->request('DescribeProjectIssueTypeList', $data); + $response = $this->client->requestProjectApi('DescribeProjectIssueTypeList'); return $response['IssueTypes']; } public function getIssueStatuses(array $data) { $this->validate($data, [ - 'ProjectName' => 'string|required', 'IssueType' => [ 'required_without:IssueTypeId', Rule::in(Issue::TYPE), ], 'IssueTypeId' => 'nullable|integer', ]); - $response = $this->core->request('DescribeProjectIssueStatusList', $data); + $response = $this->client->requestProjectApi('DescribeProjectIssueStatusList', $data); return $response['ProjectIssueStatusList']; } } diff --git a/tests/Acceptance/ArtifactTest.php b/tests/Acceptance/ArtifactTest.php index f0d6027..e344828 100644 --- a/tests/Acceptance/ArtifactTest.php +++ b/tests/Acceptance/ArtifactTest.php @@ -2,9 +2,7 @@ namespace Tests\Acceptance; -use Coding\Core; -use Coding\Issue; -use GuzzleHttp\Client; +use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Psr7; class ArtifactTest extends TestCase @@ -16,14 +14,14 @@ public function testUploadAndDownload() $package = 'status.txt'; $version = date('Ymd.Hi.s', time()); file_put_contents($package, $version); - $client = new Client(); + $http = new GuzzleClient(); $body = Psr7\Utils::tryFopen($package, 'r'); $url = "https://${teamDomain}-generic.pkg.coding.net/${projectName}/generic/${package}?version=${version}"; $auth = [ getenv('CODING_USERNAME'), getenv('CODING_PASSWORD'), ]; - $response = $client->request('PUT', $url, [ + $response = $http->request('PUT', $url, [ 'auth' => $auth, 'body' => $body, ]); @@ -31,7 +29,7 @@ public function testUploadAndDownload() // Download $tmpfname = tempnam(sys_get_temp_dir(), $package); - $client->request('GET', $url, [ + $http->request('GET', $url, [ 'auth' => $auth, 'sink' => $tmpfname, ]); diff --git a/tests/Acceptance/GitBranchTest.php b/tests/Acceptance/GitBranchTest.php index c9a5981..15ee1d3 100644 --- a/tests/Acceptance/GitBranchTest.php +++ b/tests/Acceptance/GitBranchTest.php @@ -12,7 +12,7 @@ public function testIndex() $data = [ 'DepotId' => getenv('CODING_DEPOT_ID'), ]; - $branch = new GitBranch($this->token); + $branch = new GitBranch($this->client); $result = $branch->index($data); $names = []; foreach ($result as $branch) { diff --git a/tests/Acceptance/IssueTest.php b/tests/Acceptance/IssueTest.php index 67aedbe..7fa8aef 100644 --- a/tests/Acceptance/IssueTest.php +++ b/tests/Acceptance/IssueTest.php @@ -11,19 +11,17 @@ class IssueTest extends TestCase public function testCrud() { $data = [ - 'ProjectName' => $this->projectName, 'Name' => $this->faker->sentence, 'Priority' => $this->faker->randomElement(Issue::PRIORITY), 'Type' => 'DEFECT', ]; - $issue = new Issue($this->token); - $projectSetting = new ProjectSetting($this->token); + $issue = new Issue($this->client); + $projectSetting = new ProjectSetting($this->client); $result = $issue->create($data); $this->assertTrue(is_numeric($result['Code'])); $params = [ - 'ProjectName' => $this->projectName, 'IssueCode' => $result['Code'], ]; $result = $issue->get($params); @@ -31,7 +29,6 @@ public function testCrud() $this->assertEmpty($result['StoryPoint']); $statuses = $projectSetting->getIssueStatuses([ - 'ProjectName' => $this->projectName, 'IssueTypeId' => $result['IssueTypeId'], ]); diff --git a/tests/Acceptance/IterationTest.php b/tests/Acceptance/IterationTest.php index f6cead6..5008ff9 100644 --- a/tests/Acceptance/IterationTest.php +++ b/tests/Acceptance/IterationTest.php @@ -8,17 +8,15 @@ class IterationTest extends TestCase { public function testCrud() { - $data = [ - 'ProjectName' => $this->projectName, + $params = [ 'Name' => $this->faker->sentence(2), ]; - $iteration = new Iteration($this->token); - $createResult = $iteration->create($data); + $iteration = new Iteration($this->client); + $createResult = $iteration->create($params); $this->assertTrue(is_numeric($createResult['Code'])); $params = [ - 'ProjectName' => $this->projectName, 'IterationCode' => $createResult['Code'], ]; $getResult = $iteration->get($params); diff --git a/tests/Acceptance/ProjectSettingTest.php b/tests/Acceptance/ProjectSettingTest.php index 2049f45..2980f5e 100644 --- a/tests/Acceptance/ProjectSettingTest.php +++ b/tests/Acceptance/ProjectSettingTest.php @@ -9,9 +9,8 @@ class ProjectSettingTest extends TestCase { public function testGetIssueStatuses() { - $projectSetting = new ProjectSetting($this->token); + $projectSetting = new ProjectSetting($this->client); $result = $projectSetting->getIssueStatuses([ - 'ProjectName' => $this->projectName, 'IssueType' => 'DEFECT', ]); $this->assertEquals('DEFECT', $result[0]['IssueType']); diff --git a/tests/Acceptance/TestCase.php b/tests/Acceptance/TestCase.php index 21f6bef..7303b06 100644 --- a/tests/Acceptance/TestCase.php +++ b/tests/Acceptance/TestCase.php @@ -2,27 +2,37 @@ namespace Tests\Acceptance; -use Coding\Core; +use Coding\Client; use InvalidArgumentException; use Faker\Factory; use Faker\Generator; -use Mockery\Mock; use PHPUnit\Framework\TestCase as PhpUnitTestBase; class TestCase extends PhpUnitTestBase { protected Generator $faker; - protected string $token; + protected Client $client; protected string $projectName; protected function setUp(): void { parent::setUp(); - $this->faker = Factory::create(); - $this->token = getenv('CODING_TOKEN'); - $this->projectName = getenv('CODING_PROJECT_NAME'); - if (empty($this->token) || empty($this->projectName)) { - throw new InvalidArgumentException('Please set CODING_TOKEN and CODING_PROJECT_NAME environment variables'); + $personalToken = getenv('CODING_PERSONAL_TOKEN'); + $projectName = getenv('CODING_PROJECT_NAME'); + $projectToken = getenv('CODING_PROJECT_TOKEN'); + if (empty($personalToken) && empty($projectToken)) { + throw new InvalidArgumentException('Please set CODING_PERSONAL_TOKEN or CODING_PROJECT_TOKEN' + . ' environment variables'); + } + if (empty($projectName)) { + throw new InvalidArgumentException('Please set CODING_PROJECT_NAME environment variable'); } + + $this->faker = Factory::create(); + $this->client = new Client(); + $this->client->setPersonalToken($personalToken); + $this->client->setProjectName($projectName); + $this->client->setProjectToken($projectToken); + $this->projectName = $projectName; } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 0777867..178762e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,7 +2,7 @@ namespace Tests; -use Coding\Core; +use Coding\Client; use Faker\Factory; use Faker\Generator; use Mockery\Mock; @@ -11,19 +11,21 @@ class TestCase extends PhpUnitTestBase { protected Generator $faker; - protected string $token; + protected string $personalToken; protected string $projectName; - protected bool $needCoreMock = true; - protected $coreMock; + protected bool $needClientMock = true; + protected $clientMock; protected function setUp(): void { parent::setUp(); $this->faker = Factory::create(); - $this->token = $this->faker->md5; + $this->personalToken = $this->faker->md5; $this->projectName = $this->faker->slug; - if ($this->needCoreMock) { - $this->coreMock = \Mockery::mock(Core::class, [])->makePartial(); + if ($this->needClientMock) { + $this->clientMock = \Mockery::mock(Client::class, [])->makePartial(); + $this->clientMock->setPersonalToken($this->personalToken); + $this->clientMock->setProjectName($this->projectName); } } diff --git a/tests/Unit/ClientTest.php b/tests/Unit/ClientTest.php new file mode 100644 index 0000000..d1a1eec --- /dev/null +++ b/tests/Unit/ClientTest.php @@ -0,0 +1,175 @@ +httpMock = $this->getMockBuilder(GuzzleClient::class)->getMock(); + } + + public function testRequestSuccess() + { + $responseBody = file_get_contents($this->dataPath('CreateIterationResponse.json')); + $action = $this->faker->word; + $number = ($this->faker->randomDigitNotZero()); + $data = array_combine($this->faker->words($number), $this->faker->words($number)); + + $this->httpMock->expects($this->once()) + ->method('request') + ->with( + 'POST', + 'https://e.coding.net/open-api', + [ + 'headers' => [ + 'Accept' => 'application/json', + 'Authorization' => "token $this->personalToken", + 'Content-Type' => 'application/json' + ], + 'json' => array_merge([ + 'Action' => $action, + 'ProjectName' => $this->projectName, + ], $data) + ] + ) + ->willReturn(new Response(200, [], $responseBody)); + $client = new Client(); + $client->setHttpClient($this->httpMock); + $client->setProjectName($this->projectName); + $client->setPersonalToken($this->personalToken); + $result = $client->requestProjectApi($action, $data); + $this->assertEquals(json_decode($responseBody, true)['Response'], $result); + } + + public function testRequestFailed() + { + $responseBody = file_get_contents($this->dataPath('CreateIterationResponseError.json')); + $action = $this->faker->word; + $number = ($this->faker->randomDigitNotZero()); + $data = array_combine($this->faker->words($number), $this->faker->words($number)); + + $this->httpMock->expects($this->once()) + ->method('request') + ->with( + 'POST', + 'https://e.coding.net/open-api', + [ + 'headers' => [ + 'Accept' => 'application/json', + 'Authorization' => "token $this->personalToken", + 'Content-Type' => 'application/json' + ], + 'json' => array_merge([ + 'Action' => $action, + ], $data) + ] + ) + ->willReturn(new Response(200, [], $responseBody)); + $client = new Client(); + $client->setHttpClient($this->httpMock); + $client->setPersonalToken($this->personalToken); + $this->expectException(ApiError::class); + $this->expectExceptionMessage(json_decode($responseBody, true)['Response']['Error']['Message']); + $client->request($action, $data, false); + } + + public function testValidateProjectNameFailed() + { + $action = $this->faker->word; + $number = ($this->faker->randomDigitNotZero()); + $data = array_combine($this->faker->words($number), $this->faker->words($number)); + + $this->httpMock->expects($this->never())->method('request'); + $client = new Client(); + $client->setHttpClient($this->httpMock); + $client->setPersonalToken($this->personalToken); + $this->expectException(ValidationException::class); + $this->expectExceptionMessage('Need set project name first.'); + $client->requestProjectApi($action, $data); + } + + public function testUseProjectToken() + { + $responseBody = file_get_contents($this->dataPath('CreateIterationResponse.json')); + $action = $this->faker->word; + $number = ($this->faker->randomDigitNotZero()); + $data = array_combine($this->faker->words($number), $this->faker->words($number)); + $projectToken = $this->faker->md5; + + $this->httpMock->expects($this->once()) + ->method('request') + ->with( + 'POST', + 'https://e.coding.net/open-api', + [ + 'headers' => [ + 'Accept' => 'application/json', + 'Authorization' => "token $projectToken", + 'Content-Type' => 'application/json' + ], + 'json' => array_merge([ + 'Action' => $action, + 'ProjectName' => $this->projectName, + ], $data) + ] + ) + ->willReturn(new Response(200, [], $responseBody)); + $client = new Client(); + $client->setHttpClient($this->httpMock); + $client->setPersonalToken($this->personalToken); + $client->setProjectName($this->projectName); + $client->setProjectToken($projectToken); + $result = $client->requestProjectApi($action, $data); + $this->assertEquals(json_decode($responseBody, true)['Response'], $result); + } + + + public function testUsePersonalToken() + { + $responseBody = file_get_contents($this->dataPath('CreateIterationResponse.json')); + $action = $this->faker->word; + $number = ($this->faker->randomDigitNotZero()); + $data = array_combine($this->faker->words($number), $this->faker->words($number)); + $projectToken = $this->faker->md5; + + $this->httpMock->expects($this->once()) + ->method('request') + ->with( + 'POST', + 'https://e.coding.net/open-api', + [ + 'headers' => [ + 'Accept' => 'application/json', + 'Authorization' => "token $this->personalToken", + 'Content-Type' => 'application/json' + ], + 'json' => array_merge([ + 'Action' => $action, + 'ProjectName' => $this->projectName, + ], $data) + ] + ) + ->willReturn(new Response(200, [], $responseBody)); + $client = new Client(); + $client->setHttpClient($this->httpMock); + $client->setPersonalToken($this->personalToken); + $client->setProjectName($this->projectName); + $client->setProjectToken($projectToken); + $client->usePersonalToken = true; + $result = $client->requestProjectApi($action, $data); + $this->assertEquals(json_decode($responseBody, true)['Response'], $result); + } +} diff --git a/tests/Unit/CoreTest.php b/tests/Unit/CoreTest.php deleted file mode 100644 index 499a015..0000000 --- a/tests/Unit/CoreTest.php +++ /dev/null @@ -1,81 +0,0 @@ -clientMock = $this->getMockBuilder(Client::class)->getMock(); - } - - public function testRequestSuccess() - { - $responseBody = file_get_contents($this->dataPath('CreateIterationResponse.json')); - $action = $this->faker->word; - $number = ($this->faker->randomDigitNotZero()); - $data = array_combine($this->faker->words($number), $this->faker->words($number)); - - $this->clientMock->expects($this->once()) - ->method('request') - ->with( - 'POST', - 'https://e.coding.net/open-api', - [ - 'headers' => [ - 'Accept' => 'application/json', - 'Authorization' => "token $this->token", - 'Content-Type' => 'application/json' - ], - 'json' => array_merge([ - 'Action' => $action, - ], $data) - ] - ) - ->willReturn(new Response(200, [], $responseBody)); - $core = new Core($this->token, $this->clientMock); - $result = $core->request($action, $data); - $this->assertEquals(json_decode($responseBody, true)['Response'], $result); - } - - public function testRequestFailed() - { - $responseBody = file_get_contents($this->dataPath('CreateIterationResponseError.json')); - $action = $this->faker->word; - $number = ($this->faker->randomDigitNotZero()); - $data = array_combine($this->faker->words($number), $this->faker->words($number)); - - $this->clientMock->expects($this->once()) - ->method('request') - ->with( - 'POST', - 'https://e.coding.net/open-api', - [ - 'headers' => [ - 'Accept' => 'application/json', - 'Authorization' => "token $this->token", - 'Content-Type' => 'application/json' - ], - 'json' => array_merge([ - 'Action' => $action, - ], $data) - ] - ) - ->willReturn(new Response(200, [], $responseBody)); - $core = new Core('', $this->clientMock); - $core->setToken($this->token); - $this->expectException(ApiError::class); - $this->expectExceptionMessage(json_decode($responseBody, true)['Response']['Error']['Message']); - $core->request($action, $data); - } -} diff --git a/tests/Unit/GitBranchTest.php b/tests/Unit/GitBranchTest.php index b72223e..955c69a 100644 --- a/tests/Unit/GitBranchTest.php +++ b/tests/Unit/GitBranchTest.php @@ -20,12 +20,12 @@ public function testIndex() 'PageSize' => $this->faker->randomNumber(), 'KeyWord' => $this->faker->word(), ]; - $this->coreMock->shouldReceive('request')->times(1)->withArgs([ + $this->clientMock->shouldReceive('requestProjectApi')->times(1)->withArgs([ 'DescribeGitBranches', $data ])->andReturn($response); - $branch = new GitBranch($this->token, $this->coreMock); + $branch = new GitBranch($this->clientMock); $result = $branch->index($data); $this->assertEquals($response['Branches'], $result); } diff --git a/tests/Unit/IssueTest.php b/tests/Unit/IssueTest.php index 954d4a6..2c46ea1 100644 --- a/tests/Unit/IssueTest.php +++ b/tests/Unit/IssueTest.php @@ -8,6 +8,14 @@ class IssueTest extends TestCase { + private Issue $issue; + + protected function setUp(): void + { + parent::setUp(); + $this->issue = new Issue($this->clientMock); + } + public function testCreateSuccessWithOnlyRequiredParams() { $response = json_decode( @@ -15,18 +23,16 @@ public function testCreateSuccessWithOnlyRequiredParams() true )['Response']; $data = [ - 'ProjectName' => $this->projectName, 'Name' => $this->faker->sentence, 'Priority' => $this->faker->randomElement(Issue::PRIORITY), 'Type' => $this->faker->randomElement(Issue::TYPE), ]; - $this->coreMock->shouldReceive('request')->times(1)->withArgs([ + $this->clientMock->shouldReceive('requestProjectApi')->times(1)->withArgs([ 'CreateIssue', $data ])->andReturn($response); - $issue = new Issue($this->token, $this->coreMock); - $result = $issue->create($data); + $result = $this->issue->create($data); $this->assertEquals($response['Issue'], $result); } @@ -38,7 +44,6 @@ public function testCreateSuccessWithAllParams() )['Response']; $startDate = $this->faker->date(); $data = [ - 'ProjectName' => $this->projectName, 'Name' => $this->faker->sentence, 'Priority' => $this->faker->randomElement(Issue::PRIORITY), 'Type' => $this->faker->randomElement(Issue::TYPE), @@ -80,14 +85,12 @@ public function testCreateSuccessWithAllParams() ], ], ]; - $this->coreMock->shouldReceive('request')->times(1)->withArgs([ + $this->clientMock->shouldReceive('requestProjectApi')->times(1)->withArgs([ 'CreateIssue', $data ])->andReturn($response); - $issue = new Issue('', $this->coreMock); - $issue->setToken($this->token); - $result = $issue->create($data); + $result = $this->issue->create($data); $this->assertEquals($response['Issue'], $result); } @@ -98,16 +101,14 @@ public function testDelete() true )['Response']; $data = [ - 'ProjectName' => $this->projectName, 'IssueCode' => $this->faker->randomNumber(), ]; - $this->coreMock->shouldReceive('request')->times(1)->withArgs([ + $this->clientMock->shouldReceive('requestProjectApi')->times(1)->withArgs([ 'DeleteIssue', $data ])->andReturn($response); - $issue = new Issue($this->token, $this->coreMock); - $this->assertTrue($issue->delete($data)); + $this->assertTrue($this->issue->delete($data)); } public function testGet() @@ -117,16 +118,14 @@ public function testGet() true )['Response']; $data = [ - 'ProjectName' => $this->projectName, 'IssueCode' => $this->faker->randomNumber(), ]; - $this->coreMock->shouldReceive('request')->times(1)->withArgs([ + $this->clientMock->shouldReceive('requestProjectApi')->times(1)->withArgs([ 'DescribeIssue', $data ])->andReturn($response); - $issue = new Issue($this->token, $this->coreMock); - $result = $issue->get($data); + $result = $this->issue->get($data); $this->assertEquals($response['Issue'], $result); } @@ -137,17 +136,15 @@ public function testUpdate() true )['Response']; $data = [ - 'ProjectName' => $this->projectName, 'IssueCode' => $this->faker->randomNumber(), 'StoryPoint' => "1.0", ]; - $this->coreMock->shouldReceive('request')->times(1)->withArgs([ + $this->clientMock->shouldReceive('requestProjectApi')->times(1)->withArgs([ 'ModifyIssue', $data ])->andReturn($response); - $issue = new Issue($this->token, $this->coreMock); - $result = $issue->update($data); + $result = $this->issue->update($data); $this->assertEquals($response['Issue'], $result); } } diff --git a/tests/Unit/IterationTest.php b/tests/Unit/IterationTest.php index 97ec9b5..bce38eb 100644 --- a/tests/Unit/IterationTest.php +++ b/tests/Unit/IterationTest.php @@ -9,6 +9,14 @@ class IterationTest extends TestCase { + private Iteration $iteration; + + protected function setUp(): void + { + parent::setUp(); + $this->iteration = new Iteration($this->clientMock); + } + public function testCreateSuccessWithOnlyRequiredParams() { $response = json_decode( @@ -16,16 +24,14 @@ public function testCreateSuccessWithOnlyRequiredParams() true )['Response']; $data = [ - 'ProjectName' => $this->projectName, 'Name' => $this->faker->title, ]; - $this->coreMock->shouldReceive('request')->times(1)->withArgs([ + $this->clientMock->shouldReceive('requestProjectApi')->times(1)->withArgs([ 'CreateIteration', $data ])->andReturn($response); - $iteration = new Iteration($this->token, $this->coreMock); - $result = $iteration->create($data); + $result = $this->iteration->create($data); $this->assertEquals($response['Iteration'], $result); } @@ -36,17 +42,15 @@ public function testCreateSuccessWithRequiredParamsAndNull() true )['Response']; $data = [ - 'ProjectName' => $this->projectName, 'Name' => $this->faker->title, 'Goal' => null, ]; - $this->coreMock->shouldReceive('request')->times(1)->withArgs([ + $this->clientMock->shouldReceive('requestProjectApi')->times(1)->withArgs([ 'CreateIteration', $data ])->andReturn($response); - $iteration = new Iteration($this->token, $this->coreMock); - $result = $iteration->create($data); + $result = $this->iteration->create($data); $this->assertEquals($response['Iteration'], $result); } @@ -58,49 +62,39 @@ public function testCreateSuccessWithAllParams() )['Response']; $startAt = $this->faker->date(); $data = [ - 'ProjectName' => $this->projectName, 'Name' => $this->faker->title, 'Goal' => $this->faker->sentence, 'Assignee' => $this->faker->randomNumber(), 'StartAt' => $startAt, 'EndAt' => date('Y-m-d', strtotime($startAt) + $this->faker->randomDigitNotZero() * 86400), ]; - $this->coreMock->shouldReceive('request')->times(1)->withArgs([ + $this->clientMock->shouldReceive('requestProjectApi')->times(1)->withArgs([ 'CreateIteration', $data ])->andReturn($response); - $iteration = new Iteration('', $this->coreMock); - $iteration->setToken($this->token); - $result = $iteration->create($data); + $result = $this->iteration->create($data); $this->assertEquals($response['Iteration'], $result); } public function testCreateFailedRequired() { - $data = [ - 'ProjectName' => $this->projectName, - ]; - - $iteration = new Iteration($this->token); $this->expectException(ValidationException::class); $this->expectExceptionMessage('The name field is required.'); - $iteration->create($data); + $this->iteration->create([]); } public function testCreateFailedBefore() { $data = [ - 'ProjectName' => $this->projectName, 'Name' => $this->faker->title, 'StartAt' => '2021-10-23', 'EndAt' => '2021-10-22', ]; - $iteration = new Iteration($this->token); $this->expectException(ValidationException::class); $this->expectExceptionMessage('The end at must be a date after start at.'); - $iteration->create($data); + $this->iteration->create($data); } public function testGet() @@ -110,16 +104,14 @@ public function testGet() true )['Response']; $data = [ - 'ProjectName' => $this->projectName, 'IterationCode' => $this->faker->randomNumber(), ]; - $this->coreMock->shouldReceive('request')->times(1)->withArgs([ + $this->clientMock->shouldReceive('requestProjectApi')->times(1)->withArgs([ 'DescribeIteration', $data ])->andReturn($response); - $iteration = new Iteration($this->token, $this->coreMock); - $result = $iteration->get($data); + $result = $this->iteration->get($data); $this->assertEquals($response['Iteration'], $result); } @@ -130,18 +122,15 @@ public function testUpdate() true )['Response']; $data = [ - 'ProjectName' => $this->projectName, 'IterationCode' => $this->faker->randomNumber(), 'Goal' => $this->faker->sentence, ]; - $this->coreMock->shouldReceive('request')->times(1)->withArgs([ + $this->clientMock->shouldReceive('requestProjectApi')->times(1)->withArgs([ 'ModifyIteration', $data ])->andReturn($response); - $iteration = new Iteration('', $this->coreMock); - $iteration->setToken($this->token); - $result = $iteration->update($data); + $result = $this->iteration->update($data); $this->assertEquals($response['Iteration'], $result); } @@ -152,15 +141,13 @@ public function testDelete() true )['Response']; $data = [ - 'ProjectName' => $this->projectName, 'IterationCode' => $this->faker->randomNumber(), ]; - $this->coreMock->shouldReceive('request')->times(1)->withArgs([ + $this->clientMock->shouldReceive('requestProjectApi')->times(1)->withArgs([ 'DeleteIteration', $data ])->andReturn($response); - $issue = new Iteration($this->token, $this->coreMock); - $this->assertTrue($issue->delete($data)); + $this->assertTrue($this->iteration->delete($data)); } } diff --git a/tests/Unit/ProjectSettingTest.php b/tests/Unit/ProjectSettingTest.php index 2491b41..d814494 100644 --- a/tests/Unit/ProjectSettingTest.php +++ b/tests/Unit/ProjectSettingTest.php @@ -14,16 +14,12 @@ public function testGetIssueTypes() file_get_contents($this->dataPath('DescribeProjectIssueTypeListResponse.json')), true )['Response']; - $data = [ - 'ProjectName' => $this->projectName, - ]; - $this->coreMock->shouldReceive('request')->times(1)->withArgs([ - 'DescribeProjectIssueTypeList', - $data + $this->clientMock->shouldReceive('requestProjectApi')->times(1)->withArgs([ + 'DescribeProjectIssueTypeList' ])->andReturn($response); - $projectSetting = new ProjectSetting($this->token, $this->coreMock); - $result = $projectSetting->getIssueTypes($data); + $projectSetting = new ProjectSetting($this->clientMock); + $result = $projectSetting->getIssueTypes(); $this->assertEquals($response['IssueTypes'], $result); } @@ -34,16 +30,15 @@ public function testGetIssueStatuses() true )['Response']; $data = [ - 'ProjectName' => $this->projectName, 'IssueType' => $this->faker->randomElement(Issue::TYPE), 'IssueTypeId' => $this->faker->randomNumber(), ]; - $this->coreMock->shouldReceive('request')->times(1)->withArgs([ + $this->clientMock->shouldReceive('requestProjectApi')->times(1)->withArgs([ 'DescribeProjectIssueStatusList', $data ])->andReturn($response); - $projectSetting = new ProjectSetting($this->token, $this->coreMock); + $projectSetting = new ProjectSetting($this->clientMock); $result = $projectSetting->getIssueStatuses($data); $this->assertEquals($response['ProjectIssueStatusList'], $result); } From ffbcb27a29396db9cdbb7d768046fda5b1ba3ef5 Mon Sep 17 00:00:00 2001 From: sink Date: Wed, 5 Jan 2022 12:12:40 +0800 Subject: [PATCH 2/3] docs: #40 project name --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fe641a8..53d33c2 100644 --- a/README.md +++ b/README.md @@ -24,14 +24,16 @@ use Coding\Client; use Coding\Iteration; $client = new Client(); -$client->setProjectName('project-foo'); +$projectName = 'project-foo'; +$client->setProjectName($projectName); $client->setProjectToken('c127894e5a851cef22dc317f882dfb9ca6054321'); $iteration = new Iteration($client); $result = $iteration->create([ 'Name' => 'Sprint 1', ]); -echo "https://my-team.coding.net/p/{$projectName}/iterations/${result['Code']}/issues\n"; +$teamDomain = 'my-team'; +echo "https://${teamDomain}.coding.net/p/{$projectName}/iterations/${result['Code']}/issues\n"; ``` ## Resources From 18ff9a91bc8fbd76dee9d009c1a38ce4059e0b36 Mon Sep 17 00:00:00 2001 From: sink Date: Wed, 5 Jan 2022 13:27:38 +0800 Subject: [PATCH 3/3] ci: #40 project token --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7a7ee98..78b76b4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,10 +38,11 @@ jobs: - name: Acceptance Test run: ./vendor/bin/phpunit tests/Acceptance env: - CODING_TOKEN: ${{ secrets.CODING_TOKEN }} + CODING_PERSONAL_TOKEN: ${{ secrets.CODING_PERSONAL_TOKEN }} CODING_PASSWORD: ${{ secrets.CODING_PASSWORD }} CODING_USERNAME: ${{ secrets.CODING_USERNAME }} CODING_TEAM_DOMAIN: ${{ secrets.CODING_TEAM_DOMAIN }} + CODING_PROJECT_TOKEN: ${{ secrets.CODING_PROJECT_TOKEN }} CODING_PROJECT_NAME: ${{ secrets.CODING_PROJECT_NAME }} CODING_DEPOT_ID: ${{ secrets.CODING_DEPOT_ID }}