|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AppBundle\Tests\Subscriber; |
| 4 | + |
| 5 | +use AppBundle\Event\GitHubEvent; |
| 6 | +use AppBundle\GitHubEvents; |
| 7 | +use AppBundle\Issues\Status; |
| 8 | +use AppBundle\Repository\Repository; |
| 9 | +use AppBundle\Subscriber\StatusChangeOnPushSubscriber; |
| 10 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 11 | + |
| 12 | +class StatusChangeOnPushSubscriberTest extends \PHPUnit_Framework_TestCase |
| 13 | +{ |
| 14 | + private $statusChangeSubscriber; |
| 15 | + |
| 16 | + private $statusApi; |
| 17 | + |
| 18 | + private $repository; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var EventDispatcher |
| 22 | + */ |
| 23 | + private static $dispatcher; |
| 24 | + |
| 25 | + public static function setUpBeforeClass() |
| 26 | + { |
| 27 | + self::$dispatcher = new EventDispatcher(); |
| 28 | + } |
| 29 | + |
| 30 | + protected function setUp() |
| 31 | + { |
| 32 | + $this->statusApi = $this->getMock('AppBundle\Issues\StatusApi'); |
| 33 | + $this->statusChangeSubscriber = new StatusChangeOnPushSubscriber($this->statusApi); |
| 34 | + $this->repository = new Repository('weaverryan', 'symfony', [], null); |
| 35 | + |
| 36 | + self::$dispatcher->addSubscriber($this->statusChangeSubscriber); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @dataProvider getStatuses |
| 41 | + */ |
| 42 | + public function testOnPushingCommits($currentStatus, $statusChange) |
| 43 | + { |
| 44 | + $this->statusApi->expects($this->any()) |
| 45 | + ->method('getIssueStatus') |
| 46 | + ->with(1234, $this->repository) |
| 47 | + ->will($this->returnValue($currentStatus)); |
| 48 | + |
| 49 | + if (null !== $statusChange) { |
| 50 | + $this->statusApi->expects($this->once()) |
| 51 | + ->method('setIssueStatus') |
| 52 | + ->with(1234, Status::NEEDS_REVIEW, $this->repository); |
| 53 | + } |
| 54 | + |
| 55 | + $event = new GitHubEvent([ |
| 56 | + 'action' => 'synchronize', |
| 57 | + 'pull_request' => $this->getPullRequestData(), |
| 58 | + ], $this->repository); |
| 59 | + |
| 60 | + self::$dispatcher->dispatch(GitHubEvents::PULL_REQUEST, $event); |
| 61 | + |
| 62 | + $responseData = $event->getResponseData(); |
| 63 | + |
| 64 | + $this->assertCount(2, $responseData); |
| 65 | + $this->assertSame(1234, $responseData['pull_request']); |
| 66 | + $this->assertSame($statusChange, $responseData['status_change']); |
| 67 | + } |
| 68 | + |
| 69 | + public function getStatuses() |
| 70 | + { |
| 71 | + return array( |
| 72 | + array(Status::NEEDS_WORK, Status::NEEDS_REVIEW), |
| 73 | + array(Status::REVIEWED, null), |
| 74 | + array(Status::WORKS_FOR_ME, null), |
| 75 | + array(Status::NEEDS_REVIEW, null), |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + public function testOnNonPushPullRequestEvent() |
| 80 | + { |
| 81 | + $this->statusApi->expects($this->any()) |
| 82 | + ->method('getIssueStatus') |
| 83 | + ->with(1234, $this->repository) |
| 84 | + ->will($this->returnValue(Status::NEEDS_WORK)); |
| 85 | + |
| 86 | + $event = new GitHubEvent(array( |
| 87 | + 'action' => 'labeled', |
| 88 | + 'pull_request' => $this->getPullRequestData(), |
| 89 | + ), $this->repository); |
| 90 | + |
| 91 | + self::$dispatcher->dispatch(GitHubEvents::PULL_REQUEST, $event); |
| 92 | + |
| 93 | + $responseData = $event->getResponseData(); |
| 94 | + |
| 95 | + $this->assertCount(1, $responseData); |
| 96 | + $this->assertSame('labeled', $responseData['unsupported_action']); |
| 97 | + } |
| 98 | + |
| 99 | + public function testWipPullRequest() |
| 100 | + { |
| 101 | + $this->statusApi->expects($this->any()) |
| 102 | + ->method('getIssueStatus') |
| 103 | + ->with(1234, $this->repository) |
| 104 | + ->will($this->returnValue(Status::NEEDS_WORK)); |
| 105 | + |
| 106 | + $event = new GitHubEvent(array( |
| 107 | + 'action' => 'synchronize', |
| 108 | + 'pull_request' => $this->getPullRequestData('[wip] needs some more work.'), |
| 109 | + ), $this->repository); |
| 110 | + |
| 111 | + self::$dispatcher->dispatch(GitHubEvents::PULL_REQUEST, $event); |
| 112 | + |
| 113 | + $responseData = $event->getResponseData(); |
| 114 | + |
| 115 | + $this->assertCount(2, $responseData); |
| 116 | + $this->assertSame(1234, $responseData['pull_request']); |
| 117 | + $this->assertSame(null, $responseData['status_change']); |
| 118 | + } |
| 119 | + |
| 120 | + private function getPullRequestData($title = 'Default title.') |
| 121 | + { |
| 122 | + return [ |
| 123 | + 'number' => 1234, |
| 124 | + 'title' => $title, |
| 125 | + ]; |
| 126 | + } |
| 127 | +} |
0 commit comments