Skip to content

Commit 1d846f1

Browse files
Closes #1126
1 parent 42545dd commit 1d846f1

File tree

9 files changed

+49
-18
lines changed

9 files changed

+49
-18
lines changed

ChangeLog-12.5.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
1414
* [#1107](https://github.com/sebastianbergmann/php-code-coverage/pull/1107): Do not sort code coverage data over and over
1515
* [#1108](https://github.com/sebastianbergmann/php-code-coverage/pull/1108): Do not sort covered files data over and over
1616
* [#1109](https://github.com/sebastianbergmann/php-code-coverage/pull/1109): Represent line coverage data using objects
17+
* [#1126](https://github.com/sebastianbergmann/php-code-coverage/issues/1126): Add test execution time to `<test>` elements under `projects/tests` in the XML reports index file
1718
* [#1127](https://github.com/sebastianbergmann/php-code-coverage/issues/1127): Add SHA-1 hash of content of SUT source file to XML report
1819

1920
[12.5.0]: https://github.com/sebastianbergmann/php-code-coverage/compare/12.4.0...main

src/CodeCoverage.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
/**
4040
* Provides collection functionality for PHP code coverage information.
4141
*
42-
* @phpstan-type TestType array{size: string, status: string}
42+
* @phpstan-type TestType array{size: string, status: string, time: float}
4343
* @phpstan-type TargetedLines array<non-empty-string, list<positive-int>>
4444
*/
4545
final class CodeCoverage
@@ -189,11 +189,11 @@ public function start(string $id, ?TestSize $size = null, bool $clear = false):
189189
$this->cachedReport = null;
190190
}
191191

192-
public function stop(bool $append = true, ?TestStatus $status = null, null|false|TargetCollection $covers = null, ?TargetCollection $uses = null): RawCodeCoverageData
192+
public function stop(bool $append = true, ?TestStatus $status = null, null|false|TargetCollection $covers = null, ?TargetCollection $uses = null, float $time = 0.0): RawCodeCoverageData
193193
{
194194
$data = $this->driver->stop();
195195

196-
$this->append($data, null, $append, $status, $covers, $uses);
196+
$this->append($data, null, $append, $status, $covers, $uses, $time);
197197

198198
$this->currentId = null;
199199
$this->currentSize = null;
@@ -207,7 +207,7 @@ public function stop(bool $append = true, ?TestStatus $status = null, null|false
207207
* @throws TestIdMissingException
208208
* @throws UnintentionallyCoveredCodeException
209209
*/
210-
public function append(RawCodeCoverageData $rawData, ?string $id = null, bool $append = true, ?TestStatus $status = null, null|false|TargetCollection $covers = null, ?TargetCollection $uses = null): void
210+
public function append(RawCodeCoverageData $rawData, ?string $id = null, bool $append = true, ?TestStatus $status = null, null|false|TargetCollection $covers = null, ?TargetCollection $uses = null, float $time = 0.0): void
211211
{
212212
if ($id === null) {
213213
$id = $this->currentId;
@@ -280,6 +280,7 @@ public function append(RawCodeCoverageData $rawData, ?string $id = null, bool $a
280280
$this->tests[$id] = [
281281
'size' => $size->asString(),
282282
'status' => $status->asString(),
283+
'time' => $time,
283284
];
284285

285286
$this->data->markCodeAsExecutedByTestCase($id, $rawData);

src/Report/Xml/Tests.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace SebastianBergmann\CodeCoverage\Report\Xml;
1111

1212
use function assert;
13+
use function sprintf;
1314
use DOMElement;
1415
use SebastianBergmann\CodeCoverage\CodeCoverage;
1516

@@ -44,5 +45,6 @@ public function addTest(string $test, array $result): void
4445
$node->setAttribute('name', $test);
4546
$node->setAttribute('size', $result['size']);
4647
$node->setAttribute('status', $result['status']);
48+
$node->setAttribute('time', sprintf('%F', $result['time']));
4749
}
4850
}

tests/_files/Report/XML/CoverageForBankAccount/index.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
</build>
77
<project source="%s">
88
<tests>
9-
<test name="BankAccountTest::testBalanceIsInitiallyZero" size="unknown" status="unknown"/>
10-
<test name="BankAccountTest::testBalanceCannotBecomeNegative" size="unknown" status="unknown"/>
11-
<test name="BankAccountTest::testBalanceCannotBecomeNegative2" size="unknown" status="unknown"/>
12-
<test name="BankAccountTest::testDepositWithdrawMoney" size="unknown" status="unknown"/>
9+
<test name="BankAccountTest::testBalanceIsInitiallyZero" size="unknown" status="unknown" time="0.100000"/>
10+
<test name="BankAccountTest::testBalanceCannotBecomeNegative" size="unknown" status="unknown" time="0.200000"/>
11+
<test name="BankAccountTest::testBalanceCannotBecomeNegative2" size="unknown" status="unknown" time="0.300000"/>
12+
<test name="BankAccountTest::testDepositWithdrawMoney" size="unknown" status="unknown" time="0.400000"/>
1313
</tests>
1414
<directory name="%s">
1515
<totals>

tests/_files/Report/XML/CoverageForBankAccountWithoutSource/index.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
</build>
77
<project source="%s">
88
<tests>
9-
<test name="BankAccountTest::testBalanceIsInitiallyZero" size="unknown" status="unknown"/>
10-
<test name="BankAccountTest::testBalanceCannotBecomeNegative" size="unknown" status="unknown"/>
11-
<test name="BankAccountTest::testBalanceCannotBecomeNegative2" size="unknown" status="unknown"/>
12-
<test name="BankAccountTest::testDepositWithdrawMoney" size="unknown" status="unknown"/>
9+
<test name="BankAccountTest::testBalanceIsInitiallyZero" size="unknown" status="unknown" time="0.100000"/>
10+
<test name="BankAccountTest::testBalanceCannotBecomeNegative" size="unknown" status="unknown" time="0.200000"/>
11+
<test name="BankAccountTest::testBalanceCannotBecomeNegative2" size="unknown" status="unknown" time="0.300000"/>
12+
<test name="BankAccountTest::testDepositWithdrawMoney" size="unknown" status="unknown" time="0.400000"/>
1313
</tests>
1414
<directory name="%s">
1515
<totals>

tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</build>
77
<project source="%s">
88
<tests>
9-
<test name="ClassWithAnonymousFunction" size="unknown" status="unknown"/>
9+
<test name="ClassWithAnonymousFunction" size="unknown" status="unknown" time="0.000000"/>
1010
</tests>
1111
<directory name="%s">
1212
<totals>

tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</build>
77
<project source="%s">
88
<tests>
9-
<test name="FileWithIgnoredLines" size="unknown" status="unknown"/>
9+
<test name="FileWithIgnoredLines" size="unknown" status="unknown" time="0.000000"/>
1010
</tests>
1111
<directory name="%s">
1212
<totals>

tests/src/TestCase.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,7 @@ protected function getLineCoverageForBankAccount(): CodeCoverage
10311031
Target::forMethod(BankAccount::class, 'getBalance'),
10321032
],
10331033
),
1034+
time: 0.1,
10341035
);
10351036

10361037
$coverage->start(
@@ -1045,6 +1046,7 @@ protected function getLineCoverageForBankAccount(): CodeCoverage
10451046
Target::forMethod(BankAccount::class, 'withdrawMoney'),
10461047
],
10471048
),
1049+
time: 0.2,
10481050
);
10491051

10501052
$coverage->start(
@@ -1059,6 +1061,7 @@ protected function getLineCoverageForBankAccount(): CodeCoverage
10591061
Target::forMethod(BankAccount::class, 'depositMoney'),
10601062
],
10611063
),
1064+
time: 0.3,
10621065
);
10631066

