|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AppBundle\Tests\Issues; |
| 4 | + |
| 5 | +use AppBundle\Event\GitHubEvent; |
| 6 | +use AppBundle\GitHubEvents; |
| 7 | +use AppBundle\Issues\IssueListener; |
| 8 | +use AppBundle\Issues\Status; |
| 9 | +use AppBundle\Issues\StatusApi; |
| 10 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 11 | + |
| 12 | +/** |
| 13 | + * @author Bernhard Schussek <bschussek@gmail.com> |
| 14 | + */ |
| 15 | +abstract class IssueListenerTest extends \PHPUnit_Framework_TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var StatusApi|\PHPUnit_Framework_MockObject_MockObject |
| 19 | + */ |
| 20 | + protected $statusApi; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var IssueListener |
| 24 | + */ |
| 25 | + private $listener; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var EventDispatcher |
| 29 | + */ |
| 30 | + private static $dispatcher; |
| 31 | + |
| 32 | + public static function setUpBeforeClass() |
| 33 | + { |
| 34 | + self::$dispatcher = new EventDispatcher(); |
| 35 | + } |
| 36 | + |
| 37 | + protected function setUp() |
| 38 | + { |
| 39 | + $this->statusApi = $this->getMock('AppBundle\Issues\StatusApi'); |
| 40 | + $this->listener = $this->getListener($this->statusApi); |
| 41 | + self::$dispatcher->addSubscriber($this->listener); |
| 42 | + } |
| 43 | + |
| 44 | + protected function tearDown() |
| 45 | + { |
| 46 | + self::$dispatcher->removeSubscriber($this->listener); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @dataProvider getCommentsForStatusChange |
| 51 | + */ |
| 52 | + public function testOnIssueComment($comment, $expectedStatus) |
| 53 | + { |
| 54 | + if (null !== $expectedStatus) { |
| 55 | + $this->statusApi->expects($this->once()) |
| 56 | + ->method('setIssueStatus') |
| 57 | + ->with(1234, $expectedStatus); |
| 58 | + } |
| 59 | + |
| 60 | + $event = new GitHubEvent(array( |
| 61 | + 'issue' => array('number' => 1234), |
| 62 | + 'comment' => array('body' => $comment), |
| 63 | + )); |
| 64 | + |
| 65 | + self::$dispatcher->dispatch(GitHubEvents::ISSUE_COMMENT, $event); |
| 66 | + |
| 67 | + $responseData = $event->getResponseData(); |
| 68 | + |
| 69 | + $this->assertCount(2, $responseData); |
| 70 | + $this->assertSame(1234, $responseData['issue']); |
| 71 | + $this->assertSame($expectedStatus, $responseData['status_change']); |
| 72 | + } |
| 73 | + |
| 74 | + public function getCommentsForStatusChange() |
| 75 | + { |
| 76 | + return array( |
| 77 | + array('Have a great day!', null), |
| 78 | + // basic tests for status change |
| 79 | + array('Status: needs review', Status::NEEDS_REVIEW), |
| 80 | + array('Status: needs work', Status::NEEDS_WORK), |
| 81 | + array('Status: reviewed', Status::REVIEWED), |
| 82 | + // accept quotes |
| 83 | + array('Status: "reviewed"', Status::REVIEWED), |
| 84 | + array("Status: 'reviewed'", Status::REVIEWED), |
| 85 | + // accept trailing punctuation |
| 86 | + array('Status: works for me!', Status::WORKS_FOR_ME), |
| 87 | + array('Status: works for me.', Status::WORKS_FOR_ME), |
| 88 | + // play with different formatting |
| 89 | + array('STATUS: REVIEWED', Status::REVIEWED), |
| 90 | + array('**Status**: reviewed', Status::REVIEWED), |
| 91 | + array('**Status:** reviewed', Status::REVIEWED), |
| 92 | + array('**Status: reviewed**', Status::REVIEWED), |
| 93 | + array('**Status: reviewed!**', Status::REVIEWED), |
| 94 | + array('**Status: reviewed**.', Status::REVIEWED), |
| 95 | + array('Status:reviewed', Status::REVIEWED), |
| 96 | + array('Status: reviewed', Status::REVIEWED), |
| 97 | + // reject missing colon |
| 98 | + array('Status reviewed', null), |
| 99 | + // multiple matches - use the last one |
| 100 | + array("Status: needs review \r\n that is what the issue *was* marked as.\r\n Status: reviewed", Status::REVIEWED), |
| 101 | + // "needs review" does not come directly after status: , so there is no status change |
| 102 | + array('Here is my status: I\'m really happy! I realize this needs review, but I\'m, having too much fun Googling cats!', null), |
| 103 | + // reject if the status is not on a line of its own |
| 104 | + // use case: someone posts instructions about how to change a status |
| 105 | + // in a comment |
| 106 | + array('You should include e.g. the line `Status: needs review` in your comment', null), |
| 107 | + array('Before the ticket was in state "Status: reviewed", but then the status was changed', null), |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + public function testOnPullRequestOpen() |
| 112 | + { |
| 113 | + $this->statusApi->expects($this->once()) |
| 114 | + ->method('setIssueStatus') |
| 115 | + ->with(1234, Status::NEEDS_REVIEW); |
| 116 | + |
| 117 | + $event = new GitHubEvent(array( |
| 118 | + 'action' => 'open', |
| 119 | + 'pull_request' => array('number' => 1234), |
| 120 | + )); |
| 121 | + |
| 122 | + self::$dispatcher->dispatch(GitHubEvents::PULL_REQUEST, $event); |
| 123 | + |
| 124 | + $responseData = $event->getResponseData(); |
| 125 | + |
| 126 | + $this->assertCount(2, $responseData); |
| 127 | + $this->assertSame(1234, $responseData['pull_request']); |
| 128 | + $this->assertSame(Status::NEEDS_REVIEW, $responseData['status_change']); |
| 129 | + } |
| 130 | + |
| 131 | + public function testOnPullRequestNotOpen() |
| 132 | + { |
| 133 | + $this->statusApi->expects($this->never()) |
| 134 | + ->method('setIssueStatus'); |
| 135 | + |
| 136 | + $event = new GitHubEvent(array( |
| 137 | + 'action' => 'close', |
| 138 | + )); |
| 139 | + |
| 140 | + self::$dispatcher->dispatch(GitHubEvents::PULL_REQUEST, $event); |
| 141 | + |
| 142 | + $responseData = $event->getResponseData(); |
| 143 | + |
| 144 | + $this->assertCount(1, $responseData); |
| 145 | + $this->assertSame('close', $responseData['unsupported_action']); |
| 146 | + } |
| 147 | + |
| 148 | + public function testOnIssuesLabeledBug() |
| 149 | + { |
| 150 | + $this->statusApi->expects($this->once()) |
| 151 | + ->method('getIssueStatus') |
| 152 | + ->with(1234) |
| 153 | + ->willReturn(null); |
| 154 | + |
| 155 | + $this->statusApi->expects($this->once()) |
| 156 | + ->method('setIssueStatus') |
| 157 | + ->with(1234, Status::NEEDS_REVIEW); |
| 158 | + |
| 159 | + $event = new GitHubEvent(array( |
| 160 | + 'action' => 'labeled', |
| 161 | + 'issue' => array('number' => 1234), |
| 162 | + 'label' => array('name' => 'bug'), |
| 163 | + )); |
| 164 | + |
| 165 | + self::$dispatcher->dispatch(GitHubEvents::ISSUES, $event); |
| 166 | + |
| 167 | + $responseData = $event->getResponseData(); |
| 168 | + |
| 169 | + $this->assertCount(2, $responseData); |
| 170 | + $this->assertSame(1234, $responseData['issue']); |
| 171 | + $this->assertSame(Status::NEEDS_REVIEW, $responseData['status_change']); |
| 172 | + } |
| 173 | + |
| 174 | + public function testOnIssuesLabeledBugIgnoresCase() |
| 175 | + { |
| 176 | + $this->statusApi->expects($this->once()) |
| 177 | + ->method('getIssueStatus') |
| 178 | + ->with(1234) |
| 179 | + ->willReturn(null); |
| 180 | + |
| 181 | + $this->statusApi->expects($this->once()) |
| 182 | + ->method('setIssueStatus') |
| 183 | + ->with(1234, Status::NEEDS_REVIEW); |
| 184 | + |
| 185 | + $event = new GitHubEvent(array( |
| 186 | + 'action' => 'labeled', |
| 187 | + 'issue' => array('number' => 1234), |
| 188 | + 'label' => array('name' => 'BUG'), |
| 189 | + )); |
| 190 | + |
| 191 | + self::$dispatcher->dispatch(GitHubEvents::ISSUES, $event); |
| 192 | + |
| 193 | + $responseData = $event->getResponseData(); |
| 194 | + |
| 195 | + $this->assertCount(2, $responseData); |
| 196 | + $this->assertSame(1234, $responseData['issue']); |
| 197 | + $this->assertSame(Status::NEEDS_REVIEW, $responseData['status_change']); |
| 198 | + } |
| 199 | + |
| 200 | + public function testOnIssuesIgnoresNonBugs() |
| 201 | + { |
| 202 | + $this->statusApi->expects($this->never()) |
| 203 | + ->method('getIssueStatus'); |
| 204 | + |
| 205 | + $this->statusApi->expects($this->never()) |
| 206 | + ->method('setIssueStatus'); |
| 207 | + |
| 208 | + $event = new GitHubEvent(array( |
| 209 | + 'action' => 'labeled', |
| 210 | + 'issue' => array('number' => 1234), |
| 211 | + 'label' => array('name' => 'feature'), |
| 212 | + )); |
| 213 | + |
| 214 | + self::$dispatcher->dispatch(GitHubEvents::ISSUES, $event); |
| 215 | + |
| 216 | + $responseData = $event->getResponseData(); |
| 217 | + |
| 218 | + $this->assertCount(2, $responseData); |
| 219 | + $this->assertSame(1234, $responseData['issue']); |
| 220 | + $this->assertNull($responseData['status_change']); |
| 221 | + } |
| 222 | + |
| 223 | + public function testOnIssuesIgnoresIfExistingStatus() |
| 224 | + { |
| 225 | + $this->statusApi->expects($this->once()) |
| 226 | + ->method('getIssueStatus') |
| 227 | + ->with(1234) |
| 228 | + ->willReturn(Status::NEEDS_WORK); |
| 229 | + |
| 230 | + $this->statusApi->expects($this->never()) |
| 231 | + ->method('setIssueStatus'); |
| 232 | + |
| 233 | + $event = new GitHubEvent(array( |
| 234 | + 'action' => 'labeled', |
| 235 | + 'issue' => array('number' => 1234), |
| 236 | + 'label' => array('name' => 'bug'), |
| 237 | + )); |
| 238 | + |
| 239 | + self::$dispatcher->dispatch(GitHubEvents::ISSUES, $event); |
| 240 | + |
| 241 | + $responseData = $event->getResponseData(); |
| 242 | + |
| 243 | + $this->assertCount(2, $responseData); |
| 244 | + $this->assertSame(1234, $responseData['issue']); |
| 245 | + $this->assertNull($responseData['status_change']); |
| 246 | + } |
| 247 | + |
| 248 | + public function testOnIssuesIgnoresNonLabeled() |
| 249 | + { |
| 250 | + $this->statusApi->expects($this->never()) |
| 251 | + ->method('getIssueStatus'); |
| 252 | + |
| 253 | + $this->statusApi->expects($this->never()) |
| 254 | + ->method('setIssueStatus'); |
| 255 | + |
| 256 | + $event = new GitHubEvent(array( |
| 257 | + 'action' => 'close', |
| 258 | + 'issue' => array('number' => 1234), |
| 259 | + 'label' => array('name' => 'bug'), |
| 260 | + )); |
| 261 | + |
| 262 | + self::$dispatcher->dispatch(GitHubEvents::ISSUES, $event); |
| 263 | + |
| 264 | + $responseData = $event->getResponseData(); |
| 265 | + |
| 266 | + $this->assertCount(1, $responseData); |
| 267 | + $this->assertSame('close', $responseData['unsupported_action']); |
| 268 | + } |
| 269 | + |
| 270 | + abstract protected function getListener($statusApi); |
| 271 | +} |
0 commit comments