Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Throw PhaseNotStarted exception when phase was not started #421

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Exception/PhaseNotStarted.php
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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