10641067
$coverage->start(
@@ -1075,6 +1078,7 @@ protected function getLineCoverageForBankAccount(): CodeCoverage
10751078
Target::forMethod(BankAccount::class, 'withdrawMoney'),
10761079
],
10771080
),
1081+
time: 0.4,
10781082
);
10791083

10801084
return $coverage;
@@ -1110,6 +1114,7 @@ protected function getPathCoverageForBankAccount(): CodeCoverage
11101114
Target::forMethod(BankAccount::class, 'getBalance'),
11111115
],
11121116
),
1117+
time: 0.5,
11131118
);
11141119

11151120
$coverage->start(
@@ -1124,6 +1129,7 @@ protected function getPathCoverageForBankAccount(): CodeCoverage
11241129
Target::forMethod(BankAccount::class, 'withdrawMoney'),
11251130
],
11261131
),
1132+
time: 0.6,
11271133
);
11281134

11291135
$coverage->start(
@@ -1138,6 +1144,7 @@ protected function getPathCoverageForBankAccount(): CodeCoverage
11381144
Target::forMethod(BankAccount::class, 'depositMoney'),
11391145
],
11401146
),
1147+
time: 0.7,
11411148
);
11421149

11431150
$coverage->start(
@@ -1154,6 +1161,7 @@ protected function getPathCoverageForBankAccount(): CodeCoverage
11541161
Target::forMethod(BankAccount::class, 'withdrawMoney'),
11551162
],
11561163
),
1164+
time: 0.8,
11571165
);
11581166

