Skip to content

Commit fac35fd

Browse files
sebastianbergmanntheseerlocalheinzsebastianheuerSchrank
committed
Closes #5652
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 <[email protected]> Co-authored-by: Arne Blankerts <[email protected]> Co-authored-by: Andreas Möller <[email protected]> Co-authored-by: Sebastian Heuer <[email protected]> Co-authored-by: Fabian Blechschmidt <[email protected]> Co-authored-by: Frank Sons <[email protected]>
1 parent 15b2ae8 commit fac35fd

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

.psalm/baseline.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
<file src="src/Event/Value/Telemetry/HRTime.php">
111111
<ImpureMethodCall>
112112
<code><![CDATA[fromSecondsAndNanoseconds]]></code>
113+
<code><![CDATA[fromSecondsAndNanoseconds]]></code>
113114
</ImpureMethodCall>
114115
</file>
115116
<file src="src/Event/Value/Telemetry/MemoryUsage.php">

ChangeLog-10.5.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

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

5+
## [10.5.12] - 2024-MM-DD
6+
7+
### Fixed
8+
9+
* [#5652](https://github.com/sebastianbergmann/phpunit/issues/5652): `HRTime::duration()` throws `InvalidArgumentException`
10+
511
## [10.5.11] - 2024-02-25
612

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

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

134+
[10.5.12]: https://github.com/sebastianbergmann/phpunit/compare/10.5.11...10.5
128135
[10.5.11]: https://github.com/sebastianbergmann/phpunit/compare/10.5.10...10.5.11
129136
[10.5.10]: https://github.com/sebastianbergmann/phpunit/compare/10.5.9...10.5.10
130137
[10.5.9]: https://github.com/sebastianbergmann/phpunit/compare/10.5.8...10.5.9

src/Event/Value/Telemetry/HRTime.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ public function nanoseconds(): int
5656
return $this->nanoseconds;
5757
}
5858

59-
/**
60-
* @throws InvalidArgumentException
61-
*/
6259
public function duration(self $start): Duration
6360
{
6461
$seconds = $this->seconds - $start->seconds();
@@ -71,7 +68,7 @@ public function duration(self $start): Duration
7168
}
7269

7370
if ($seconds < 0) {
74-
throw new InvalidArgumentException('Start needs to be smaller.');
71+
return Duration::fromSecondsAndNanoseconds(0, 0);
7572
}
7673

7774
return Duration::fromSecondsAndNanoseconds(

tests/unit/Event/Value/Telemetry/HRTimeTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function testFromSecondsAndNanosecondsReturnsHRTime(): void
124124
}
125125

126126
#[DataProvider('provideStartGreaterThanEnd')]
127-
public function testDurationRejectsStartGreaterThanEnd(int $startSeconds, int $startNanoseconds, int $endSeconds, int $endNanoseconds): void
127+
public function testDurationIgnoresStartGreaterThanEnd(int $startSeconds, int $startNanoseconds, int $endSeconds, int $endNanoseconds): void
128128
{
129129
$start = HRTime::fromSecondsAndNanoseconds(
130130
$startSeconds,
@@ -136,10 +136,10 @@ public function testDurationRejectsStartGreaterThanEnd(int $startSeconds, int $s
136136
$endNanoseconds,
137137
);
138138

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

142-
$end->duration($start);
141+
$this->assertSame(0, $duration->seconds());
142+
$this->assertSame(0, $duration->nanoseconds());
143143
}
144144

145145
#[DataProvider('provideStartEndAndDuration')]

0 commit comments

Comments
 (0)