Skip to content

Commit 18d4491

Browse files
authored
Merge pull request #421 from ergebnis/fix/phase-not-started
Fix: Throw `PhaseNotStarted` exception when phase was not started
2 parents 75e490f + b795cce commit 18d4491

File tree

4 files changed

+83
-12
lines changed

4 files changed

+83
-12
lines changed

src/Exception/PhaseNotStarted.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) 2021-2023 Andreas Möller
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source code.
10+
*
11+
* @see https://github.com/ergebnis/phpunit-slow-test-detector
12+
*/
13+
14+
namespace Ergebnis\PHPUnit\SlowTestDetector\Exception;
15+
16+
use Ergebnis\PHPUnit\SlowTestDetector\PhaseIdentifier;
17+
18+
/**
19+
* @internal
20+
*/
21+
final class PhaseNotStarted extends \InvalidArgumentException
22+
{
23+
public static function fromPhaseIdentifier(PhaseIdentifier $phaseIdentifier): self
24+
{
25+
return new self(\sprintf(
26+
'Phase identified by "%s" has not been started.',
27+
$phaseIdentifier->toString(),
28+
));
29+
}
30+
}

src/TimeKeeper.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,17 @@ public function start(
3535
);
3636
}
3737

38+
/**
39+
* @throws Exception\PhaseNotStarted
40+
*/
3841
public function stop(
3942
PhaseIdentifier $phaseIdentifier,
4043
Time $stopTime
4144
): Phase {
4245
$key = $phaseIdentifier->toString();
4346

4447
if (!\array_key_exists($key, $this->phaseStarts)) {
45-
return Phase::create(
46-
$phaseIdentifier,
47-
$stopTime,
48-
$stopTime,
49-
);
48+
throw Exception\PhaseNotStarted::fromPhaseIdentifier($phaseIdentifier);
5049
}
5150

5251
$phaseStart = $this->phaseStarts[$key];
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) 2021-2023 Andreas Möller
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source code.
10+
*
11+
* @see https://github.com/ergebnis/phpunit-slow-test-detector
12+
*/
13+
14+
namespace Ergebnis\PHPUnit\SlowTestDetector\Test\Unit\Exception;
15+
16+
use Ergebnis\PHPUnit\SlowTestDetector\Exception;
17+
use Ergebnis\PHPUnit\SlowTestDetector\PhaseIdentifier;
18+
use Ergebnis\PHPUnit\SlowTestDetector\Test;
19+
use PHPUnit\Framework;
20+
21+
/**
22+
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Exception\PhaseNotStarted
23+
*
24+
* @uses \Ergebnis\PHPUnit\SlowTestDetector\PhaseIdentifier
25+
*/
26+
final class PhaseNotStartedTest extends Framework\TestCase
27+
{
28+
use Test\Util\Helper;
29+
30+
public function testFromPhaseIdentifierReturnsException(): void
31+
{
32+
$phaseIdentifier = PhaseIdentifier::fromString(self::faker()->word());
33+
34+
$exception = Exception\PhaseNotStarted::fromPhaseIdentifier($phaseIdentifier);
35+
36+
$message = \sprintf(
37+
'Phase identified by "%s" has not been started.',
38+
$phaseIdentifier->toString(),
39+
);
40+
41+
self::assertSame($message, $exception->getMessage());
42+
}
43+
}

test/Unit/TimeKeeperTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Ergebnis\PHPUnit\SlowTestDetector\Test\Unit;
1515

16+
use Ergebnis\PHPUnit\SlowTestDetector\Exception;
1617
use Ergebnis\PHPUnit\SlowTestDetector\PhaseIdentifier;
1718
use Ergebnis\PHPUnit\SlowTestDetector\Test;
1819
use Ergebnis\PHPUnit\SlowTestDetector\Time;
@@ -23,6 +24,7 @@
2324
* @covers \Ergebnis\PHPUnit\SlowTestDetector\TimeKeeper
2425
*
2526
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Duration
27+
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Exception\PhaseNotStarted
2628
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Phase
2729
* @uses \Ergebnis\PHPUnit\SlowTestDetector\PhaseIdentifier
2830
* @uses \Ergebnis\PHPUnit\SlowTestDetector\PhaseStart
@@ -32,7 +34,7 @@ final class TimeKeeperTest extends Framework\TestCase
3234
{
3335
use Test\Util\Helper;
3436

35-
public function testStopReturnsPhaseWhenPhaseHasNotBeenStarted(): void
37+
public function testStopThrowsPhaseNotStartedExceptionWhenPhaseHasNotBeenStarted(): void
3638
{
3739
$faker = self::faker();
3840

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

4547
$timeKeeper = new TimeKeeper();
4648

47-
$phase = $timeKeeper->stop(
49+
$this->expectException(Exception\PhaseNotStarted::class);
50+
51+
$timeKeeper->stop(
4852
$phaseIdentifier,
4953
$stopTime,
5054
);
51-
52-
self::assertSame($phaseIdentifier, $phase->phaseIdentifier());
53-
self::assertSame($stopTime, $phase->startTime());
54-
self::assertSame($stopTime, $phase->stopTime());
55-
self::assertEquals($stopTime->duration($stopTime), $phase->duration());
5655
}
5756

5857
public function testStopReturnsPhaseWhenPhaseHasBeenStarted(): void

0 commit comments

Comments
 (0)