Skip to content

Commit

Permalink
Merge pull request #18 from ergebnis/feature/duration-comparator
Browse files Browse the repository at this point in the history
Enhancement: Implement DurationComparator
  • Loading branch information
ergebnis-bot authored Jan 24, 2021
2 parents 3684509 + ce25a21 commit 9ee06a5
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ 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

[#6]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/6
[#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
32 changes: 32 additions & 0 deletions src/Comparator/DurationComparator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/

namespace Ergebnis\PHPUnit\SlowTestDetector\Comparator;

use PHPUnit\Event;

final class DurationComparator
{
public function compare(Event\Telemetry\Duration $one, Event\Telemetry\Duration $two): int
{
if ($one->isLessThan($two)) {
return -1;
}

if ($one->isGreaterThan($two)) {
return 1;
}

return 0;
}
}
59 changes: 59 additions & 0 deletions test/Unit/Comparator/DurationComparatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/

namespace Ergebnis\PHPUnit\SlowTestDetector\Test\Unit\Comparator;

use Ergebnis\PHPUnit\SlowTestDetector\Comparator\DurationComparator;
use Ergebnis\Test\Util;
use PHPUnit\Event;
use PHPUnit\Framework;

/**
* @internal
*
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Comparator\DurationComparator
*/
final class DurationComparatorTest extends Framework\TestCase
{
use Util\Helper;

public function testReturnsMinusOneWhenOneIsLessThanTwo(): void
{
$one = Event\Telemetry\Duration::fromSeconds(5);
$two = Event\Telemetry\Duration::fromSeconds(6);

$comparator = new DurationComparator();

self::assertSame(-1, $comparator->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));
}
}

0 comments on commit 9ee06a5

Please sign in to comment.