Skip to content

Commit

Permalink
Closes #5652
Browse files Browse the repository at this point in the history
Six people, see below, spent more than two hours, to research this issue. They eventually gave up and decided to not raise an exception and instead return a Duration object that represents 0 seconds and 0 nanoseconds.

Co-authored-by: Sebastian Bergmann <sb@sebastian-bergmann.de>
Co-authored-by: Arne Blankerts <Arne@Blankerts.de>
Co-authored-by: Andreas Möller <am@localheinz.com>
Co-authored-by: Sebastian Heuer <sebastian@phpeople.de>
Co-authored-by: Fabian Blechschmidt <blechschmidt@fabian-blechschmidt.de>
Co-authored-by: Frank Sons <frank.sons@code-quality.de>
  • Loading branch information
6 people committed Mar 8, 2024
1 parent 15b2ae8 commit fac35fd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
1 change: 1 addition & 0 deletions .psalm/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<file src="src/Event/Value/Telemetry/HRTime.php">
<ImpureMethodCall>
<code><![CDATA[fromSecondsAndNanoseconds]]></code>
<code><![CDATA[fromSecondsAndNanoseconds]]></code>
</ImpureMethodCall>
</file>
<file src="src/Event/Value/Telemetry/MemoryUsage.php">
Expand Down
7 changes: 7 additions & 0 deletions ChangeLog-10.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes of the PHPUnit 10.5 release series are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.

## [10.5.12] - 2024-MM-DD

### Fixed

* [#5652](https://github.com/sebastianbergmann/phpunit/issues/5652): `HRTime::duration()` throws `InvalidArgumentException`

## [10.5.11] - 2024-02-25

### Fixed
Expand Down Expand Up @@ -125,6 +131,7 @@ All notable changes of the PHPUnit 10.5 release series are documented in this fi

* [#5563](https://github.com/sebastianbergmann/phpunit/issues/5563): `createMockForIntersectionOfInterfaces()` does not automatically register mock object for expectation verification

[10.5.12]: https://github.com/sebastianbergmann/phpunit/compare/10.5.11...10.5
[10.5.11]: https://github.com/sebastianbergmann/phpunit/compare/10.5.10...10.5.11
[10.5.10]: https://github.com/sebastianbergmann/phpunit/compare/10.5.9...10.5.10
[10.5.9]: https://github.com/sebastianbergmann/phpunit/compare/10.5.8...10.5.9
Expand Down
5 changes: 1 addition & 4 deletions src/Event/Value/Telemetry/HRTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ public function nanoseconds(): int
return $this->nanoseconds;
}

/**
* @throws InvalidArgumentException
*/
public function duration(self $start): Duration
{
$seconds = $this->seconds - $start->seconds();
Expand All @@ -71,7 +68,7 @@ public function duration(self $start): Duration
}

if ($seconds < 0) {
throw new InvalidArgumentException('Start needs to be smaller.');
return Duration::fromSecondsAndNanoseconds(0, 0);

Check failure on line 71 in src/Event/Value/Telemetry/HRTime.php

View workflow job for this annotation

GitHub Actions / Type Checker

MissingThrowsDocblock

src/Event/Value/Telemetry/HRTime.php:71:20: MissingThrowsDocblock: PHPUnit\Event\InvalidArgumentException is thrown but not caught - please either catch or add a @throws annotation (see https://psalm.dev/169)
}

return Duration::fromSecondsAndNanoseconds(

Check failure on line 74 in src/Event/Value/Telemetry/HRTime.php

View workflow job for this annotation

GitHub Actions / Type Checker

MissingThrowsDocblock

src/Event/Value/Telemetry/HRTime.php:74:16: MissingThrowsDocblock: PHPUnit\Event\InvalidArgumentException is thrown but not caught - please either catch or add a @throws annotation (see https://psalm.dev/169)
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/Event/Value/Telemetry/HRTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function testFromSecondsAndNanosecondsReturnsHRTime(): void
}

#[DataProvider('provideStartGreaterThanEnd')]
public function testDurationRejectsStartGreaterThanEnd(int $startSeconds, int $startNanoseconds, int $endSeconds, int $endNanoseconds): void
public function testDurationIgnoresStartGreaterThanEnd(int $startSeconds, int $startNanoseconds, int $endSeconds, int $endNanoseconds): void
{
$start = HRTime::fromSecondsAndNanoseconds(
$startSeconds,
Expand All @@ -136,10 +136,10 @@ public function testDurationRejectsStartGreaterThanEnd(int $startSeconds, int $s
$endNanoseconds,
);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Start needs to be smaller.');
$duration = $end->duration($start);

$end->duration($start);
$this->assertSame(0, $duration->seconds());
$this->assertSame(0, $duration->nanoseconds());
}

#[DataProvider('provideStartEndAndDuration')]
Expand Down

0 comments on commit fac35fd

Please sign in to comment.