Skip to content

Commit 67252ff

Browse files
committed
Adding an integration test for the IssueStatusChanger and fixing bugs it uncovered
1 parent 342dc2f commit 67252ff

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

src/AppBundle/GitHub/IssueStatusChanger.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ public function setIssueStatusLabel($issueNumber, $newStatus)
3636
);
3737

3838
$labelMap = StatusManager::getLabelToStatusMap();
39-
foreach ($currentLabels as $currentLabel) {
39+
foreach ($currentLabels as $currentLabelData) {
40+
// get the name of the label
41+
$currentLabel = $currentLabelData['name'];
42+
4043
// is the label a "status label"? No? Then skip it
4144
if (!isset($labelMap[$currentLabel])) {
4245
continue;

src/AppBundle/GitHub/StatusManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static function getLabelForStatus($status)
8181
*/
8282
public static function getLabelToStatusMap()
8383
{
84-
return array_keys(self::$labels);
84+
return array_flip(self::$labels);
8585
}
8686

8787
/**
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace AppBundle\Tests\GitHub;
4+
5+
use AppBundle\GitHub\IssueStatusChanger;
6+
use AppBundle\GitHub\StatusManager;
7+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
8+
9+
class IssueStatusChangerTest extends KernelTestCase
10+
{
11+
public function testIntegrationSetIssueStatusLabel()
12+
{
13+
self::bootKernel();
14+
$container = self::$kernel->getContainer();
15+
16+
/** @var IssueStatusChanger $issueStatusChanger */
17+
$issueStatusChanger = $container->get('app.issue_status_changer');
18+
$testIssueNumber = $this->getTestIssueNumber();
19+
20+
$issueStatusChanger->setIssueStatusLabel($testIssueNumber, StatusManager::STATUS_NEEDS_WORK);
21+
$this->assertIssueLabels(
22+
$testIssueNumber,
23+
array(StatusManager::getLabelForStatus(StatusManager::STATUS_NEEDS_WORK))
24+
);
25+
}
26+
27+
/**
28+
* Guarantees there's an issue on GitHub we can use for integration testing
29+
*
30+
* @return integer
31+
*/
32+
private function getTestIssueNumber()
33+
{
34+
$container = self::$kernel->getContainer();
35+
36+
$repoUsername = $container->getParameter('repository_username');
37+
$repoName = $container->getParameter('repository_name');
38+
$githubClient = $container->get('app.github_client');
39+
$data = $githubClient->api('issue')->find(
40+
$repoUsername, $repoName, 'open', 'IntegrationTest'
41+
);
42+
43+
if (isset($data['issues'][0])) {
44+
$issueNumber = $data['issues'][0]['number'];
45+
} else {
46+
$data = $githubClient->api('issue')->create(
47+
$repoUsername,
48+
$repoName,
49+
array(
50+
'title' => 'IntegrationTest issue',
51+
'body' => 'This is an issue used for integration testing'
52+
)
53+
);
54+
55+
$issueNumber = $data['number'];
56+
}
57+
58+
// clear all the labels
59+
$githubClient->issues()->labels()->clear(
60+
$repoUsername,
61+
$repoName,
62+
$issueNumber
63+
);
64+
// give it a Status: Reviewed
65+
$githubClient->issues()->labels()->add(
66+
$repoUsername,
67+
$repoName,
68+
$issueNumber,
69+
StatusManager::getLabelForStatus(StatusManager::STATUS_REVIEWED)
70+
);
71+
72+
return $issueNumber;
73+
}
74+
75+
private function assertIssueLabels($issueNumber, $expectedLabels)
76+
{
77+
$container = self::$kernel->getContainer();
78+
79+
$repoUsername = $container->getParameter('repository_username');
80+
$repoName = $container->getParameter('repository_name');
81+
$githubClient = $container->get('app.github_client');
82+
$data = $githubClient->issues()->labels()->all(
83+
$repoUsername, $repoName, $issueNumber
84+
);
85+
86+
$actualLabelNames = array_map(function($value) {
87+
return $value['name'];
88+
}, $data);
89+
90+
$this->assertEquals($expectedLabels, $actualLabelNames);
91+
}
92+
}

0 commit comments

Comments
 (0)