|
| 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\Service\ComplementGenerator; |
| 11 | +use App\Service\SymfonyVersionProvider; |
| 12 | +use App\Subscriber\UnsupportedBranchSubscriber; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | +use Psr\Log\NullLogger; |
| 15 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 16 | + |
| 17 | +class UnsupportedBranchSubscriberTest extends TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var IssueApi |
| 21 | + */ |
| 22 | + private $issueApi; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var Repository |
| 26 | + */ |
| 27 | + private $repository; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var EventDispatcher |
| 31 | + */ |
| 32 | + private $dispatcher; |
| 33 | + |
| 34 | + protected function setUp() |
| 35 | + { |
| 36 | + $this->issueApi = $this->createMock(NullIssueApi::class); |
| 37 | + $symfonyVersionProvider = $this->getMockBuilder(SymfonyVersionProvider::class) |
| 38 | + ->disableOriginalConstructor() |
| 39 | + ->setMethods(['getSupportedVersions']) |
| 40 | + ->getMock(); |
| 41 | + $symfonyVersionProvider->method('getSupportedVersions')->willReturn(['4.4', '5.1']); |
| 42 | + |
| 43 | + $subscriber = new UnsupportedBranchSubscriber($symfonyVersionProvider, $this->issueApi, new ComplementGenerator(), new NullLogger()); |
| 44 | + $this->repository = new Repository('carsonbot-playground', 'symfony', null); |
| 45 | + |
| 46 | + $this->dispatcher = new EventDispatcher(); |
| 47 | + $this->dispatcher->addSubscriber($subscriber); |
| 48 | + } |
| 49 | + |
| 50 | + public function testOnPullRequestOpen() |
| 51 | + { |
| 52 | + $this->issueApi->expects($this->once()) |
| 53 | + ->method('commentOnIssue'); |
| 54 | + |
| 55 | + $event = new GitHubEvent([ |
| 56 | + 'action' => 'opened', |
| 57 | + 'pull_request' => [ |
| 58 | + 'number' => 1234, |
| 59 | + 'base' => ['ref' => '4.3'], |
| 60 | + ], |
| 61 | + 'repository' => [ |
| 62 | + 'default_branch' => '5.x', |
| 63 | + ], |
| 64 | + ], $this->repository); |
| 65 | + |
| 66 | + $this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST); |
| 67 | + $responseData = $event->getResponseData(); |
| 68 | + |
| 69 | + $this->assertCount(2, $responseData); |
| 70 | + $this->assertSame(1234, $responseData['pull_request']); |
| 71 | + $this->assertSame('4.3', $responseData['unsupported_branch']); |
| 72 | + } |
| 73 | + |
| 74 | + public function testOnPullRequestOpenDefaultBranch() |
| 75 | + { |
| 76 | + $this->issueApi->expects($this->never()) |
| 77 | + ->method('commentOnIssue'); |
| 78 | + |
| 79 | + $event = new GitHubEvent([ |
| 80 | + 'action' => 'opened', |
| 81 | + 'pull_request' => [ |
| 82 | + 'number' => 1234, |
| 83 | + 'base' => ['ref' => '4.4'], |
| 84 | + ], |
| 85 | + 'repository' => [ |
| 86 | + 'default_branch' => '5.x', |
| 87 | + ], |
| 88 | + ], $this->repository); |
| 89 | + |
| 90 | + $this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST); |
| 91 | + $responseData = $event->getResponseData(); |
| 92 | + $this->assertEmpty($responseData); |
| 93 | + } |
| 94 | + |
| 95 | + public function testOnPullRequestNotOpen() |
| 96 | + { |
| 97 | + $this->issueApi->expects($this->never()) |
| 98 | + ->method('commentOnIssue'); |
| 99 | + |
| 100 | + $event = new GitHubEvent([ |
| 101 | + 'action' => 'close', |
| 102 | + ], $this->repository); |
| 103 | + |
| 104 | + $this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST); |
| 105 | + $responseData = $event->getResponseData(); |
| 106 | + $this->assertEmpty($responseData); |
| 107 | + } |
| 108 | +} |
0 commit comments