|
| 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