|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Tests\Subscriber; |
| 4 | + |
| 5 | +use App\Api\Issue\IssueApi; |
| 6 | +use App\Api\Issue\NullIssueApi; |
| 7 | +use App\Event\GitHubEvent; |
| 8 | +use App\GitHubEvents; |
| 9 | +use App\Model\Repository; |
| 10 | +use App\Subscriber\MismatchBranchDescriptionSubscriber; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use Psr\Log\NullLogger; |
| 13 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 14 | + |
| 15 | +final class MismatchBranchDescriptionSubscriberTest extends TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var IssueApi |
| 19 | + */ |
| 20 | + private $issueApi; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var Repository |
| 24 | + */ |
| 25 | + private $repository; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var EventDispatcher |
| 29 | + */ |
| 30 | + private $dispatcher; |
| 31 | + |
| 32 | + protected function setUp(): void |
| 33 | + { |
| 34 | + $this->issueApi = $this->createMock(NullIssueApi::class); |
| 35 | + |
| 36 | + $subscriber = new MismatchBranchDescriptionSubscriber($this->issueApi, new NullLogger()); |
| 37 | + $this->repository = new Repository('carsonbot-playground', 'symfony', null); |
| 38 | + |
| 39 | + $this->dispatcher = new EventDispatcher(); |
| 40 | + $this->dispatcher->addSubscriber($subscriber); |
| 41 | + } |
| 42 | + |
| 43 | + public function testOnPullRequestOpenMatch() |
| 44 | + { |
| 45 | + $this->issueApi->expects($this->never()) |
| 46 | + ->method('commentOnIssue'); |
| 47 | + |
| 48 | + $body = <<<TXT |
| 49 | +| Q | A |
| 50 | +| ------------- | --- |
| 51 | +| Branch? | 6.2 |
| 52 | +| Bug fix? | yes/no |
| 53 | +TXT; |
| 54 | + |
| 55 | + $event = new GitHubEvent([ |
| 56 | + 'action' => 'opened', |
| 57 | + 'pull_request' => [ |
| 58 | + 'number' => 1234, |
| 59 | + 'body' => $body, |
| 60 | + 'base' => [ |
| 61 | + 'ref' => '6.2', |
| 62 | + ], |
| 63 | + ], |
| 64 | + ], $this->repository); |
| 65 | + |
| 66 | + $this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST); |
| 67 | + $responseData = $event->getResponseData(); |
| 68 | + |
| 69 | + $this->assertCount(0, $responseData); |
| 70 | + } |
| 71 | + |
| 72 | + public function testOnPullRequestOpenNotMatch() |
| 73 | + { |
| 74 | + $this->issueApi->expects($this->once()) |
| 75 | + ->method('commentOnIssue'); |
| 76 | + |
| 77 | + $body = <<<TXT |
| 78 | +| Q | A |
| 79 | +| ------------- | --- |
| 80 | +| Branch? | 6.1 |
| 81 | +| Bug fix? | yes/no |
| 82 | +TXT; |
| 83 | + |
| 84 | + $event = new GitHubEvent([ |
| 85 | + 'action' => 'opened', |
| 86 | + 'pull_request' => [ |
| 87 | + 'number' => 1234, |
| 88 | + 'body' => $body, |
| 89 | + 'base' => [ |
| 90 | + 'ref' => '6.2', |
| 91 | + ], |
| 92 | + ], |
| 93 | + ], $this->repository); |
| 94 | + |
| 95 | + $this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST); |
| 96 | + $responseData = $event->getResponseData(); |
| 97 | + |
| 98 | + $this->assertCount(1, $responseData); |
| 99 | + $this->assertSame(1234, $responseData['pull_request']); |
| 100 | + } |
| 101 | + |
| 102 | + public function testOnPullRequestOpenWithoutBranchRow() |
| 103 | + { |
| 104 | + $this->issueApi->expects($this->never()) |
| 105 | + ->method('commentOnIssue'); |
| 106 | + |
| 107 | + $body = <<<TXT |
| 108 | +| Q | A |
| 109 | +| ------------- | --- |
| 110 | +| Bug fix? | yes/no |
| 111 | +TXT; |
| 112 | + |
| 113 | + $event = new GitHubEvent([ |
| 114 | + 'action' => 'opened', |
| 115 | + 'pull_request' => [ |
| 116 | + 'number' => 1234, |
| 117 | + 'body' => $body, |
| 118 | + 'base' => [ |
| 119 | + 'ref' => '6.2', |
| 120 | + ], |
| 121 | + ], |
| 122 | + ], $this->repository); |
| 123 | + |
| 124 | + $this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST); |
| 125 | + $responseData = $event->getResponseData(); |
| 126 | + |
| 127 | + $this->assertCount(0, $responseData); |
| 128 | + } |
| 129 | + |
| 130 | + public function testOnPullRequestOpenBadBranchFormat() |
| 131 | + { |
| 132 | + $this->issueApi->expects($this->once()) |
| 133 | + ->method('commentOnIssue'); |
| 134 | + |
| 135 | + $body = <<<TXT |
| 136 | +| Q | A |
| 137 | +| ------------- | --- |
| 138 | +| Branch? | 6.2 for features / 4.4, 5.4, 6.0 or 6.1 for bug fixes <!-- see below --> |
| 139 | +| Bug fix? | yes/no |
| 140 | +TXT; |
| 141 | + |
| 142 | + $event = new GitHubEvent([ |
| 143 | + 'action' => 'opened', |
| 144 | + 'pull_request' => [ |
| 145 | + 'number' => 1234, |
| 146 | + 'body' => $body, |
| 147 | + 'base' => [ |
| 148 | + 'ref' => '6.2', |
| 149 | + ], |
| 150 | + ], |
| 151 | + ], $this->repository); |
| 152 | + |
| 153 | + $this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST); |
| 154 | + $responseData = $event->getResponseData(); |
| 155 | + |
| 156 | + $this->assertCount(1, $responseData); |
| 157 | + $this->assertSame(1234, $responseData['pull_request']); |
| 158 | + } |
| 159 | + |
| 160 | + public function testOnPullRequestOpenBranchNotInTable() |
| 161 | + { |
| 162 | + $this->issueApi->expects($this->never()) |
| 163 | + ->method('commentOnIssue'); |
| 164 | + |
| 165 | + $body = <<<TXT |
| 166 | +Branch? 6.2 |
| 167 | +TXT; |
| 168 | + |
| 169 | + $event = new GitHubEvent([ |
| 170 | + 'action' => 'opened', |
| 171 | + 'pull_request' => [ |
| 172 | + 'number' => 1234, |
| 173 | + 'body' => $body, |
| 174 | + 'base' => [ |
| 175 | + 'ref' => '6.2', |
| 176 | + ], |
| 177 | + ], |
| 178 | + ], $this->repository); |
| 179 | + |
| 180 | + $this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST); |
| 181 | + $responseData = $event->getResponseData(); |
| 182 | + |
| 183 | + $this->assertCount(0, $responseData); |
| 184 | + } |
| 185 | +} |
0 commit comments