Skip to content

Commit

Permalink
Merge pull request #421 from ergebnis/fix/phase-not-started
Browse files Browse the repository at this point in the history
Fix: Throw `PhaseNotStarted` exception when phase was not started
  • Loading branch information
localheinz committed Dec 17, 2023
2 parents 75e490f + b795cce commit 18d4491
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 12 deletions.
30 changes: 30 additions & 0 deletions src/Exception/PhaseNotStarted.php
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2023 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\Exception;

use Ergebnis\PHPUnit\SlowTestDetector\PhaseIdentifier;

/**
* @internal
*/
final class PhaseNotStarted extends \InvalidArgumentException
{
public static function fromPhaseIdentifier(PhaseIdentifier $phaseIdentifier): self
{
return new self(\sprintf(
'Phase identified by "%s" has not been started.',
$phaseIdentifier->toString(),
));
}
}
9 changes: 4 additions & 5 deletions src/TimeKeeper.php
Expand Up @@ -35,18 +35,17 @@ public function start(
);
}

/**
* @throws Exception\PhaseNotStarted
*/
public function stop(
PhaseIdentifier $phaseIdentifier,
Time $stopTime
): Phase {
$key = $phaseIdentifier->toString();

if (!\array_key_exists($key, $this->phaseStarts)) {
return Phase::create(
$phaseIdentifier,
$stopTime,
$stopTime,
);
throw Exception\PhaseNotStarted::fromPhaseIdentifier($phaseIdentifier);
}

$phaseStart = $this->phaseStarts[$key];
Expand Down
43 changes: 43 additions & 0 deletions test/Unit/Exception/PhaseNotStartedTest.php
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2023 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\Exception;

use Ergebnis\PHPUnit\SlowTestDetector\Exception;
use Ergebnis\PHPUnit\SlowTestDetector\PhaseIdentifier;
use Ergebnis\PHPUnit\SlowTestDetector\Test;
use PHPUnit\Framework;

/**
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Exception\PhaseNotStarted
*
* @uses \Ergebnis\PHPUnit\SlowTestDetector\PhaseIdentifier
*/
final class PhaseNotStartedTest extends Framework\TestCase
{
use Test\Util\Helper;

public function testFromPhaseIdentifierReturnsException(): void
{
$phaseIdentifier = PhaseIdentifier::fromString(self::faker()->word());

$exception = Exception\PhaseNotStarted::fromPhaseIdentifier($phaseIdentifier);

$message = \sprintf(
'Phase identified by "%s" has not been started.',
$phaseIdentifier->toString(),
);

self::assertSame($message, $exception->getMessage());
}
}
13 changes: 6 additions & 7 deletions test/Unit/TimeKeeperTest.php
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\PHPUnit\SlowTestDetector\Test\Unit;

use Ergebnis\PHPUnit\SlowTestDetector\Exception;
use Ergebnis\PHPUnit\SlowTestDetector\PhaseIdentifier;
use Ergebnis\PHPUnit\SlowTestDetector\Test;
use Ergebnis\PHPUnit\SlowTestDetector\Time;
Expand All @@ -23,6 +24,7 @@
* @covers \Ergebnis\PHPUnit\SlowTestDetector\TimeKeeper
*
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Duration
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Exception\PhaseNotStarted
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Phase
* @uses \Ergebnis\PHPUnit\SlowTestDetector\PhaseIdentifier
* @uses \Ergebnis\PHPUnit\SlowTestDetector\PhaseStart
Expand All @@ -32,7 +34,7 @@ final class TimeKeeperTest extends Framework\TestCase
{
use Test\Util\Helper;

public function testStopReturnsPhaseWhenPhaseHasNotBeenStarted(): void
public function testStopThrowsPhaseNotStartedExceptionWhenPhaseHasNotBeenStarted(): void
{
$faker = self::faker();

Expand All @@ -44,15 +46,12 @@ public function testStopReturnsPhaseWhenPhaseHasNotBeenStarted(): void

$timeKeeper = new TimeKeeper();

$phase = $timeKeeper->stop(
$this->expectException(Exception\PhaseNotStarted::class);

$timeKeeper->stop(
$phaseIdentifier,
$stopTime,
);

self::assertSame($phaseIdentifier, $phase->phaseIdentifier());
self::assertSame($stopTime, $phase->startTime());
self::assertSame($stopTime, $phase->stopTime());
self::assertEquals($stopTime->duration($stopTime), $phase->duration());
}

public function testStopReturnsPhaseWhenPhaseHasBeenStarted(): void
Expand Down

0 comments on commit 18d4491

Please sign in to comment.