|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace DR\Review\Tests\Unit\Service\Git\Review\ReviewDiffService; |
| 5 | + |
| 6 | +use DR\Review\Entity\Git\Diff\DiffComparePolicy; |
| 7 | +use DR\Review\Entity\Repository\Repository; |
| 8 | +use DR\Review\Entity\Review\CodeReview; |
| 9 | +use DR\Review\Entity\Revision\Revision; |
| 10 | +use DR\Review\Service\Git\Review\FileDiffOptions; |
| 11 | +use DR\Review\Service\Git\Review\ReviewDiffService\RecoverableReviewDiffService; |
| 12 | +use DR\Review\Service\Git\Review\ReviewDiffService\ReviewDiffServiceInterface; |
| 13 | +use DR\Review\Tests\AbstractTestCase; |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
| 16 | +use stdClass; |
| 17 | +use Symfony\Component\Process\Exception\ProcessFailedException; |
| 18 | +use Throwable; |
| 19 | + |
| 20 | +#[CoversClass(RecoverableReviewDiffService::class)] |
| 21 | +class RecoverableReviewDiffServiceTest extends AbstractTestCase |
| 22 | +{ |
| 23 | + private ReviewDiffServiceInterface&MockObject $diffService; |
| 24 | + private RecoverableReviewDiffService $service; |
| 25 | + |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + parent::setUp(); |
| 29 | + $this->diffService = $this->createMock(ReviewDiffServiceInterface::class); |
| 30 | + $this->service = new RecoverableReviewDiffService($this->diffService); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @throws Throwable |
| 35 | + */ |
| 36 | + public function testGetDiffForRevisions(): void |
| 37 | + { |
| 38 | + $repository = new Repository(); |
| 39 | + $revisions = [new Revision()]; |
| 40 | + $options = new FileDiffOptions(5, DiffComparePolicy::ALL); |
| 41 | + |
| 42 | + $this->diffService->expects(self::once())->method('getDiffForRevisions')->with($repository, $revisions, $options); |
| 43 | + |
| 44 | + $this->service->getDiffForRevisions($repository, $revisions, $options); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @throws Throwable |
| 49 | + */ |
| 50 | + public function testGetDiffForBranchExpectTwoAttempts(): void |
| 51 | + { |
| 52 | + $callCount = new stdClass(); |
| 53 | + $callCount->count = 0; |
| 54 | + $repository = (new Repository())->setMainBranchName('master'); |
| 55 | + $review = (new CodeReview())->setTargetBranch('foobar')->setRepository($repository); |
| 56 | + $revisions = [new Revision()]; |
| 57 | + $options = new FileDiffOptions(5, DiffComparePolicy::ALL); |
| 58 | + $branchName = 'branch'; |
| 59 | + |
| 60 | + $exception = $this->createMock(ProcessFailedException::class); |
| 61 | + |
| 62 | + $this->diffService->expects(self::exactly(2))->method('getDiffForBranch') |
| 63 | + ->with($review, $revisions, $branchName, $options) |
| 64 | + ->willThrowException($exception); |
| 65 | + |
| 66 | + $this->expectException(ProcessFailedException::class); |
| 67 | + $this->service->getDiffForBranch($review, $revisions, $branchName, $options); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @throws Throwable |
| 72 | + */ |
| 73 | + public function testGetDiffForBranchExpectOneAttemptOnMasterBranch(): void |
| 74 | + { |
| 75 | + $callCount = new stdClass(); |
| 76 | + $callCount->count = 0; |
| 77 | + $repository = (new Repository())->setMainBranchName('master'); |
| 78 | + $review = (new CodeReview())->setTargetBranch('master')->setRepository($repository); |
| 79 | + $revisions = [new Revision()]; |
| 80 | + $options = new FileDiffOptions(5, DiffComparePolicy::ALL); |
| 81 | + $branchName = 'branch'; |
| 82 | + |
| 83 | + $exception = $this->createMock(ProcessFailedException::class); |
| 84 | + |
| 85 | + $this->diffService->expects(self::once())->method('getDiffForBranch') |
| 86 | + ->with($review, $revisions, $branchName, $options) |
| 87 | + ->willThrowException($exception); |
| 88 | + |
| 89 | + $this->expectException(ProcessFailedException::class); |
| 90 | + $this->service->getDiffForBranch($review, $revisions, $branchName, $options); |
| 91 | + } |
| 92 | +} |
0 commit comments