11591167
return $coverage;
@@ -1276,6 +1284,7 @@ protected function getLineCoverageForNamespacedBankAccount(): CodeCoverage
12761284
TargetCollection::fromArray([
12771285
Target::forMethod(BankAccountTrait::class, 'withdrawMoney'),
12781286
]),
1287+
time: 0.9,
12791288
);
12801289

12811290
$coverage->start(
@@ -1288,6 +1297,7 @@ protected function getLineCoverageForNamespacedBankAccount(): CodeCoverage
12881297
TargetCollection::fromArray([
12891298
Target::forMethod(BankAccountTrait::class, 'depositMoney'),
12901299
]),
1300+
time: 1.0,
12911301
);
12921302

12931303
$coverage->start(
@@ -1302,6 +1312,7 @@ protected function getLineCoverageForNamespacedBankAccount(): CodeCoverage
13021312
Target::forMethod(BankAccountTrait::class, 'depositMoney'),
13031313
Target::forMethod(BankAccountTrait::class, 'withdrawMoney'),
13041314
]),
1315+
time: 1.1,
13051316
);
13061317

13071318
return $coverage;
@@ -1335,6 +1346,7 @@ protected function getLineCoverageForBankAccountForFirstTwoTests(): CodeCoverage
13351346
Target::forMethod(BankAccount::class, 'getBalance'),
13361347
],
13371348
),
1349+
time: 1.2,
13381350
);
13391351

13401352
$coverage->start(
@@ -1349,6 +1361,7 @@ protected function getLineCoverageForBankAccountForFirstTwoTests(): CodeCoverage
13491361
Target::forMethod(BankAccount::class, 'withdrawMoney'),
13501362
],
13511363
),
1364+
time: 1.3,
13521365
);
13531366

13541367
return $coverage;
@@ -1380,6 +1393,7 @@ protected function getLineCoverageForBankAccountForLastTwoTests(): CodeCoverage
13801393
Target::forMethod(BankAccount::class, 'depositMoney'),
13811394
],
13821395
),
1396+
time: 1.4,
13831397
);
13841398

13851399
$coverage->start(
@@ -1396,6 +1410,7 @@ protected function getLineCoverageForBankAccountForLastTwoTests(): CodeCoverage
13961410
Target::forMethod(BankAccount::class, 'withdrawMoney'),
13971411
],
13981412
),
1413+
time: 1.5,
13991414
);
14001415

14011416
return $coverage;
@@ -1491,6 +1506,7 @@ protected function getPathCoverageForBankAccountForFirstTwoTests(): CodeCoverage
14911506
Target::forMethod(BankAccount::class, 'getBalance'),
14921507
],
14931508
),
1509+
time: 1.6,
14941510
);
14951511

14961512
$coverage->start(
@@ -1505,6 +1521,7 @@ protected function getPathCoverageForBankAccountForFirstTwoTests(): CodeCoverage
15051521
Target::forMethod(BankAccount::class, 'withdrawMoney'),
15061522
],
15071523
),
1524+
time: 1.7,
15081525
);
15091526

15101527
return $coverage;
@@ -1538,6 +1555,7 @@ protected function getPathCoverageForBankAccountForLastTwoTests(): CodeCoverage
15381555
Target::forMethod(BankAccount::class, 'depositMoney'),
15391556
],
15401557
),
1558+
time: 1.8,
15411559
);
15421560

15431561
$coverage->start(
@@ -1554,6 +1572,7 @@ protected function getPathCoverageForBankAccountForLastTwoTests(): CodeCoverage
15541572
Target::forMethod(BankAccount::class, 'withdrawMoney'),
15551573
],
15561574
),
1575+
time: 1.9,
15571576
);
15581577

