|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Copyright (c) 2021-2023 Andreas Möller |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE.md file that was distributed with this source code. |
| 10 | + * |
| 11 | + * @see https://github.com/ergebnis/phpunit-slow-test-detector |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Ergebnis\PHPUnit\SlowTestDetector\Test\Unit; |
| 15 | + |
| 16 | +use Ergebnis\PHPUnit\SlowTestDetector\Duration; |
| 17 | +use Ergebnis\PHPUnit\SlowTestDetector\Exception; |
| 18 | +use Ergebnis\PHPUnit\SlowTestDetector\Test; |
| 19 | +use Ergebnis\PHPUnit\SlowTestDetector\Time; |
| 20 | +use PHPUnit\Framework; |
| 21 | + |
| 22 | +#[Framework\Attributes\CoversClass(Time::class)] |
| 23 | +#[Framework\Attributes\UsesClass(Duration::class)] |
| 24 | +final class TimeTest extends Framework\TestCase |
| 25 | +{ |
| 26 | + use Test\Util\Helper; |
| 27 | + |
| 28 | + public function testFromSecondsAndNanosecondsRejectsNegativeSeconds(): void |
| 29 | + { |
| 30 | + $this->expectException(Exception\InvalidSeconds::class); |
| 31 | + |
| 32 | + Time::fromSecondsAndNanoseconds( |
| 33 | + -1, |
| 34 | + 0, |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + public function testFromSecondsAndNanosecondsRejectsNegativeNanoseconds(): void |
| 39 | + { |
| 40 | + $this->expectException(Exception\InvalidNanoseconds::class); |
| 41 | + |
| 42 | + Time::fromSecondsAndNanoseconds( |
| 43 | + 0, |
| 44 | + -1, |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + public function testFromSecondsAndNanosecondsRejectsNanosecondsGreaterThan999999999(): void |
| 49 | + { |
| 50 | + $this->expectException(Exception\InvalidNanoseconds::class); |
| 51 | + |
| 52 | + Time::fromSecondsAndNanoseconds( |
| 53 | + 0, |
| 54 | + 1000000000, |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + public function testFromSecondsAndNanosecondsReturnsTime(): void |
| 59 | + { |
| 60 | + $faker = self::faker(); |
| 61 | + |
| 62 | + $seconds = $faker->numberBetween(0, 999); |
| 63 | + $nanoseconds = $faker->numberBetween(0, 999_999_999); |
| 64 | + |
| 65 | + $time = Time::fromSecondsAndNanoseconds( |
| 66 | + $seconds, |
| 67 | + $nanoseconds, |
| 68 | + ); |
| 69 | + |
| 70 | + self::assertSame($seconds, $time->seconds()); |
| 71 | + self::assertSame($nanoseconds, $time->nanoseconds()); |
| 72 | + } |
| 73 | + |
| 74 | + #[Framework\Attributes\DataProvider('provideStartGreaterThanEnd')] |
| 75 | + public function testDurationRejectsStartGreaterThanEnd( |
| 76 | + int $startSeconds, |
| 77 | + int $startNanoseconds, |
| 78 | + int $endSeconds, |
| 79 | + int $endNanoseconds, |
| 80 | + ): void { |
| 81 | + $start = Time::fromSecondsAndNanoseconds( |
| 82 | + $startSeconds, |
| 83 | + $startNanoseconds, |
| 84 | + ); |
| 85 | + |
| 86 | + $end = Time::fromSecondsAndNanoseconds( |
| 87 | + $endSeconds, |
| 88 | + $endNanoseconds, |
| 89 | + ); |
| 90 | + |
| 91 | + $this->expectException(Exception\InvalidStart::class); |
| 92 | + |
| 93 | + $end->duration($start); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @return array<string, array{0: int, 1: int, 2: int, 3: int}> |
| 98 | + */ |
| 99 | + public static function provideStartGreaterThanEnd(): array |
| 100 | + { |
| 101 | + return [ |
| 102 | + 'seconds-greater' => [ |
| 103 | + 11, |
| 104 | + 1, |
| 105 | + 10, |
| 106 | + 1, |
| 107 | + ], |
| 108 | + 'seconds-and-nanoseconds-greater' => [ |
| 109 | + 11, |
| 110 | + 1, |
| 111 | + 10, |
| 112 | + 0, |
| 113 | + ], |
| 114 | + 'nanoseconds-greater' => [ |
| 115 | + 10, |
| 116 | + 1, |
| 117 | + 10, |
| 118 | + 0, |
| 119 | + ], |
| 120 | + ]; |
| 121 | + } |
| 122 | + |
| 123 | + #[Framework\Attributes\DataProvider('provideStartEndAndDuration')] |
| 124 | + public function testDurationReturnsDifferenceBetweenEndAndStart( |
| 125 | + int $startSeconds, |
| 126 | + int $startNanoseconds, |
| 127 | + int $endSeconds, |
| 128 | + int $endNanoseconds, |
| 129 | + Duration $duration, |
| 130 | + ): void { |
| 131 | + $start = Time::fromSecondsAndNanoseconds( |
| 132 | + $startSeconds, |
| 133 | + $startNanoseconds, |
| 134 | + ); |
| 135 | + |
| 136 | + $end = Time::fromSecondsAndNanoseconds( |
| 137 | + $endSeconds, |
| 138 | + $endNanoseconds, |
| 139 | + ); |
| 140 | + |
| 141 | + self::assertEquals($duration, $end->duration($start)); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @return array<string, array{0: int, 1: int, 2: int, 3: int, 4: Duration}> |
| 146 | + */ |
| 147 | + public static function provideStartEndAndDuration(): array |
| 148 | + { |
| 149 | + return [ |
| 150 | + 'start-equal-to-end' => [ |
| 151 | + 10, |
| 152 | + 50, |
| 153 | + 10, |
| 154 | + 50, |
| 155 | + Duration::fromSecondsAndNanoseconds(0, 0), |
| 156 | + ], |
| 157 | + 'start-smaller-than-end' => [ |
| 158 | + 10, |
| 159 | + 50, |
| 160 | + 12, |
| 161 | + 70, |
| 162 | + Duration::fromSecondsAndNanoseconds(2, 20), |
| 163 | + ], |
| 164 | + 'start-nanoseconds-greater-than-end-nanoseconds' => [ |
| 165 | + 10, |
| 166 | + 50, |
| 167 | + 12, |
| 168 | + 30, |
| 169 | + Duration::fromSecondsAndNanoseconds(1, 999999980), |
| 170 | + ], |
| 171 | + ]; |
| 172 | + } |
| 173 | +} |
0 commit comments