From ce25a212eefb0bde6360b74fcc5e971b35f049f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sun, 24 Jan 2021 14:06:51 +0100 Subject: [PATCH] Enhancement: Implement DurationComparator --- CHANGELOG.md | 4 +- src/Comparator/DurationComparator.php | 32 ++++++++++ .../Comparator/DurationComparatorTest.php | 59 +++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/Comparator/DurationComparator.php create mode 100644 test/Unit/Comparator/DurationComparatorTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 734ddbba..88689aa2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ For a full diff see [`7afa59c...main`][7afa59c...main]. * Added `Subscriber\TestPreparedSubscriber` ([#12]), by [@localheinz] * Added `Subscriber\TestPassedSubscriber` ([#13]), by [@localheinz] * Added `Formatter\ToMillisecondsDurationFormatter` ([#17]), by [@localheinz] +* Added `Comparator\DurationComparator` ([#18]), by [@localheinz] [7afa59c...main]: https://github.com/ergebnis/phpunit-slow-test-detector/compare/7afa59c...main @@ -22,6 +23,7 @@ For a full diff see [`7afa59c...main`][7afa59c...main]. [#8]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/8 [#12]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/12 [#13]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/13 -[#17]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/137 +[#17]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/17 +[#18]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/18 [@localheinz]: https://github.com/localheinz diff --git a/src/Comparator/DurationComparator.php b/src/Comparator/DurationComparator.php new file mode 100644 index 00000000..9524449c --- /dev/null +++ b/src/Comparator/DurationComparator.php @@ -0,0 +1,32 @@ +isLessThan($two)) { + return -1; + } + + if ($one->isGreaterThan($two)) { + return 1; + } + + return 0; + } +} diff --git a/test/Unit/Comparator/DurationComparatorTest.php b/test/Unit/Comparator/DurationComparatorTest.php new file mode 100644 index 00000000..eb45ada2 --- /dev/null +++ b/test/Unit/Comparator/DurationComparatorTest.php @@ -0,0 +1,59 @@ +compare($one, $two)); + } + + public function testReturnsZeroWhenOneEqualsTwo(): void + { + $one = Event\Telemetry\Duration::fromSeconds(5); + $two = Event\Telemetry\Duration::fromSeconds(5); + + $comparator = new DurationComparator(); + + self::assertSame(0, $comparator->compare($one, $two)); + } + + public function testReturnsPlusOneWhenOneIsGreaterThanTwo(): void + { + $one = Event\Telemetry\Duration::fromSeconds(5); + $two = Event\Telemetry\Duration::fromSeconds(4); + + $comparator = new DurationComparator(); + + self::assertSame(1, $comparator->compare($one, $two)); + } +}