Skip to content

Commit f5e224f

Browse files
committed
プロジェクト→課題→添付ファイル→コメントのAPI経由更新をテストシナリオに
1 parent 0da683b commit f5e224f

File tree

5 files changed

+255
-0
lines changed

5 files changed

+255
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace tests\Feature\API;
4+
5+
use tests\Feature\API\TestCase\TestCaseIssueApiClientForUpdate;
6+
7+
class ApiClientCrudIssueTest extends TestCaseIssueApiClientForUpdate {
8+
//class ApiClientCrudIssueTest extends TestCaseProjectApiClientForUpdate {
9+
10+
protected $admin_user;
11+
12+
public function test_create_update_delete_backlog_project_issue () {
13+
$api = $this->api;
14+
//
15+
$projectId = $this->project_id;
16+
$issueId = $this->issue_id;
17+
$issueKey = $this->issue_key;
18+
$this->assertIsInt( $projectId );
19+
$this->assertIsInt( $issueId );
20+
$this->assertNotEmpty($issueKey);
21+
// コメント+添付ファイル
22+
$com = $this->addCommentWithAttachment();
23+
$this->assertIsInt( $com->id );
24+
// コメント追加
25+
$com = $this->addComment( 'これはサンプルです。' );
26+
$this->assertIsInt( $com->id );
27+
// 課題の更新。
28+
$description = "API経由で課題を作成。通知、担当者設定。\n\n\n\n画像を貼り付ける\n\n![image][sample.jpg]";
29+
$iss = $this->updateIssue( $description );
30+
$this->assertIsInt( $iss->id );
31+
$this->assertEquals( $issueId, $iss->id );
32+
$this->assertEquals( $issueKey, $iss->issueKey );
33+
$this->assertEquals( $description, $iss->description );
34+
// コメント履歴を取る。
35+
$comment_history = $api->getCommentList( $issueId, ['order' => 'asc'] );//旧ー>新
36+
//3件.更新処理してるので取得結果は3件になる。。
37+
$this->assertEquals( 3, count( $comment_history ) );
38+
// コメント履歴一覧には、コメント以外に、課題への変更が入っている。
39+
$this->assertEquals( $comment_history[0]->changeLog[0]->field, 'attachment'/*添付ファイル作成のChangelog*/ );
40+
$this->assertEquals( $comment_history[1]->changeLog, []/* コメント投稿のChangelog */ );
41+
$this->assertEquals( $comment_history[2]->changeLog[0]->field, 'description'/*description変更のChangelog*/ );
42+
}
43+
protected function updateIssue($description=''){
44+
$api = $this->api;
45+
$params = [
46+
'description'=>$description
47+
];
48+
$ret = $api->updateIssue($this->issue_id,$params);
49+
return $ret;
50+
}
51+
52+
protected function addComment($content='コメント'){
53+
$api = $this->api;
54+
$params = [
55+
'content' => $content,
56+
'notifiedUserId' => [$this->findAdminUser()->id],
57+
];
58+
$ret = $api->addComment($this->issue_id,$params);
59+
return $ret;
60+
}
61+
62+
protected function addAttachment () {
63+
$sample = $this->sample_jpeg_file();
64+
$api = $this->api;
65+
$param = ['multipart' => [[
66+
'name' => "file",
67+
'contents' => $sample['content'],
68+
"filename" => $sample['name'],
69+
]]];
70+
$ret = $api->postAttachmentFile( $param );
71+
return $ret;
72+
}
73+
74+
protected function addCommentWithAttachment () {
75+
$attach = $this->addAttachment();
76+
$params = [
77+
'content' => sprintf( "画像を貼り付ける\n![image][%s]", $attach->name ),
78+
'attachmentId' => [$attach->id],
79+
];
80+
$api = $this->api;
81+
$ret = $api->addComment( $this->issue_id, $params );
82+
return $ret;
83+
}
84+
85+
86+
87+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace tests\Feature\API\TestCase;
4+
5+
class TestCaseIssueApiClientForUpdate extends TestCaseProjectApiClientForUpdate {
6+
7+
protected $issue_id;
8+
protected $issue_key;
9+
10+
public function __construct ( ?string $name = null, array $data = [], $dataName = '' ) {
11+
parent::__construct( $name, $data, $dataName );
12+
$this->api->disableLogging();
13+
}
14+
15+
16+
protected function setUp (): void {
17+
//return;
18+
parent::setUp();
19+
if ( !$this->hasIssue() ) {
20+
$this->createIssue();
21+
usleep(2000);
22+
}
23+
}
24+
25+
protected function tearDown (): void {
26+
//return;
27+
if ( $this->hasIssue() ) {
28+
$this->deleteIssue();
29+
usleep(200);
30+
}
31+
parent::tearDown();
32+
}
33+
34+
public function hasIssue () {
35+
try {
36+
$ret = $this->api->getIssue( $this->issue_id );
37+
return true;
38+
} catch (\Exception $e) {
39+
return false;
40+
}
41+
}
42+
43+
public function createIssue () {
44+
$params = ['projectId' => $this->project_id,
45+
'summary' => 'apiからのテスト',
46+
'description' => 'API経由で課題を作成しますー担当もします。',
47+
'issueTypeId' => $this->findIssueTypeId()->id,
48+
'priorityId' => 2,
49+
'notifiedUserId' => [$this->findAdminUser()->id],
50+
'assigneeId' => $this->findAdminUser()->id,
51+
];
52+
$ret = $this->api->addIssue( $params );
53+
$this->issue_id = $ret->id;
54+
$this->issue_key = $ret->issueKey;
55+
$this->wait(fn()=>$this->hasIssue()!==true,100,' issue create');
56+
return $ret;
57+
}
58+
59+
public function deleteIssue () {
60+
$ret = $this->api->deleteIssue( $this->issue_id );
61+
$this->wait(fn()=>$this->hasIssue()!==false,100,' issue delete');
62+
return $ret;
63+
}
64+
65+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace tests\Feature\API\TestCase;
4+
5+
use tests\TestCase;
6+
use tests\Feature\API\Trait\EntitySearch;
7+
8+
class TestCaseProjectApiClientForUpdate extends TestCase {
9+
protected $project_id = null;
10+
protected $project_key = 'API_SAMPLE';
11+
protected $api = null;
12+
use EntitySearch;
13+
14+
public function __construct ( ?string $name = null, array $data = [], $dataName = '' ) {
15+
parent::__construct( $name, $data, $dataName );
16+
$rand = join( array_map( fn( $e ) => chr( random_int( 65, 90 ) ), range( 0, 3 ) ) );
17+
$this->project_key = $this->project_key.'_'.$rand.random_int( 100, 999 );
18+
$this->api = $this->api_client();
19+
$this->api->disableLogging();
20+
}
21+
protected function sample_jpeg_file(){
22+
$sample_filename = realpath(__DIR__.'/../../../sample-data/sample.jpg');
23+
return [
24+
'name' => basename($sample_filename),
25+
'content'=> file_get_contents($sample_filename)
26+
];
27+
}
28+
29+
protected function createProject () {
30+
$params = ['form_params' => ['key' => $this->project_key, 'name' => 'APIから作成テスト']];
31+
$p = $this->api->addProject( $params );
32+
$this->project_id = $p->id;
33+
$this->wait( fn() => $this->hasProject() !== true, 100,'project create' );
34+
}
35+
36+
protected function wait ( callable $cond, $max=100, $mess='post' ) {
37+
foreach ( range( 0, $max) as $item ) {
38+
if ( $cond() ) {
39+
//dump( $mess.' waiting.' );
40+
sleep( 1 );
41+
} else {
42+
//dump( $mess.' end.' );
43+
break;
44+
}
45+
}
46+
}
47+
48+
protected function hasProject () {
49+
try {
50+
$this->api->getProject( $this->project_id );
51+
return true;
52+
} catch (\Exception) {
53+
return false;
54+
}
55+
}
56+
57+
protected function setUp (): void {
58+
if ( !$this->hasProject() ) {
59+
$this->createProject();
60+
usleep( 100 );
61+
}
62+
}
63+
64+
protected function tearDown (): void {
65+
if ( $this->hasProject() ) {
66+
$this->deleteProject();
67+
usleep( 100 );
68+
}
69+
}
70+
71+
72+
protected function deleteProject () {
73+
$this->api->deleteProject( $this->project_key );
74+
$this->wait( fn() => $this->hasProject() !== false , 100,'project delete');
75+
}
76+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
4+
namespace tests\Feature\API\Trait;
5+
trait EntitySearch {
6+
public function findIssueTypeId () {
7+
$api = $this->api;
8+
$project_id = $this->project_id ?? 91928;
9+
$list = $api->getIssueTypeList( $project_id );
10+
usort( $list, fn( $a, $b ) => $a->id <=> $b->id );
11+
return $list[0];
12+
}
13+
14+
public function findAdminUser () {
15+
if ( $this->admin_user ) {
16+
return $this->admin_user;
17+
}
18+
$api = $this->api;
19+
$list = $api->getUserList();
20+
$list = array_filter( $list, fn( $e ) => $e->roleType == 1 );
21+
//usort($list,fn($a,$b)=>$a->id<=>$b->id);
22+
usort( $list, fn( $a, $b ) => $b->id <=> $a->id );
23+
$this->admin_user = $list[0];
24+
return $this->admin_user;
25+
}
26+
27+
}

tests/sample-data/sample.jpg

10.2 KB
Loading

0 commit comments

Comments
 (0)