15591578
return $coverage;
@@ -1947,6 +1966,7 @@ protected function getCoverageForFilesWithUncoveredIncluded(): CodeCoverage
19471966
TargetCollection::fromArray([
19481967
Target::forMethod(BankAccount::class, 'getBalance'),
19491968
]),
1969+
time: 2.0,
19501970
);
19511971

19521972
$coverage->start(
@@ -1959,6 +1979,7 @@ protected function getCoverageForFilesWithUncoveredIncluded(): CodeCoverage
19591979
TargetCollection::fromArray([
19601980
Target::forMethod(BankAccount::class, 'withdrawMoney'),
19611981
]),
1982+
time: 2.1,
19621983
);
19631984

19641985
$coverage->start(
@@ -1971,6 +1992,7 @@ protected function getCoverageForFilesWithUncoveredIncluded(): CodeCoverage
19711992
TargetCollection::fromArray([
19721993
Target::forMethod(BankAccount::class, 'depositMoney'),
19731994
]),
1995+
time: 2.2,
19741996
);
19751997

19761998
$coverage->start(
@@ -1985,6 +2007,7 @@ protected function getCoverageForFilesWithUncoveredIncluded(): CodeCoverage
19852007
Target::forMethod(BankAccount::class, 'depositMoney'),
19862008
Target::forMethod(BankAccount::class, 'withdrawMoney'),
19872009
]),
2010+
time: 2.3,
19882011
);
19892012

19902013
return $coverage;
@@ -2018,6 +2041,7 @@ protected function getCoverageForFilesWithUncoveredExcluded(): CodeCoverage
20182041
TargetCollection::fromArray([
20192042
Target::forMethod(BankAccount::class, 'getBalance'),
20202043
]),
2044+
time: 2.4,
20212045
);
20222046

20232047
$coverage->start(
@@ -2030,6 +2054,7 @@ protected function getCoverageForFilesWithUncoveredExcluded(): CodeCoverage
20302054
TargetCollection::fromArray([
20312055
Target::forMethod(BankAccount::class, 'withdrawMoney'),
20322056
]),
2057+
time: 2.5,
20332058
);
20342059

20352060
$coverage->start(
@@ -2042,6 +2067,7 @@ protected function getCoverageForFilesWithUncoveredExcluded(): CodeCoverage
20422067
TargetCollection::fromArray([
20432068
Target::forMethod(BankAccount::class, 'depositMoney'),
20442069
]),
2070+
time: 2.6,
20452071
);
20462072

20472073
$coverage->start(
@@ -2056,6 +2082,7 @@ protected function getCoverageForFilesWithUncoveredExcluded(): CodeCoverage
20562082
Target::forMethod(BankAccount::class, 'depositMoney'),
20572083
Target::forMethod(BankAccount::class, 'withdrawMoney'),
20582084
]),
2085+
time: 2.7,
20592086
);
20602087

20612088
return $coverage;

tests/tests/CodeCoverageTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public function testCollect(): void
4848

4949
$this->assertEquals(
5050
[
51-
'BankAccountTest::testBalanceIsInitiallyZero' => ['size' => 'unknown', 'status' => 'unknown'],
52-
'BankAccountTest::testBalanceCannotBecomeNegative' => ['size' => 'unknown', 'status' => 'unknown'],
53-
'BankAccountTest::testBalanceCannotBecomeNegative2' => ['size' => 'unknown', 'status' => 'unknown'],
54-
'BankAccountTest::testDepositWithdrawMoney' => ['size' => 'unknown', 'status' => 'unknown'],
51+
'BankAccountTest::testBalanceIsInitiallyZero' => ['size' => 'unknown', 'status' => 'unknown', 'time' => 0.1],
52+
'BankAccountTest::testBalanceCannotBecomeNegative' => ['size' => 'unknown', 'status' => 'unknown', 'time' => 0.2],
53+
'BankAccountTest::testBalanceCannotBecomeNegative2' => ['size' => 'unknown', 'status' => 'unknown', 'time' => 0.3],
54+
'BankAccountTest::testDepositWithdrawMoney' => ['size' => 'unknown', 'status' => 'unknown', 'time' => 0.4],
5555
],
5656
$coverage->getTests(),
5757
);

0 commit comments

Comments
 (0)