|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * This file is part of PHPUnit. |
| 4 | + * |
| 5 | + * (c) Sebastian Bergmann <[email protected]> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | +namespace PHPUnit\Framework\Attributes; |
| 11 | + |
| 12 | +use DateTime; |
| 13 | +use Exception; |
| 14 | +use LogicException; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +final class RetryTest extends TestCase |
| 18 | +{ |
| 19 | + private static int $retryNumber = 0; |
| 20 | + private static ?DateTime $start = null; |
| 21 | + |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + self::$retryNumber = 0; |
| 25 | + self::$start = new DateTime; |
| 26 | + } |
| 27 | + |
| 28 | + #[Retry(3)] |
| 29 | + public function testRetriesUntilMaxAttemptsThenSucceeds(): void |
| 30 | + { |
| 31 | + if (self::$retryNumber < 3) { |
| 32 | + self::$retryNumber++; |
| 33 | + |
| 34 | + throw new Exception; |
| 35 | + } |
| 36 | + |
| 37 | + $this->assertSame(3, self::$retryNumber); |
| 38 | + } |
| 39 | + |
| 40 | + #[Retry(1)] |
| 41 | + public function testSingleRetryThenThrowsExpectedException(): void |
| 42 | + { |
| 43 | + if (self::$retryNumber < 1) { |
| 44 | + self::$retryNumber++; |
| 45 | + |
| 46 | + throw new Exception; |
| 47 | + } |
| 48 | + |
| 49 | + $this->expectException(Exception::class); |
| 50 | + $this->expectExceptionMessage('test exception two'); |
| 51 | + $this->assertSame(1, self::$retryNumber); |
| 52 | + |
| 53 | + throw new Exception('test exception two'); |
| 54 | + } |
| 55 | + |
| 56 | + #[Retry(2, 0, LogicException::class)] |
| 57 | + public function testRetryWithUnmatchedExceptionTypeFailsImmediately(): void |
| 58 | + { |
| 59 | + $this->expectException(Exception::class); |
| 60 | + $this->expectExceptionMessage('test exception'); |
| 61 | + $this->assertSame(0, self::$retryNumber); |
| 62 | + self::$retryNumber++; |
| 63 | + |
| 64 | + throw new Exception('test exception'); |
| 65 | + } |
| 66 | + |
| 67 | + #[Retry(2, 0, LogicException::class)] |
| 68 | + #[Retry(2)] |
| 69 | + public function testMultipleRetryAttributesFallBackToDefaultRetry(): void |
| 70 | + { |
| 71 | + if (self::$retryNumber < 2) { |
| 72 | + self::$retryNumber++; |
| 73 | + |
| 74 | + throw new Exception; |
| 75 | + } |
| 76 | + |
| 77 | + $this->assertSame(2, self::$retryNumber); |
| 78 | + } |
| 79 | + |
| 80 | + #[Retry(5, 0, LogicException::class)] |
| 81 | + public function testRetriesUntilLogicExceptionStopsThrowing(): void |
| 82 | + { |
| 83 | + if (self::$retryNumber < 5) { |
| 84 | + self::$retryNumber++; |
| 85 | + |
| 86 | + throw new LogicException; |
| 87 | + } |
| 88 | + |
| 89 | + $this->assertSame(5, self::$retryNumber); |
| 90 | + } |
| 91 | + |
| 92 | + #[Retry(1, 2)] |
| 93 | + public function testRetryDelaysExecutionBySpecifiedSeconds(): void |
| 94 | + { |
| 95 | + $end = new DateTime; |
| 96 | + |
| 97 | + if (self::$retryNumber < 1) { |
| 98 | + self::$retryNumber++; |
| 99 | + |
| 100 | + throw new Exception; |
| 101 | + } |
| 102 | + |
| 103 | + $diffInSeconds = $end->getTimestamp() - self::$start->getTimestamp(); |
| 104 | + |
| 105 | + $this->assertSame(1, self::$retryNumber); |
| 106 | + $this->assertSame(2, $diffInSeconds); |
| 107 | + } |
| 108 | +} |
0 commit comments