|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Tests\Subscriber; |
| 4 | + |
| 5 | +use App\Event\GitHubEvent; |
| 6 | +use App\GitHubEvents; |
| 7 | +use App\Issues\GitHub\MilestonesApi; |
| 8 | +use App\Issues\Status; |
| 9 | +use App\Repository\Repository; |
| 10 | +use App\Subscriber\MilestoneNewPRSubscriber; |
| 11 | +use App\Subscriber\NeedsReviewNewPRSubscriber; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 14 | +use App\Issues\StatusApi; |
| 15 | + |
| 16 | +class MilestoneNewPRSubscriberTest extends TestCase |
| 17 | +{ |
| 18 | + private $subscriber; |
| 19 | + |
| 20 | + private $milestonesApi; |
| 21 | + |
| 22 | + private $repository; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var EventDispatcher |
| 26 | + */ |
| 27 | + private $dispatcher; |
| 28 | + |
| 29 | + protected function setUp() |
| 30 | + { |
| 31 | + $this->milestonesApi = $this->createMock(MilestonesApi::class); |
| 32 | + $this->subscriber = new MilestoneNewPRSubscriber($this->milestonesApi); |
| 33 | + $this->repository = new Repository('nyholm', 'symfony', [], null); |
| 34 | + |
| 35 | + $this->dispatcher = new EventDispatcher(); |
| 36 | + $this->dispatcher->addSubscriber($this->subscriber); |
| 37 | + } |
| 38 | + |
| 39 | + public function testOnPullRequestOpen() |
| 40 | + { |
| 41 | + $this->milestonesApi->expects($this->once()) |
| 42 | + ->method('exists') |
| 43 | + ->with($this->repository, '4.4') |
| 44 | + ->willReturn(true); |
| 45 | + |
| 46 | + $this->milestonesApi->expects($this->once()) |
| 47 | + ->method('updateMilestone') |
| 48 | + ->with($this->repository, 1234, '4.4'); |
| 49 | + |
| 50 | + $event = new GitHubEvent([ |
| 51 | + 'action' => 'opened', |
| 52 | + 'pull_request' => [ |
| 53 | + 'number' => 1234, |
| 54 | + 'base' => [ 'ref' => '4.4' ] |
| 55 | + ], |
| 56 | + 'repository' => [ |
| 57 | + 'default_branch' => 'master' |
| 58 | + ] |
| 59 | + ], $this->repository); |
| 60 | + |
| 61 | + $this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST); |
| 62 | + $responseData = $event->getResponseData(); |
| 63 | + |
| 64 | + $this->assertCount(2, $responseData); |
| 65 | + $this->assertSame(1234, $responseData['pull_request']); |
| 66 | + $this->assertSame('4.4', $responseData['milestone']); |
| 67 | + } |
| 68 | + |
| 69 | + public function testOnPullRequestOpenDefaultBranch() |
| 70 | + { |
| 71 | + $this->milestonesApi->expects($this->never()) |
| 72 | + ->method('updateMilestone'); |
| 73 | + |
| 74 | + $event = new GitHubEvent([ |
| 75 | + 'action' => 'opened', |
| 76 | + 'pull_request' => [ |
| 77 | + 'number' => 1234, |
| 78 | + 'base' => [ 'ref' => 'master' ] |
| 79 | + ], |
| 80 | + 'repository' => [ |
| 81 | + 'default_branch' => 'master' |
| 82 | + ] |
| 83 | + ], $this->repository); |
| 84 | + |
| 85 | + $this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST); |
| 86 | + $responseData = $event->getResponseData(); |
| 87 | + $this->assertEmpty($responseData); |
| 88 | + } |
| 89 | + |
| 90 | + public function testOnPullRequestOpenMilestoneNotExist() |
| 91 | + { |
| 92 | + $this->milestonesApi->expects($this->once()) |
| 93 | + ->method('exists') |
| 94 | + ->with($this->repository, '4.4') |
| 95 | + ->willReturn(false); |
| 96 | + |
| 97 | + $this->milestonesApi->expects($this->never()) |
| 98 | + ->method('updateMilestone'); |
| 99 | + |
| 100 | + $event = new GitHubEvent([ |
| 101 | + 'action' => 'opened', |
| 102 | + 'pull_request' => [ |
| 103 | + 'number' => 1234, |
| 104 | + 'base' => [ 'ref' => '4.4' ] |
| 105 | + ], |
| 106 | + 'repository' => [ |
| 107 | + 'default_branch' => 'master' |
| 108 | + ] |
| 109 | + ], $this->repository); |
| 110 | + |
| 111 | + $this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST); |
| 112 | + $responseData = $event->getResponseData(); |
| 113 | + $this->assertEmpty($responseData); |
| 114 | + } |
| 115 | + |
| 116 | + public function testOnPullRequestNotOpen() |
| 117 | + { |
| 118 | + $this->milestonesApi->expects($this->never()) |
| 119 | + ->method('updateMilestone'); |
| 120 | + |
| 121 | + $event = new GitHubEvent([ |
| 122 | + 'action' => 'close', |
| 123 | + ], $this->repository); |
| 124 | + |
| 125 | + $this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST); |
| 126 | + $responseData = $event->getResponseData(); |
| 127 | + $this->assertEmpty($responseData); |
| 128 | + } |
| 129 | +} |
0 commit comments